makefile - 仅当先决条件发生变化时,如何让 GNU make 重建虚假目标?

标签 makefile gnu-make

就我而言,我有 requirements目标,安装需要的 Python 包和 test ,它运行测试并依赖于前一个。

安装依赖项是一个很长的操作,我希望它仅在 requirements.txt 时执行变化。我怎样才能做到这一点?

这是我现在拥有的 Makefile 的一个简化示例:

.PHONY: test requirements

requirements: requirements.txt
    pip install -r $<

test: tests/ | requirements
    py.test $^

最佳答案

正如@user1034749 指出的,Make 比较了文件的修改时间。如果你想让它知道什么时候 requirements.txt自从上次安装后被修改,你必须给它一个修改时间与上次安装时间相同的文件,以便它可以比较两者。换句话说,您必须有一个虚拟文件并在执行安装时对其进行修改。你可以叫它任何你喜欢的东西,但我会称之为“安装”:

.PHONY: test

installation: requirements.txt
    pip install -r $<
    touch $@

test: tests/ | installation
    py.test $^

关于makefile - 仅当先决条件发生变化时,如何让 GNU make 重建虚假目标?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33597117/

相关文章:

c - 架构冲突?在 mac 上找不到体系结构 x86_64 的符号

docker - 在传递给 docker run 命令的变量中转义 $

makefile - 如何在GNU make中获取makefile的父目录的基本名称?

makefile - 判断 make 是在 windows 还是 linux 上运行

makefile - 无法在 makefile 中打印换行符

C Makefile 链接器错误 -

c++ - 大型 C++ 项目的 Makefile 模板?

makefile - 如何防止 Webpack 从 Makefile 配方中多次运行

makefile - Makefile中的可变目标

c++ - 无法从 CMake 生成 makefile