I have a tool that I use to generate C++ header files as part of the build. I only want to write out the generated headers if they changed from what is already on disk so that I don’t trigger a rebuild of the object files that depend on those headers. I’m trying to do this with GNU Make. My problem is, because of the conditional write, I end up in a situation where the prerequisites are always newer than the target.
Here’s a simplified example of my code:
$(TMP_GENERATED_FILE): $(SCRIPT_INPUT_FILES)
$(SCRIPT) $< -o $@
$(GENERATED_FILE): $(TMP_GENERATED_FILE)
cmp -s "$<" "$@" || cp -f "$<" "$@"
# Other rules for the .o files which depend on $(GENERATED_FILE)
The problem is $(TMP_GENERATED_FILE)
will almost always be newer than $(GENERATED_FILE)
because I only write out $(GENERATED_FILE)
if it differs from $(TMP_GENERATED_FILE)
. This means the recipe for $(GENERATED_FILE)
executes on every build. Is there any way to avoid this? The desired state is the subsequent runs of make would do nothing if nothing changed.
Edit: One other attempt was the following:
$(TEMP_GENERATED_FILE): $(SCRIPT_INPUT_FILES)
$(SCRIPT) $< -o $@
# Get make to reread the Makefile after generating $(TEMP_GENERATED_FILE) so that the condition below works correctly
.PHONY: run_script
run_script: $(TEMP_GENERATED_FILE)
-include run_script
ifeq ($(shell cmp -s "$(TEMP_GENERATED_FILE)" "$(GENERATED_FILE)" && echo identical || echo different), different)
$(GENERATED_FILE): $(TMP_GENERATED_FILE)
cp -f "$<" "$@"
endif
but it seems like I run into the issue described here: ” To avoid this make will not attempt to remake makefiles which are marked phony”
ryan is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
7
The short answer is there’s no straightforward way to avoid it.
All versions of make use the fileystem as their “database” tracking what files are out of date or not. In particular they use the modification time of files to detect when something is out of date.
You can prevent make from rebuilding things that depend on the GENERATED_FILE
by not updating the modification time of the GENERATED_FILE
. But if you don’t update the modification time of the GENERATED_FILE
then the next time you run make it will still be out of date with respect to its prerequisites.
There’s no way to change this algorithm.
The only way to avoid re-running the cmp
recipe would be, if you discover that the GENERATED_FILE
doesn’t need to be rebuilt, to reset the timestamps of your prerequisites (e.g., TMP_GENERATED_FILE
and all the SCRIPT_INPUT_FILES
) so they were older than GENERATED_FILE
and the next time you run make it will not think they are outdated.
But, make is not the only tool that uses modification times of files for important decisions so that change could have other impacts. I personally would not advise it, and instead just live with the extra cmp
being run every time. It’s not much of a burden IMO.
3
You have too complicated build of the header. Here is a working example:
# makefile
all: hello
hello: hello.o
gcc $^ -o $@
hello.o: hello.c hello.h
gcc -c $< -o $@
hello.h: hello.h.in1 hello.h.in2
cat $^ > $@
// hello.c
#include "hello.h"
int main() {
fputs("hellon", stdout);
return 0;
}
// hello.h.in1
/* autogenerated header */
// hello.h.in2
#include <stdio.h>
Testing:
$ make
cat hello.h.in1 hello.h.in2 > hello.h
gcc -c hello.c -o hello.o
gcc hello.o -o hello
$ touch hello.h.in2
$ make
cat hello.h.in1 hello.h.in2 > hello.h
gcc -c hello.c -o hello.o
gcc hello.o -o hello
$ touch hello.c
$ make
gcc -c hello.c -o hello.o
gcc hello.o -o hello
$ make
make: Nothing to be done for 'all'.
Questions?
2