根据两个 shell 变量的状态退出 Makefile

发布于 2024-09-18 14:14:53 字数 814 浏览 5 评论 0原文

make checksource 期间,$CROSS_COMPILE 应为 "whatever"。如果 $CROSS_COMPILE 未设置,make 应该抛出错误并退出。

我的 Makefile 中有以下规则:

.PHONY: checksource

all: checksource default

checksource:
    $(if $(and $(ifeq ($(CROSS_COMPILE), whatever)), $(ifeq ($(VARIABLE),))), \
    ($(shell echo "Error! VARIABLE not defined!") \
     $(shell exit 2)))

现在,如果 $CROSS_COMPILE 设置为任意值并且 $VARIABLE 未定义,则 make 不退出。

$CROSS_COMPILE

$> echo $CROSS_COMPILE
whatever
$>

$VARIABLE未定义:

$> echo $VARIABLE
$>

我可以使用嵌套的ifeq,但我想让它变得漂亮(并了解更多信息)关于 Makefile 操作)。

During make checksource, $CROSS_COMPILE should be "whatever". If $CROSS_COMPILE is not set, make should throw an error and exit.

I have the following rule in my Makefile:

.PHONY: checksource

all: checksource default

checksource:
    $(if $(and $(ifeq ($(CROSS_COMPILE), whatever)), $(ifeq ($(VARIABLE),))), \
    ($(shell echo "Error! VARIABLE not defined!") \
     $(shell exit 2)))

Right now if $CROSS_COMPILE is set to whatever and $VARIABLE is undefined, the make doesn't exit.

$CROSS_COMPILE:

gt; echo $CROSS_COMPILE
whatever
gt;

$VARIABLE is not defined:

gt; echo $VARIABLE
gt;

I could use a nested ifeq, but I want to make it pretty (and learn a bit more about Makefile operations).

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

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

发布评论

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

评论(2

内心激荡 2024-09-25 14:14:53

不存在$(ifeq)这样的东西。我仍然认为你应该在 makefile 本身中进行检查,而不是作为目标之一:

ifeq ($(CROSS_COMPILE),whatever)
ifeq ($(VARIABLE),)
$(error Variables not set correctly.)
endif
endif

如果你决定避免嵌套 ifeq

ifeq ($(or $(subst whatever,,$(CROSS_COMPILE)),$(VARIABLE)),)
$(error Variables not set correctly.)
endif

但我看不出这是一个改进。如果您想在目标中执行此操作,只需使用 shell,而不必担心 make 函数:

checksource:
    @if [ "$(CROSS_COMPILE)" = whatever -a -z "$(VARIABLE)" ]; then \
        echo "Error: Variables not set correctly"; exit 2; \
    else true; fi

我仍然会选择第一个选项,因为您可以在 stat 全部完成之前停止 make检查 Makefile 中的文件名并决定开始执行 checksource

There is no such thing as $(ifeq). I still think you should do the check in the makefile itself, not as one of the targets:

ifeq ($(CROSS_COMPILE),whatever)
ifeq ($(VARIABLE),)
$(error Variables not set correctly.)
endif
endif

And if you're set on avoiding nested ifeq:

ifeq ($(or $(subst whatever,,$(CROSS_COMPILE)),$(VARIABLE)),)
$(error Variables not set correctly.)
endif

But I fail to see how that's an improvement. If you want to do it in a target, just use the shell and don't bother with make functions:

checksource:
    @if [ "$(CROSS_COMPILE)" = whatever -a -z "$(VARIABLE)" ]; then \
        echo "Error: Variables not set correctly"; exit 2; \
    else true; fi

I'd still go with the first option, because you can stop make before it stat s all the files names in Makefile and decides to start executing checksource.

睡美人的小仙女 2024-09-25 14:14:53

在 make 中执行此操作始终比使用 shell 更好(无论是通过 $(shell) 还是配方)。如果您确实在配方中进行了检查,则意味着 Makefile 可以包含不需要此特定断言的其他目标。

assert = $(if $(filter whatever,${CROSS_COMPILE}),$(if ${VARIABLE},,$(error Urk! Variable problem)))

checksource:
        ${assert}some shell commands...

PS 如果您使用 --warn-undefined-variables 运行原始 make,您可能已经知道为什么宏无法正确扩展:

$ make -f 1.mak CROSS_COMPILE=whatever --warn-undefined-variables
1.mak:6: warning: undefined variable `ifeq (whatever, whatever)'
make: *** No rule to make target `default', needed by `all'.  Stop.

Doing it in make is always better than using the shell (whether via $(shell) or a recipe). If you do do the check in a recipe, then it means that the Makefile can contain other targets that do not need this particular assert.

assert = $(if $(filter whatever,${CROSS_COMPILE}),$(if ${VARIABLE},,$(error Urk! Variable problem)))

checksource:
        ${assert}some shell commands...

P.S. If you ran your original make with --warn-undefined-variables you may have got some clue why your macros were not expanding properly:

$ make -f 1.mak CROSS_COMPILE=whatever --warn-undefined-variables
1.mak:6: warning: undefined variable `ifeq (whatever, whatever)'
make: *** No rule to make target `default', needed by `all'.  Stop.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文