Search code examples
makefile

How to run all Makefiles in parallel?


I'm trying to run all Makefiles under sub-directory.

But problem is that I can't run all Makefiles in parallel.

How do I run the Makefiles at once in parallel?

I came across the snippet code as the below

SUBDIRS := $(wildcard */.)

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

.PHONY: all $(SUBDIRS)

or

all:
    cd library  && $(MAKE) all && cd ..
    cd programs && $(MAKE) all && cd ..
    cd tests    && $(MAKE) all && cd ..

But they can't all run in parallel.


Solution

  • The recommended way to parallelize make runs is to use the -j option.

    $ make -j
    or 
    $ make -j <jobs/threads>
    

    The -j option takes an optional parameter of number of jobs/threads to use, which usually should track to the number of logical cpus (nproc). The default is unlimited.