Changing build settings in library.json extraScript does not work

I have a library with a few sample programs (esp32, Arduino, but that probably doesn’t matter).

I have an extra_script.py that creates a version-file from git information and it adds some defines to env["BUILD_FLAGS"] and a few other modifications.

If I run this script for my samples (by specifying extra_scripts = pre:extra_script.py in my platformio.ini) it works perfectly fine.

But if I run the script through my library.json build.extraScript parameter it does not work.
The script does run: the version file is created, and the script prints logging messages that I get. But the changes to env are somehow not honoured.

Any suggestions on what I could be doing wrong?

Have you taken care to modify the correct construction environments, if in doubt all?

https://docs.platformio.org/en/latest/scripting/construction_environments.html

See Unable to solve error ".o uses VFP register arguments, .elf does not" - #6 by maxgerhardt

1 Like

Thank you @maxgerhardt , that was indeed the issue: I was using Import("env") but I should use DefaultEnvironment() in this case. I had overlooked the relevant sentence in the documentation:

If you use library.json and extraScript, the Import("env") refers to the library’s isolated environment from which a script was called (not to the global environemnt). Please use env = DefaultEnvironment() instead to access the global environemnt.

Hmm. Modifying DefaultEnvironment() does’t seem to work.

One problem is that any changes I make to BUILD_FLAGS (for example appending -DMY_VERSION=42) aren’t actually picked up by the builds.

But moreover for some boards I seem to break the build process in a more serious way by modifying the DefaultEnvironment(). Building for the nodemcuv2 board for example it will not create the local.eagle.app.v6.common.ld script if I make any changes. Running with pio ci -v indeed shows that the step is skipped…

Shouldn’t that be in in DefaultEnvironment().Append(CPPDEFINES=[("MY_VERSION", 42)])?

@maxgerhardt indeed. I was modifying BUILD_FLAGS, which works for running your extra-script through platformio.ini but doesn’t work if it is run through library.json.

By changing this to modifying CPPFLAGS and ensuring I modify both env and DefaultEnvironment() it now works everywhere.