[Scons-users] Fwd: How to access SCons ARGUMENTS in helper package?

Mats Wichmann mats at wichmann.us
Thu Oct 19 09:45:41 EDT 2023


On 10/18/23 17:40, Ben Farmer wrote:
> Hi mailing list!
> 
> I hope this email is not too annoying, but the user guide said this was 
> the place to ask questions.
> 
> So, I am trying to understand how to retrieve command line arguments, 
> that have been saved into Variables by doing e.g.
> 
> opts = Variables()
> opts.AddVariables(...)
> 
> According to the docs at 
> https://scons.org/doc/production/HTML/scons-user/ch10s02.html 
> <https://scons.org/doc/production/HTML/scons-user/ch10s02.html> I think 
> it says I should retrieve variable values by doing
> 
> ARGUMENTS.get('myarg', <default value>)
> 
> However I just get
> 
> scons: Reading SConscript files ...
> NameError: name 'ARGUMENTS' is not defined
> 
> when I try to run this with SCons.
> 
> I'm kind of a SCons noob though and I don't really understand how SCons 
> works or what it is doing. I know Python well and the things scons does 
> don't make sense to me as a python developer. Looks like it kind of 
> isn't exactly Python, just Python powered? I should not view the 
> SConstruct files as Python scripts? Because indeed they don't seem to 
> ever "import" any of the stuff they use. I guess SCons just handles that 
> behind the scenes?
> 
> I assume my problem is that actually I am trying to do my ARGUMENTS.get 
> call inside a python "tools" package that we use in my team, that 
> defines a bunch of stuff for SCons to do across multiple projects. And 
> that "tools" package is a straight-up Python package that is imported 
> into the SConstruct file. So I guess it is not receiving whatever 
> auto-magic treatment the SConstruct files get to make these global 
> variables/functions available to them, and I need to somehow import them 
> via the SCons Python API? However I searched the SCons API docs and 
> couldn't find anything about ARGUMENTS, so maybe I am supposed to do 
> this some other way via the API?

Since you say you know Python well, some detail (the user guide tries 
not to assume much if any Python knowlege so it won't say this): the 
primary build configuration files (SConstruct plus any subsidiary files 
named SConscript. or whatever you choose to name these) are evaluated by 
calling exec using the globals from the "main" part of SCons, so that 
they end up with the intentionally program-global symbols (the public 
API) in scope.  Thus, in a sense they become part of SCons itself, 
rather than external scripts that need to import things. You're free to 
use your own Python scripts in the build as well, but SCons doesn't 
"see" those so they won't get this special treatment, and you have to do 
the import of the symbols that make up the public API yourself.  This is 
in fact mentioned at the very end of the manpage section on variables:

https://scons.org/doc/production/HTML/scons-man.html#sconscript_variables

Try adding the import, see what that does for you, then come back with 
more questions.

The "API Docs" are more of an internal document - the manpage is the 
documentation of the public API.  As a side effect of the the dynamic 
nature of SCons, the API docs actually miss many things in the public 
API, because the API doc generation is a static run of Sphinx against 
the source code, and things like builders (Program, Object, etc.) are 
actually only generated at run-time, so there is not a specific function 
in the source code to pick up the docstring from.  Just so you know...




More information about the Scons-users mailing list