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.

  • 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.


    Well, I guess it's possible in theory. The scons executable itself is just a python script and all it does is execute SCons.Script.main() after modifying the sys.path variable. But you would probably have to start digging deep into the source to really understand how to make it do things you want.

    A much cleaner solution is calling your script from the SConscript file, which is probably the intended usage and should prove much easier.


    Thanks for the answers. I ended up using this method:

    The python script writes a list of options into a text file, closes it, and invokes scons -f MySConscript_file using a subprocess call. The SConstruct reads values from the text file into a list, and uses them.

    链接地址: http://www.djcxy.com/p/67920.html

    上一篇: Scons复制头文件以构建目录

    下一篇: 从Python脚本调用scons