tags:

views:

531

answers:

2

Is there a way to tell scons to use a particular file to setup the default environment? I am using TI DSPs and the compiler is something different than cc; I'd like to have one "environment file" that defines where the compiler is, and what the default flags are, and then be able to use this for several projects.

Any suggestions?

+1  A: 

You can use the normal python utilities to read a file or process XML and then import it into your env. If you don't have some external file that you need to import into SCons, then you can simply encode the environment in the scons file. If, for some reason, your environment is defined in a Perl dictionary ( as in my case...) you can either try to use PyPerl or convert the Perl dictionary into YAML and then read the YAML into python. ( I was able to do the later, but not the former).

Let's say you simply have a file that you need to read which has environment variables in the form:

ENV_VAR1 ENV_VAL1
ENV_VAR2 ENV_VAL2
...

You could import this into your SConstruct.py file like:

import os

env_file =  open('PATH_TO_ENV_FILE','r')

lines = env.file.readlines()

split_regex = re.compile('^(?P<env_var>[\w_]+) *(?P<env_val>.*)')
for line in lines:
    regex_search = split_regex.search(line)
    if regex_search:
        env_var = regex_search.group('env_var')
        env_val = regex_search.group('env_val').strip()
        os.environ[env_var] = env_val

base_env = Environment(ENV=os.environ)

# even though the below lines seem redundant, it was necessary in my build 
# flow...
for key in os.environ.iterkeys():
    base_env[key] = os.environ[key]

If you want to stick this ugliness inside a different file and then import it from your main SConstruct.py file, you can add the following to enable access to the 'Environment' class from your other file:

from SCons.Environment import *

Then in your main SConstruct.py file, import the env file like:

from env_loader import *
Ross Rogers
thanks -- this is probably kiddie stuff but I don't know Python and don't want to waste a lot of time struggling through it just to do one thing.
Jason S
hmmm, all the loose ends haven't been tied up for me, but it looks like it gets me most of the way there
Jason S
A: 

SInclusion file: ... myenv = Environment(...) ...

SConstruct file: ... execfile('SInclusion') ... myenv.Object(...) ...

Eugene
Jason S