I'm new to building Makefiles and am trying to determine how fail a build target if a variable is empty. I want to be able to pass in the variable as an environment variable or as a make parameter.
Say I have a makefile like this:
VER ?=
step0:
echo "step0 should work"
step1:
echo "step1 should enforce variable"
ifeq($(VER), "")
$(error VER is not set)
endif
echo "Success: Value of Ver ${VER}"
step2:
echo "step2 should work"
I want to be able to run the following test cases:
VER="foo" make step1
# should result in printing the "Success:" line
OR
export VER=foo
make step1
# should result in printing the "Success:" line
OR
make step1 VER=foo
# should result in printing the "Success:" line
OR
make step1
# should result in printing "VER is not set"
However, when I run make step
using any of the above, I always get the VER is not set
error.
Simply put, how can I test for a variable in a specific make target and respond with an error message if it's not set? (but other make targets would not care if the variable is set or not)
A couple of things:
First, you must keep your Make commands and shell commands neatly segregated. This:
ifeq ($(A),$(B))
...
endif
Is Make syntax. You would probably have trouble if you passed that ifeq (...)
to the shell. The commands in a makefile recipe are shell commands, to be passed to the shell. To use the Make ifeq
conditional in the middle of a rule, do it like this:
step1:
some command
ifeq ($(A),$(B))
another command
endif
yet another command
Note that there are no TABs preceding ifeq
and endif
; those are not commands to be passed to the shell, they are for consumption by Make.
Second, this:
ifeq(...)
should be this:
ifeq (...)
The space matters (at least in my version of Make).
Third, this:
ifeq ($(VER), "")
should be this:
ifeq ($(VER),)
unless you actually intend that the variable should contain the string ' ""'.
(You could have discovered those last to yourself, playing with ifeq
in isolation; always test new tools in isolation.)
After those changes, the makefile works for me. If it doesn't work for you, let me know and we'll hammer it out.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments