Test your make-fu (update Makefile to maintain a version.h file)

Lubomir I. Ivanov neolit123 at gmail.com
Mon Feb 25 15:24:31 PST 2013


your make-fu is probably better than my, but here is an attempt to
write a Makefile that reads a version.h file and compares that to a
"git describe" version.
there are some comments in the code.

ideas partially from the other thread on this topic.

----------------------------------------
Makefile:
----------------------------------------
# version rule goes here
all: version test.o

define update_ver
	@echo updating version.h to $(1)
	$(shell echo \#define VERSION \"$(1)\" > version.h)
endef

version:
# if not i na git repo VER should be ""
	$(eval VER = $(shell git describe --tags --abbrev=7 2>&1 | \
		grep -E -v "Not a git repository|No names found"))
# check contents of version.h for $(VER)
	$(eval VER_MATCH = $(findstring "$(VER)", $(shell cat version.h | cut
-d' ' -f3)))
# ugly flip of the value here, since i need the inverse and i don't
know about a $(ifnot...) function
	$(if $(VER_MATCH), $(eval VER_MATCH =), $(eval VER_MATCH = NOUPDATE))
# if there is a version from git and it does not match the one in the
version.h let's update version.h
	$(if $(VER), $(if $(VER_MATCH), $(call update_ver,$(VER))))

# file depends on version.h
test.o: test.c version.h
	gcc test.c -o test.o

clean:
	rm test.o

----------------------------------------
version.h
----------------------------------------
#define VERSION "v1.0.0"


------------------
other notes:
- uses only grep, cat, cut
- this is a portable version, things like "if / fi" aren't portable
outside of bash/sh, but the current Makefile uses them anyway.
- runs on the windows native shell and on bash/sh with the msys toolchain.
- seems to work with GNU Make 3.82

lubomir
--


More information about the subsurface mailing list