Search code examples
pythonscons

Invoking scons from a Python script


I'm new to scons and Python. I was wondering if there's a way to invoke scons from within a python script.

My python script accepts from the user, a list of directories where the code to be compiled together is located (in addition to doing some other non-trivial things). It also generates a string which is to be used as a name of the executable created by scons.

I want to pass this information from my python script to scons, and then invoke scons. Is there a simple way to do this?

I can think of the following possibilities:

  • use subprocess.call("scons"...) I'm not sure if scons accepts all the info I need to pass as command line arguments
  • have the python script write into a file. Have the SConscript parse the file and get the information passed.

Solution

  • Ive done this using the subprocess.call() python function to encapsulate the somewhat long and complex command line args needed for the particular project I was working on. There are those that argued that the invocation should have been simpler so as not to need to encapsulate it, but that's a different subject :)

    One option to consider instead of using command line arguments is to use the SCONSFLAGS environment variable as mentioned here. Personally, I prefer not to use this option.

    All the options you need can be passed in as command line options. A good way to customize and handle SCons command line args is with the SCons AddOption() function. Additionally, you can get simple variable=value variables by using the SCons ARGUMENTS dictionary. Other related SCons command line functions are GetOption() and SetOption().

    Regarding passing in a string for the executable name: SCons may not like this. At the very least, consider you execute your script once with a particular executable string, then a second time with a different one, then you want to clean, you may end up leaving executables around if SCons isnt executed with the same executable name.