Makefile - 运行 使用 ifeq 的时间决策
Makefile - run time decisions using ifeq
我正在尝试解决一个特定问题,其中变量在一个配方中分配,然后在其他配方中解释,所有这些都在 运行 时间内完成。据我了解,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
很明显只有 "check" 目标做了它应该做的,其他两个根本不起作用。
我仍然不完全清楚这些目标应该如何交互(它们之间的任何交互通常都是一个坏主意,因为并行 make 执行意味着如果不通过它们之间的先决条件明确排序,则无法保证执行顺序)。但是假设非并行 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
我正在尝试解决一个特定问题,其中变量在一个配方中分配,然后在其他配方中解释,所有这些都在 运行 时间内完成。据我了解,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
很明显只有 "check" 目标做了它应该做的,其他两个根本不起作用。
我仍然不完全清楚这些目标应该如何交互(它们之间的任何交互通常都是一个坏主意,因为并行 make 执行意味着如果不通过它们之间的先决条件明确排序,则无法保证执行顺序)。但是假设非并行 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