GNU Make: Check number of parallel jobs

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

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.