6

I have a setup where make is going through a bunch of subdirectories and making inside those directories. I would like it to stop the build on a failure immediately. The code snippet below illustrates this. Can someone point me in the right direction on how the makefile should be set up or some documentation about building from a top level down through subdirectories?

SUBDIRS = \
test1 \
test2 

all clean check :
    @for dir in $(SUBDIRS); do \
        if [ -d $$dir ]; then (cd $$dir; $(MAKE) $@) fi \
    done
Stephen Burke
  • 882
  • 3
  • 10
  • 25
  • 1
    The solution is given in `info make`: http://www.gnu.org/software/make/manual/make.html#Phony-Targets – dma_k Jun 22 '10 at 18:18

2 Answers2

10

I am in the (apparent) minority that disagrees with "Recursive Make Considered Harmful". I've written recursive Make systems for large, messy code bases, and they work quite nicely.

Here's how to do it:

all: $(SUBDIRS)

$(SUBDIRS): force
    @ $(MAKE) -s -C $@

.PHONY: force
force :;

(I've added the -s to make things quieter.)

EDIT: To pass a target down to the submakes (I should have done this before):

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

all: TARGET=all
check: TARGET=check
clean: TARGET=clean
# No, you can't do TARGET=$@, or at least I don't know how to.

# recursive call to make
$(SUBDIRS): force
    @ $(MAKE) -s -C $@ $(TARGET)

.PHONY: force
    force :;
Alec Jacobson
  • 6,032
  • 5
  • 51
  • 88
Beta
  • 96,650
  • 16
  • 149
  • 150
  • 1
    Using recursive make, knowing full well it's limitations, is one thing. But if you're saying you literally "disagree" with the RMCH paper (i.e. you think it draws wrong conclusions), I (and many others I'm sure) would be very interested in hearing your counter-arguments. – Dan Moulding Mar 17 '10 at 17:08
  • 1
    @Dan Moulding, I think that the paper contains some valid points and some flawed arguments, and that its main conclusion is overstated. Can you suggest a good forum for counter-arguments? I don't have a blog and I don't want to clutter SO with my long-winded critiques if they don't really pertain to the OP's question. – Beta Mar 17 '10 at 18:41
  • Thanks for pointing me in the right direction! What happens when you have multiple targets & want to pass that target down to the subdirectories? So if instead of "all:" you would have "all check clean:" How would I get that to the subdirectory make line so it would go into the subdirectory & do a make clean or make check etc? – Stephen Burke Mar 17 '10 at 21:22
  • you may try publishing your article here (http://www.cmcrossroads.com/ask-mr-make/). I'd love to read it anyways. – P Shved Mar 22 '10 at 20:36
  • TARG should be TARGET. (SO doesn't allow 2-character edits). – gdw2 Feb 08 '13 at 22:56
1

I would urge you to abandon the recursive make approach. It could cause endless amounts of difficulties later as your Makefiles grow. See the paper Recursive Make Considered Harmful for a very good explanation of why invoking make recursively is a bad idea.

An immediate benefit you'll realize from switching to non-recursive make is that this problem you are experiencing right now will simply evaporate. You will not have this problem with a non-recursive Makefile.

Also, feel free to check out this boilerplate non-recursive Makefile that I've created. It requires GNU Make 3.81, but is really easy to use. At the very least, it can act as a good example of a non-recursive Makefile, if you want to create your own.

Dan Moulding
  • 211,373
  • 23
  • 97
  • 98
  • I figured this would be a debated point dealing with recursive make and all. Don't worry though, this is maintaining an old build system. I use the autotools for other things. – Stephen Burke Mar 17 '10 at 21:26
  • Better link for "Recursive Make Considered Harmful" is http://aegis.sourceforge.net/auug97.pdf as that link seems to be dead. – rfay Feb 10 '17 at 22:39