Arduino Stack Exchange is a question and answer site for developers of open-source hardware and software that is compatible with Arduino. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Is there any way to get the current Git tag/commit identifier into an Arduino sketch binary?

For normal desktop program development, you can pull some tricks with the Makefile (as pointed out in this StackExchange answer). However, there is no Makefile for an Arduino sketch. Right?

I could make a batch file that I run before every compile, but there is the very real possibility I will forget to run it, and incorrect version information is worse than no version information.

At the moment, I am using the preprocessor macros __DATE__ and __TIME__ to try and identify what version is running on the board. This works, but tracking backwards from a compile date/time to a source control commit is more guesswork than anything.

share|improve this question
    
You could ditch the Arduino IDE and go for a Makefile-based development. – Edgar Bonet May 1 at 7:13
    
tracking backwards from a compile date/time to a source control commit is more guesswork than anything - why? Commits have the date in them. – Nick Gammon May 1 at 21:17
    
@EdgarBonet I think that is what I am looking for! I will give that a try. If you post that as an answer, I can accept it. – DAVe3283 May 3 at 1:37
up vote 0 down vote accepted

If you want to do Makefile-like tricks, one possibility is to use a Makefile-based workflow instead of the Arduino IDE. There are a few generic Arduino Makefiles floating around the Web. One quite popular is Sudar Muthu's Arduino Makefile. With it, you just write a small per-project Makefile that sets a few variables and then includes the generic Makefile. There is one limitation with this approach though: the Arduino IDE normally modifies your .ino file before submitting it to the compiler, in two ways:

  1. It #includes <Arduino.h>
  2. It writes prototypes near the beginning of the program for every function you define.

Sudar Muthu's Makefile does include Arduino.h (it does not touch your code, using instead the -include gcc option), but it does not write the function prototypes for you. It is thus not 100% compatible with the Arduino IDE.

Another possibility would be to completely write yourself a Makefile that calls Arduino Builder. This should be 100% compatible with the IDE.

share|improve this answer

I'm not sure what the commit ID would prove. I use git for source control for my Arduino sketches, but a particular upload might be halfway between the previous commit and the next one. That is, the code on the board might not match any particular commit.

The date/time would at least identify approximately where in the commit stream the code is.

A possibility would be to run a cron job that periodically (say, every minute) updates a .h file with the current commit ID from the branch, and include that in the sketch.

share|improve this answer
    
It is true that you can be compiling with a dirty working copy, but at least you know for sure what commit you started with for that firmware. With just a date, you can't know for sure which branch was compiled, especially if multiple branches were updated at about the same time. And finally, using date/time leads to problems with time zones, lending even more ambiguity to exactly what was compiled. – DAVe3283 May 3 at 1:39
    
If you say so. A "dirty working copy" could be anything. I'm not sure what the original commit ID proves. using date/time leads to problems with time zones - how many time zones are you in? I'm in one, personally. – Nick Gammon May 3 at 7:13
    
I still work with friends from college, and we now live in different states. The time zone thing has bit me in the past, I'm not trying to be a jerk or anything. Knowing the original commit ID lets me know what branch the sketch came from, at least. I do agree that a dirty working copy can indicate anything, but generally it is close to the base commit. – DAVe3283 May 7 at 5:26
    
People have made makefiles to compiler their Arduino stuff. I can't think of any really simple way of doing it, short of my suggestion of a cron job to update a .h file regularly. – Nick Gammon May 7 at 22:06

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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