scons: SConsCPPConditionalScanner fails evaluate nested defines
The SConsCPPConditionalScanner fails to evaluate a #if pre-processor directive to True if the following defines are passed:
env['CPPDEFINES'] = ['A=1', 'B=C', 'C=1']
and if I have a code with the following line: main.c contains this
#if A==B
#include "header1.h"
#endif
int main()
{
return 0;
}
SConstruct:
import SCons.Scanner.C
env = Environment()
env['CPPPATH'] = [Dir('.')]
env['CPPDEFINES'] = [('A',1),('B', 'C'), ('C',1)]
expected_deps = [File('header1.h')]
c_scanner = SCons.Scanner.C.CConditionalScanner()
deps = c_scanner(File('main.c'), env, env['CPPPATH'])
# Compare deps with expected_deps
if deps == expected_deps:
print("Dependencies match expected dependencies.")
else:
raise("!!! Dependencies do not match expected dependencies !!!")
The SConsCPPConditionalScanner seems to treat the value of B define as a string instead of another define thus resulting in the evaluation of #if False instead of True.
- Link to SCons Users thread discussing your issue. https://discord.com/channels/571796279483564041/1301211142915362918
- Version of SCons 4.8.1
- Version of Python 3.10.12
- How you installed SCons snap install scons
- What Platform are you on? (Linux/Windows and which version) Ubuntu 22.04.3 LTS
- How to reproduce your issue? See attached SCons file
- How you invoke scons (The command line you’re using “scons --flags some_arguments”) scons
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Comments: 33 (11 by maintainers)
Commits related to this issue
- Fix Issue #4623. SConsCPPConditionalScanner misinterpreting #ifdef x123UL — committed to bdbaddog/scons by bdbaddog 2 years ago
- Improve C scanner conditional inclusion Simplistic macro replacement is now done on the contents of CPPDEFINES, to improve accuracy of conditional inclusion as compared to the real preprocessor (ref:... — committed to mwichmann/scons by mwichmann 2 years ago
- Improve C scanner conditional inclusion Simplistic macro replacement is now done on the contents of CPPDEFINES, to improve accuracy of conditional inclusion as compared to the real preprocessor (ref:... — committed to mwichmann/scons by mwichmann 2 years ago
- Improve C scanner conditional inclusion Simplistic macro replacement is now done on the contents of CPPDEFINES, to improve accuracy of conditional inclusion as compared to the real preprocessor (ref:... — committed to mwichmann/scons by mwichmann 2 years ago
- Improve C scanner conditional inclusion Simplistic macro replacement is now done on the contents of CPPDEFINES, to improve accuracy of conditional inclusion as compared to the real preprocessor (ref:... — committed to mwichmann/scons by mwichmann 2 years ago
- Improve C scanner conditional inclusion Simplistic macro replacement is now done on the contents of CPPDEFINES, to improve accuracy of conditional inclusion as compared to the real preprocessor (ref:... — committed to mwichmann/scons by mwichmann 2 years ago
- Improve C scanner conditional inclusion Simplistic macro replacement is now done on the contents of CPPDEFINES, to improve accuracy of conditional inclusion as compared to the real preprocessor (ref:... — committed to mwichmann/scons by mwichmann 2 years ago
@relfock See https://github.com/SCons/scons/pull/4629#issuecomment-2467069747 for some notes concerning the SCons C preprocessor implementation.
Disclaimer: it is possible that some of the information presented may be erroneous. Maintain a healthy skepticism.
I can confirm that the macro expansion for
#if(and possibly other statements) is not recursive. Only the first expansion is performed. In this case, evaluation results in a non empty string (the expanded string containing another macro) which is True causing the inclusion of the header.The
#includestatement itself does perform/attempt a recursive evaluation.@relfock - can you create an enhancement request for any remaining issues, and then we’ll close this one?
@relfock - updated code in PR. Please take a look and comment there.
Please don’t post screenshots… Also move these comments over to the PR… #4624 I see the issue now. That shouldn’t be hard to fix.
Fix for this UL/L issue in https://github.com/SCons/scons/pull/4624
And indeed that wasn’t too hard to find…
That will turn the
A1LintoA1in the preprocessor directives extracted from the file (that happens inSCons.cpp.CPP_to_Python).ah, I see. yes, setting it to 0 still picks the header.
I’d argue overcomplicated pre-processor logic to determine defines and includes might be better replaced with logic in your SConstruct. That said the preprocessing c scanner has never been a full c pre-processor implementation. So I’d expect that once you get beyond fairly simple preprocessor logic, it’s not going to work presently.
A quick hack shows that one level of substitution on
CPPDEFINESis easy enough and “fixes” that specific problem. Not entirely convinced this is “the right thing to do”.I cleaned the example up a little bit, and yes, I can see the same thing happen.
Well, yes. Internally, it converts the
CPPDEFINESto a dict to match against, and that conversion gives{'A': 1, 'B': 'C', 'C': 1}. Then it takes each right-hand-side of the tuple it’s made out of recognized preprocessor directives, makes it a Python expression, and does an eval of the expression against a namespace containing that dict. I think the conditional scanner would have to specializePreProcessor.eval_expressionto have any chance of knowing to substitute values at this level, as the base class one doesn’t know about this.@ivankravets any thoughts? I think you knew this code fairly well…