env.AddPreAction() help

I’m trying to add an extra script that needs to be called every time that the user (me) hits the Build button.

Here’s what I have tried:

At the bottom of my script, I have tried each of these individually:

  • env.AddPreAction("$BUILD_DIR/src/KiniMain.cpp.o", before_build) - That only runs the script if KiniMain.c is going to be compiled. The timing of the script execution is right, but I want this to get triggered both when there is any changed source files and also when there is no changed source files. I want the script to be executed at this point, always.
  • env.AddPreAction("$BUILD_DIR/firmware.elf", before_build) - That only runs the script if the firmware.elf files is definitely going to be made. If there are no changes to the source files, then my extra_scripts does not get called. However, this runs my script after the compilation of the source files which is too late for my needs.
  • env.AddPreAction("buildprog", before_build) - doesn’t run my script at all.
  • env.AddPreAction("build", before_build) - doesn’t run my script at all.

I’ve tried a few other things too without success, like adding a new target, but I am out of ideas. Any help would be appreciated.

I think you can just use a regular post script and execute the functionality directly based on the current build target, just like Marlin does.

so if that function returns True, you do your stuff.

When you say to use a regular postscript, do you mean:
env.AddPostAction(“$BUILD_DIR/src/xxx.c”, my_script()?
I searched to see how Marlin did an env.Add…, and here is one:

But I think post is too late. At that point, the decision was already made whether to build or not, and I want to know ANYTIME that the user hits the build button.

No, “in a post script” means in a post script, i.e. a file which is referenced as extra_scripts = post:some_file.py in the platformio.ini. The post is even optional.

Wow, thank you so very much!!!

This turned out to be so very simple. I just delete my:

env.AddPreAction(“$BUILD_DIR/firmware.elf”, before_build)

from my extra_scripts file. I didn’t dawn on me that I already had what I needed except that the only work I was doing at the time the script was being executed was to add a preAction function.

Instead, now, when my extra scripts runs, I just do the work I need to do, make any necessary changes to the one header file I am interested in, and the build system will compile the appropriate files, if I’ve made changes to that header, or if there are other changes unrelated that need to be compiled. I’m sure there are edge cases I need to watch for, but for now I’ve made a lot of progress.

Thank you!!