mirror of
https://github.com/Kitware/CMake.git
synced 2025-10-14 02:08:27 +08:00
FindProtobuf: Add PLUGIN parameter to protobuf_generate
This allows for example reusing the function for generating gRPC code by specifying which grpc plugin to use.
This commit is contained in:
@@ -140,7 +140,7 @@ Example:
|
|||||||
|
|
||||||
function(protobuf_generate)
|
function(protobuf_generate)
|
||||||
set(_options APPEND_PATH DESCRIPTORS)
|
set(_options APPEND_PATH DESCRIPTORS)
|
||||||
set(_singleargs LANGUAGE OUT_VAR EXPORT_MACRO PROTOC_OUT_DIR)
|
set(_singleargs LANGUAGE OUT_VAR EXPORT_MACRO PROTOC_OUT_DIR PLUGIN)
|
||||||
if(COMMAND target_sources)
|
if(COMMAND target_sources)
|
||||||
list(APPEND _singleargs TARGET)
|
list(APPEND _singleargs TARGET)
|
||||||
endif()
|
endif()
|
||||||
@@ -171,6 +171,10 @@ function(protobuf_generate)
|
|||||||
set(_dll_export_decl "dllexport_decl=${protobuf_generate_EXPORT_MACRO}:")
|
set(_dll_export_decl "dllexport_decl=${protobuf_generate_EXPORT_MACRO}:")
|
||||||
endif()
|
endif()
|
||||||
|
|
||||||
|
if(protobuf_generate_PLUGIN)
|
||||||
|
set(_plugin "--plugin=${protobuf_generate_PLUGIN}")
|
||||||
|
endif()
|
||||||
|
|
||||||
if(NOT protobuf_generate_GENERATE_EXTENSIONS)
|
if(NOT protobuf_generate_GENERATE_EXTENSIONS)
|
||||||
if(protobuf_generate_LANGUAGE STREQUAL cpp)
|
if(protobuf_generate_LANGUAGE STREQUAL cpp)
|
||||||
set(protobuf_generate_GENERATE_EXTENSIONS .pb.h .pb.cc)
|
set(protobuf_generate_GENERATE_EXTENSIONS .pb.h .pb.cc)
|
||||||
@@ -245,7 +249,7 @@ function(protobuf_generate)
|
|||||||
add_custom_command(
|
add_custom_command(
|
||||||
OUTPUT ${_generated_srcs}
|
OUTPUT ${_generated_srcs}
|
||||||
COMMAND protobuf::protoc
|
COMMAND protobuf::protoc
|
||||||
ARGS --${protobuf_generate_LANGUAGE}_out ${_dll_export_decl}${protobuf_generate_PROTOC_OUT_DIR} ${_dll_desc_out} ${_protobuf_include_path} ${_abs_file}
|
ARGS --${protobuf_generate_LANGUAGE}_out ${_dll_export_decl}${protobuf_generate_PROTOC_OUT_DIR} ${_plugin} ${_dll_desc_out} ${_protobuf_include_path} ${_abs_file}
|
||||||
DEPENDS ${_abs_file} protobuf::protoc
|
DEPENDS ${_abs_file} protobuf::protoc
|
||||||
COMMENT "Running ${protobuf_generate_LANGUAGE} protocol buffer compiler on ${_proto}"
|
COMMENT "Running ${protobuf_generate_LANGUAGE} protocol buffer compiler on ${_proto}"
|
||||||
VERBATIM )
|
VERBATIM )
|
||||||
|
@@ -6,5 +6,6 @@ add_test(NAME FindProtobuf.Test COMMAND
|
|||||||
${build_generator_args}
|
${build_generator_args}
|
||||||
--build-project TestFindProtobuf
|
--build-project TestFindProtobuf
|
||||||
--build-options ${build_options}
|
--build-options ${build_options}
|
||||||
|
"-DCMake_TEST_FindProtobuf_gRPC=${CMake_TEST_FindProtobuf_gRPC}"
|
||||||
--test-command ${CMAKE_CTEST_COMMAND} -V -C $<CONFIGURATION>
|
--test-command ${CMAKE_CTEST_COMMAND} -V -C $<CONFIGURATION>
|
||||||
)
|
)
|
||||||
|
@@ -52,3 +52,16 @@ target_include_directories(test_desc PRIVATE ${CMAKE_CURRENT_BINARY_DIR})
|
|||||||
target_link_libraries(test_desc msgs ${Protobuf_LIBRARIES})
|
target_link_libraries(test_desc msgs ${Protobuf_LIBRARIES})
|
||||||
target_compile_features(test_desc PRIVATE cxx_std_11)
|
target_compile_features(test_desc PRIVATE cxx_std_11)
|
||||||
add_test(NAME test_desc COMMAND test_desc ${DESC_PROTO_DESC})
|
add_test(NAME test_desc COMMAND test_desc ${DESC_PROTO_DESC})
|
||||||
|
|
||||||
|
if(CMake_TEST_FindProtobuf_gRPC)
|
||||||
|
find_program(gRPC_CPP_PLUGIN grpc_cpp_plugin)
|
||||||
|
add_library(msgs_grpc msgs/example_service.proto)
|
||||||
|
target_include_directories(msgs_grpc PUBLIC ${CMAKE_CURRENT_BINARY_DIR})
|
||||||
|
target_link_libraries(msgs_grpc PUBLIC ${Protobuf_LIBRARIES})
|
||||||
|
protobuf_generate(TARGET msgs_grpc LANGUAGE cpp)
|
||||||
|
protobuf_generate(TARGET msgs_grpc LANGUAGE grpc GENERATE_EXTENSIONS .grpc.pb.h .grpc.pb.cc PLUGIN "protoc-gen-grpc=${gRPC_CPP_PLUGIN}")
|
||||||
|
|
||||||
|
add_executable(test_generate_grpc main-generate-grpc.cxx)
|
||||||
|
target_link_libraries(test_generate_grpc PRIVATE msgs_grpc)
|
||||||
|
add_test(NAME test_generate_grpc COMMAND test_generate_grpc)
|
||||||
|
endif()
|
||||||
|
6
Tests/FindProtobuf/Test/main-generate-grpc.cxx
Normal file
6
Tests/FindProtobuf/Test/main-generate-grpc.cxx
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
#include <msgs/example_service.grpc.pb.h>
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
return 0;
|
||||||
|
}
|
8
Tests/FindProtobuf/Test/msgs/example_service.proto
Normal file
8
Tests/FindProtobuf/Test/msgs/example_service.proto
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
syntax = "proto3";
|
||||||
|
package example.msgs;
|
||||||
|
|
||||||
|
import "google/protobuf/empty.proto";
|
||||||
|
|
||||||
|
service ExampleService {
|
||||||
|
rpc nothing(google.protobuf.Empty) returns (google.protobuf.Empty) {}
|
||||||
|
}
|
Reference in New Issue
Block a user