Makefile - 使用ifeq运行时决策

时间:2015-05-04 18:29:03

标签: makefile gnu-make

我正在尝试解决一个特定的问题,即在一个配方中分配变量,然后在其他配方中解释,所有这些都在运行时。据我了解,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

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

1 个答案:

答案 0 :(得分:1)

I'm still not entirely clear how these targets are supposed to interact (any interaction between them is generally a bad idea as parallel make execution means that without being explicitly sequenced via prerequisites between them execution order is not guaranteed). But assuming non-parallel make and that each target is supposed to output one of the lines of output I believe this does what you want.

.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