I have a script that saves the .hex output to a directory when I build a project, for use with an external programmer.
Rather than always simply saving to ./output.hex
, I would like to know how to do two things:
- Save relative to the
main.cpp
file (containingloop()
andsetup()
for an AVR) - Save to a directory specified in
platformio.ini
For the first option, I don’t know how to determine the directory of the main file.
For the second option, I think I can use configparser
to read custom configuration variables, but I don’t know how to do it for the specific environment called, e.g. main vs test1, since I don’t know how to determine which environment is being built within the script.
[env]
...
src_filter = +<*> -<.git/> -<src/> -<tests/> -<example/> -<examples/> -<lib/>
extra_scripts = post:extra_script.py
[env:main]
src_filter = ${env.src_filter} +<src/>
savedir = ${workspacedir}
[env:test1]
src_filter = ${env.src_filter} +<test/test1/>
savedir = ./test/
and extra_script.py:
Import("env", "projenv")
from shutil import copyfile
def save_hex(*args, **kwargs):
print("Copying hex output to project directory...")
target = str(kwargs['target'][0])
copyfile(target, 'output.hex')
print("Done.")
env.AddPostAction("$BUILD_DIR/${PROGNAME}.hex", save_hex) #post action for .hex