Starlark 是類似 Python 的設定語言,原本要在 Bazel 中使用,並受到其他工具採用。Bazel 的 BUILD
和 .bzl
檔案是以 Starlark 的慣用方言編寫,但通常只會簡稱為「Starlark」,特別是在強調功能是以 Build Language 表示,而不是以 Bazel 的內建或「原生」部分表示時。Bazel 會使用許多建構相關函式 (例如 glob
、genrule
、java_binary
等) 來增強核心語言。
詳情請參閱 Bazel 和 Starlark 說明文件,並參閱 Rules SIG 範本做為新規則集的起點。
空白規則
如要建立第一項規則,請建立 foo.bzl
檔案:
def _foo_binary_impl(ctx):
pass
foo_binary = rule(
implementation = _foo_binary_impl,
)
呼叫 rule
函式時,必須定義回呼函式。邏輯會位於那裡,但您可以暫時將函式留空。ctx
引數會提供目標的相關資訊。
您可以載入規則,並透過 BUILD
檔案使用。
在相同目錄中建立 BUILD
檔案:
load(":foo.bzl", "foo_binary")
foo_binary(name = "bin")
現在可以建立目標:
$ bazel build bin
INFO: Analyzed target //:bin (2 packages loaded, 17 targets configured).
INFO: Found 1 target...
Target //:bin up-to-date (nothing to build)
即使此規則沒有任何作用,但運作方式與其他規則相同:此規則有必要名稱,但支援 visibility
、testonly
和 tags
等常見屬性。
評估模型
在進一步探討前,請務必瞭解程式碼評估方式。
使用一些輸出陳述式更新 foo.bzl
:
def _foo_binary_impl(ctx):
print("analyzing", ctx.label)
foo_binary = rule(
implementation = _foo_binary_impl,
)
print("bzl file evaluation")
並構思:
load(":foo.bzl", "foo_binary")
print("BUILD file")
foo_binary(name = "bin1")
foo_binary(name = "bin2")
ctx.label
對應至要分析的目標標籤。ctx
物件有許多實用欄位和方法;您可以在 API 參考資料中找到完整清單。
查詢程式碼:
$ bazel query :all
DEBUG: /usr/home/bazel-codelab/foo.bzl:8:1: bzl file evaluation
DEBUG: /usr/home/bazel-codelab/BUILD:2:1: BUILD file
//:bin2
//:bin1
觀察幾個主題:
- 系統會先列印「bzl file evaluation」部分。評估
BUILD
檔案之前,Bazel 會評估其載入的所有檔案。如果有多個BUILD
檔案在載入 foo.bzl,則您只會看到「bzl file evaluation」出現一次,因為 Bazel 會快取評估結果。 - 系統不會呼叫回呼函式
_foo_binary_impl
。Bazel 查詢會載入BUILD
檔案,但不會分析目標。
如要分析目標,請使用 cquery
(「設定的查詢」) 或 build
指令:
$ bazel build :all
DEBUG: /usr/home/bazel-codelab/foo.bzl:2:5: analyzing //:bin1
DEBUG: /usr/home/bazel-codelab/foo.bzl:2:5: analyzing //:bin2
INFO: Analyzed 2 targets (0 packages loaded, 0 targets configured).
INFO: Found 2 targets...
如您所見,現在 _foo_binary_impl
已呼叫兩次,每個目標各一次。
請注意,系統不會再次輸出「bzl file evaluation」和「BUILD file」,因為系統會在呼叫 bazel query
後快取 foo.bzl
的評估作業。Bazel 只會在實際執行時發出 print
陳述式。
建立檔案
如要提高規則的實用性,請更新規則來產生檔案。首先,宣告檔案並命名在此範例中,建立名稱與目標相同的檔案:
ctx.actions.declare_file(ctx.label.name)
如果現在執行 bazel build :all
,系統會顯示錯誤訊息:
The following files have no generating action:
bin2
每次宣告檔案時,都必須透過建立動作告知 Bazel 如何產生檔案。使用 ctx.actions.write
建立含有指定內容的檔案。
def _foo_binary_impl(ctx):
out = ctx.actions.declare_file(ctx.label.name)
ctx.actions.write(
output = out,
content = "Hello\n",
)
程式碼有效,但沒有任何作用:
$ bazel build bin1
Target //:bin1 up-to-date (nothing to build)
ctx.actions.write
函式會註冊一項動作,藉此指示 Bazel 如何產生檔案。不過,Bazel 只會在實際要求時建立檔案。因此,最後一個步驟是告知 Bazel 該檔案是規則的輸出內容,而不是規則實作中使用的暫存檔案。
def _foo_binary_impl(ctx):
out = ctx.actions.declare_file(ctx.label.name)
ctx.actions.write(
output = out,
content = "Hello!\n",
)
return [DefaultInfo(files = depset([out]))]
請稍後查看 DefaultInfo
和 depset
函式。目前,我們假設最後一行是選擇規則輸出內容的方式。
現在,請執行 Bazel:
$ bazel build bin1
INFO: Found 1 target...
Target //:bin1 up-to-date:
bazel-bin/bin1
$ cat bazel-bin/bin1
Hello!
已成功產生檔案!
屬性
為了讓規則更實用,請使用 attr
模組新增屬性,並更新規則定義。
新增名為 username
的字串屬性:
foo_binary = rule(
implementation = _foo_binary_impl,
attrs = {
"username": attr.string(),
},
)
接著,請在 BUILD
檔案中設定:
foo_binary(
name = "bin",
username = "Alice",
)
如要存取回呼函式中的值,請使用 ctx.attr.username
。舉例來說:
def _foo_binary_impl(ctx):
out = ctx.actions.declare_file(ctx.label.name)
ctx.actions.write(
output = out,
content = "Hello {}!\n".format(ctx.attr.username),
)
return [DefaultInfo(files = depset([out]))]
請注意,您可以將屬性設為必要屬性或設定預設值。查看 attr.string
的說明文件。您也可以使用其他類型的屬性,例如boolean或整數清單。
依附元件
依附元件屬性 (例如 attr.label
和 attr.label_list
) 會將擁有該屬性的目標中的依附元件宣告至其標籤出現在屬性值的目標。這類屬性構成目標圖表的基礎。
在 BUILD
檔案中,目標標籤會顯示為字串物件,例如 //pkg:name
。在實作函式中,目標可做為 Target
物件存取。舉例來說,您可以使用 Target.files
查看目標傳回的檔案。
多個檔案
根據預設,只有規則建立的目標可能會顯示為依附元件 (例如 foo_library()
目標)。如果希望屬性接受輸入檔案的目標 (例如存放區中的來源檔案),可以使用 allow_files
並指定系統接受的副檔名清單 (或使用 True
來允許任何副檔名):
"srcs": attr.label_list(allow_files = [".java"]),
您可以使用 ctx.files.<attribute name>
存取檔案清單。舉例來說,您可以透過以下方式存取 srcs
屬性中的檔案清單:
ctx.files.srcs
單一檔案
如果只需要一個檔案,請使用 allow_single_file
:
"src": attr.label(allow_single_file = [".java"])
即可透過 ctx.file.<attribute name>
存取這個檔案:
ctx.file.src
使用範本建立檔案
您可以建立規則,根據範本產生 .cc 檔案。此外,您也可以使用 ctx.actions.write
輸出規則實作函式中建構的字串,但這有兩個問題。首先,隨著範本變得更大,將記憶體放在獨立的檔案中,並避免在分析階段建構大型字串,會更有效率。其次,使用獨立檔案會讓使用者更為方便。請改用 ctx.actions.expand_template
,這會在範本檔案上執行替代。
建立 template
屬性,宣告範本檔案的依附元件:
def _hello_world_impl(ctx):
out = ctx.actions.declare_file(ctx.label.name + ".cc")
ctx.actions.expand_template(
output = out,
template = ctx.file.template,
substitutions = {"{NAME}": ctx.attr.username},
)
return [DefaultInfo(files = depset([out]))]
hello_world = rule(
implementation = _hello_world_impl,
attrs = {
"username": attr.string(default = "unknown person"),
"template": attr.label(
allow_single_file = [".cc.tpl"],
mandatory = True,
),
},
)
使用者可以像這樣使用規則:
hello_world(
name = "hello",
username = "Alice",
template = "file.cc.tpl",
)
cc_binary(
name = "hello_bin",
srcs = [":hello"],
)
如果您不想向使用者公開範本並一律使用相同的範本,則可設定預設值並將屬性設為不公開:
"_template": attr.label(
allow_single_file = True,
default = "file.cc.tpl",
),
以底線開頭的屬性屬於私人性質,無法在 BUILD
檔案中設定。範本現在是「隱含依附元件」:每個 hello_world
目標都有這個檔案的依附元件。別忘了更新 BUILD
檔案並使用 exports_files
,讓其他套件也能看到這個檔案:
exports_files(["file.cc.tpl"])