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

Most upvoted comments

@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.

My expectation is that if in the code I have #define FEATURE_A_ENABLED 0 it should not pick header1.h as a dependency and vice versa.

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 #include statement 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.

And indeed that wasn’t too hard to find…

    [r'(\d+)(?:L|UL)?',  '\\1'],

That will turn the A1L into A1 in the preprocessor directives extracted from the file (that happens in SCons.cpp.CPP_to_Python).

if in the code I have #define FEATURE_A_ENABLED 0 it should not pick header1.h as a dependency

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 CPPDEFINES is easy enough and “fixes” that specific problem. Not entirely convinced this is “the right thing to do”.

$ scons -Q
DEBUG: dep= header1.h
Dependencies match expected dependencies.
scons: `.' is up to date.

I cleaned the example up a little bit, and yes, I can see the same thing happen.

The SConsCPPConditionalScanner seems to treat the value of B define as a string instead of another define

Well, yes. Internally, it converts the CPPDEFINES to 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 specialize PreProcessor.eval_expression to 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…