Advanced scripting: script wont run

I need an extra pair of eyes, my script won’t run…

in platformio.ini:

platform = espressif32
board = esp32dev
build_flags =
extra_scripts =
monitor_filters = esp32_exception_decoder

the script:


def thisShouldRun(source, target, env):
  print("\n==> this should run\n")

env.AddPreAction("buildprog", thisShouldRun)

print("\n==> but, only this runs...\n")

When running pio run , it only gives but, only this runs...

Probably there is a mistake somewhere but I can’t seem to spot it. Any help?

Documentation says buildprog not build? Have you tried that?

env.AddPreAction("buildprog", callback...)
env.AddPostAction("buildprog", callback...)

yes, typing error. I changed the original post:

env.AddPreAction("build", thisShouldRun)
env.AddPreAction("buildprog", thisShouldRun)

Observation: When I remove the pre: in the platformio.ini, the callback function is executed after the program is built.

Building in release mode

==> but, only this runs...
Checking size .pio\build\esp32\firmware.elf
Advanced Memory Usage is available via "PlatformIO Home > Project Inspect"
RAM:   [          ]   4.5% (used 14700 bytes from 327680 bytes)
Flash: [==        ]  16.0% (used 209795 bytes from 1310720 bytes) v2.6
thisShouldRun(["buildprog"], [".pio\build\esp32\firmware.bin"])

==> this should run

(second-last line in output due to Verbose Build).

This is correct because without the pre:, the default is post:.

…there might be a sitation, after reading docs, that buildprog is not a known target yet because if you’re doing a pre: the script gets executed before the main platform script, which defines buildprog?

Semantically if you want to run something before the build starts, isn’t the “but only this runs” already the right place? Or do you want a hook before building a specific file to modify it last-minute?

I want it to run before compilation starts.

The method in the script fetches some html/css/js files and adds the content to a C header to be included in the build. Depending on the env it should minify the contents or not. I could off course use two different scripts but now I was trying to make one script and catch the env to differentiate in the script.

There are many ways to achieve this, but now I’m intrigued why I can’t make it work using this method.

In projects I know, e.g. esp8266_milight_hub, they also do it directly in the main execution flow of the pre script

Same here, but no confirmed clue why that won’t work. Maybe @ivankravets knows about the possibility/impossibility of using the buildprog target in a the pre: script?

1 Like

There is no target buildprog BEFORE/PRE build process. So, this is why it does not work.

Why you do not just call your callback in PRE without any actions as @maxgerhardt mentioned?

If you need to call a callback AFTER all actions in PRE script but BEFORE SCons will start building project, please use Redirecting...

So, you can setup “dummy” middleware with the IS_RUN flag. So, the only SCons starts building the first source file, you call your callback and mark IS_RUN=True

1 Like

I understand. But then I don’t understand this in the docs:

What is a pre action in a post script?

env.AddPreAction("buildprog", callback...)

What is a pre action in a post script?

nevermind, found it myself after some experimenting.

Is it still true that it’s not possible to define a pre action to the buildprog target?
If so, why does the documentation Pre & Post Actions — PlatformIO latest documentation specifically use this as an example:

# Custom actions when building program/firmware

env.AddPreAction("buildprog", callback...)

Of course, I write here because I also fail to do this. Running the code directly in the extra script would mean I won’t get any advantages of SCons, such as only rebuilding when it’s necessary.

If this functionality is not of interest for PlatformIO, may I suggest:

  • Remove it as an example
  • Document that this is impossible
  • Make the build system emit a warning if someone adds a pre action to buildprog

Because it’s in a post: script, not a pre: script.

1 Like