Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SCons configuration file and default values

I have a project which I build using SCons (and MinGW/gcc depending on the platform). This project depends on several other libraries (lets call them libfoo and libbar) which can be installed on different places for different users.

Currently, my SConstruct file embeds hard-coded path to those libraries (say, something like: C:\libfoo).

Now, I'd like to add a configuration option to my SConstruct file so that a user who installed libfoo at another location (say C:\custom_path\libfoo) can do something like:

> scons --configure --libfoo-prefix=C:\custom_path\libfoo

Or:

> scons --configure
scons: Reading SConscript files ...
scons: done reading SConscript files.
### Environment configuration ###
Please enter location of 'libfoo' ("C:\libfoo"): C:\custom_path\libfoo
Please enter location of 'libbar' ("C:\libfoo"): C:\custom_path\libbar
### Configuration over ###

Once chosen, those configuration options should be written to some file and reread automatically every time scons runs.

Does scons provide such a mechanism ? How would I achieve this behavior ? I don't exactly master Python so even obvious (but complete) solutions are welcome.

Thanks.

like image 546
ereOn Avatar asked Sep 16 '10 09:09

ereOn


People also ask

What is a SCons file?

SCons is a computer software build tool that automatically analyzes source code file dependencies and operating system adaptation requirements from a software project description and generates final binary executables for installation on the target operating system platform.

How do you clean SCons?

When using SCons, it is unnecessary to add special commands or target names to clean up after a build. Instead, you simply use the -c or --clean option when you invoke SCons, and SCons removes the appropriate built files.

Which Python does SCons use?

SCons will work with Python 2.7. x or with Python 3.5 or later.

What compiler does SCons use?

The Visual C++ compiler option that SCons uses by default to generate PDB information is /Z7 . This works correctly with parallel ( -j ) builds because it embeds the debug information in the intermediate object files, as opposed to sharing a single PDB file between multiple object files.


1 Answers

SCons has a feature called "Variables". You can set it up so that it reads from command line argument variables pretty easily. So in your case you would do something like this from the command line:

scons LIBFOO=C:\custom_path\libfoo

... and the variable would be remembered between runs. So next time you just run scons and it uses the previous value of LIBFOO.

In code you use it like so:

# read variables from the cache, a user's custom.py file or command line
# arguments
var = Variables(['variables.cache', 'custom.py'], ARGUMENTS)
# add a path variable
var.AddVariables(PathVariable('LIBFOO',
        'where the foo library is installed',
        r'C:\default\libfoo', PathVariable.PathIsDir))

env = Environment(variables=var)
env.Program('test', 'main.c', LIBPATH='$LIBFOO')

# save variables to a file
var.Save('variables.cache', env)

If you really wanted to use "--" style options then you could combine the above with the AddOption function, but it is more complicated.

This SO question talks about the issues involved in getting values out of the Variables object without passing them through an Environment.

like image 160
richq Avatar answered Oct 03 '22 20:10

richq