Can I tell if Makefile uses -jobs?

I want to set some variables based on whether parallel assemblies are enabled, so I tried this:

jobs:
»·echo "executing jobs job"

ifneq (,$(findstring -j,$(MAKEFLAGS)))
»·$(warning "parallel!")
else
»·$(warning "not parallel!")
endif

And here is what happens:

$ make -j2
Makefile:2: "not parallel!"
echo "executing jobs job"
executing jobs job

I also tried testing $(JOBS), but no luck.

Can I tell inside the Makefile that the -jobs option was used?


Additional Information:

$ make --version
GNU Make 3.81
Copyright (C) 2006  Free Software Foundation, Inc.
This is free software; see the source for copying conditions.
There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A
PARTICULAR PURPOSE.

This program built for x86_64-pc-linux-gnu
+4
source share
2 answers

It is surprising that it ${MAKEFLAGS}will receive only -jwhen it is expanded during the expansion of the recipe.

Makefile:

$(warning [${MAKEFLAGS}])

.PHONY: all
all:
    $(warning [${MAKEFLAGS}])
    echo Now do something useful

Run:

$ make -j5
1:1: []
1:5: [ -j --jobserver-fds=3,4]
echo Now do something useful
Now do something useful
+2
source

About the extension MAKEFLAGSin @bobbogo Answer : if we look at the code, I think I can explain the behavior:

, main make define_makeflags .

/* Define the MAKEFLAGS and MFLAGS variables to reflect the settings of the
   command switches.  Include options with args if ALL is nonzero.
   Don't include options with the 'no_makefile' flag set if MAKEFILE.  */

static struct variable *
define_makeflags (int all, int makefile)
{
......

main:

1)

 /* Set up the MAKEFLAGS and MFLAGS variables for makefiles to see.
    Initialize it to be exported but allow the makefile to reset it.  */
 define_makeflags (0, 0)->export = v_export;

2)

 /* Set up MAKEFLAGS and MFLAGS again, so they will be right.  */

 define_makeflags (1, 0);

3)

 /* Set up 'MAKEFLAGS' specially while remaking makefiles.  */
 define_makeflags (1, 1);

, , .

all false. . all false, define_makeflags " ", j . , switch .

SWAG :

, ifneq define_makeflags . , , MAKEFLAGS , Makefile, .

doc1, doc2:

archive.a: ...
ifneq (,$(findstring t,$(MAKEFLAGS)))
        +touch archive.a
        +ranlib -t archive.a
else
        ranlib archive.a
endif

MAKEFLAGS , , char MAKEFLAGS .

- . , -, , . , .

+1

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


All Articles