0
votes

I have a number of subdirectories, with Makefiles inside, and I want a "master" Makefile that basically calls Makefiles in those directories:

Makefile
foo/Makefile
bar/Makefile

So that typing make foo in root directory is equivalent to cd foo && make. I can write simple Makefile to acieve this:

foo:
    cd foo && make

bar:
    cd bar && make

But I don't want to list those subdirectories, so I can add new directory with Makefile without having to modify "master" Makefile.

2

2 Answers

1
votes

I think make is probably not the best tool for this specific problem. Why not a shell script?

#!/bin/sh
cd $1 && make

If you're really determined to do it with only make you could use a makefile like this:

DIRS=$(patsubst %/,%,$(wildcard */))
.PHONY: $(DIRS)
$(DIRS):
    @cd $@ && $(MAKE)
0
votes

I think what you want is something like this:

subdirs := $(shell find * -name Makefile | sed /\/Makefile$//)

$(subdirs):
    $(MAKE) -C $@

A couple of quick points:

  • you should not call make directly from within a makefile. Use $(MAKE) instead.
  • $(MAKE) -C dir does the same thing as cd dir && make
  • if your makefiles are guaranteed to be exactly one level down, use ls */Makefile instead of find * -name Makefile. The posted solution will find all makefiles in sub-directories, which may not be what you want.
  • you should be careful that one of the sub-directory names does not conflict with any of your other target names in the main makefile. Some sort of directory naming / target naming convention would be required for that.