【问题标题】:Makefile - run time decisions using ifeqMakefile - 使用 ifeq 的运行时决策
【发布时间】:2015-05-04 18:29:03
【问题描述】:

我正在尝试解决一个特定问题,其中变量在一个配方中分配,然后在其他配方中进行解释,所有这些都是在运行时进行的。据我了解, ifeq 条件在解析过程中进行评估,这对我不起作用,因为其中一些总是错误的。有没有办法实现我正在尝试做的事情(预期输出如下)?如果需要,我会提供更多信息。

我在 Linux Mint 17.1 上使用 make 版本 3.81。

这是我目前所拥有的:

fourth =

all: check valueOfFourth definitionOfFourth
.PHONY: all

check:
    @echo "TEST"$(cnt)
ifeq ($(first),$(second))
    @echo "1. First condition"
    $(eval fourth = "first")
else ifeq ($(first),$(third))
    @echo "1. Second condition"
    $(eval fourth = "second")
else
    @echo "1. Conditions weren't met"
endif

valueOfFourth:
ifeq ($(fourth),"first")
    @echo "2. First"
else ifeq ($(fourth),"second")
    @echo "2. Second"
else
    @echo "2."
endif

definitionOfFourth:
ifeq ($(fourth),)
    @echo "3. Variable is not defined"
else
    @echo "3. Variable is defined"
endif

它是这样调用的:

make cnt="1" first="x" second="x" third="y" && printf "\n" && \
make cnt="2" first="x" second="y" third="x" && printf "\n" && \
make cnt="3" first="x" second="y" third="z"

预期输出:

TEST1
1. First condition
2. First
3. Variable is defined

TEST2
1. Second condition
2. Second
3. Variable is defined

TEST3
1. Conditions weren't met
2. 
3. Variable is not defined

实际输出:

TEST1
1. First condition
2. 
3. Variable is not defined

TEST2
1. Second condition
2. 
3. Variable is not defined

TEST3
1. Conditions weren't met
2. 
3. Variable is not defined

很明显,只有“检查”目标做了它应该做的事情,其他两个根本不起作用。

【问题讨论】:

  • 你为什么要这样做?你想用这个解决什么问题?使用特定于目标的变量分配(适用于所有目标的先决条件)对您有帮助吗?
  • @EtanReisner 这些只是教授给我们的练习。我,我自己,一般来说是全新的。这与构建源代码没有太大关系,它只是在玩变量和控制流。说明不是很清楚,这肯定没有帮助。
  • 练习是让它与这里写的 make 一起工作?或者,更一般地说,在 make 中使用条件来影响通过 make 过程发生的事情?
  • 不是让它按书面形式工作(这就是我写的试图解决这个问题),而是通过使用 ifeq、变量、虚假目标等来获得预期的输出。
  • 您是否有特定的虚假目标或需要支持的命令行来执行此操作?

标签: makefile gnu-make


【解决方案1】:

我仍然不完全清楚这些目标应该如何交互(它们之间的任何交互通常都是一个坏主意,因为并行执行意味着如果没有通过它们之间的先决条件显式排序,则无法保证执行顺序)。但是假设非并行 make 并且每个目标都应该输出其中一条输出线,我相信这可以满足您的需求。

.PHONY: all
all: check valueOfFourth definitionOfFourth

ifeq ($(first),$(second))
    fourth = First
    condmsg = $(fourth) condition
else ifeq ($(first),$(third))
    fourth = Second
    condmsg = $(fourth) condition
else
    condmsg = Conditions weren'\''t met
endif

check:
    @echo 'TEST$(cnt)'
    @echo '1. $(condmsg)'

valueOfFourth:
    @echo '2. $(fourth)'

definitionOfFourth:
ifeq ($(fourth),)
    @echo "3. Variable is not defined"
else
    @echo "3. Variable is defined"
endif

【讨论】:

  • 这不是我在这里得到的。我刚刚复制并粘贴了这个 makefile(针对目标中的前导选项卡进行了编辑)和来自 OP 的命令并得到了预期的输出。有多个不同版本的make。确保您正确复制了生成文件。 md5sum Makefile -> 290fb1651f4c9257908d988c6295d585.
  • 您可以尝试将其上传到文件托管服务,因为简单的 C/P 会导致空格而不是制表符,因此 make 解析器会有点生气。我必须更改这些 - 哈希肯定不会相同。
  • 哈希是标签文件的。我运行 make 并得到你的输出的文字文件。您应该能够对复制和粘贴的文件进行空格制表符并获得具有相同总和的文件。这就是为什么我说我在粘贴并列出总和后编辑了前导选项卡。只有五行需要编辑。 sed -e '15,16s/^\s\+/\t/;19s/^\s\+/\t/;23,25s/^\s\+/\t/' 没有尾随空格,末尾没有空行等
猜你喜欢
  • 2011-12-01
  • 2011-09-21
  • 2012-08-13
  • 2012-04-13
  • 1970-01-01
  • 2023-03-07
  • 1970-01-01
  • 1970-01-01
  • 2016-02-03
相关资源
最近更新 更多