How can I determine which -j option was provided to do

In the Racket system, we have a build step that calls a program that can run several parallel tasks at once. Since this is called from make , it would be nice to respect the -j option, originally called by make .

However, as far as I can tell, there is no way to get the value of the -j parameter inside the Makefile or even as an environment variable in the programs that make calls.

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

+4
source share
2 answers

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

 opts: @echo $(MAKEFLAGS) 

and this will result in the correct value for the -j option.

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

(In make 4.1 it is still broken). Needless to say, instead of echo you can call the script to execute the correct MAKEFLAGS parsing

+2
source

Note. This answer relates to make version 3.82 and earlier. For a better answer from version 4.2 see Dima Pasechnik's answer.


You cannot determine which -j option was provided to create. Information about the number of jobs is not available in the usual way from make or its subprocesses in accordance with the following quote:

The top make and all its sub-make processes use the channel to communicate with each other to ensure that no more than N jobs are launched in all make files.

(taken from a file named NEWS in the make 3.82 source tree)

The top make process acts as a job server, passing tokens to sub-make processes through the pipe. It seems your goal is to do your own parallel processing and still respect the specified maximum number of simultaneous jobs specified in make . To achieve this, you will somehow have to embed yourself in the message through this channel. However, this is an unnamed channel, and as far as I can see, there is no way for your own process to join the server-server mechanism.

By the way, the β€œpre-processed version of flags” that you mentioned contains the expression --jobserver-fds=3,4 , which is used to transfer information about the channel endpoints between make processes. This reveals a bit of what is happening under the hood ...

+1
source

Source: https://habr.com/ru/post/1416154/


All Articles