makefile自定义函数

时间:2012-02-19 02:32:23

标签: cross-platform makefile gnu-make

我正在尝试在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

我做错了什么?

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