簡體   English   中英

特定於目標的變量作為 Makefile 中的先決條件

[英]Target-specific Variables as Prerequisites in a Makefile

我正在嘗試編寫一個具有類似目標負載的 GNU make Makefile,其中構建命令在它們之間略有不同。 我正在嘗試使用特定目標的變量來表示這些變化。 其中一些變量值指的是我想用作先決條件的文件。 例如:

target_1:special_filename=target1_prereq
target_2:special_filename=target2_prereq

target_1 target_2: common_filename $(special_filename)
    do_something common_filename --a-weird-option=$(special_filename)

當我調用'make target_1'時,如果它不存在,我希望它make target1_prereq。 目前,它似乎沒有使用 target1_prereq 作為先決條件,即使使用正確的參數調用構建命令 (do_something)。

我正在使用 GNU Make 3.80。


編輯:來自真實系統的更多並發症。 一些變量本身基於其他變量的值。 手動指定先決條件不會擴展。 一個稍微復雜的例子:

target_1:special_filename_base=target1_prereq
target_2:special_filename_base=target2_prereq

some_filename_a = $(special_filename_base).exta
some_filename_b = $(special_filename_base).extb

target_1 target_2: common_filename $(special_filename_b) $(special_filename_a)
    do_something common_filename --a-weird-option=$(special_filename_a) --second=$(special_filename_b)

特定於目標的變量僅在目標的命令(或其他特定於目標的賦值)中定義; 它不能用作目標的先決條件之一。 我認為在 Make 中沒有一種干凈的方法可以做你想做的事情,但是有幾種笨拙的方法,例如:

EXTENSIONS = .exta .extb
target_1: $(addprefix target1_prereq,$(EXTENSIONS))
target_2: $(addprefix target2_prereq,$(EXTENSIONS))

target_1 target_2: common_filename
    do_something common_filename --a-weird-option=$(filter %.exta,$^) --second=$(filter %.extb,$^)

作為一個簡單的解決方法:

target_1:special_filename=target1_prereq
target_1:target1_prereq
target_2:special_filename=target2_prereq
target_2:target2_prereq

target_1 target_2: common_filename $(special_filename)
    do_something common_filename --a-weird-option=$(special_filename)

有一些冗余,但它是本地化的,所以還不錯。

我找到了一種相當干凈的方法來繞過這個限制。 它會是這樣的:

target_1:export special_filename_base=target1_prereq
target_2:export special_filename_base=target2_prereq

some_filename_a = $(special_filename_base).exta
some_filename_b = $(special_filename_base).extb

target_1 target_2:
    $(MAKE) -f $(firstword $(MAKEFILE_LIST)) target-proxy

target-proxy: common_filename $(special_filename_b) $(special_filename_a)
    do_something common_filename --a-weird-option=$(special_filename_a) --second=$(special_filename_b)

兩個重要的點:

  1. export目標變量,以便在我們重新運行 Makefile 時可以訪問它們。
  2. 創建一個具有target_1 target_2所有原始先決條件的代理目標,並在target_1 target_2再次使用此代理目標調用 Makefile。 由於目標特定變量將不得不那么值(我們在那個時候的配方)他們export版,他們將在提供target-proxy -瞧:)

這種方法的缺點是我們正在創建另一個make過程 - 如果它只是另一個過程,那么它可能沒問題,但是 YMMV 所以要格外小心。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM