Search code examples
c++sconsgenerated

How do I set up scons to build a project that has generated source files?


I'm working on a C++ project that has some hand coded source files, as well as some source and header files that are generated by a command line tool. The actual source and header files generated are determined by the contents of a JSON file that the tool reads, and so cannot be hardcoded into the scons script. I would like to set up scons so that if I clean the project, then make it, it will know to run the command line tool to generate the generated source and header files as the first step, and then after that compile both my hand coded files and the generated source files and link them to make my binary. Is this possible? I'm at a loss as to how to achieve this, so any help would be much appreciated.


Solution

  • Yes, this is possible. Depending on which tool you're using to create the header/source files, you want to check our ToolIndex at https://bitbucket.org/scons/scons/wiki/ToolsIndex , or read our guide https://bitbucket.org/scons/scons/wiki/ToolsForFools for writing your own Builder. Based on your description you'll probably have to write your own Emitter, which parses the JSON input file and returns the filenames that will finally result from the call. Then, all you need to do is:

    # creates foo.h/cpp and bar.h/cpp
    env.YourBuilder('input.json') 
    
    env.Program(Glob('*.cpp'))
    

    The Glob will find the created files, even if they don't physically exist on the hard drive yet, and add them to the overall dependencies. If you have further questions or problems arise, please consider subscribing to our User mailing list at [email protected] (see also http://scons.org/lists.html ).