通訊協定緩衝區規則

規則

proto_lang_toolchain

proto_lang_toolchain(name, blacklisted_protos, command_line, compatible_with, deprecation, distribs, exec_compatible_with, exec_properties, features, licenses, mnemonic, plugin, plugin_format_flag, progress_message, proto_compiler, restricted_to, runtime, tags, target_compatible_with, testonly, visibility)

已淘汰,請改為前往 https://github.com/bazelbuild/rules_proto

指定 LANG_proto_library 規則 (例如java_proto_library) 應叫用 proto-編譯器。 部分 LANG_proto_library 規則允許透過指令列標記指定要使用的工具鍊;請參閱相關說明文件。

一般而言,除非您要調整 Java 編譯器,否則不應編寫這類規則。

沒有編譯器。proto-compiler 取自我們附加的 proto_library 規則。它會以指令列旗標的形式傳遞給 Blaze。有一些功能需要在 proto_library 規則本身上叫用 proto 編譯器。強制執行 LANG_proto_library 使用的編譯器,有利於執行 proto_library 採用的編譯器。

範例

例如:

proto_lang_toolchain(
    name = "javalite_toolchain",
    command_line = "--javalite_out=shared,immutable:$(OUT)",
    plugin = ":javalite_plugin",
    runtime = ":protobuf_lite",
)

引數

屬性
name

Name; required

此目標的專屬名稱。

blacklisted_protos

List of labels; optional

系統不會針對 blacklisted_protossrcs 屬性中的檔案產生程式碼。這會用於已連結到 proto 執行階段的 .proto 檔案,例如 any.proto
command_line

String; required

這個值會傳遞至 proto 編譯器,以產生程式碼。只包含這個程式碼產生器/外掛程式專屬的部分 (例如不包含 -I 參數)
  • $(OUT) 專屬於 LANG_proto_library。規則應定義如何解讀這個變數。以 Java 為例,$(OUT) 將替換為要建立的 src-jar 檔案名稱。
mnemonic

String; optional; default is "GenProto"

這個值會設為 protoc 動作的記憶。
plugin

Label; optional

如有提供,將會提供給呼叫 proto-編譯器的動作,並傳遞至 proto-編譯器:--plugin=protoc-gen-PLUGIN=.
plugin_format_flag

String; optional

如有提供,這個值會傳遞至 proto-編譯器,以便使用外掛程式。值必須包含單一 %s,以外掛程式執行檔取代。--plugin=protoc-gen-PLUGIN=.
progress_message

String; optional; default is "Generating proto_library %{label}"

這個值將設為 protoc 動作的進度訊息。
proto_compiler

Label; optional

proto 編譯器執行檔。 如有提供,這個目標就會做為 proto 編譯器產生程式碼。
runtime

Label; optional

產生程式碼做為編譯依據的語言特定程式庫。確切行為是 LANG_proto_library 專屬。舉例來說,Java 應針對執行階段進行編譯。

proto_library

proto_library(name, deps, srcs, data, compatible_with, deprecation, distribs, exec_compatible_with, exec_properties, exports, features, import_prefix, licenses, restricted_to, strip_import_prefix, tags, target_compatible_with, testonly, visibility)

已淘汰,請改用 https://github.com/bazelbuild/rules_proto

使用 proto_library 定義通訊協定緩衝區的程式庫,這些緩衝區可能來自多種語言。proto_library 可能會列於支援規則的 deps 子句中,例如 java_proto_library

透過指令列編譯時,proto_library 會建立名為 foo-descriptor-set.proto.bin 的檔案,這是為規則 srcs 的訊息設定的描述元。這個檔案是序列化的 FileDescriptorSet,詳情請參閱 https://developers.google.com/protocol-buffers/docs/techniques#self-description

這只包含 proto_library 規則直接提及的 .proto 檔案相關資訊;您可以透過 [ProtoInfo].transitive_descriptor_sets Starlark 供應商取得一系列遞移描述元集。請參閱 ProtoInfo.java 中的說明文件。

建議的程式碼整理方式:

  • 每個 .proto 檔案只能套用一項 proto_library 規則。
  • 名為 foo.proto 的檔案位於名為 foo_proto 的規則中,且該規則位於相同套件中。
  • 納入名為 foo_protoproto_library[language]_proto_library 應稱為 foo_[language]_proto,且位於相同套件中。

引數

屬性
name

Name; required

此目標的專屬名稱。

deps

List of labels; optional

目標依附的其他 proto_library 規則清單。proto_library 只能依附於其他 proto_library 目標。可能取決於語言專屬的程式庫。
srcs

List of labels; optional

系統為了建立目標而處理的 .proto.protodevel 檔案清單。這通常不是空白清單。srcs 可以留空的用途是 alias-library。這是具有 deps 中一或多個其他 proto_library 的 proto_library 規則。這個模式可用於例如匯出永久名稱下的公用 API。
exports

List of labels; optional

可在 proto 來源中透過「匯入公開」參照的 proto_library 目標清單。
import_prefix

String; optional

要新增至這項規則中 .proto 檔案路徑的前置字串。

設定完成後,您即可透過此規則在存放區相關路徑前面加上此屬性值,存取這項規則 srcs 屬性中的 .proto 來源檔案。

系統會先移除 strip_import_prefix 屬性中的前置字串,再新增這個前置字串。

strip_import_prefix

String; optional

要從這項規則中 .proto 檔案路徑中移除的前置字串。

設定完成後,這項規則的 srcs 屬性中的 .proto 來源檔案可在路徑中存取,其前置字串有這個前置字串。

如果是相對路徑 (開頭不是斜線),則會視為套件相關路徑。如果是絕對值,系統會視為存放區相關路徑。

移除這個前置字串後,系統就會新增 import_prefix 屬性中的前置字串。