makefile Using .PHONY for recursive invocations of 'make' command


Example

Recursive use of make means using make as a command within a makefile. This technique is useful when a large project contains sub-directories, each having their respective makefiles. The following example will help understand advantage of using .PHONY with recursive make.

/main
     |_ Makefile
     |_ /foo
            |_ Makefile
            |_ ... // other files
     |_ /bar
            |_ Makefile
            |_ ... // other files
     |_ /koo
            |_ Makefile
            |_ ... // other files

To run sub-directory's makefile from within the makefile of main, the main's makefile would have looping as shown below (there are other ways in which this can be achieved, but that is out of scope of the current topic)

SUBDIRS = foo bar koo

subdirs:
        for dir in $(SUBDIRS); do \
          $(MAKE) -C $$dir; \
        done

However, there are pitfalls with this method.

  1. Any error detected in a sub-make is ignored by this rule, so it will continue to build the rest of the directories even when one fails.
  2. Make's ability to perform Parallel execution of multiple build targets is not utilized since only one rule is used.

By declaring the sub-directories as .PHONY targets (you must do this as the sub-directory obviously always exists; otherwise it won’t be built) these problems can be overcome.

SUBDIRS = foo bar koo

.PHONY: subdirs $(SUBDIRS)

subdirs: $(SUBDIRS)

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