目标是仅在补丁不存在的情况下应用补丁。如果补丁存在则不做任何事情。 我使用下面的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补丁返回的错误代码。 提前谢谢......
答案 0 :(得分:0)
首次读取makefile时(而不是运行配方时)将评估您的ifeq
。另一方面,eval
在配方运行(之后)之前不会执行。因此,PATCH_PRESENT
在解析时不等于0
,而make将扩展该子句的else部分。运行eval
时,if语句已经过评估并从内存中消失。
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