3
votes

The docs provides:

'.EXPORT_ALL_VARIABLES'

Simply by being mentioned as a target, this tells 'make' to export all variables to child processes by default. *Note Communicating Variables to a Sub-'make': Variables/Recursion.

However, the following makefiles show that only by making .EXPORT_ALL_VARIABLES a phony target, then and only then, will it have the desired effect on the makefile, i.e. to export ALL variables.

Makefile(version 1) is:

ifeq "$(MAKELEVEL)" "0"

foo=bar

.DEFAULT:;

all: .EXPORT_ALL_VARIABLES
    @$(MAKE)

else

all:
    @echo 'foo is: $(foo)'

endif

Running, we get:

make[1]: Entering directory '/home/myname'
foo is:
make[1]: Leaving directory '/home/myname'

Makefile(version 2) is:

ifeq "$(MAKELEVEL)" "0"

foo=bar

.DEFAULT:;

all: .EXPORT_ALL_VARIABLES
    @$(MAKE)

# This line is added in THIS version.
.PHONY: .EXPORT_ALL_VARIABLES

else

all:
    @echo 'foo is: $(foo)'

endif

Running, we get:

make[1]: Entering directory '/home/myname'
foo is: bar
make[1]: Leaving directory '/home/myname'

Now, the only difference between these 2 versions of makefile, is that in the 2nd version, the .EXPORT_ALL_VARIABLES was made phony.

Why is the 'phoniness' needed in order to work?

1

1 Answers

5
votes

Simply by being mentioned as a target,

Why is the 'phoniness' needed in order to work?

It's not. You didn't declare .EXPORT_ALL_VARIABLES as a target, you declared it as a prerequisite:

all: .EXPORT_ALL_VARIABLES

That's a prerequisite, not a target. If you declare it as a target:

.EXPORT_ALL_VARIABLES:

then it will work and you won't have to declare it phony.

A more accurate question would be, why does declaring .EXPORT_ALL_VARIABLES as phony work even though it's not declared as a target? It happens because things that are marked phony are assumed to be targets even if they're not explicitly mentioned as such. That may or may not be a bug, depending on how you interpret the intent of .PHONY.

Your questions recently seem to follow a pattern: read the documentation, then write a makefile that does something similar to but not the same as what the documentation says, observe it doesn't work as described, then ask why not.