carandraug
carandraug

Reputation: 13091

writing a recursive make recipe with prerequisite on parent directory

I am trying to write a recursive make recipe. In this recipe, each target is dependent on a file with an equal name on the parent directory. A minimal (non-working) example:

foo/.dirstamp:
  mkdir $(dir $@)
  touch $@

.SECONDEXPANSION:
%/.dirstamp: $$(dir $$*).dirstamp
  mkdir $(dir $@)
  touch $@

With this example, I would expect make foo/bar/qux/lol/.dirstamp to generate the whole directory tree (if it does not exist), touching all .dirstamp files along the way. However, it does not work:

$ ls # note that there is nothing, make is meant to create the dir tree
Makefile
$ make --debug=v foo/bar/qux/lol/.dirstamp
GNU Make 4.0
[...]
Reading makefiles...
Reading makefile 'Makefile'...
Updating goal targets....
Considering target file 'foo/bar/qux/lol/.dirstamp'.
 File 'foo/bar/qux/lol/.dirstamp' does not exist.
 Finished prerequisites of target file 'foo/bar/qux/lol/.dirstamp'.
Must remake target 'foo/bar/qux/lol/.dirstamp'.
make: *** No rule to make target 'foo/bar/qux/lol/.dirstamp'.  Stop.

It works fine as long as the recursive recipe only needs to be expanded twice, e.g., make foo/bar/.dirstamp works fine.

How can this work for an arbitrary number of levels? How can I handle a recursive expansion for the target and prerequisites names?

Note: my real problem is that the prerequisites of my recipes are in a root directory different from the target so I am using the recipe above to duplicate the directory tree. I know about mkdir -p which seems to work fine in GNU systems. I am still interested on knowing how I would solve the recursion problem for arbitrary levels. which no longer works because part of the team is using Mac and mounting this directories over smb.

More details on the actual problem: prerequisites are in data/x/y/z while targets go into results/x/y/z. However, the results directory tree does not exist and needs to be created as needed. To solve this, I made the creation of parent directories an order-only prerequisite (via the .dirstamp files on my minimal example above).

Upvotes: 1

Views: 1009

Answers (1)

carandraug
carandraug

Reputation: 13091

After an hint from @EtanReisner on the question:

make won't apply a rule more than once. That's a built-in (intentional) limitation. Without working around that with manual recursion or manually building the set of targets and using a static pattern rule (which may or may not actually work I'm not sure) there's not much you can do about this.

I worked up this solution:

RESULT_DIRS := $(patsubst data/%, results/%, $(shell find data/* -type d -print))
DIRSTAMPS := $(addsuffix /.dirstamp, $(RESULT_DIRS))

results/.dirstamp:
    mkdir $(dir $@)
    touch $@

.SECONDEXPANSION:
$(DIRSTAMPS): $$(dir $$(patsubst %/.dirstamp, %, $$@)).dirstamp
    mkdir $(dir $@)
    touch $@

It will duplicate the data directory tree in results as the dirstamp files are required. They are required by making them prerequisites of the other recipes (note the | which makes them order-only prerequisites):

results/%/foo.analysis: data/%/foo.data | results/%/.dirstamp
    $(SOME_ANALYSIS_PROGRAM) $^ > $@

Upvotes: 3

Related Questions