PlatformIO Community

Custom target: script should not run when upload

I want to use advanced scripting to add a custom target as described here Custom Targets — PlatformIO latest documentation
It works fine. I can do pio run -t my_custom_target -e my_environment and it does what I want. But when I execute pio run -t upload -e my_environment it also executes my custom script before uploading the firmware. But I don’t want it to do that. How can I accomplish that? The script should be independent of the build and upload process.

Thank you very much!

But then in your script you just do the env.AddCustomTarget() and not execute any other function right directly? Since everything is callback function based, the function put in as actions in env.AddCustomTarget should not be executed.

1 Like

I think I got the problem. I added a python function as action but that does not seem to work. It has to be a string that can be evaluated by the console, right?

When I do it like this, it prints to the console even if I use ‘upload’ as target.

Import("env")

env.AddCustomTarget(
    name="custom_script",
    dependencies=None,
    actions=[print("-------------CUSTOM----------------")],
    title="Run Custom Script",
    description="",
    always_build=True
)

But if I use a string, that only gets printed when I run my custom target and not when using the upload target.

Import("env")

env.AddCustomTarget(
    name="custom_script",
    dependencies=None,
    actions=['echo "-------------CUSTOM----------------"'],
    title="Run Custom Script",
    description="",
    always_build=True
)

Reference a function that takes any arguments:

1 Like

Yes, if a string is entered as actions (or an array thereof), it is interpreted as shell commands to execute (with all the PlatformIO tools loaded of course). But you can also reference a Python function there and execute arbitrary logic.

This doesn’t work because you directly execute function and put the return value of print() into the actions array (which is None). You must only reference the name of function there that has the above shown prototype, and in that function you can then print(), and it will only be executed when your custom target is run.

1 Like