I'm trying to integrate the output of git describe in my binaries:
// version.c.in
#include <stdio.h>
const char version[] __attribute__((used)) = "##BUILDVERSION##:@BUILDVERSION@";
int main() {
printf("%s\n", version);
return 0;
}
Make generates a version.c out of it:
// Makefile
SOURCEFILES = $(wildcard *.c) version.c
PROGRAMS = $(addprefix build/,$(SOURCEFILES:.c=.out))
.PHONY: version.c
all: $(PROGRAMS)
clean:
rm -fR build
build/%.out: build/%.o Makefile
mkdir -p build
gcc -g $< -o $@
build/%.o: %.c Makefile
mkdir -p build
gcc -Wall -Werror -g -c $< -o $@
version.c: version.c.in Makefile
cp $< $@_temp
sed -i "s/@BUILDVERSION@/$$(git describe --always --dirty=-dirty)/g" $@_temp
@cmp -s $@_temp $@ || mv $@_temp $@
rm -f $@_temp
I would like to achieve to trigger a rebuild only if the output of git describe changes. With my current approach the linker step is always called, although version.c does not even change. But it is .PHONY, which probably causes the rebuild of all targets depending on it. If I make the target not .PHONY anymore I'm missing a rebuild for instance if I commit or add a tag.
I also could not find a suitable file in .git/ as a possible dependency of version.c.
Is there a way to solve this?