如何让 Makefile 通知新文件的存在?

How can I make a Makefile notice the existence of a new file?

我想使用 Makefile 以下列方式编写文件 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 文件之后。

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

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