Dynamic build_flags in platformio.ini / UNIX_TIME


I’m trying to get a dynamic value like build time stamp or git commit version into the firmware. I thought the build_flags with the -D option combined with the extra script would be the right choice but for some reason the variable doesn’t get replaced.

build_flags=-D BUILD_TIMESTAMP=000
extra_script = timestamp.py

And in timestamp.py:

import datetime

from SCons.Script import DefaultEnvironment

env = DefaultEnvironment()
env.Replace(build_flags = ‘-D BUILD_TIMESTAMP=111’)

But BUILD_TIMESTAMP doesn’t get replaced and the printing BUILD_TIMESTAMP returns 0. Any ideas? Or any better option to get a value identifying the current build into the firmware? This should also work for the CI build…

Access upload_port from platformio.ini from extra_script.py

You don’t need extra script. Just use build_flags = -DBUILD_TIMESTAMP=$UNIX_TIME

This is invalid code. It should be:

from time import time

from SCons.Script import DefaultEnvironment

env = DefaultEnvironment()
env.Append(CPPDEFINES=['BUILD_TIMESTAMP=%d' % time()])


Have this bash file to set up build flags:

Can we replace this with python script?
I tried your code, with this directiive in my platformio.ini
extra_scripts = pre:my_build_flags.py
but it does not change build flags.

The reason is to build project from IDE, deal with errors there, having python scripts for every set of build flags.


Why do you not use http://docs.platformio.org/en/latest/projectconf/section_env_build.html#build-flags ?

build_flags = 

You can re-test all flags with pio run -v command.