Search code examples
commandexecutescons

scons: how to define command/target that only takes place during 'scons -c'?


Before building the targets I wish to create some directory structures, I know I can use:

env = Environment()
env.Execute('mkdir -p xxx')

But this will cause "mkdir -p' to be executed even when I do clean up:

scons -c

And the "env.Execute" will gets called.

I wish there's some command or target that's only taking place when I execute 'scons -c'

How to achieve that? Thanks.


Solution

  • The -c option is a built in scons option and you can check if it was set with GetOption('clean').

    You could then call the commands conditionally based off the value of the 'clean' option. Here is an example:

    env = Environment()
    if not GetOption('clean'):
        env.Execute('mkdir -p xxx')
    else:
        env.Execute('echo "Cleaning up..."')
    

    More info on the other built in options can be found here: https://scons.org/doc/production/HTML/scons-user.html#sect-command-line-option-strings https://scons.org/doc/production/HTML/scons-man.html#options