I am trying to write a Makefile that evaluates results from Bash commands, e.g., uname
.
Makefile:
OS1 = $(uname)
OS2 = Darwin
all:
@echo $(value OS1)
ifeq ($(uname),Darwin)
@echo "OK"
else
@echo "Fail"
endif
ifeq ($(OS1),Darwin)
@echo "OK"
else
@echo "Fail"
endif
ifeq ($(OS2),Darwin)
@echo "OK"
else
@echo "Fail"
endif
Output:
Darwin
Fail
Fail
OK
How can I compare the variable OS1
or the command $(uname)
to the literal Darwin
inside an ifeq
? From what I have read, the second ifeq
in my Makefile should work, but it doesn't.
I am using GNU Make 3.81 for i386-apple-darwin11.3.0 on OS X 10.9.3.
So put SHELL := /bin/bash at the top of your makefile, and you should be good to go. See "Target-specific Variable Values" in the documentation for more details. That line can go anywhere in the Makefile, it doesn't have to be immediately before the target.
The ifeq directive begins the conditional, and specifies the condition. It contains two arguments, separated by a comma and surrounded by parentheses. Variable substitution is performed on both arguments and then they are compared.
There are a lot of different questions and answers about Makefiles, variables and shell commands. But as it turns out, looking into the manual is sometimes more reliable than searching Stackoverflow.
First, I didn't know the different ways that variables can be assigned in GNU make: https://www.gnu.org/software/make/manual/make.html#Reading-Makefiles
Second, the shell
function, which is required in this case, only works in combination with the :=
(immediate) operator:
https://www.gnu.org/software/make/manual/make.html#Shell-Function
Thus the correct Makefile looks like this:
OS := $(shell uname)
all:
@echo $(OS)
ifeq ($(shell uname),Darwin)
@echo "OK"
else
@echo "Fail"
endif
ifeq ($(OS),Darwin)
@echo "OK"
else
@echo "Fail"
endif
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With