Skip to content
Snippets Groups Projects
Commit 1fa5cb3c authored by Aleš Friedl's avatar Aleš Friedl
Browse files

Ticket #30347 - add cmake build for c++

parent 54e1dd7d
Branches
Tags
No related merge requests found
......@@ -5,3 +5,7 @@ tag = True
tag_name = {new_version}
[bumpversion:file:setup.cfg]
[bumpversion:file:cmake/common.cmake]
search = API_MESSENGER_VERSION "{current_version}"
replace = API_MESSENGER_VERSION "{new_version}"
#
# Locate and configure the gRPC library
#
# Adds the following targets:
#
# gRPC::grpc - gRPC library
# gRPC::grpc++ - gRPC C++ library
# gRPC::grpc++_reflection - gRPC C++ reflection library
# gRPC::grpc_cpp_plugin - C++ generator plugin for Protocol Buffers
# gRPC::grpc_python_plugin - Python generator plugin for Protocol Buffers
#
#
# Generates C++ sources from the .proto files
#
# grpc_generate_cpp (<SRCS> <HDRS> <DEST> [<ARGN>...])
#
# SRCS - variable to define with autogenerated source files
# HDRS - variable to define with autogenerated header files
# DEST - directory where the source files will be created
# ARGN - .proto files
#
function(GRPC_GENERATE_CPP SRCS HDRS DEST)
if(NOT ARGN)
message(SEND_ERROR "Error: GRPC_GENERATE_CPP() called without any proto files")
return()
endif()
get_filename_component(PROTOBUF_IMPORT_DIRS ${PROTOBUF_IMPORT_DIRS} ABSOLUTE)
list(APPEND _grpc_include_path -I ${PROTOBUF_IMPORT_DIRS})
set(${SRCS})
set(${HDRS})
foreach(FIL ${ARGN})
get_filename_component(FIL ${FIL} ABSOLUTE)
string(REPLACE "${PROTOBUF_IMPORT_DIRS}/" "" REL_DIR ${FIL})
get_filename_component(REL_DIR ${REL_DIR} DIRECTORY)
if(REL_DIR)
string(APPEND REL_DIR "/")
endif()
get_filename_component(FIL_WE ${FIL} NAME_WE)
set(_dst_src "${DEST}/${REL_DIR}${FIL_WE}.grpc.pb.cc")
set(_dst_hdr "${DEST}/${REL_DIR}${FIL_WE}.grpc.pb.h")
list(APPEND ${SRCS} "${_dst_src}")
list(APPEND ${HDRS} "${_dst_hdr}")
add_custom_command(
OUTPUT "${_dst_src}"
"${_dst_hdr}"
COMMAND protobuf::protoc
ARGS --grpc_out ${DEST} ${_grpc_include_path} --plugin=protoc-gen-grpc=${GRPC_CPP_PLUGIN} ${FIL}
DEPENDS ${FIL} protobuf::protoc gRPC::grpc_cpp_plugin
COMMENT "Running C++ gRPC compiler on ${FIL}"
VERBATIM )
endforeach()
set_source_files_properties(${${SRCS}} ${${HDRS}} PROPERTIES GENERATED TRUE)
set(${SRCS} ${${SRCS}} PARENT_SCOPE)
set(${HDRS} ${${HDRS}} PARENT_SCOPE)
endfunction()
# Find gRPC include directory
find_path(GRPC_INCLUDE_DIR grpc/grpc.h)
mark_as_advanced(GRPC_INCLUDE_DIR)
# Find gRPC library
find_library(GRPC_LIBRARY NAMES grpc)
mark_as_advanced(GRPC_LIBRARY)
add_library(gRPC::grpc UNKNOWN IMPORTED)
set_target_properties(gRPC::grpc PROPERTIES
INTERFACE_INCLUDE_DIRECTORIES ${GRPC_INCLUDE_DIR}
INTERFACE_LINK_LIBRARIES "-lpthread;-ldl"
IMPORTED_LOCATION ${GRPC_LIBRARY}
)
# Find gRPC C++ library
find_library(GRPC_GRPC++_LIBRARY NAMES grpc++)
mark_as_advanced(GRPC_GRPC++_LIBRARY)
add_library(gRPC::grpc++ UNKNOWN IMPORTED)
set_target_properties(gRPC::grpc++ PROPERTIES
INTERFACE_INCLUDE_DIRECTORIES ${GRPC_INCLUDE_DIR}
INTERFACE_LINK_LIBRARIES gRPC::grpc
IMPORTED_LOCATION ${GRPC_GRPC++_LIBRARY}
)
# Find gRPC C++ reflection library
find_library(GRPC_GRPC++_REFLECTION_LIBRARY NAMES grpc++_reflection)
mark_as_advanced(GRPC_GRPC++_REFLECTION_LIBRARY)
add_library(gRPC::grpc++_reflection UNKNOWN IMPORTED)
set_target_properties(gRPC::grpc++_reflection PROPERTIES
INTERFACE_INCLUDE_DIRECTORIES ${GRPC_INCLUDE_DIR}
INTERFACE_LINK_LIBRARIES gRPC::grpc++
IMPORTED_LOCATION ${GRPC_GRPC++_REFLECTION_LIBRARY}
)
# Find gRPC CPP generator
find_program(GRPC_CPP_PLUGIN NAMES grpc_cpp_plugin)
mark_as_advanced(GRPC_CPP_PLUGIN)
add_executable(gRPC::grpc_cpp_plugin IMPORTED)
set_target_properties(gRPC::grpc_cpp_plugin PROPERTIES
IMPORTED_LOCATION ${GRPC_CPP_PLUGIN}
)
include(${CMAKE_ROOT}/Modules/FindPackageHandleStandardArgs.cmake)
FIND_PACKAGE_HANDLE_STANDARD_ARGS(GrpcCpp DEFAULT_MSG
GRPC_LIBRARY GRPC_INCLUDE_DIR GRPC_GRPC++_REFLECTION_LIBRARY GRPC_CPP_PLUGIN)
#
# Locate and configure the Google Protocol Buffers library
#
# Adds the following targets:
#
# protobuf::libprotobuf - Protobuf library
# protobuf::libprotobuf-lite - Protobuf lite library
# protobuf::libprotoc - Protobuf Protoc Library
# protobuf::protoc - protoc executable
#
#
# Generates C++ sources from the .proto files
#
# protobuf_generate_cpp (<SRCS> <HDRS> <DEST> [<ARGN>...])
#
# SRCS - variable to define with autogenerated source files
# HDRS - variable to define with autogenerated header files
# DEST - directory where the source files will be created
# ARGN - .proto files
#
function(PROTOBUF_GENERATE_CPP SRCS HDRS DEST)
if(NOT ARGN)
message(SEND_ERROR "Error: PROTOBUF_GENERATE_CPP() called without any proto files")
return()
endif()
get_filename_component(PROTOBUF_IMPORT_DIRS ${PROTOBUF_IMPORT_DIRS} ABSOLUTE)
list(APPEND _protobuf_include_path -I ${PROTOBUF_IMPORT_DIRS})
set(${SRCS})
set(${HDRS})
foreach(FIL ${ARGN})
get_filename_component(FIL ${FIL} ABSOLUTE)
string(REPLACE "${PROTOBUF_IMPORT_DIRS}/" "" REL_DIR ${FIL})
get_filename_component(REL_DIR ${REL_DIR} DIRECTORY)
if(REL_DIR)
string(APPEND REL_DIR "/")
endif()
get_filename_component(FIL_WE ${FIL} NAME_WE)
set(_dst_src "${DEST}/${REL_DIR}${FIL_WE}.pb.cc")
set(_dst_hdr "${DEST}/${REL_DIR}${FIL_WE}.pb.h")
list(APPEND ${SRCS} "${_dst_src}")
list(APPEND ${HDRS} "${_dst_hdr}")
add_custom_command(
OUTPUT "${_dst_src}"
"${_dst_hdr}"
COMMAND protobuf::protoc
ARGS --cpp_out ${DEST} ${_protobuf_include_path} ${FIL}
DEPENDS ${FIL} protobuf::protoc
COMMENT "Running C++ protocol buffer compiler on ${FIL}"
VERBATIM )
endforeach()
set_source_files_properties(${${SRCS}} ${${HDRS}} PROPERTIES GENERATED TRUE)
set(${SRCS} ${${SRCS}} PARENT_SCOPE)
set(${HDRS} ${${HDRS}} PARENT_SCOPE)
endfunction()
# Find the include directory
find_path(PROTOBUF_INCLUDE_DIR google/protobuf/service.h)
mark_as_advanced(PROTOBUF_INCLUDE_DIR)
# The Protobuf library
find_library(PROTOBUF_LIBRARY NAMES protobuf)
mark_as_advanced(PROTOBUF_LIBRARY)
add_library(protobuf::libprotobuf UNKNOWN IMPORTED)
set_target_properties(protobuf::libprotobuf PROPERTIES
INTERFACE_INCLUDE_DIRECTORIES ${PROTOBUF_INCLUDE_DIR}
INTERFACE_LINK_LIBRARIES pthread
IMPORTED_LOCATION ${PROTOBUF_LIBRARY}
)
# The Protobuf lite library
find_library(PROTOBUF_LITE_LIBRARY NAMES protobuf-lite)
mark_as_advanced(PROTOBUF_LITE_LIBRARY)
add_library(protobuf::libprotobuf-lite UNKNOWN IMPORTED)
set_target_properties(protobuf::libprotobuf-lite PROPERTIES
INTERFACE_INCLUDE_DIRECTORIES ${PROTOBUF_INCLUDE_DIR}
INTERFACE_LINK_LIBRARIES pthread
IMPORTED_LOCATION ${PROTOBUF_LITE_LIBRARY}
)
# The Protobuf Protoc Library
find_library(PROTOBUF_PROTOC_LIBRARY NAMES protoc)
mark_as_advanced(PROTOBUF_PROTOC_LIBRARY)
add_library(protobuf::libprotoc UNKNOWN IMPORTED)
set_target_properties(protobuf::libprotoc PROPERTIES
INTERFACE_INCLUDE_DIRECTORIES ${PROTOBUF_INCLUDE_DIR}
INTERFACE_LINK_LIBRARIES protobuf::libprotobuf
IMPORTED_LOCATION ${PROTOBUF_PROTOC_LIBRARY}
)
# Find the protoc Executable
if(NOT TARGET protobuf::protoc)
find_program(PROTOBUF_PROTOC_EXECUTABLE NAMES protoc)
mark_as_advanced(PROTOBUF_PROTOC_EXECUTABLE)
add_executable(protobuf::protoc IMPORTED)
set_target_properties(protobuf::protoc PROPERTIES
IMPORTED_LOCATION ${PROTOBUF_PROTOC_EXECUTABLE})
endif()
include(${CMAKE_ROOT}/Modules/FindPackageHandleStandardArgs.cmake)
FIND_PACKAGE_HANDLE_STANDARD_ARGS(ProtobufCpp DEFAULT_MSG
PROTOBUF_LIBRARY PROTOBUF_INCLUDE_DIR PROTOBUF_PROTOC_EXECUTABLE)
From MIT licenced example on https://github.com/IvanSafonov/grpc-cmake-example
if(DEFINED COMMON_CMAKE_CF4CE60F81004462ADD810242A9E6448)
return()
endif()
set(COMMON_CMAKE_CF4CE60F81004462ADD810242A9E6448 1)
set(API_MESSENGER_VERSION "1.1.0")
message(STATUS "API_MESSENGER_VERSION: ${API_MESSENGER_VERSION}")
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}")
set(PROTOS_DIR ${CMAKE_CURRENT_LIST_DIR}/..)
set(PROTOBUF_IMPORT_DIRS ${PROTOS_DIR})
set(FRED_MESSENGER_GRPC_FILES
${PROTOS_DIR}/fred_api/messenger/service_email_grpc.proto
${PROTOS_DIR}/fred_api/messenger/service_letter_grpc.proto
${PROTOS_DIR}/fred_api/messenger/service_sms_grpc.proto)
set(FRED_MESSENGER_PROTO_FILES
${PROTOS_DIR}/fred_api/messenger/common_types.proto
${PROTOS_DIR}/fred_api/messenger/email.proto
${PROTOS_DIR}/fred_api/messenger/letter.proto
${PROTOS_DIR}/fred_api/messenger/sms.proto
${FRED_MESSENGER_GRPC_FILES})
set(FRED_MESSENGER_GENERATE_INTO ${CMAKE_CURRENT_BINARY_DIR})
if(DEFINED CPP_CMAKE_217F4EEA277746D19940EF023ADD7D93)
return()
endif()
set(CPP_CMAKE_217F4EEA277746D19940EF023ADD7D93 1)
include("${CMAKE_CURRENT_LIST_DIR}/common.cmake")
find_package(ProtobufCpp REQUIRED)
find_package(GrpcCpp REQUIRED)
include_directories(${PROTOBUF_INCLUDE_DIR} ${GRPC_INCLUDE_DIR} ${CMAKE_CURRENT_BINARY_DIR})
if(CMAKE_CXX_STANDARD LESS 11)
message(FATAL_ERROR "This file requires compiler and library support for the ISO C++ 2011 standard. This support must be enabled with the -std=c++11 or -std=gnu++11 compiler options. Set CMAKE_CXX_STANDARD to at least 11")
endif()
protobuf_generate_cpp(FRED_MESSENGER_PROTO_SRCS FRED_MESSENGER_PROTO_HDRS ${FRED_MESSENGER_GENERATE_INTO} ${FRED_MESSENGER_PROTO_FILES})
grpc_generate_cpp(FRED_MESSENGER_GRPC_SRCS FRED_MESSENGER_GRPC_HDRS ${FRED_MESSENGER_GENERATE_INTO} ${FRED_MESSENGER_GRPC_FILES})
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment