Is it possible to run a post-build script from a library?
In the library.json I already added "build":{"extraScript:":"versioning.py"}.
The script runs before the build process and creates the files versioning.txt in the project folder and version.h in the src folder.
I would also like to add another script that runs after the build process and copies the resulting firmware.bin to a folder firmware and creates another file firmware.nfo.
You can create a post action on the firmware.bin file as shown in the docs and execute arbitrary logic.
You might need to use the newly available projenv environment (and only this environment, not others) for this, as showcased in Force RTTI from library? - #23 by cziter15.
This feature is only available in the dev version for now, hasn’t yet been published into a new release. CLI + pio upgrade --dev to upgrade, then retry with projenv.
Since it’s a library, I would gladly do without the developer version. This could cause problems for the users of the library who do not have the developer version installed.
Is the execution of a post-build script dependent on the “projenv”?
(PS: Some library authors restor to just storing the python script in the library and then tell users to reference it in the platformio.ini, like in here).
This is a historical issue. It is a bug from the PlatformIO’s perspective but from the SCons (which we use internally for the building) this is a feature. See
So, if the target is not ready yet, action will not be assigned.
Nevertheless, I agree that we should fix that. So, I played a little bit and wrapped SCons handlers to catch these cases when targets are not ready. See
Please re-test with PIO Core 6.1-dev. We increased PIO Core minor version. A lot of changes and new features. Does it work for you?
With 6.1.0b1 and the same test code, only in combination with environment = DefaultEnvironment() (not env or projenv), it triggers the e.AddPostAction("$BUILD_DIR/${PROGNAME}.hex", post_bin_action) action. Post actions for buildprog and checkprogsize are not triggered. Registering these callbacks only in env, only in projenv or in both makes no AddPostAction() callback trigger – strange.
This is correct behavior. There are no targets in these envs on which you plan to assign actions. These targets are declared in the main/global env where “progenv” and “env” (lib env) are cloned/isolated environments. We have this info in the docs Construction Environments — PlatformIO latest documentation
Sorry for the late feedback.
I have tested the latest dev version and 6.1.0.
In both versions it works with DefaultEnvironment() (as the note in the documentation describes).