3

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:

  1. How to include that information in the code
  2. 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 by Serial.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

1

That is a tough one... Because arduino-builder is basically a custom go script you don't get the facilities you get in a Makefile, like shell execution.

I would be inclined to do this kind of thing outside that environment - use some external program or tool to generate a config.h file (maybe use something like GNU Autotools) that contains your custom defines. That can then be #included in your sketch.

0

For the date and time of compilation there are predefined preprocessor symbols: __DATE__ and __TIME__.

  • Unfortunately these are at compile time and not at link time. – jantje Apr 6 '16 at 15:55
  • Seems appropriate; the OP used compiler command-string examples. – JRobert Apr 6 '16 at 17:37
  • I'm not stating it is inappropriate. I'm adding that this solution has a drawback. From experience I learned that a IDE that only builds changed files (like the newest versions of the arduino IDE) will end up with outdated dates and time on the Arduino. – jantje Apr 6 '16 at 18:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.