如何使用配方中的eval Makefile变量来有条件地执行配方中的某些命令

时间:2018-01-19 15:13:40

标签: bash makefile recipe

目标是仅在补丁不存在的情况下应用补丁。如果补丁存在则不做任何事情。 我使用下面的makefile规则。

C_FILE_PATCH_SIG=###MAGIC_CODE;

C_FILE_CODE=~/code/file.c
C_PATCH_FILE=~/test.patch

.tmp/patch_c:
    cp ${C_PATCH_FILE} ${SDK}
ifneq ($(PATCH_DONE), 1)
    $(MAKE) applypatch || $(MAKE) helppatch
endif
    @echo DONE > .tmp/patch_c

applypatch:
    @echo "Patching ${C_FILE_CODE}"
    if grep -Fq '${C_FILE_PATCH_SIG}' ${C_FILE_CODE} ; \
    then \
    echo 1 > .tmp/PATCH_PRESENT_file; \
    else \
    echo 0 > .tmp/PATCH_PRESENT_file;\
    fi
    cat .tmp/PATCH_PRESENT_file
#   $(eval PATCH_PRESENT := `cat .tmp/PATCH_PRESENT_file`)
    $(eval PATCH_PRESENT := $(shell cat .tmp/PATCH_PRESENT_file))
    @echo "WWWWWW  PATCH_PRESENT=[$(PATCH_PRESENT)]  WWWWWWW"
ifeq ($(PATCH_PRESENT), 0)
    @echo "Applying the patch $(PATCH_PRESENT)"
    cd ~/code && git apply -v ${C_PATCH_FILE}
else
    @echo "NOT Applying the patch $(PATCH_PRESENT)"
endif

helppatch:
    @echo -e "\n\n\n"
    @echo -e "++++++++++ Apply below patch manually then run 'make build PATCH_DONE=1' ++++++++++\n\n"
    @echo -e "+++++++++++++++++++++++++++++++++++++"
    @cat ${C_PATCH_FILE}
    @echo -e "+++++++++++++++++++++++++++++++++++++"
    @echo -e "\n\n\n"
    false

但它总是评估ifeq的else部分。 我在哪里做错了?

如果我使用带有shell多行的git的patch命令,我会松开git补丁返回的错误代码。 提前谢谢......

1 个答案:

答案 0 :(得分:0)

首次读取makefile时(而不是运行配方时)将评估您的ifeq。另一方面,eval在配方运行(之后)之前不会执行。因此,PATCH_PRESENT在解析时不等于0,而make将扩展该子句的else部分。运行eval时,if语句已经过评估并从内存中消失。

顺便说一句,更简洁的方法是用bash做所有事情:

applypatch:
    @echo "Patching ${C_FILE_CODE}"
    @if grep -Fq '${C_FILE_PATCH_SIG}' ${C_FILE_CODE}; then \
         echo "NOT Applying the patch" \
     else \
         echo "Applying the patch" \
         cd ~/code && git apply -v ${C_PATCH_FILE} \
     fi