2017-05-16 63 views
0

我需要複製幾個文件。做一個生成文件:用GNU複製文件使

FILES=foo.txt d1/bar.dat d2/baz.txt 
TARGETDIR=/app 

targets=$(addprefix $(TARGETDIR)/,$(FILES)) 

all: $(targets) 

$(targets): $(FILES) 
     cp $(subst $(TARGETDIR)/,,[email protected]) [email protected] 

文件複製正確,但如果我不touch foo.txt,所有這三個文件複製。

我知道「正確的方法」是這樣定義的三個規則:

$(TARGETDIR)/foo.txt: foo.txt 
    cp $^ [email protected] 
$(TARGETDIR)/d1/bar.dat: d1/bar.dat 
    cp $^ [email protected] 
$(TARGETDIR)/d2/baz.txt: d2/baz.txt 
    cp $^ [email protected] 

但在這種情況下,我不得不爲all規則寫入文件的名稱兩次,一次爲這些規則和一次。

有沒有辦法在前提條件下爲每個名稱的「規則」乘以規則? 喜歡的東西

$(TARGETDIR)/%: $(FILES) 
    cp $< [email protected] 

回答