Search code examples
sconscompiler-flags

How can I add per-file defines to a scons project


I'm in the process of porting a makefile project to scons and I can't figure out how to create a unique #define for each file. I would like to have the base filename for each file defined in order to support some custom debug macros. In the makefile, I'm able to do this with the following definition.

-DBASE_FILE_NAME=\"$(<F)\"

I'm not sure how to do this or if it is even possible in scons and would appreciate any feedback.


Solution

  • After some experimentation, the following seems to work.

    import os
    from glob import glob
    
    # use Python glob, not scons Glob!
    CPP_FILES = glob('./src/*.cpp')
    
    env = Environment(CPPPATH='./include', etc...)
    
    for f in CPP_FILES:
        env.Object(f, CPPDEFINES={'BASE_FILENAME' : "\\\"" + os.path.basename(f) + "\\\""})
    
    O_FILES = [os.path.splitext(f)[0] + '.o' for f in CPP_FILES]
    
    env.Program('myprogram', O_FILES)
    

    This lets me define things on a per-file basis without listing the files out individually.