问题
I've been searching for an hour, and this information appears to be nowhere...
I'd like to be able to extract (and possibly use) the number of requested make "jobs," as passed via the -j option, or by Make itself in the case of sub-makes, in the Makefile.
The most promising thing I've seen so far is the $(MAKEFLAGS) variable, but on my system (if I do, say, make -j2) the contents of this variable are only "--jobserver-fds=3,4 -j". Is there any way to get the actual number of jobs passed with -j?
回答1:
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')
回答2:
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.
来源:https://stackoverflow.com/questions/5303553/gnu-make-extracting-argument-to-j-within-makefile