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
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 :;
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With