简体   繁体   中英

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.

Ie 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)

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)

Even a MAKEFLAGS += -j$(NPROCS) would not work if you have '::' instead of ':' for your rule, as illustrated by that recent fix in Git 2.25.2 (March 2020)

See commit 2607d39 (18 Feb 2020) by Jeff King ( peff ) .
(Merged by Junio C Hamano -- gitster -- in commit 29b09c5 , 02 Mar 2020)

doc-diff : use single-colon rule in rendering Makefile

Signed-off-by: Jeff King

When rendering the troff manpages to text via "man", we create an ad-hoc Makefile and feed it to " make ".
The purpose here is two-fold:

  • reuse results from a prior interrupted render of the same tree
  • use make's -j option to build in parallel

But the second part doesn't seem to work (at least with my version of GNU make, 4.2.1). It just runs one render at a time.

We use a double-colon " all " rule for each file, like:

 all:: foo foo: ...actual render recipe... all:: bar bar: ...actual render recipe... ...and so on...

And it's this double-colon that seems to inhibit the parallelism .
We can just switch to a regular single-colon rule.

Even though we do have multiple rules for "all" here, we don't have any recipe to execute for "all" (we only care about triggering its dependencies), so the distinction is irrelevant.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM