How to differentiate between targets from an extra_scripts script?

I’d like to perform different actions depending on whether the “build” or “clean” targets are running. I couldn’t figure out a way to determine this from within my “pre:” extra_scripts script. Is there a way to do this?

I should add: I’m using a shell script, called from this python (named

import os


Then print out the COMMAND_LINE_TARGETS.

also here


I get this:

NameError: name 'SCons' is not defined:

Here’s the full “extra_scripts” script:

from SCons.Script import (COMMAND_LINE_TARGETS, AlwaysBuild,
                          Builder, Default, DefaultEnvironment)

Is there a simple “extra_scripts” script that tells me true or false to this question:
“Is the current target ‘clean’?”

None of my experiments have worked, and I’m not following the example. Also, I’m brand new at Python and at SCons, so I’m having much difficulty figuring this out.

extra_scripts are not invoked when the target is only clean in the first place, so this case can’t be catched.

When I run “clean”, this prints for “”:

Current CLI targets []
Current Build targets []

Here’s the “extra_script” script (“pre”):


print("Current CLI targets", COMMAND_LINE_TARGETS)
print("Current Build targets", BUILD_TARGETS)

I’m entirely unclear when “pre” scripts run and when “post” scripts run.

Here’s what I’m trying to do: I have a shell script that runs as part of a “post” python script (it’s one line: os.system('.')) that processes some data and creates some source files just before compilation. I want to delete those files when I call “clean”.


1 Like

Yeah extra_scripts = with


if env.IsCleanTarget():
    print("==== WE WANT TO DO A CLEAN ====")    

works fine.

> pio run -t clean -v -e gd32350g_start
Processing gd32350g_start (board: gd32350g_start; framework: arduino; platform:; platform_packages: framework-arduinogd32@; monitor_speed: 115200; extra_scripts:
Removing .pio\build\gd32350g_start
Done cleaning
1 Like