我想用一個Makefile寫一個文件static/config.js
以下方式:如何使Makefile注意到新文件的存在?
- 如果
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
文件做一些同樣的問題。
也使用'$(config_file)'作爲prereq。但是,如果'static/config.js'比'js/config_local.js'更新,那麼這對你的示例場景沒有幫助,因爲這就是make的作用(避免了它不需要再做的工作)。 –
另外'config_file = $(word 1,$(通配符js/config_local.js js/config.js))'會爲您提供一行中存在的第一個。 –
@Etan - 將'$(config_file)'添加到'static/config.js'的依賴關係似乎沒有改變任何東西。 – Avril