[Scons-users] Print out messages only if target is building

Matthew Marinets Matthew.Marinets at Kardium.com
Wed Aug 1 16:11:38 EDT 2018

This sounds like a build-time change.

I generally think of a SCons build in terms of two time-periods: SConscript time and build time.

In SConscript time, the scripts behave like normal Python and will not build targets. Builders at this stage only format how to build files, but don't actually build them.
In build time, SCons figures out which files it has to build, which among those are out-of-date, and builds them. The build actions and order is controlled by how you ran the builders in SConscript time.

If you're putting a print statement in around a builder, that's SConscript time. It will always run if Python will run that code.
If you only want the message to display in build time, you need to get it into a build action somehow.

How to get a Python print statement into an action? You need to affect the Action objects or the strings they're building their actions from. The simplest example is probably:
	print("This will print during SConscript time.")
	outputs = env.SomeBuilder(target = target, source = source)
	env.AddPreAction(outputs, action = lambda target, source, env: print("This will print during build time."))

The strategy I use for organizing my outputs is to just print a big line of dashes at the bottom of my SConstruct files. It's the last line of Python code, so anything that gets printed after that line has to be printed during build time.

What you probably want to do is modify your builder's action. If it's a SCons built-in or is formatted like a SCons built-in, you should be able to find the (XX)COM variable in the environment (env.Dump() if you don't know what's in your environment). You can then modify this string based on if the SDK exists:

Example: changing ALL Program() builders to print an error if SOME_SDK is not in the external environment.
	env = Environment()
	# format env
	# call SConscript()
	if not os.environ.get("SOME_SDK"):
		env['LINKCOM'] = "echo Not building $TARGET because SOME_SDK is not formatted"
	# everything printed after this line in the SCons output is printed at build-time

If you want to affect only specific calls to a builder (stop Program() in one SConscript but not another), you're going to have to run those builders with separate environments. At the top of the SConscript, you'd call:
	env = env.Clone()
Note that this means that changes to the environment in that SConscript will only affect builders / actions in that SConscript.


-----Original Message-----
From: Scons-users <scons-users-bounces at scons.org> On Behalf Of Mats Wichmann
Sent: August 1, 2018 11:30
To: scons-users at scons.org
Subject: Re: [Scons-users] Print out messages only if target is building

On 08/01/2018 11:03 AM, Bill Deegan wrote:
> SConscripts are not targets.
> Can you provide more detail?
> Do you want some text output when a give program is built? Or a 
> library? or something else?

I understood the question this way, maybe got it wrong:

a bunch of targets defined, each a sample app, including x and y

if I ask for x, y on scons command line, and x cannot build due to external reason (missing tools), print a message and skip x

if I ask for y then don't print the message about skipping x since it's irrelevant anyway

> On Wed, Aug 1, 2018 at 9:28 AM, Mats Wichmann <mats at wichmann.us> wrote:
>> On 08/01/2018 06:19 AM, Anna Cheremnykh wrote:
>>> Hi,
>>> I stuck with a problem: I want to print message only when given
>>> target(Scinscript) is building.
>>> What we have: several samples, each sample in separate directory 
>>> with separate Sconscript. Some of samples require 3rdparty SDKs and 
>>> environment variables set to these SDKs. But if developer doesn't 
>>> have such env var - sample build should be just skipped without errors.
>>> So in sconsripts we added something like this:
>>> if not os.getenv('SOME_SDK_ROOT'):
>>>     print('SOME_SDK_ROOT  not found, ' + sample_name + ' build skipped')
>>>     Return()
>>> I understand that scons read all sconsript files first. And I see 
>>> this message every time even if I build another samples or another 
>>> components. So my question how NOT to print messages if I call 
>>> another target from another Sconscript? Thanks!
>> I think you can look at COMMAND_LINE_TARGETS and BUILD_TARGETS to 
>> maybe figure something out?
>> https://scons.org/doc/3.0.1/HTML/scons-user.html#sect-command-line-ta
>> rgets
>> _______________________________________________
>> Scons-users mailing list
>> Scons-users at scons.org
>> https://pairlist4.pair.net/mailman/listinfo/scons-users
> _______________________________________________
> Scons-users mailing list
> Scons-users at scons.org
> https://pairlist4.pair.net/mailman/listinfo/scons-users

Scons-users mailing list
Scons-users at scons.org

More information about the Scons-users mailing list