There’s got to be some super simple solution to this that I’m missing….
How do I use a Python pre-build script to change the default upload target path to something other than the default, e.g. from “firmware.elf” to “firmware-bootloader.elf”?
NOTE: This is not “changing the build output filename”…no, this is changing the uploaded filename after building with the default filename is complete. Completely useless unless as in my case, I’m doing an ELF merge via said build script…and want to upload the resulting file, without overwriting the original file.
Digging into the builder script, we can see where the OpenOCD command line is set:
This indicates that the path passed to OpenOCD is contained in an (environment? PIO context? SCONS context?) variable named “$SOURCE”.
There’s a hint in the PlatformIO documentation that this value is “replaced by a real firmware/binary”: (upload_command — PlatformIO latest documentation)
However, I have not been able to find where this “replacement” takes place. A PlatformIO Github search turns up no results for “$SOURCE” or even “SOURCE” (case sensitive: “/(?-i)SOURCE/”).
An environment dump in the script callback…
Import("env")
def before_upload(source, target, env):
print(source, target, env.Dump())
env.AddPreAction("upload", before_upload)
…has the filename provided in the “target” argument (and writing to this makes no change)–but there’s no environment entry for “SOURCE”:
'SMARTLINK': <function smart_link at 0x75235a069080>,
'SPAWN': <function subprocess_spawn at 0x7523596623e0>,
'STATIC_AND_SHARED_OBJECTS_ARE_THE_SAME': 0,
'TARGET_ARCH': None,
'TARGET_OS': None,
In the environment, the “firmware” is found in “PROGNAME”…but changing it with:
env.Replace(PROGNAME="${PIOENV}-bootloader")
(or as appropriate) does not make any difference to the path that “$SOURCE” is replaced with before invoking OpenOCD.
In this community post, someone asked a vaguely similar question:
but the solution ended up being different than here.
Where is this “$SOURCE” resolved, and how can I override it via a Python build script?