What I am trying to accomplish is to include information about the state of my environment at build time in my code. For example, I may want the setup()
code to print the git sha it was built against, and the time of build.
There are two related issues here:
- How to include that information in the code
- How to specify to the build environment what to generate in the first place.
The first can be addressed in a number of ways:
- -D options can be generated at build time, and the corresponding defined macros expanded in the code. For example, the compiler could be invoked as
g++ -DGIT_SHA=$(git rev-parse HEAD) -DDATE=$(date '+%Y%m%d-%H:%M:%S')
, and my code could have something like#define strfy(_x) #_x
followed bySerial.println(strfy(GIT_SHA));
. Or the build tool could generate a header file that would define these as external string variables, which the main sketch could then include and print. Or any other number of tricks.
The question I am really asking is: how should I specify this sort of stuff for arduino-builder
?
Ideally, it would be a custom recipe in platform.txt
, but if there is a way to invoke shell commands in recipes and use the results to modify other variables (such as preproc.macro.flags
), I haven't been able to find it (if there is documentation, it's really well hidden). Ideally I would want to be able to say preproc.macros.flags=-w -x c++ -E -CC -DGIT_SHA=$(git rev-parse HEAD)
and have the shell command be executed at the point.
So, before I set out to hack arduino-builder, do people have any opinion on this? My main criterion is something that would be of general value and has a chance of being incorporated in the code, rather than a kludge that only works for me and which I have to patch every time I upgrade. Please do not suggest workarounds. I can think of several myself!
I did try to search, and this sort of issue has come up before, but I didn't see any satisfactory answers from people who knew what they were talking about.
Thanks!
/ji