Trying to replace the #define for build


#1

I use Arduino for STM32. I need to place interrupt vectors table at a different address to use my firmware with bootloader.
For moving the interrupt vectors table, I need to change the preprocessor #define VECT_TAB_ADDR which is defined in platforms\ststm32\builder\frameworks\arduino\maple\stm32f1.py.

Supplying new value was easy - just add -D VECT_TAB_ADDR=whatever to build_flags. But the compiler throws a warning that preprocessor symbol was redefined. If I enable verbose compiler output, I see that VECT_TAB_ADDR is defined on the command line twice.

So, the removing of old symbol value is where I’m stuck.

I tried build_unflags - doesn’t work.
I tried adding extra_scripts = post_extra_script.py to platformio.ini. The contents of post_extra_script.py are:

Import("env")
env.Replace(CPPDEFINES=[d for d in env['CPPDEFINES'] if d != ('VECT_TAB_ADDR', 134217728)])

So that it should remove the definition. Doesn’t work! The firmware is built with old value of VECT_TAB_ADDR. Looks like the CPPDEFINES are already cached somewhere else at the time my extra script is run, and changing them doesn’t influence anything.

How can it be done without editing stm32f1.py deep in STM32 platform tree?


#2

Thanks again! Fixed in

Please pio upgrade --dev and try

Import("env")
env.ProcessUnFlags("-DVECT_TAB_ADDR")
env.Append(CPPDEFINES=("VECT_TAB_ADDR", whatever))

#3

It’s now broken again :frowning:

The correct script now should be

Import("env")
env.ProcessUnFlags("-DVECT_TAB_ADDR=134217728")
env.Append(CPPDEFINES=("VECT_TAB_ADDR", whatever))

or

Import("env")
env.Replace(CPPDEFINES=[d for d in env['CPPDEFINES'] if d != ('VECT_TAB_ADDR', 134217728)])
env.Replace(CPPDEFINES=[d for d in env['CPPDEFINES']] + [('VECT_TAB_ADDR', 0x8005000)])

Looks like both variants work for me.


#4

Could you reproduce it again in the latest version of PIO Core?


#5

@ivankravets
Yes, I can reproduce it. But I think the way it works now is even better in some aspects. It allows to specifically undefine a build flag with specific content, whereas previously it undefined a build flag with certain name regardless of its content.
Maybe it’s possible to keep both variants of env.ProcessUnFlags?


#6

The previous behavior was incorrect. We should handle macro as a pair of name and value. If you need to remove all macros by name, the env.Replace(CPPDEFINES) is a right way.

Please note that the last PIO Core 3.5.4+ has 2 different build environment: global env and projenv which is isolated from frameworks, etc. We use it to build especially project source file.

Import("env", "projenv")

See http://docs.platformio.org/en/latest/projectconf/advanced_scripting.html#before-pre-and-after-post-actions