如何让Makefile注意到新文件的存在?

时间:2015-04-16 19:19:28

标签: makefile

我想使用Makefile以下列方式编写文件static/config.js

  • 如果js/config_local.js存在,请将其复制到static/config.js
  • 否则,将js/config.js(始终存在)复制到static/config.js

到目前为止,我有一些看起来像这样的东西:

# If there's a config_local.js file, use that, otherwise use config.js
ifneq ($(wildcard js/config_local.js),)
config_file = js/config_local.js
else
config_file = js/config.js
endif

static/config.js: js/config.js js/config_local.js
    cp $(config_file) static/config.js

js/config_local.js:

clean:
    rm -f static/*

除了如果没有js/config_local.js文件且我运行make,然后我创建一个js/config_local.js文件并再次运行make之外,这大部分都有效,它认为它不需要做任何事情。我猜这是因为Makefile中的空js/config_local.js目标,但如果删除它,那么如果js/config_local.js文件不存在则无法构建。

我还尝试删除空的js/config_local.js目标并将static/config.js目标的依赖关系设置为js/*.js,但这有同样的问题,即我没有注意到它需要做一些事情创建js/config_local.js文件。

1 个答案:

答案 0 :(得分:2)

检查文件时间,而不是内容。 A .PHONY将始终强制操作。缺点是它总是复制。使用-p开关可以保留文件时间。

.PHONY: static/config.js
static/config.js : $(firstword $(wildcard js/config_local.js js/config.js))
cp -p $< $@
相关问题