GNU make:在Makefile中将参数提取到-j [英] GNU make: Extracting argument to -j within Makefile

查看:316
本文介绍了GNU make:在Makefile中将参数提取到-j的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经搜索了一个小时,而该信息似乎无处不在...

I've been searching for an hour, and this information appears to be nowhere...

我希望能够提取(并可能使用)通过-j选项传递的请求的make作业"的数量,或者在子文件的Make中通过Make本身传递的数量.

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.

到目前为止,我看到的最有希望的事情是$(MAKEFLAGS)变量,但是在我的系统上(如果我这样做,例如,使-j2),该变量的内容仅是"--jobserver-fds = 3,4 -j".有什么方法可以获取通过-j传递的实际作业数量?

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?

推荐答案

实际上,有一种方法可以在* nix上的makefile中完全实现此功能.

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,但这几乎是给定的.它也可以进一步改进以处理--jobs

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

根据评论中的要求使用正则表达式并支持--jobs的版本:

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')

这篇关于GNU make:在Makefile中将参数提取到-j的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆