Optimize protobuf generation process in CMakeLists.txt.
Signed-off-by: Tao He <linzhu.ht@alibaba-inc.com>
This commit is contained in:
parent
72b215a513
commit
bb33b76544
|
|
@ -10,25 +10,30 @@ find_library(CPPREST_LIB NAMES cpprest)
|
||||||
find_path(CPPREST_INCLUDE_DIR NAMES cpprest/http_client.h)
|
find_path(CPPREST_INCLUDE_DIR NAMES cpprest/http_client.h)
|
||||||
|
|
||||||
find_package(Boost REQUIRED COMPONENTS system thread locale random)
|
find_package(Boost REQUIRED COMPONENTS system thread locale random)
|
||||||
|
if (APPLE)
|
||||||
|
# If we're on OS X check for Homebrew's copy of OpenSSL instead of Apple's
|
||||||
|
if (NOT OpenSSL_DIR)
|
||||||
|
find_program(HOMEBREW brew)
|
||||||
|
if (HOMEBREW STREQUAL "HOMEBREW-NOTFOUND")
|
||||||
|
message(WARNING "Homebrew not found: not using Homebrew's OpenSSL")
|
||||||
|
if (NOT OPENSSL_ROOT_DIR)
|
||||||
|
message(WARNING "Use -DOPENSSL_ROOT_DIR for non-Apple OpenSSL")
|
||||||
|
endif()
|
||||||
|
else()
|
||||||
|
execute_process(COMMAND brew --prefix openssl
|
||||||
|
OUTPUT_VARIABLE OPENSSL_ROOT_DIR
|
||||||
|
OUTPUT_STRIP_TRAILING_WHITESPACE)
|
||||||
|
endif()
|
||||||
|
endif()
|
||||||
|
endif()
|
||||||
find_package(OpenSSL REQUIRED)
|
find_package(OpenSSL REQUIRED)
|
||||||
find_package(Protobuf REQUIRED)
|
find_package(Protobuf REQUIRED)
|
||||||
|
|
||||||
include(${CMAKE_CURRENT_SOURCE_DIR}/cmake/FindGRPC.cmake)
|
include(${CMAKE_CURRENT_SOURCE_DIR}/cmake/FindGRPC.cmake)
|
||||||
set(GRPC_LIBRARIES ${GPR_LIBRARY} ${GRPC_LIBRARY} ${GRPC_GRPC++_LIBRARY})
|
set(GRPC_LIBRARIES ${GPR_LIBRARY} ${GRPC_LIBRARY} ${GRPC_GRPC++_LIBRARY})
|
||||||
|
|
||||||
file(GLOB_RECURSE PROTO_SRC RELATIVE "${CMAKE_CURRENT_SOURCE_DIR}/proto" "proto/*.proto")
|
# will set `PROTOBUF_GENERATES`, indicates all generated .cc files, and a target `protobuf_generates`.
|
||||||
|
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/proto)
|
||||||
# Generate protobuf definitions
|
|
||||||
execute_process(COMMAND ${Protobuf_PROTOC_EXECUTABLE}
|
|
||||||
-I ${CMAKE_CURRENT_SOURCE_DIR}/proto
|
|
||||||
--cpp_out=${CMAKE_CURRENT_SOURCE_DIR}/proto
|
|
||||||
${PROTO_SRC})
|
|
||||||
execute_process(COMMAND ${Protobuf_PROTOC_EXECUTABLE}
|
|
||||||
-I ${CMAKE_CURRENT_SOURCE_DIR}/proto
|
|
||||||
--grpc_out=${CMAKE_CURRENT_SOURCE_DIR}/proto
|
|
||||||
--plugin=protoc-gen-grpc=${GRPC_CPP_PLUGIN}
|
|
||||||
${CMAKE_CURRENT_SOURCE_DIR}/proto/rpc.proto
|
|
||||||
${CMAKE_CURRENT_SOURCE_DIR}/proto/v3lock.proto)
|
|
||||||
|
|
||||||
enable_testing()
|
enable_testing()
|
||||||
include_directories(SYSTEM ${CPPREST_INCLUDE_DIR}
|
include_directories(SYSTEM ${CPPREST_INCLUDE_DIR}
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,31 @@
|
||||||
|
file(GLOB_RECURSE PROTO_SRCS RELATIVE "${CMAKE_CURRENT_SOURCE_DIR}" "*.proto")
|
||||||
|
|
||||||
|
# use `protobuf_generate` rather than `protobuf_generate_cpp` since we want to
|
||||||
|
# output the generated files to source dir, rather than binary dir.
|
||||||
|
protobuf_generate(
|
||||||
|
LANGUAGE cpp
|
||||||
|
OUT_VAR PROTO_GENERATES
|
||||||
|
PROTOC_OUT_DIR "${CMAKE_CURRENT_SOURCE_DIR}"
|
||||||
|
PROTOS ${PROTO_SRCS}
|
||||||
|
)
|
||||||
|
|
||||||
|
grpc_generate_cpp(PROTO_GRPC_GENERATES PROTO_GRPC_GENERATES_HDRS
|
||||||
|
"${CMAKE_CURRENT_SOURCE_DIR}"
|
||||||
|
"${CMAKE_CURRENT_SOURCE_DIR}/rpc.proto"
|
||||||
|
"${CMAKE_CURRENT_SOURCE_DIR}/v3lock.proto"
|
||||||
|
)
|
||||||
|
|
||||||
|
# populate `PROTOBUF_GENERATES` in the parent scope.
|
||||||
|
set(PROTOBUF_GENERATES)
|
||||||
|
foreach(cxx_file ${PROTO_GENERATES})
|
||||||
|
if(cxx_file MATCHES "cc$")
|
||||||
|
list(APPEND PROTOBUF_GENERATES ${cxx_file})
|
||||||
|
endif()
|
||||||
|
endforeach()
|
||||||
|
foreach(cxx_file ${PROTO_GRPC_GENERATES})
|
||||||
|
list(APPEND PROTOBUF_GENERATES ${cxx_file})
|
||||||
|
endforeach()
|
||||||
|
|
||||||
|
set(PROTOBUF_GENERATES ${PROTOBUF_GENERATES} PARENT_SCOPE)
|
||||||
|
set_source_files_properties(${PROTOBUF_GENERATES} PROPERTIES GENERATED TRUE)
|
||||||
|
add_custom_target(protobuf_generates DEPENDS ${PROTOBUF_GENERATES})
|
||||||
|
|
@ -1,10 +1,11 @@
|
||||||
file(GLOB_RECURSE CPP_CLIENT_SRC
|
file(GLOB_RECURSE CPP_CLIENT_SRC
|
||||||
RELATIVE "${CMAKE_CURRENT_SOURCE_DIR}"
|
RELATIVE "${CMAKE_CURRENT_SOURCE_DIR}"
|
||||||
"${CMAKE_CURRENT_SOURCE_DIR}/*.cpp"
|
"${CMAKE_CURRENT_SOURCE_DIR}/*.cpp"
|
||||||
"${CMAKE_CURRENT_SOURCE_DIR}/**/*.cpp"
|
"${CMAKE_CURRENT_SOURCE_DIR}/**/*.cpp")
|
||||||
"${CMAKE_CURRENT_SOURCE_DIR}/../proto/*.cc")
|
|
||||||
|
|
||||||
add_library(etcd-cpp-api SHARED ${CPP_CLIENT_SRC})
|
set_source_files_properties(${PROTOBUF_GENERATES} PROPERTIES GENERATED TRUE)
|
||||||
|
add_library(etcd-cpp-api SHARED ${CPP_CLIENT_SRC} ${PROTOBUF_GENERATES})
|
||||||
|
add_dependencies(etcd-cpp-api protobuf_generates)
|
||||||
set_property(TARGET etcd-cpp-api PROPERTY CXX_STANDARD 11)
|
set_property(TARGET etcd-cpp-api PROPERTY CXX_STANDARD 11)
|
||||||
target_include_directories(etcd-cpp-api PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/../proto)
|
target_include_directories(etcd-cpp-api PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/../proto)
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue