LiKao
LiKao

Reputation: 10658

How to get make to correctly create dependencies when generated headers are involved

I am trying to get make to automatically build some generated headers and then create the depency (.d) files automatically. However I am not sure how to get make to do both targets correctly and in the right order.

Here is what I have so far:

all: test

test: test.o
    g++ test.o -o test

test.o: test.cc test.d external-headers
    g++ test.cc -c -o test.o

test.d: test.cc external-headers
    g++ -MM -MT $@ $< -o $@

external/some_header.hh:
    mkdir -p external
    touch external/some_header.hh

.PHONY: external-headers

-include test.d

external-headers: external/some_header.hh

Looking at the dependencies this looks correct, because I have to make the external header before I can create any .d file. Also if the external-headers are missing they will have to be regenerated. However this small Makefile causes GNU make to go into an endless loop. I used the debugging option, to find out, why it does this.

What I could see was, that make keeps restarting itself each time it recreates any included file. This is ok and what I would expect. However upon each restart, it recreates the external-headers target because it is phony. However although it does not have to create anything in the subsequent targets, this means a dependent target for the inclues has been rebuilt since the last invocation, so it rebuilds it's includes and then restarts.

Is there a good solution to this problem? The simplest idea I had was to make the .d files dependent on the header external/some_header.hh directly and skip the target in between. When tested this works fine. However in the real situation, i have a whole bunch of generated headers, and I want to easily be able to rebuild all of them at once.

Another Idea I had, was to add a file somewhere which is actually called external-headers and touch this file each time this target is called. This way make could store when it last rebuilt the target and actually notice it won't have to do anything.

Is there a better way to handle something like this?

Upvotes: 2

Views: 250

Answers (1)

vonbrand
vonbrand

Reputation: 11791

The kludge around this is to generate the file as file.tmp, and replace file with file.tmp if they differ. That way make doesn't see a new file, and doesn't restart everything. Look at the Makefiles autotools creates with the ./configure dance, it uses that extensively.

Upvotes: 1

Related Questions