Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GNU Make: Check number of parallel jobs

I'd like to add a quick check to a (GNU) makefile which can alert the user to the availability of -j/--jobs (parallel make). That is, something like

$ make
  TIP: this will build faster if you use use "make -j"
  Building ..

$ make -j 
  Building in parallel ..

How can I determine the number of parallel jobs when a Makefile is executed?

There is a trick here http://blog.jgc.org/2015/03/gnu-make-insanity-finding-value-of-j.html

and a proposed change to GNU Make itself here https://github.com/esantoro/make/commit/b0334e7f3009dc58dbc8e6e6fdec94711537fb3b

but perhaps there is something newer and/or easier.

like image 758
Patrick Sanan Avatar asked Feb 19 '18 10:02

Patrick Sanan


2 Answers

The simplest/best solution is to upgrade your version of GNU make to 4.2 or above. Starting with that version, the MAKEFLAGS variable will provide the full -j option including the number. The NEWS file says:

  • The amount of parallelism can be determined by querying MAKEFLAGS, even when the job server is enabled (previously MAKEFLAGS would always contain only "-j", with no number, when job server was enabled).

So:

$ make --version
GNU Make 4.2.1
    ...

$ echo 'all:;@echo $(MAKEFLAGS)' | make -f-

$ echo 'all:;@echo $(MAKEFLAGS)' | make -f- -j
-j
$ echo 'all:;@echo $(MAKEFLAGS)' | make -f- -j10
-j10 --jobserver-auth=3,4
$ echo 'all:;@echo $(patsubst -j%,%,$(filter -j%,$(MAKEFLAGS)))' | make -f- -j10
10
like image 122
MadScientist Avatar answered Nov 10 '22 14:11

MadScientist


You can determine the number of jobs easier and faster than that blog proposes by using make Jobserver protocol:

SHELL := /bin/bash

all:
    @${MAKE} --no-print-directory job_count_test

job_count_test: 
    @+[[ "${MAKEFLAGS}" =~ --jobserver[^=]+=([0-9]+),([0-9]+) ]] && ( J=""; while read -t0 -u $${BASH_REMATCH[1]}; do read -N1 -u $${BASH_REMATCH[1]}; J="$${J}$${REPLY}"; done; echo "Building with $$(expr 1 + $${#J}) jobs."; echo -n $$J >&$${BASH_REMATCH[2]} ) || echo "TIP: this will build faster if you use use \"make -j$$(grep -c processor /proc/cpuinfo)\""

.PHONY: all job_count_test

And then:

$ make 
TIP: this will build faster if you use use "make -j8"
$ make -j12
Building with 12 jobs.
like image 37
Maxim Egorushkin Avatar answered Nov 10 '22 14:11

Maxim Egorushkin