Search code examples
pythonscons

SCons: how to add cpp files to source list


In SCons, I have a pre-build step that generates an unknown number of files. Once these files are generated, I need to be able to add the cpp files to my source list. I am an absolute beginner at SCons, and I'm not sure what is the correct path to take. What is the best method for doing this?

Original: The basic/original build steps are as follows:

fpmFile = Dir('#').Dir('src').entry_abspath("FabricKINECT.fpm.json")
# The next step generates a bunch of source files
cppHeader = env.Command(
  [env.File(target + '.h')],
  klSources,
  [[kl2edkBin, fpmFile, "-o", hdir, "-c", cppdir]]
  )
env.Depends(cppSources, cppHeader)

# We pass in the supplied filelist to the build command
# however, this list does not include the cpp files generated above
# Currently I am hard-coding the generated files into
# the cppSources list, but I want to add them in dynamically
return env.SharedLibrary(
  '-'.join([target, buildOS, buildArch]),
  cppSources
  )

What I've Tried I've tried several different angles:

http://www.scons.org/wiki/DynamicSourceGenerator, but from what I could figure out, this creates seperate build targets for each file, whereas I want them all to be included in my library build

Using an emitter: SCons to generate variable number of targets, but I can't seem to get the dependency worked out - my scanner runs before anything else, no matter how I assign dependencies

I tried making another Command to gather a list of files -

def gatherGenCpp(target, source, env):
  allFiles = Glob('generated/cpp/*.cpp')
  # clear dummy target
  del target[:]
  for f in allFiles:
    target.append(f)

genSources = env.Command(['#dummy-file'], cppdir, gatherGenCpp)
env.Depends(genSources, cppSources)

allSources = genSources + cppSources
return env.SharedLibrary(
  '-'.join([target, buildOS, buildArch]),
  allSources
  )

This however fails with

fatal error LNK1181: cannot open input file 'dummy-file.obj'

I guess its because even though I clean the dummy-file entry out of the targets of the command, this happens after its registered with build system (and the expected targets are made.

all this to say - how would you implement the following:

  • A command generates a bunch of CPP files
  • These files are added to a passed in list of files
  • We build a dll out of this list of cpp files.

Any suggestions?


Solution

  • In the end, I simply ran an exec statement in scons to generate the files. Sometimes, the shortest path is best :)