Pregunta

I'm looking to write a single makefile that calls a target in it itself in all the subdirectories of the current directory. I've come up with this as a starting point:

SUBDIRS := $(shell find . -maxdepth 1 -type d | sed -e 's/^/\"/;s/$$/\"/')

all:
        @for d in $(SUBDIRS); do \
                ($(MAKE) -f "../$(lastword $(MAKEFILE_LIST))" -C "$$d" gallery.html); \
        done

gallery.html:
        echo "Making gallery\n";
  1. Is this the right approach to the problem?
  2. Is that really the "cleanest" way to discover diretories and quote them correctly?

I'm happy enough using GNU make extensions.

¿Fue útil?

Solución

What you have there is essentially an obfuscated shell script -- you're not using any features of make itself.

Do your subdirectory names contain space characters? If so, then make is perhaps simply not the right tool for the job: It really wants to work with space-separated lists of words, and has no concept of quoting that could help it deal with spaces in paths.

If the subdirectory names contain space characters, I would split things up into a separate shell script and makefile, like so:

#/bin/sh
find . -maxdepth 1 -type d | while read d; do
    make -f `dirname $0`/Makefile -C "$d" gallery.html
done

and

gallery.html:
        echo "Making gallery\n";

(keeping in mind that, if there are more paths with spaces inside your subdirectories, make could still be a poor choice)

If the subdirectory names do not contain space characters, then things can be made much simpler, and you can also avoid the recursive make invocation (google "recursive make considered harmful" for reasons why):

SUBDIRS := $(wildcard */)

all: $(SUBDIRS:%=%/gallery.html)

%/gallery.html:
    cd $* && echo "Making gallery in `pwd`\n"

Otros consejos

This kind of recursion makes sense only if gallery.html has some complicated local prerequisites, and even then it probably isn't the best way to go. And this sensitivity to the name of the makefile is a very bad sign. But if that's what you want, I suggest the following change:

.PHONY: $(SUBDIRS)
all: $(SUBDIRS)

$(SUBDIRS):
    $(MAKE) -f "../$(lastword $(MAKEFILE_LIST))" -C $@ gallery.html
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top