强制Makefile在构建目标之前执行脚本 [英] Force Makefile to execute script before building targets
问题描述
我正在使用Makefile.
I am using Makefiles.
但是,在执行任何目标之前,有一个我要执行的命令(zsh脚本). 我该怎么做?
However, there is a command (zsh script) I want executed before any targets is executed. How do I do this?
谢谢!
推荐答案
有几种技术可以在构建目标之前执行代码.您应该选择哪一个取决于您想做什么,以及为什么要这样做. (zsh脚本有什么作用?为什么必须执行它?)
There are several techniques to have code executed before targets are built. Which one you should choose depends a little on exactly what you want to do, and why you want to do it. (What does the zsh script do? Why do you have to execute it?)
您可以按照@John的建议进行操作;将zsh脚本放置为第一个依赖项.然后,应将zsh
目标标记为.PHONY
,除非它实际上生成了名为zsh
的文件.
You can either do like @John suggests; placing the zsh script as the first dependency. You should then mark the zsh
target as .PHONY
unless it actually generates a file named zsh
.
另一种解决方案(至少在GNU make中)是调用$(shell ...)
函数作为变量赋值的一部分:
Another solution (in GNU make, at least) is to invoke the $(shell ...)
function as part of a variable assignment:
ZSH_RESULT:=$(shell zsh myscript.zsh)
这将在解析makefile之后且在执行任何目标之前执行脚本.如果您递归调用makefile,它也会执行脚本.
This will execute the script as soon as the makefile is parsed, and before any targets are executed. It will also execute the script if you invoke the makefile recursively.
这篇关于强制Makefile在构建目标之前执行脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!