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.


my_build_flags.sh - is one’s set of custom build flags. It’s personal, not common and everyone who build soft could once edit and save his own set of build flags and use it later, but platformio.ini is the common file placed in repository. It must be the same for everyone, but build flags should be customizable…

Now the only way to use saved sets of build flags is console. To test something we need to put build flags in platformio.ini or in program and after all remove that…


Please check Advanced Scripting in docs. You can automate it.


Thank you for support!!!

Finally I solved the task with this lines in platformio.ini

build_flags = !sh build_flags_esp8266.sh

And build_flags_esp8266.sh is like:

export FLAGS="$FLAGS -DPIO_SRC_REV="$(git log --pretty=format:%h_%ad -1 --date=short)
echo $FLAGS

then just
platformio init --ide clion

Build flags set in python script affect compilation but they doesn’t get into CMake files, generated for IDE with command:
platformio init --ide clion

And I can’t see what build flags were set in python script!

platform = espressif8266
framework = arduino
board = nodemcuv2
lib_ldf_mode = chain+
build_flags = !echo -n “-DPIO_SRC_REV=”$(git log --pretty=format:%h_%ad -1 --date=short)
extra_scripts = my_build_flags_esp8266.py
lib_deps =

I found that script useful for setting build flags:

from platformio import util
Import(“env”, “projenv”)

def setBuildFlag2(buildFlag, value):
projenv.Append(CPPDEFINES=(buildFlag, value))

def setBuildFlag(buildFlag):



Please file an issue here https://github.com/platformio/platformio-core/issues and provide back link to this discussion. Thanks!