Simple

Add the following line to your Makefile

HGVERSION:= $(shell hg parents --template 'hgid: {node|short}')

and

-DHGVERSION="\"${HGVERSION}\""

to your CPPFLAGS. Now one can use the define HGVERSION inside the code.

Rebuild files using HGVERSION

Make rebuilds targets only if one of the dependencies is newer. That means even if the version has changed, due to a change somewhere, files using HGVERSION will not be rebuilt automatically. To overcome this, add a new dependency in the Makefile to update these files:

version.o: hgstamp
.PHONY: hgstamp
hgstamp:
        [ -f $@ ] || touch $@
        echo $(HGVERSION) | cmp -s $@ - || echo $(HGVERSION) > $@

In this example version.o has the extra dependency hgstamp. The hgstamp rule updates hgstamp if and only if the version has really changed. To make sure the hgstamp rule will be executed every time, hgstamp is a phony target (see make manual).

Autotools

If you are using automake add the following lines to your Makefile.am:

HGVERSION:= $(shell hg -R $(top_srcdir) parents --template 'hgid: {node|short}' 2> /dev/null || grep node $(top_srcdir)/.hg_archival.txt 2> /dev/null || true )
AM_CPPFLAGS = -DHGVERSION="\"${HGVERSION}\""

version.o: hgstamp
.PHONY: hgstamp
hgstamp:
        [ -f $@ ] || touch $@
        echo $(HGVERSION) | cmp -s $@ - || echo $(HGVERSION) > $@

The definition of HGVERSION is more robust here. It tries to get the version in the following order

LaTeX

Just add the following lines to your Makefile

HGID:=$(shell hg parents -R . --template "{node|short}" | sed 's/.*/\\renewcommand{\\hgid}{&}/')
.PHONY: hgstamp
hgid.tex:
        [ -f $@ ] || touch $@
        echo '$(HGID)' | cmp -s $@ - || echo '$(HGID)' > $@

and this lines to your main tex file:

\newcommand{\hgid}{null}
\input{hgid}

Now one can use the command \hgid to get the version everywhere.


CategoryHowTo