Makefile variables - defined internally, from the command line and from the environment
Archive - Originally posted on "The Horse's Mouth" - 2012-03-22 10:23:24 - Graham EllisHow do you control makefiles from the environment in which they are run? You can do so with make variables, which can be set externally from your calling shell / program, or from the make command line.
If I set a shell variable and export it, it will be available in my Makefile.
If I specify a variable on the command line to make, then it will be available in my Makefile.
If I specify a varaible with ":=" within my Makefile, it will be immediatelly avaiable thereafter.
If you specify the same variable in more than one of these ways, it's the latter that takes precedence.
Within my Makefile, I can also specify a variable with "?=", in which case that setting takes a lower precedence - in other words, I'm specifying a safety net to be used only if the value's not specified in the outer shell or on the command line.
And - to add a further step to the complexity - I can also specify a variable with "=" within my Makefile. In this case, the actual substitution is delayed until the variable is actually used (and indeed it's then substitued recuresively). So the value assigned will be a run time value, rather than an immediate value. If you're an experienced Tcl programmer, this is rather like a deferred block in {} as opposed to an immediate parameter in "".
In order to help delegates to whom I was teaching Make earlier this week, I put together an example that sets various variable types. The full make file is [here]. Have a look at the mode variable.
munchkin:csv grahamellis$ make clean
Melksham and
Mode no. 30, written by Graham of Melksham
Written by Graham of [i]
Written by Graham of Melksham [d]
Copyright, Well House Consultants
Copyright, Well House Consultants,
Make level is 0
Mode setting from file and is recursive
munchkin:csv grahamellis$ make mode=40 clean
Melksham and
Mode no. 40, written by Graham of Melksham
Written by Graham of [i]
Written by Graham of Melksham [d]
Copyright, Well House Consultants
Copyright, Well House Consultants,
Make level is 0
Mode setting from command line and is recursive
munchkin:csv grahamellis$ export mode=50
munchkin:csv grahamellis$ make clean
Melksham and
Mode no. 50, written by Graham of Melksham
Written by Graham of [i]
Written by Graham of Melksham [d]
Copyright, Well House Consultants
Copyright, Well House Consultants,
Make level is 0
Mode setting from environment and is recursive
munchkin:csv grahamellis$
You'll note how I can get back and see where the variable was set from as well, using the make origin function, and tell if it's recursive (deferred) or not, using the make flavor function.
Not included within this example - I can also use += to add onto the end of a variable.
This Makefile example also includes manu other features such as GNUMake's conditionals, and other functions - covered during the course and published here for delegates to refer back to online.