bashshellmakefileparallel-processing

makefile run targets in parallel without using command line flags


I have the following makefile with two targets.

Is there a way to run these two targets in parallel to maximize the cores' capabilities? I saw this section of the manual, but I'm not sure that is sufficient for my purpose, since I want to handle it within the makefile and not from the command line.

I.e. run the module1 and module2 targets in parallel.

This is the makefile:

all: module1 module2

.PHONY: module1
module1:
    @echo "run module 1"
    DIR=$(PWD)
    @echo $(DIR)

.PHONY: module2
module2:
    @echo "run module2"

cleanup:
    fzr clean $(DIR)

Solution

  • You can set make options that you usually pass to make via its command line invokation in the makefile itself. Add this line to your makefile

    MAKEFLAGS += -j2
    

    and you can invoke make without the -j flag, it will still spawn two processes to build targets in parallel, when they aren't dependent on each other. To automatically determine the number of jobs to spawn, you can use this on linux

    NPROCS = $(shell grep -c 'processor' /proc/cpuinfo)
    MAKEFLAGS += -j$(NPROCS)
    

    and on MacOS

    NPROCS = $(shell sysctl hw.ncpu  | grep -o '[0-9]\+')
    MAKEFLAGS += -j$(NPROCS)