makefile自定义函数

发布于 2025-01-06 13:53:14 字数 472 浏览 1 评论 0原文

我试图在 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

我做错了什么?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

浅忆流年 2025-01-13 13:53:14

ifeq ... else ... endif

我会将条件移出define 指令。无论如何,在Make执行过程中目标平台是不会改变的,所以不需要每次调用platform时都解析$(UNAME)

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

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
硪扪都還晓 2025-01-13 13:53:14

另一种选择是连接 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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文