Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

makefile run targets in parallel

I’ve the following makefile, with two rules.

Is there a way to run these two rules in parallel, I mean to maximize the core’s capabilities? I see this section but not sure that I got how to use it for my purpose, since I want to handle it within the makefile and not from the command line.

I.e. run module1 & 2 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)
like image 280
07_05_GuyT Avatar asked Jul 03 '18 07:07

07_05_GuyT


People also ask

How do I run a makefile in parallel?

To start GNU Make in parallel mode it's enough to specify either the -j or --jobs option on the command-line. The argument to the option is the maximum number of processes that GNU Make will run in parallel. For example, typing make --jobs=4 will allow GNU Make to run up to four subprocesses in parallel.

What is a target in a makefile?

A simple makefile consists of “rules” with the following shape: target … : prerequisites … recipe … … A target is usually the name of a file that is generated by a program; examples of targets are executable or object files. A target can also be the name of an action to carry out, such as ' clean ' (see Phony Targets).

What is dependency in makefile?

A dependency is a file that is used as input to create the target. A target often depends on several files. A command is an action that make carries out. A rule may have more than one command, each on its own line.

What make command does?

The make command assists in maintaining a set of programs, usually pertaining to a particular software project, by building up-to-date versions of programs. The make command is most useful for medium-sized programming projects.


1 Answers

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)
like image 60
lubgr Avatar answered Sep 22 '22 15:09

lubgr