makefile ifeq比较了外壳的输出

发布于 2025-02-09 10:21:24 字数 1376 浏览 0 评论 0原文

我正在尝试比较Makefile评估价值的输出(GNU Make)。但是,即使回声值为0ifeq块也不认为它是相等的,如果我只是将0作为比较器。

但是,如果我将以下内容作为比较器$(Shell Echo 0),则ifeq将接受它作为同等值。

需要明确的是,以下是我将要实现的“模拟”(请记住$(eval Zero_value:= $​​(Shell Echo 0))只是一个“模型”,命令替换$(shell echo 0))在某个时候将输出0

zero-test:

    $(eval ZERO_VALUE := $(shell echo 0))
    echo "Echo-ed value: ${ZERO_VALUE}"

ifeq ($(ZERO_VALUE),$(shel echo 0))
    echo "1st approach Value is zero"
else
    echo "1st approach Value is not zero"
endif

ifeq ($(ZERO_VALUE),0)
    echo "2nd approach Value is zero"
else
    echo "2nd approach Value is not zero"
endif

进行进行零测试时的输出是:

Echo-ed value: 0
1st approach Value is zero
2nd approach Value is not zero

最后,我的主要问题是:

  • 为什么当“回声”值实际上是0时,被认为是平等的吗?
  • 有可能让我的第二种方法工作吗?只需对此感到更自然:)

此外,以下是我的make -v输出:

❯ make -v       
GNU Make 4.3
Built for x86_64-pc-linux-gnu
Copyright (C) 1988-2020 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

I am trying to compare output from evaluated value on a Makefile ( GNU make ). However, even when the echo-ed value is 0, the ifeq block not consider it is equal if I just put 0 as the comparator.

But if I put following as a comparator $(shell echo 0), then ifeq will accept it as an equal value.

To be clear, following is a "mock" what I am going to achieve ( keep in mind that $(eval ZERO_VALUE := $(shell echo 0)) is just a "mockup", there will a command replacing $(shell echo 0)) there, which will output 0 at some point.

zero-test:

    $(eval ZERO_VALUE := $(shell echo 0))
    echo "Echo-ed value: ${ZERO_VALUE}"

ifeq ($(ZERO_VALUE),$(shel echo 0))
    echo "1st approach Value is zero"
else
    echo "1st approach Value is not zero"
endif

ifeq ($(ZERO_VALUE),0)
    echo "2nd approach Value is zero"
else
    echo "2nd approach Value is not zero"
endif

The output when doing make zero-test would be:

Echo-ed value: 0
1st approach Value is zero
2nd approach Value is not zero

Finally, my main questions are:

  • Why ($(ZERO_VALUE),0) is not considered equal while the "echo-ed" value is actually 0?
  • Is it possible to have my 2nd approach work? Just feel more natural with that :)

In addition, following is my make -v output:

❯ make -v       
GNU Make 4.3
Built for x86_64-pc-linux-gnu
Copyright (C) 1988-2020 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

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

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

发布评论

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

评论(1

我还不会笑 2025-02-16 10:21:25

我看到了几个问题:

  • 您在食谱中分配了变量,然后尝试在食谱之外进行测试。那只是行不通的。
  • 您尝试在同一食谱中读取变量。如果没有Oneshell,则在配方中的每一行都在单独的子壳中运行,这意味着没有可变设置持续存在。
  • 您使用评估来分配变量。那不是您(应该)这样做的方式。
  • 您拼错了“ Shel”。

尝试解决上述问题:

# Set outside recipe for ifeq to work right
ZERO_VALUE := $(shell echo 0)

zero-test:
    echo "Echo-ed value: ${ZERO_VALUE}"

# Fix "shel" typo
ifeq ($(ZERO_VALUE),$(shell echo 0))
    echo "1st approach Value is zero"
else
    echo "1st approach Value is not zero"
endif

ifeq ($(ZERO_VALUE),0)
    echo "2nd approach Value is zero"
else
    echo "2nd approach Value is not zero"
endif

如果您必须在食谱中传达零value,则应考虑使用oneshell或使用文件代替变量,因为它们持续在子壳调用中。

# Beware of accidental unquoted expansions...
.ONESHELL:
zero-test:
    ZERO_VALUE="$(echo 0)"
    echo "Echo-ed value: $ZERO_VALUE"

I see several problems:

  • You assign the variable within a recipe, then try test it outside the recipe. That just won't work.
  • You try read the variable within the same recipe. Without ONESHELL each line in a recipe is run in a separate subshell, meaning no variable settings persist.
  • You use eval to assign the variable. That's not how you (should) do it.
  • You misspelled "shel".

Attempts to fix above problems:

# Set outside recipe for ifeq to work right
ZERO_VALUE := $(shell echo 0)

zero-test:
    echo "Echo-ed value: ${ZERO_VALUE}"

# Fix "shel" typo
ifeq ($(ZERO_VALUE),$(shell echo 0))
    echo "1st approach Value is zero"
else
    echo "1st approach Value is not zero"
endif

ifeq ($(ZERO_VALUE),0)
    echo "2nd approach Value is zero"
else
    echo "2nd approach Value is not zero"
endif

If you must communicate ZERO_VALUE within the recipe you should consider either using ONESHELL, or use files instead of variables since they persist across subshell invocations.

# Beware of accidental unquoted expansions...
.ONESHELL:
zero-test:
    ZERO_VALUE="$(echo 0)"
    echo "Echo-ed value: $ZERO_VALUE"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文