Make failure in subdirectory make not stopping build

gnu-make, makefile, multiple-makefiles

Solution

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 :;

Problem

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 ```

Original source