Reputation: 9445
I'm using autotools and git in a C/C++ program.
I would like my program to be able to show from which commit it was compiled. For example it would show:
$ myprog --version
Binary compiled from commit 9ddb505
In bash, it's easy to determine the current commit:
COMMIT=$(git log -1 --pretty='%h')
But this syntax is not valid inside a Makefile.am file.
Let's say I have a very simple Makefile.am:
bin_PROGRAMS = myprog
myprog_SOURCES = main.c
myprog_CPPFLAGS = -Wall -DGIT_COMMIT=\"??SOMETHING??\"
How can I pass the right commit value to my source, considering that the commit name must be up-to-date on each call of make
?
I tried also (in Makefile.am):
COMMIT=\"$(shell git log -1 --pretty='%h')\"
But this syntax generate an error with automake:
src/Makefile.am:3: warning: shell git log -1 --pretty='%h': non-POSIX variable name src/Makefile.am:3: (probably a GNU make extension)
, and it's colored in red with Vim's syntax (filetype=automake).
The solution would be to pass directly this line to be included in the final Makefile, but how can I do this in Makefile.am? I tried to escape the $
sign, with no success.
Upvotes: 3
Views: 1258
Reputation: 330
Use this in Makefile.am
AM_CXXFLAGS = -DGIT_VERSION="\"$(shell git log -1 --pretty='%h')\""
You will still get the warning
Makefile.am:14: warning: shell git log -1 --pretty='%h': non-POSIX variable name
Makefile.am:14: (probably a GNU make extension)
Make it disappear with:
AM_INIT_AUTOMAKE(-Wno-portability)
in configure.am
Upvotes: 0
Reputation: 101
I think for you the missing link appears to be the shell cmd in Make.
You can do in your Makefile:
GIT_MSG=\"$(shell git log -1 --pretty='%h')\"
CFLAGS=-Wall -DGIT_COMMIT=$(GIT_MSG)
Then use GIT_COMMIT
as a macro in your C/C++ source files.
P.s.:
Use the following for something like master-1399ff4
:
GIT_MSG=\"$(shell echo "`git symbolic-ref HEAD 2> /dev/null | cut -b 12-`-`git log --pretty=format:\"%h\" -1`, ")\"
And use git describe
to print tags if your git repo has any.
Upvotes: 1
Reputation: 97282
Add
GITVERSION:= $(shell git log -1 --pretty='%h')
to your Makefile and
-DGITVERSION="\"${GITVERSION}\""
to your CPPFLAGS and you can use GITVERSION inside code
Upvotes: 1