Makefile 在运行make目标之前检查cmake
我已经完成了以下makefileMakefile 在运行make目标之前检查cmake,makefile,cmake,Makefile,Cmake,我已经完成了以下makefile .PHONY: check check: @[ -f `which cmake` ] || $(error Install cmake first) .PHONY: check2 check2: @[ -f `which cmake` ] || echo "Install cmake first" .PHONY: exec exec: check @echo "Compiling executable ..." @[
.PHONY: check
check:
@[ -f `which cmake` ] || $(error Install cmake first)
.PHONY: check2
check2:
@[ -f `which cmake` ] || echo "Install cmake first"
.PHONY: exec
exec: check
@echo "Compiling executable ..."
@[ -d ./build ] || mkdir ./build
@cd ./build; cmake ../src; make
当targetcheck2工作时(当cmake存在时,我没有收到消息“先安装cmake”,当它不存在时,我收到消息),targetcheck始终退出,即使cmake存在
因此,我不能对targetexec使用targetcheck。我必须使用targetcheck2,当然,还必须放弃,因为它找不到cmake
我希望make在开始回显“编译可执行文件…”之前退出。当make决定需要重建目标时,它首先要做的是展开配方中的所有make宏(变量)和函数。然后,它将生成的展开文本发送到shell 在“检查”规则中,您使用的是make函数$(error…),因此,make一旦决定构建该目标,就会展开配方,其中包括展开$(error…)函数和make退出。shell不可能运行命令的一部分(如
[-f…]
),然后以某种方式指示make应该调用或不应该调用make函数
如果你想让make失败,你所要做的就是让你的配方失败。你的检查2
配方会打印一条消息,但它仍然成功,因此它不会阻止make继续。你可以这样做:
.PHONY: check2
check2:
@[ -f `which cmake` ] || { echo "Install cmake first"; exit 1; }
.PHONY: exec
exec: check2
@echo "Compiling executable ..."
@[ -d ./build ] || mkdir ./build
@cd ./build; cmake ../src; make
或者,您可以将测试放入exec目标本身:
.PHONY: exec
exec:
@[ -f `which cmake` ] || { echo "Install cmake first"; exit 1; }
@echo "Compiling executable ..."
@[ -d ./build ] || mkdir ./build
@cd ./build; cmake ../src; make
如果您在其他任何地方都不需要它。当make决定需要重建目标时,它所做的第一件事就是展开配方中的所有make宏(变量)和函数。然后它将生成的展开文本发送到shell 在“检查”规则中,您使用的是make函数$(error…),因此,make一旦决定构建该目标,就会展开配方,其中包括展开$(error…)函数和make exit。shell不可能运行命令的一部分(如
[-f…)
然后,以某种方式指示make应该调用或不应该调用make函数
如果你想让make失败,你所要做的就是让你的配方失败。你的检查2
配方会打印一条消息,但它仍然成功,因此它不会阻止make继续。你可以这样做:
.PHONY: check2
check2:
@[ -f `which cmake` ] || { echo "Install cmake first"; exit 1; }
.PHONY: exec
exec: check2
@echo "Compiling executable ..."
@[ -d ./build ] || mkdir ./build
@cd ./build; cmake ../src; make
或者,您可以将测试放入exec目标本身:
.PHONY: exec
exec:
@[ -f `which cmake` ] || { echo "Install cmake first"; exit 1; }
@echo "Compiling executable ..."
@[ -d ./build ] || mkdir ./build
@cd ./build; cmake ../src; make
如果你在其他地方不需要它