Build Flags Script Fail


I have followd the example in the docs for dynamic build flags using a script. However it appears the python script is not called. I am not sure how to debug this:

In platformio.ini I have added:

build_flags = !python

In the same folder as platformio.ini I have added

import subprocess

revision = (
subprocess.check_output([“git”, “rev-parse”, “HEAD”])

print(“-DGIT_REV=‘{}’” .format(revision))

I see complie errors:

error: expected primary-expression before ‘;’ token


String escaping is tricky. If you get a compile error it seems to me that the script is indeed called and will appear in project tasks “Advanced → Verbose Build” but will not have the correct form of quotes around it.

Please see Inject Board Name into Code - #2 by maxgerhardt for string escaping.

It it is maybe also preferable to not do a build_flags = !python but use extra_scripts as there you can directly add into the build environment settings (env["CPPDEFINES"] etc.).

Either that, or use the stringifaction macro in your code.

#define stringify(s) _stringifyDo(s)
#define _stringifyDo(s) #s

// use this macro instead
#define GIT_REV_STR stringify(GIT_REV)