scons icon indicating copy to clipboard operation
scons copied to clipboard

Enhance Variable() so it can use environment variables too.

Open bdbaddog opened this issue 7 years ago • 5 comments

This issue was originally created at: 2008-11-04 11:13:02. This issue was reported by: rlogel.

rlogel said at 2008-11-04 11:13:02

When using make you can pass in variables via the environment or command line for example doing either of the following are equivalent (within the make process):

$ export BUILDTYPE=DEBUG
$ make 

or

$ make BUILDTYPE=DEBUG

It would be nice if the Variables() feature could handle this in some way. Possibly by updating classes like BoolVariable and EnumVariable to have a parameter checkenv=True|False. If this feature is added it would also be helpful when the user does scons -h and the help message for each is displayed along with the default and actual value if it labeled where the actual came from something like:

$ scons -h
scons: Reading SConscript files ...
scons: done reading SConscript files.
 
BUILDTYPE: Specify build type (OPT|DEBUG|OPTDEBUG|NOOPT)
    default: OPT
    actual: DEBUG (from enviroment)

It could have three possible labels 'from default', 'from command line' or 'from environment'

Here is an example of how we accomplished the same thing is in the SConstruct file:

# Check for Environment overrides.
if "BUILDTYPE" not in ARGUMENTS and "BUILDTYPE" in os.environ:
    if not GetOption("no_progress"):
        print "scons: Defaulting BUILDTYPE based on environment"
    ARGUMENTS["BUILDTYPE"] = os.environ["BUILDTYPE"]

# Load Command line variables.
Vars = Variables()
Vars.AddVariables(
    EnumVariable(
        "BUILDTYPE",
        "Specify build type",
        "OPT",
        allowed_values=("OPT", "DEBUG", "OPTDEBUG", "NOOPT"),
        ignorecase=0,
    ),
)

Env = Environment(variables=Vars)

This way works fine but it means doing the pre-check for any variable you want to work this way instead just specifying it as a constructor argument.

gregnoel said at 2008-11-21 17:04:47

Bug party triage. Add a convenience function to access shell environment as a short-term hack until a revamp of Configure can consolidate not only this functionality but also the equivalent of Option/Variable and other stuff as well.

gregnoel said at 2008-12-26 13:20:16

Adjust triage of issues.

stevenknight said at 2009-11-10 18:00:19

stevenknight => issues@scons

bdbaddog avatar Jan 02 '18 13:01 bdbaddog

@bdbaddog what do you think? My reaction is to say "that's not the SCons way, if you want something from os.environ be explicit about it", but it seemed like there was a tentative agreement to add something to help with this:

Bug party triage. Add a convenience function to access shell environment as a short-term hack until a revamp of [blah blah future stuff not happening]

Not sure what this would look like? Or just reject the bug?

mwichmann avatar Apr 08 '24 22:04 mwichmann

hmm. if you had to specify vars=Variables(use_shell_env=True) to pick them up from the shell environment, that might be ok?

bdbaddog avatar Apr 08 '24 23:04 bdbaddog

Implementing is easy, but... paraphrasing the above, we have two ways:

$ BUILDTYPE=DEBUG scons
$ scons BUILDTYPE=DEBUG

I'm not feeling massive motivation to need the first type when the second works, just because "we can do that with Make". And Windows has to use the style more like the original, so...

$ export BUILDTYPE=DEBUG
$ scons

    or

C:\wonky\dir> set BUILDTYPE=DEBUG
C:\wonky\dir>scons> scons

mwichmann avatar Apr 08 '24 23:04 mwichmann

Just as a reference point, the MongoDB build solved this by allowing you to

  1. name "variables files" on the command line,
  2. adding a Variable named ENV,
  3. providing a variables file that propagated os.environ into ENV:
  • https://github.com/mongodb/mongo/blob/cbf481da96577c1306d5513cbfd4c264bd609b44/SConstruct#L626
  • https://github.com/mongodb/mongo/blob/cbf481da96577c1306d5513cbfd4c264bd609b44/SConstruct#L1174 -https://github.com/mongodb/mongo/blob/cbf481da96577c1306d5513cbfd4c264bd609b44/etc/scons/propagate_shell_environment.vars

With those pieces in place, you could opt into exposing the shell environment with scons --variables-files=etc/scons/propagate_shell_environment.vars .... You could also write a custom variables file that filtered and transformed os.environ into whatever form you liked before adding it to ENV.

acmorrow avatar Apr 09 '24 13:04 acmorrow

It's an entirely different topic, but we maybe ought to find a more clever way to update individual fields of ENV. Other construction vars are mostly okay to do things like Environment(CCFLAGS="-foo"), but since ENV is itself a mapping, the assignment-form of this wipes out everything else, which doesn't seem a great thing.

Anyway, this form from one of the vars files looks useful:

import os

# Causes SCons to set the C and C++ compilers via the CC and CXX shell environment variables

CC=os.environ['CC']
CXX=os.environ['CXX']

mwichmann avatar Apr 09 '24 13:04 mwichmann