views:

61

answers:

2

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
+2  A: 

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
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
+2  A: 

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 TARG=$@, or at least I don't know how to.

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

.PHONY: force
force :;
Beta
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
@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
Stephen Burke
@Beta, you may try publishing your article here (http://www.cmcrossroads.com/ask-mr-make/). I'd love to read it anyways.
Pavel Shved