GNU make: Extracting argument to -j within Makefile

I'm sorry but there is no way to identify the number of parallel jobs -- without writing an application or script that scan the process list to identify the calling parameters.

Check the source code at http://cvs.savannah.gnu.org/viewvc/make/main.c?revision=1.246&root=make&view=markup . Search for job_slots > 1.

Update: If you have control over the operating range you could wrap the make application with your own program/script, parse the parameters, set an dedicated environment variable and call the original make afterwards.


Actually there is a way to implement this completely inside your makefile on *nix.

MAKE_PID := $(shell echo $$PPID)
JOB_FLAG := $(filter -j%, $(subst -j ,-j,$(shell ps T | grep "^\s*$(MAKE_PID).*$(MAKE)")))
JOBS     := $(subst -j,,$(JOB_FLAG))

ps, grep also need to be installed, but it is almost a given. It can be further improved to handle --jobs too

A version using regex and supporting --jobs, as requested in the comments:

MAKE_PID := $(shell echo $$PPID)
JOBS := $(shell ps T | sed -n 's/.*$(MAKE_PID).*$(MAKE).* \(-j\|--jobs=\) *\([0-9][0-9]*\).*/\2/p')

Tags:

Gnu Make