通訊協定緩衝區規則

回報問題 查看原始碼

規則

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)

如果您使用的是 Bazel,請從 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-compiler 以產生程式碼。只加入本程式碼產生器/外掛程式的專屬部分 (例如,不要加入 -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-compiler 以使用外掛程式。這個值必須包含單一的 %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-specific. 例如,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 會列在支援的規則 (例如 java_proto_library) 的 deps 子句中。

透過指令列編譯時,proto_library 會建立名稱為 foo-descriptor-set.proto.bin 的檔案,也就是為規則所放置訊息的描述元集。檔案是序列化的 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 來源中的「import publics」參照的 proto_library 目標清單。
import_prefix

String; optional

這個規則中要加入 .proto 檔案路徑的前置字元。

如果設定,這項規則的 srcs 屬性中的 .proto 來源檔案就是在其存放區相關路徑前面加上該屬性值。

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

strip_import_prefix

String; optional

從這項規則的 .proto 檔案路徑中去除的前置字元。

設定後,此規則的 srcs 屬性中的 .proto 來源檔案在其路徑上會遭到截斷。

如果是相對路徑 (而非以斜線開頭),系統會將其視為套件相關的路徑。如果是絕對路徑,系統會將該路徑視為存放區相關路徑。

去除這個前置字串後,系統會在 import_prefix 屬性中加入前置字串。