通訊協定緩衝區規則

回報問題 查看來源 Nightly · 8.3 · 8.2 · 8.1 · 8.0 · 7.6

規則

cc_proto_library

查看規則來源
cc_proto_library(name, deps, aspect_hints, compatible_with, deprecation, exec_compatible_with, exec_group_compatible_with, exec_properties, features, package_metadata, restricted_to, tags, target_compatible_with, testonly, toolchains, visibility)

cc_proto_library 會從 .proto 檔案產生 C++ 程式碼。

deps 必須指向 proto_library 規則。

範例:


cc_library(
    name = "lib",
    deps = [":foo_cc_proto"],
)

cc_proto_library(
    name = "foo_cc_proto",
    deps = [":foo_proto"],
)

proto_library(
    name = "foo_proto",
)

引數

屬性
name

名稱:必填

這個目標的專屬名稱。

deps

標籤清單;預設值為 []

要產生 C++ 程式碼的 proto_library 規則清單。

java_lite_proto_library

查看規則來源
java_lite_proto_library(name, deps, aspect_hints, compatible_with, deprecation, exec_compatible_with, exec_group_compatible_with, exec_properties, features, package_metadata, restricted_to, tags, target_compatible_with, testonly, toolchains, visibility)

java_lite_proto_library 會從 .proto 檔案產生 Java 程式碼。

deps 必須指向 proto_library 規則。

範例:


java_library(
    name = "lib",
    runtime_deps = [":foo"],
)

java_lite_proto_library(
    name = "foo",
    deps = [":bar"],
)

proto_library(
    name = "bar",
)

引數

屬性
name

名稱:必填

這個目標的專屬名稱。

deps

標籤清單;預設值為 []

要產生 Java 程式碼的 proto_library 規則清單。

java_proto_library

查看規則來源
java_proto_library(name, deps, aspect_hints, compatible_with, deprecation, exec_compatible_with, exec_group_compatible_with, exec_properties, features, licenses, package_metadata, restricted_to, tags, target_compatible_with, testonly, toolchains, visibility)

java_proto_library 會從 .proto 檔案產生 Java 程式碼。

deps 必須指向 proto_library 規則。

範例:


java_library(
    name = "lib",
    runtime_deps = [":foo_java_proto"],
)

java_proto_library(
    name = "foo_java_proto",
    deps = [":foo_proto"],
)

proto_library(
    name = "foo_proto",
)

引數

屬性
name

名稱:必填

這個目標的專屬名稱。

deps

標籤清單;預設值為 []

要產生 Java 程式碼的 proto_library 規則清單。

proto_library

查看規則來源
proto_library(name, deps, srcs, data, allow_exports, aspect_hints, compatible_with, deprecation, exec_compatible_with, exec_group_compatible_with, exec_properties, exports, features, import_prefix, licenses, package_metadata, restricted_to, strip_import_prefix, tags, target_compatible_with, testonly, toolchains, visibility)

使用 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 供應商取得。請參閱 proto_info.bzl 中的說明文件。

建議的程式碼組織:

  • 每個 .proto 檔案只能有一個 proto_library 規則。
  • 名為 foo.proto 的檔案會位於名為 foo_proto 的規則中,且位於同一個套件中。
  • 包裝名為 foo_proto[language]_proto_libraryproto_library[language]_proto_library 應稱為 foo_[language]_proto,並位於相同套件中。

引數

屬性
name

名稱:必填

這個目標的專屬名稱。

deps

標籤清單;預設值為 []

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

標籤清單;預設值為 []

系統會處理 .proto.protodevel 檔案清單,以建立目標。這通常是不為空的清單。srcs 為空的一個用途是別名程式庫。這是 proto_library 規則,在 deps 中有一或多個其他 proto_library。這個模式可用於匯出具有持續性名稱的公開 API 等。
allow_exports

標籤;預設值為 None

選用允許清單,可防止 proto 程式庫重新匯出或用於 lang_proto_library,但 lang_proto_library 不在列出的其中一個套件中。
exports

標籤清單;預設值為 []

可透過 proto 來源中的「import public」參照的 proto_library 目標清單。如果您使用「import public」,但未在 exports 屬性中列出對應的程式庫,就會發生錯誤。請注意,您必須同時在 deps 和 exports 中列出程式庫,因為並非所有 lang_proto_library 實作項目都已變更。
import_prefix

字串;預設值為 ""

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

設定後,這個規則 srcs 屬性中的 .proto 來源檔案,會以這個屬性的值做為前置字元,附加至其存放區相對路徑,即可存取這些檔案。

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

strip_import_prefix

字串;預設值為 "/"

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

設定後,這個規則的 srcs 屬性中的 .proto 來源檔案,會以路徑的形式提供存取權,並截斷這個前置字元。

如果是相對路徑 (開頭不是斜線),系統會將其視為與套件相關的路徑。如果是絕對路徑,系統會解讀為存放區相對路徑。

系統會先移除前置字元,再將 import_prefix 屬性的前置字元加入其中。

py_proto_library

查看規則來源
py_proto_library(name, deps, aspect_hints, compatible_with, deprecation, exec_compatible_with, exec_group_compatible_with, exec_properties, features, package_metadata, restricted_to, tags, target_compatible_with, testonly, toolchains, visibility)
使用 `py_proto_library` 從 `.proto` 檔案產生 Python 程式庫。 慣例是將 `py_proto_library` 規則命名為 `foo_py_pb2`,前提是該規則會包裝 `proto_library` 規則 `foo_proto`。`deps` 必須指向 `proto_library` 規則。示例: ```starlark py_library( name = "lib", deps = [":foo_py_pb2"], ) py_proto_library( name = "foo_py_pb2", deps = [":foo_proto"], ) proto_library( name = "foo_proto", srcs = ["foo.proto"], ) ```

引數

屬性
name

名稱:必填

這個目標的專屬名稱。

deps

標籤清單;預設值為 []

要產生 Python 程式庫的 `proto_library` 規則清單。 通常只有一個目標:感興趣的 proto 程式庫。可以是提供 `ProtoInfo` 的任何目標。

proto_lang_toolchain

查看規則來源
proto_lang_toolchain(name, allowlist_different_package, aspect_hints, blacklisted_protos, command_line, compatible_with, deprecation, exec_compatible_with, exec_group_compatible_with, exec_properties, features, mnemonic, output_files, package_metadata, plugin, plugin_format_flag, progress_message, protoc_minimal_do_not_use, restricted_to, runtime, tags, target_compatible_with, testonly, toolchain_type, toolchains, visibility)

如果使用 Bazel,請從 https://github.com/bazelbuild/rules_proto 載入規則。

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

除非您想調整 Java 編譯器,否則通常不應編寫這類規則。

沒有編譯器。proto-compiler 是從我們附加的 proto_library 規則取得。並以指令列標記的形式傳遞至 Blaze。部分功能需要在 proto_library 規則本身上叫用 proto 編譯器。 強制編譯器使用與 proto_library 相同的編譯器,對 LANG_proto_library 有益。

範例

簡單的範例如下:


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

引數

屬性
name

名稱:必填

這個目標的專屬名稱。

allowlist_different_package

標籤;預設值為 None

blacklisted_protos

標籤清單;預設值為 []

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

字串;必填

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

字串;預設值為 "GenProto"

這個值會設為 protoc 動作的助記符。
output_files

字串;預設值為 "legacy"

控制 command_line$(OUT) 的格式,可以是單一檔案的路徑,也可以是多個檔案的輸出目錄。可能的值為「single」和「multiple」。
plugin

標籤;預設值為 None

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

字串;預設值為 ""

如果提供這個值,系統會將其傳遞至 proto 編譯器,以使用外掛程式。這個值必須包含單一 %s,並替換為外掛程式可執行檔。 --plugin=protoc-gen-PLUGIN=<executable>.
progress_message

字串;預設值為 "Generating proto_library %{label}"

這個值會設為 protoc 動作的進度訊息。
protoc_minimal_do_not_use

標籤;預設值為 None

runtime

標籤;預設值為 None

用來編譯產生程式碼的特定語言程式庫。確切行為取決於 LANG_proto_library。舉例來說,Java 應針對執行階段進行編譯。
toolchain_type

標籤;預設值為 None

proto_toolchain

查看規則來源
proto_toolchain(name, aspect_hints, command_line, compatible_with, deprecation, exec_compatible_with, exec_group_compatible_with, exec_properties, features, mnemonic, output_files, package_metadata, progress_message, proto_compiler, restricted_to, tags, target_compatible_with, testonly, toolchains, visibility)

引數

屬性
name

名稱:必填

這個目標的專屬名稱。

command_line

字串;預設值為 "--descriptor_set_out=%s"

mnemonic

字串;預設值為 "GenProtoDescriptorSet"

output_files

字串;預設值為 "single"

progress_message

字串;預設值為 "Generating Descriptor Set proto_library %{label}"

proto_compiler

標籤;預設值為 None