我正在尝试在Makefile中创建一个自定义函数来检测当前平台并相应地返回正确的文件。这是我的尝试。
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)
失败并出现以下错误。
/bin/sh: Syntax error: "(" unexpected
[Finished]make: *** [all] Error 2
我做错了什么?
答案 0 :(得分:2)
ifeq ... else ... endif
在GNU Make中为conditional directives,它们不能出现在define ... endef
内,因为后者将它们视为文字文本。 (尝试删除@
命令附近的echo
符号,您将看到评估platform
函数的实际结果)
我将条件移出define
指令。无论如何,目标平台在执行Make期间无法更改,因此每次调用$(UNAME)
时都无需解析platform
。
ifeq ($(UNAME),Linux)
platform = $1
else ifneq ($(findstring MINGW32_NT, $(UNAME)),)
platform = $2
else ifeq ($(UNAME),Darwin)
platform = $3
endif
答案 1 :(得分:1)
另一种选择是连接uname
的输出以形成特定格式的平台字符串,并相应地命名平台特定的makefile:
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