How can I tell what -j option was provided to make

旧城冷巷雨未停 提交于 2019-12-23 12:16:20

问题


In Racket's build system, we have a build step that invokes a program that can run several parallel tasks at once. Since this is invoked from make, it would be nice to respect the -j option that make was originally invoked with.

However, as far as I can tell, there's no way to get the value of the -j option from inside the Makefile, or even as an environment variable in the programs that make invokes.

Is there a way to get this value, or the command line that make was invoked with, or something similar that would have the relevant information? It would be ok to have this only work in GNU make.


回答1:


In make 4.2.1 finally they got MAKEFLAGS right. That is, you can have in your Makefile a target

opts:
    @echo $(MAKEFLAGS)

and making it will tell you the value of -j parameter right.

$ make -j10 opts
-j10 --jobserver-auth=3,4

(In make 4.1 it is still broken). Needless to say, instead of echo you can invoke a script doing proper parsing of MAKEFLAGS




回答2:


Note: this answer concerns make version 3.82 and earlier. For a better answer as of version 4.2, see the answer by Dima Pasechnik.


You can not tell what -j option was provided to make. Information about the number of jobs is not accessible in the regular way from make or its sub-processes, according to the following quote:

The top make and all its sub-make processes use a pipe to communicate with each other to ensure that no more than N jobs are started across all makes.

(taken from the file called NEWS in the make 3.82 source code tree)

The top make process acts as a job server, handing out tokens to the sub-make processes via the pipe. It seems to be your goal to do your own parallel processing and still honor the indicated maximum number of simultaneous jobs as provided to make. In order to achieve that, you would somehow have to insert yourself into the communication via that pipe. However, this is an unnamed pipe and as far as I can see, there is no way for your own process to join the job-server mechanism.

By the way, the "preprocessed version of the flags" that you mention contain the expression --jobserver-fds=3,4 which is used to communicate information about the endpoints of the pipe between the make processes. This exposes a little bit of what is going on under the hood...



来源:https://stackoverflow.com/questions/10898528/how-can-i-tell-what-j-option-was-provided-to-make

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!