Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GNU make: Extracting argument to -j within Makefile

Tags:

gnu-make

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?

like image 430
JW Peterson Avatar asked Mar 14 '11 19:03

JW Peterson


People also ask

How can I get PWD in makefile?

You can use shell function: current_dir = $(shell pwd) . Or shell in combination with notdir , if you need not absolute path: current_dir = $(notdir $(shell pwd)) .

What is $@ in makefile?

$@ is the name of the target being generated, and $< the first prerequisite (usually a source file). You can find a list of all these special variables in the GNU Make manual.


1 Answers

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') 
like image 88
yashma Avatar answered Sep 27 '22 22:09

yashma