FindProtobuf

定位並設定 Google Protocol Buffers 函式庫。

版本 3.6 新增:支援 find_package() 版本檢查。

版本 3.6 變更:所有輸入和輸出變數皆使用 Protobuf_ 前綴。為了相容性,仍然支援使用 PROTOBUF_ 前綴的變數。

以下變數可以設定,且為選用

Protobuf_SRC_ROOT_FOLDER

當使用 MSVC 編譯時,如果設定此快取變數,將會在 protobuf 預設的 VS 專案建置位置 (vsprojects/Debug 和 vsprojects/Release 或 vsprojects/x64/Debug 和 vsprojects/x64/Release) 中搜尋函式庫和二進位檔。

Protobuf_IMPORT_DIRS

要搜尋匯入的 .proto 檔案的其他目錄列表。

Protobuf_DEBUG

版本 3.6 新增。

顯示偵錯訊息。

Protobuf_USE_STATIC_LIBS

版本 3.9 新增。

設定為 ON 以強制使用靜態函式庫。預設為 OFF。

定義以下變數

Protobuf_FOUND

找到 Google Protocol Buffers 函式庫 (libprotobuf & 標頭檔)

Protobuf_VERSION

版本 3.6 新增。

找到的套件版本。

Protobuf_INCLUDE_DIRS

Google Protocol Buffers 的包含目錄

Protobuf_LIBRARIES

protobuf 函式庫

Protobuf_PROTOC_LIBRARIES

protoc 函式庫

Protobuf_LITE_LIBRARIES

protobuf-lite 函式庫

版本 3.9 新增:也定義了以下 IMPORTED 目標

protobuf::libprotobuf

protobuf 函式庫。

protobuf::libprotobuf-lite

protobuf lite 函式庫。

protobuf::libprotoc

protoc 函式庫。

protobuf::protoc

版本 3.10 新增:protoc 編譯器。

以下快取變數也可用於設定或使用

Protobuf_LIBRARY

protobuf 函式庫

Protobuf_PROTOC_LIBRARY

protoc 函式庫

Protobuf_INCLUDE_DIR

protocol buffers 的包含目錄

Protobuf_PROTOC_EXECUTABLE

protoc 編譯器

Protobuf_LIBRARY_DEBUG

protobuf 函式庫 (偵錯)

Protobuf_PROTOC_LIBRARY_DEBUG

protoc 函式庫 (偵錯)

Protobuf_LITE_LIBRARY

protobuf lite 函式庫

Protobuf_LITE_LIBRARY_DEBUG

protobuf lite 函式庫 (偵錯)

範例

find_package(Protobuf REQUIRED)
include_directories(${Protobuf_INCLUDE_DIRS})
include_directories(${CMAKE_CURRENT_BINARY_DIR})
protobuf_generate_cpp(PROTO_SRCS PROTO_HDRS foo.proto)
protobuf_generate_cpp(PROTO_SRCS PROTO_HDRS EXPORT_MACRO DLL_EXPORT foo.proto)
protobuf_generate_cpp(PROTO_SRCS PROTO_HDRS DESCRIPTORS PROTO_DESCS foo.proto)
protobuf_generate_python(PROTO_PY foo.proto)
add_executable(bar bar.cc ${PROTO_SRCS} ${PROTO_HDRS})
target_link_libraries(bar ${Protobuf_LIBRARIES})

注意

protobuf_generate_cppprotobuf_generate_python 函數,以及 add_executable()add_library() 呼叫,僅在相同目錄內正常運作。

protobuf_generate_cpp

新增自訂命令以處理 .proto 檔案為 C++

protobuf_generate_cpp (
  <srcs-var> <hdrs-var>
  [DESCRIPTORS <var>]
  [EXPORT_MACRO <macro>]
  [<proto-file>...])
<srcs-var>

用自動產生的原始碼檔案定義的變數

<hdrs-var>

用自動產生的標頭檔定義的變數

DESCRIPTORS <var>

版本 3.10 新增:變數,用於定義自動產生的描述符檔案(如果要求)。

EXPORT_MACRO <macro>

是一個巨集,應展開為 __declspec(dllexport)__declspec(dllimport),取決於編譯的內容。

<proto-file>...

.proto 檔案

protobuf_generate_python

版本 3.4 新增。

新增自訂命令以處理 .proto 檔案為 Python

protobuf_generate_python (<py-srcs-var> [<proto-file>...])
<py-srcs-var>

用自動產生的 Python 檔案定義的變數

<proto-file>...

.proto 檔案

protobuf_generate

版本 3.13 新增。

在建置時自動從 .proto schema 檔案產生原始碼檔案

protobuf_generate (
    TARGET <target>
    [LANGUAGE <lang>]
    [OUT_VAR <var>]
    [EXPORT_MACRO <macro>]
    [PROTOC_OUT_DIR <dir>]
    [PLUGIN <plugin>]
    [PLUGIN_OPTIONS <plugin-options>]
    [DEPENDENCIES <dependencies>]
    [PROTOS <proto-file>...]
    [IMPORT_DIRS <dir>...]
    [GENERATE_EXTENSIONS <extension>...]
    [PROTOC_OPTIONS <option>...]
    [PROTOC_EXE <executable>]
    [APPEND_PATH])
APPEND_PATH

一個旗標,會使所有 proto schema 檔案的基本路徑被新增至 IMPORT_DIRS

LANGUAGE <lang>

單一值:cpp 或 python。決定要產生哪種原始碼檔案。預設為 cpp。

OUT_VAR <var>

CMake 變數的名稱,將會填入產生之原始碼檔案的路徑。

EXPORT_MACRO <macro>

巨集的名稱,套用至所有產生的 Protobuf 訊息類別和外部變數。例如,可用於宣告 DLL 匯出。

PROTOC_OUT_DIR <dir>

產生之原始碼檔案的輸出目錄。預設為 CMAKE_CURRENT_BINARY_DIR

PLUGIN <plugin>

版本 3.21 新增。

一個選用的外掛程式執行檔。例如,這可能是 grpc_cpp_plugin 的路徑。

PLUGIN_OPTIONS <plugin-options>

版本 3.28 新增。

提供給外掛程式的其他選項,例如 gRPC cpp 外掛程式的 generate_mock_code=true

DEPENDENCIES <dependencies>

版本 3.28 新增。

轉發至基礎 add_custom_command 呼叫的 DEPENDS 的引數。

TARGET <target>

CMake 目標,將會把產生的檔案新增為原始碼。

PROTOS <proto-file>...

proto schema 檔案的列表。如果省略,則會使用 TARGET 中每個以 proto 結尾的原始碼檔案。

IMPORT_DIRS <dir>...

schema 檔案的通用父目錄。例如,如果 schema 檔案是 proto/helloworld/helloworld.proto 且匯入目錄是 proto/,則產生的檔案會是 ${PROTOC_OUT_DIR}/helloworld/helloworld.pb.h${PROTOC_OUT_DIR}/helloworld/helloworld.pb.cc

GENERATE_EXTENSIONS <extension>...

如果省略 LANGUAGE,則必須將此設定為 protoc 產生的擴充功能。

PROTOC_OPTIONS <option>...

版本 3.28 新增。

轉發至 protoc 的其他引數。

PROTOC_EXE <executable>

版本 4.0 新增。

用於產生 protobuf 綁定的命令名稱、路徑或 CMake 可執行檔。如果省略,則會使用 protobuf::protoc

範例

find_package(gRPC CONFIG REQUIRED)
find_package(Protobuf REQUIRED)
add_library(ProtoTest Test.proto)
target_link_libraries(ProtoTest PUBLIC gRPC::grpc++)
protobuf_generate(TARGET ProtoTest)
protobuf_generate(
    TARGET ProtoTest
    LANGUAGE grpc
    PLUGIN protoc-gen-grpc=$<TARGET_FILE:gRPC::grpc_cpp_plugin>
    PLUGIN_OPTIONS generate_mock_code=true
    GENERATE_EXTENSIONS .grpc.pb.h .grpc.pb.cc)