原始碼集合模組

此模組提供支援,針對單一檔案集合建置多個目標;每個目標中包含哪些檔案,取決於字典或 configuration_data 物件的內容。可以使用以下方式載入模組:

ssmod = import('sourceset')

使用模組的簡單範例看起來像這樣

ss = ssmod.source_set()
# Include main.c unconditionally
ss.add(files('main.c'))
# Include a.c if configuration key FEATURE1 is true
ss.add(when: 'FEATURE1', if_true: files('a.c'))
# Include zlib.c if the zlib dependency was found, and link zlib
# in the executable
ss.add(when: zlib, if_true: files('zlib.c'))
# many more rules here...
ssconfig = ss.apply(config)
executable('exe', sources: ssconfig.sources(),
           dependencies: ssconfig.dependencies())

這等同於

sources = files('main.c')
dependencies = []
if config['FEATURE1'] then
    sources += [files('a.c')]
endif
if zlib.found() then
    sources += [files('zlib.c')]
    dependencies += [zlib]
endif
# many more "if"s here...
executable('exe', sources: sources, dependencies: dependencies)

Sourcesets 可以透過單次呼叫 apply 方法使用,類似於上面的範例,但是當透過將相同的規則應用於許多不同的組態來產生多個可執行檔時,此模組特別有用。

已於 0.51.0 版本新增

函式

source_set()

ssmod.source_set()

建立並傳回新的原始碼集合物件。

傳回:一個 原始碼集合

source_set 物件

source_set 物件提供方法,可將檔案新增至原始碼集合並查詢它。在呼叫除 add 之外的任何方法之後,原始碼集合將變為不可變。

方法

add()

source_set.add([when: varnames_and_deps],
               [if_true: sources_and_deps],
               [if_false: list_of_alt_sources])
source_set.add(sources_and_deps)

規則新增至原始碼集合。規則決定在哪些條件下,某些原始碼檔案或相依性物件會包含在建置組態中。所有原始碼檔案必須存在於原始碼樹中,或者可以透過 configure_filecustom_targetgenerator 在建置樹中建立。

varnames_and_deps 是規則的條件清單,可以是字串或相依性物件(相依性物件是指具有 found() 方法的任何物件)。如果所有字串的評估結果為 true 且找到所有相依性,則規則的評估結果將為 true;然後 apply() 將在其結果中包含 if_true 關鍵字引數的內容。否則,也就是說,如果位置引數中的任何字串評估結果為 false 或未找到任何相依性,則 apply() 將改為使用 if_false 關鍵字引數的內容。

相依性也可以出現在 sources_and_deps 中。在這種情況下,遺失的相依性將被簡單地忽略,而不會停用該規則,類似於 dependencies 關鍵字引數在建置目標中的運作方式。

注意:通常最好避免混合使用原始碼集合和停用器。這是因為停用器將導致規則被完全捨棄,並且不再考慮 list_of_alt_sources

add_all()

source_set.add_all(when: varnames_and_deps,
                   if_true: [source_set1, source_set2, ...])
source_set.add_all(source_set1, source_set2, ...)

將一個或多個原始碼集合新增至另一個原始碼集合。

對於引數中列出的每個原始碼集合,只有在 varnames_and_deps 中的條件的評估結果為肯定時,apply() 才會考慮其規則。例如,以下內容

sources_b = ssmod.source_set()
sources_b.add(when: 'HAVE_A', if_true: 'file.c')
sources = ssmod.source_set()
sources.add_all(when: 'HAVE_B', if_true: sources_b)

等同於

sources = ssmod.source_set()
sources.add(when: ['HAVE_A', 'HAVE_B'], if_true: 'file.c')

all_sources()

list source_set.all_sources(...)

傳回使用 add 放置在原始碼集合中的所有原始碼的清單(包括巢狀原始碼集合),且這些原始碼沒有找不到的相依性。如果規則有找不到的相依性,則僅包含 if_false 原始碼(如果有)。

傳回:檔案物件的清單

all_dependencies() (自 0.52.0 版本起)

list source_set.all_dependencies(...)

傳回使用 add 放置在原始碼集合中的所有相依性的清單(包括巢狀原始碼集合),並且已找到這些相依性。

傳回:相依性的清單

apply()

source_files source_set.apply(conf_data[, strict: false])

將原始碼集合與字典或 configuration_data 物件比對,並傳回原始碼組態物件。原始碼組態物件允許您擷取特定組態的原始碼和相依性。

依預設,規則中指定的所有變數都必須存在於 conf_data 中。但是,在某些情況下,慣例是 conf_data 中不存在 false 組態符號;例如,當組態從 Kconfig 檔案載入時,就是這種情況。在這種情況下,您可以指定 strict: false 關鍵字引數,這會將不存在的變數視為 false。

傳回:一個 原始碼組態

source_configuration 物件

source_configuration 物件提供方法,可查詢在原始碼集合上執行 apply 作業的結果。

方法

sources()

source_config.sources()

傳回與已套用的組態對應的原始碼檔案。

傳回:檔案物件的清單

dependencies()

source_config.dependencies()

傳回與已套用的組態對應的相依性。

傳回:相依性物件的清單

搜尋的結果為