停用建置的某些部分

此功能自 0.44.0 版本開始提供。

以下是在許多專案中常見的片段

dep = dependency('foo')

# In some different directory

lib = shared_library('mylib', 'mylib.c',
  dependencies : dep)

# And in a third directory

exe = executable('mytest', 'mytest.c',
  link_with : lib)
test('mytest', exe)

這樣做沒問題,但是當您想讓建置的這部分變成可選時,就會變得有點不靈活。基本上,它會簡化為在所有目標調用周圍添加if/else語句。Meson 提供了一種更簡單的方法,可以使用停用器物件來實現相同的效果。

停用器物件是使用 disabler 函式建立的

d = disabler()

您能對停用器物件做的唯一事情是詢問它是否已找到

f = d.found() # returns false

任何其他使用停用器物件的語句都會立即返回一個停用器。例如,假設 d 包含一個停用器物件,那麼

d2 = some_func(d) # value of d2 will be disabler
d3 = true or d2   # value of d3 will be true because of short-circuiting
d4 = false or d2  # value of d4 will be disabler
if d              # neither branch is evaluated

因此,要停用依賴於上述相依性的每個目標,您可以執行以下操作

if use_foo_feature
  d = dependency('foo')
else
  d = disabler()
endif

這將此選項的處理集中在一個地方,其他建置定義檔案不需要散佈 if 語句。

搜尋的結果是