[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: prerequisite staleness criteria

From: John Graham-Cumming
Subject: Re: prerequisite staleness criteria
Date: Tue, 18 Apr 2006 10:42:53 +0200
User-agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.6) Gecko/20040208 Thunderbird/0.5 Mnenhy/

Lawrence, Lynne wrote:
Is this possible?

You can hack this into GNU Make using md5sum (I'm assuming you're on a system with Unix-like commands). Here's a little example that builds foo.o from foo.c but only updates foo.o when foo.h has changed... and changed means that its checksum has changed:

    .PHONY: all

    to-md5 = $(patsubst %,%.md5,$1)
    from-md5 = $(patsubst %.md5,%,$1)

    all: foo.o

    foo.o: foo.c
    foo.o: $(call to-md5,foo.h)

    %.md5: FORCE
@$(if $(filter-out $(shell cat $@ 2>/dev/null),$(shell md5sum $*)),md5sum $* > $@)


This works because when foo.h was mentioned in the prereq list of foo.o it was changed to foo.h.md5 by the to-md5 function. So GNU Make sees that prereqs of foo.o to be foo.c and foo.h.md5.

Then there's a pattern rule to build foo.h.md5 (the %.md5 rule) that will only update the .md5 file if the checksum has changed. In thus if and only if the checksum has changed does the md5 file get changed and hence foo.o rebuilt.

The %.md5 rule is forced to run by having a dummy prereq called FORCE so that every MD5 hash is checked for every prereq that GNU Make needs to examine. First the rule uses a filter-out/if combination to check to see if the MD5 hash has changed. If it has then the %.md5 rule will run md5sum $* > $@ (in the example md5sum foo.h > foo.h.md5). This will both update the hash in the file and changed the .md5 file's timestamp and force foo.o to build.

If within the rule for foo.o you were using $?, $^ or other automatics that work on the prereq list these need to be passed through from-md5 to remove the .md5 extension so that the real prereq is used in the commands to build foo.o. In the example I gave this isn't necessary.

If the foo.h.md5 file does not exist then the %.md5 rule will create it and force foo.o to get built.

John Graham-Cumming


GNU Make Standard Library:
GNU Make Debugger:
Fast, Parallel Builds:

Sign up for my Spam and Anti-spam Newsletter

reply via email to

[Prev in Thread] Current Thread [Next in Thread]