使用 GN 標籤進行建構

fx build 現在可直接建立 GN 目標。方法是使用一或多個 GN 目標標籤,並在指令列中加入 // 前置字串,如下所示:

# Build the `cstr` and `uuid` libraries for Fuchsia.
fx build //src/lib/cstr //src/lib/uuid

# Equivalent to the previous command.
fx build //src/lib/cstr:cstr //src/lib/uuid:uuid

如果標籤不屬於所設的 GN 圖表內,就會顯示錯誤。即使在對應的 BUILD.gn 檔案中定義了該檔案,還是需要從 //:defaultargs.gn 中定義的其他依附元件清單 (例如 universe_package_labels 等) 遞移依附元件:

# Build the corresponding tests for Fuchsia too.
$ fx build //src/lib/cstr:tests
ERROR: Unknown GN label (not in the configured graph): //src/lib/cstr:tests

如要進一步瞭解發生這種情況的原因,請參閱 gn-root-targets

GN 工具鍊後置字串

可支援 GN 工具鍊後置字串 (但括號需要加上殼層引用),如下所示:

# Build the host tests for the `cstr` library.
fx build '//src/lib/cstr:tests(//build/toolchain:host_x64)'

甚至是:

# Same as above
fx build //src/lib/cstr:tests\(//build/toolchain:host_x64\)

GN 工具鍊別名

為方便起見,您可以使用特定選項,為指令列上顯示的 next 標籤附加 GN 工具鍊後置字串,如下所示:

# Build the `cstr` and `fidl` tests for the host, and the `trace` library for Fuchsia.
fx build --host //src/lib/cstr:tests //src/lib/fidl:tests --fuchsia //src/lib/trace

別名工具鍊定義支援少量固定選項:

  • --host 與建構機器的主機工具鍊標籤 (例如 //build/toolchain:host_x64//build/toolchain:host_arm64) 相符。

  • --default 符合目前建構設定的預設工具鍊 (例如 //build/toolchain/fuchsia:riscv64)。

  • --fuchsia--default 的另一個名稱,因為 Fuchsia 二進位檔內建於預設的 GN 工具鍊 (目前使用時)。

  • --fidl//build/fidl:fidling 相符,即用於處理 FIDL 檔案的 GN 工具鍊。

日後可能會新增其他別名。

GN 工具鍊選項

--toolchain=LABEL 選項可用於特定的 GN 工具鍊標籤:

# Build the `cstr` tests for Linux/arm64 through cross-compiling
fx build --toolchain=//build/toolchain:linux_arm64 //src/lib/cstr:tests

也接受工具鍊別名,例如:

# Same as using `--host` directly as well.
fx build --toolchain=host //src/lib/cstr:tests

連結至 Ninja 目標路徑的 GN 標籤

fx build 指令一律會輸出要建構的 Ninja 目標清單,如下所示:

$ fx build //src/lib/cstr
Building Ninja target(s): obj/src/lib/cstr/cstr.stamp
...

請注意,這份清單包含對應建構指令的所有輸出內容,包括由 GN 隱含產生的輸出內容。建構其中任何一項都會觸發會一次產生所有輸出的指令。

$ fx build //src/lib/json_parser
Building Ninja target(s): obj/src/lib/json_parser/json_parser.inputdeps.stamp obj/src/lib/json_parser/json_parser.json_parser.cc.o obj/src/lib/json_parser/json_parser.rapidjson_validation.cc.o obj/src/lib/json_parser/json_parser.stamp
...

Ninja 將目標鎖定 GN 標籤

雖然系統仍支援傳遞 Ninja 目標路徑,但系統會輸出警告,並為您提供最適合使用的建構引數,例如:

$ fx build host_x64/zbi host_x64/ffx
WARNING: Use '--host //zircon/tools/zbi' instead of Ninja path 'host_x64/zbi'
WARNING: Use '--host //src/developer/ffx/frontends/ffx:ffx_bin' instead of Ninja path 'host_x64/ffx'
Building Ninja target(s): host_x64/exe.unstripped/zbi host_x64/ffx host_x64/obj/src/developer/ffx/frontends/ffx/ffx_bin.stamp host_x64/obj/zircon/tools/zbi/zbi.zbi.cc.o host_x64/zbi host_x64/zbi.build-id.stamp
...

如果不想看到這個標籤,請傳送 -- 選項分隔符之後的 Ninja 路徑,因為後方的任何項目都會直接傳遞至 Ninja:

fx build -- host_x64/zbi host_x64/ffx

然而,隨著 Bazel 遷移作業進行,日後所有 Bazel 產生的輸出內容都將不適用。