1
votes

I'm using automake.

I'd like to have a script run each time I run 'make'.

This script does a git diff and generates an MD5 sum of the diff.

The hash is written as a #define in repos_version.h e.g.:

#define REPOS_DIFF "-190886e9f895e80c42cf6b426dc85afd"

The script only rewrites this file if it doesn't exist or if the diff has is different to what is in repos_version.h already. But the script needs to be run for each make.

main.c includes repos_version.h and prints out the hash when the executable is run.

Here's Attempt 1 for Makefile.am

all: config.h
        @chmod +x gen_diff_hash.sh
        @./gen_diff_hash.sh
        $(MAKE) $(AM_MAKEFLAGS) all-recursive

This work, but I get the following error

Makefile:1234: warning: overriding recipe for target all' Makefile:734: warning: ignoring old recipe for targetall'

Here's Attempt 2 for Makefile.am

all-local:
        @chmod +x gen_diff_hash.sh
        @./gen_diff_hash.sh

main.c: repos_version.h

However, this doesn't work, as all-local seems to be run too late. A second run of 'make' does get the desired result, but that's not a runner.

So neither are great. Any ideas? I've been reading through the automake hooks documentation, but I can't see anything that suits my needs.

1

1 Answers

0
votes

You could ensure the script is always run every time Make loads the Makefile, by executing it via $(shell ./gen_diff_hash.sh) and assigning it to a throwaway variable (or using it in some other construct like an ifeq or something). Note, that this is not POSIX, and on Make implementations other than GNU this isn't valid syntax. GNU Make 4.x supports using VAR != ./gen_diff_hash.sh as well, which is compatible with BSD Make at least.

But maybe it would be a better idea to create a .PHONY: gendiff target that runs the script, and make the header depend on this gendiff. The target would then be re-evaluated every time Make checks if repos_version.h is up-to-date, rather than every time Make is run at all.