Can I tell Make to ignore environment variables?

I came across some weird behavior when using Make recent (v3.81).

Suppose I have the following Makefile:

FOOBAR = $(shell find nonexistentdirectory -iname "foobar" | tr '\n' ' ') all: @echo "Hi" clean: @echo "Hi again" 

It seems simple enough, right? It is noteworthy that FOOBAR is a "recursively extended variable", so it cannot be evaluated until I refer to it. Please note that I never turn to him. Also note that nonexistentdirectory does not exist, as you might expect.

Now suppose I install FOOBAR in a shell before I invoke Make target:

 compy386$ FOOBAR="stuff" make clean find: nonexistentdirectory: No such file or directory Hi again 

Why is FOOBAR ? Obviously, this is due to the fact that the variable is defined in the shell. Should I assume that the user could set the variables in his shell? What am I doing wrong here ?!

+6
source share
2 answers

FOOBAR is evaluated because you use the recipe (for clean ) and FOOBAR exists in the environment when you FOOBAR make . Since FOOBAR existed in the environment upon entry, it becomes an exported variable, which means that make will provide it as part of the environment for any program in the recipe. make does not have a special case echo ; he just uses the one that is found in your path. And he cannot know whether this utility will refer to a specific environment variable, so it should export all exported variables, which means that it should evaluate FOOBAR .

(For the official word, see the third paragraph, Environment Variables in the make manual. Also see the recursive make invocation recipe.)

To answer a direct question, you can tell make ignore the fact that the variable comes from the environment, so it does not re-export it without exporting it:

 unexport FOOBAR 
+2
source

Add ? before = as follows:

 FOOBAR ?= $(shell find nonexistentdirectory -iname "foobar" | tr '\n' ' ') 

This will use the environment variable if it is set, and only evaluate if it is not set or empty.

0
source

All Articles