2017-02-18 51 views
0

我有一個數據處理工作,我希望使用Make進行自動化。需要分幾步處理數百個文件。 不幸的是,基本名稱至少會改變其中一個步驟,但將這些依賴項編寫成一個單獨的文件並將其包含在其中很容易。GNU使用不同文件基名的模式規則

但是,我想避免分別編寫所有這些文件的構建指令(這非常複雜)。

我設想沿着這些路線的東西:

# automatically generated rules, included into make file 
dir1/test.bb: dir2/test_other_name.aa 
# (many more rules like the above, linking xxx.bb to yyy.aa) 

# pattern rule 
%.bb: %.aa 
     # build step using [email protected] $> 

我想是的模式規則提供的規則,以及明確的規則定義的依賴關係。可以這樣做嗎?

+0

一個更具體的例子將極大地澄清這裏提出的問題,我想。 –

回答

0

使的noddy圖案不切芥子, 只是明確地寫出規則。 (這還沒有使用模式規則的愉快的副作用。)

假設你有一個函數src-to-target這將生成目標文件名(即$(call src-to-target,dir2/test_other_name.aa)擴展到dir1/test.bb

而且,你有一個清單在${srcs},並${recipe}來源是shell命令的使用[email protected],列表$<

define src-to-target = ... # $1:source 

define recipe = 
    echo Building [email protected] from $< 
    ⋮ 
endef 

define generate-rule = # $1:source 
target := $(call src-to-taget,$1) 
targets += $${target} 
$${target}: $1 ; $${recipe} 
endef 

$(foreach _,${srcs},$(eval $(call generate-rule,$_))) 

.PHONY: all 
all: ${targets} ; : [email protected] Success 

的​​在這裏所做的所有工作。

所以,看在痛苦的細節,

  • 一是擴大${srcs}
  • 設置$_到列表中的第(dir2/test_other_name.aa說)
  • 展開$(call generate-rule,$_)

    • 展開$(call generate-rule,dir2/test_other_name.aa)
      • $1設置爲dir2/test_other_name.aa,和$(generate-rule)膨脹如下,導致文本

        target := dir1/test.bb
        targets += ${target}
        ${target}: dir2/test_other_name.aa ; ${recipe}

  • 的該塊作爲一個副作用,$(eval)吞下上述文字。 $(eval)的擴展雖然是空的。

  • $_設置爲下一個源文件。
  • 洗淨,起泡,沖洗,重複

一旦$(foreach)完成後, ${targets}包含目標的完整列表。

平行安全也。 不喜歡什麼?