2012-02-19 75 views
0

我想在Makefile中制作一个自定义函数来检测当前平台并相应地返回正确的文件。这是我的尝试。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 

我在做什么错?

回答

1

另一种选择是来连接的uname输出,以形成一定格式的平台字符串,并有相应的命名特定于平台的生成文件:

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 
2

ifeq ... else ... endif在GNU Make中为conditional directives,它们不能出现在define ... endef的内部,因为后者将它们视为文字文本。 (尝试删除近echo命令@标志,你会看到评估platform功能的实际结果)

我希望移居条件语句出define指令。无论如何,在Make的执行过程中,目标平台不能更改,因此每次调用platform时都不需要解析$(UNAME)

ifeq ($(UNAME),Linux) 
    platform = $1 
else ifneq ($(findstring MINGW32_NT, $(UNAME)),) 
    platform = $2 
else ifeq ($(UNAME),Darwin) 
    platform = $3 
endif