Reputation:
I'm trying to make a custom function in a Makefile to detect the current platform and return the proper file accordingly. Here is my attempt.
UNAME := $(shell uname -s)
define platform
ifeq ($(UNAME),Linux)
$1
else ifneq ($(findstring MINGW32_NT, $(UNAME)),)
$2
else ifeq ($(UNAME),Darwin)
$3
endif
endef
all:
@echo $(call platform,linux,windows,mac)
It fails with the following error.
/bin/sh: Syntax error: "(" unexpected
[Finished]make: *** [all] Error 2
What am I doing wrong?
Upvotes: 1
Views: 3409
Reputation: 136266
Another option would be to concatenate outputs of uname
to form a platform string in a certain format and have platform specific makefiles named accordingly:
ARCH := $(firstword $(shell uname -m))
SYS := $(firstword $(shell uname -s))
# ${SYS}.${ARCH} expands to Linux.x86_64, Linux.i686, SunOS.sun4u, etc..
include ${SYS}.${ARCH}.mk
Upvotes: 0
Reputation: 25493
ifeq ... else ... endif
are conditional directives in GNU Make, they can't appear inside define ... endef
because the latter treats them as a literal text. (Try to remove @
sign near echo
command and you will see the actual result of evaluating platform
function)
I'd move conditionals out of the define
directive. Anyway, the target platform can't change during the execution of Make, so there is no need to parse $(UNAME)
each time you call platform
.
ifeq ($(UNAME),Linux)
platform = $1
else ifneq ($(findstring MINGW32_NT, $(UNAME)),)
platform = $2
else ifeq ($(UNAME),Darwin)
platform = $3
endif
Upvotes: 3