Question

Is there a way to detect whether a variable has been set from the environment vs. on the command line?

I would like to distinguish between someone invoking make with make LIB=mylib vs. make and $LIB being defined.

Was it helpful?

Solution

Yes. You can use the origin function to determine where a variable was defined.

ifneq (,$(findstring environment,$(origin LIB)))
    # LIB was defined by the environment
else
    # LIB was defined some other way
endif

OTHER TIPS

With non-Gnu make, you could run the export command and grep for the variable in question. This works only in rules and only as long as the variable is not set as a one-shot (like in LIB=foo make).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top