0

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.

Mike Collins
  • 402
  • 3
  • 13

2 Answers2

0

Perhaps the following? (Haven't tried it, but something along those lines should work)

env.Program('filename.c',CPPDEFINES='-DBASE_FILE_NAME=\"$SOURCE\"')
bdbaddog
  • 3,357
  • 2
  • 22
  • 33
  • Thanks. I will give it a try. – Mike Collins Nov 02 '16 at 15:15
  • This doesn't seem to work because I'm currently using the "Glob('*.cpp')" to make scons build every .cpp file in the source folder, rather than listing them out individually. Adding CPPDEFINES to the environment doesn't work in this situation. I suppose I could use Python to create a list of all the .cpp files and then loop over that list calling env.Object for each one. I was just hoping the scons might have some built-in mechanism to handle this situation. – Mike Collins Nov 02 '16 at 16:55
  • How are you compiling them now? Feeding them to SharedLibrary(), or Program()? or other? – bdbaddog Nov 03 '16 at 15:56
  • I'm feeding the to Program(), just like in the example (above). – Mike Collins Nov 03 '16 at 19:05
  • It doesn't work. The command line always comes out empty. -DBASE_FILE_NAME="" – Mike Collins Nov 03 '16 at 21:07
  • Try this: env.Program('xyz','a.c',CCCOM=env['CCCOM']+' -DBASE_FILE_NAME=\"${SOURCE}\"') or env.SharedLibrary('xyz','a.c',SHCCCOM=env['SHCCCOM']+' -DBASE_FILE_NAME=\"${SOURCE}\"') – bdbaddog Nov 05 '16 at 18:01
0

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.

Mike Collins
  • 402
  • 3
  • 13
  • Why not use SCons Glob? – bdbaddog Nov 03 '16 at 15:57
  • Because glob gives me a list of filenames that I can manipulate when building the O_FILES list. Scons Glob returns a list of Pyhton objects. – Mike Collins Nov 03 '16 at 17:29
  • You don't need that. the output from all builders are nodes for the output files. if you do str(node) you get the filename. That will provide what you need. – bdbaddog Nov 03 '16 at 20:11
  • Ok, but all the examples I've seen pass the output of Glob() directly to Program(). This won't work for me because I still need to call Object() on each file in order to pass in the CPPDEFINES argument. How does using Glob() allow me to make a simpler script vs using glob()? – Mike Collins Nov 03 '16 at 20:40
  • You actually don't. Any Environment modifications you add to a builder create a OverrideEnvironment() which should be used for all subsidiary builds. (In this case transforming .c -> .o. Glob() will find files which SCons knows about, which aren't yet built. glob() will only find files in the filesystem. – bdbaddog Nov 05 '16 at 17:55