forked from IvanSafonov/grpc-cmake-example
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 994ea1d
Showing
8 changed files
with
554 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/build/* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
project(grpc-cmake-example) | ||
cmake_minimum_required(VERSION 3.2) | ||
|
||
add_compile_options(-std=c++11) | ||
|
||
# GRPC and Protocol Buffers libraries location | ||
list(APPEND CMAKE_PREFIX_PATH "/opt/grpc" "/opt/protobuf") | ||
|
||
# Cmake find modules | ||
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}/cmake") | ||
|
||
find_package(Protobuf REQUIRED) | ||
find_package(GRPC REQUIRED) | ||
|
||
set(PROTOS | ||
${CMAKE_CURRENT_SOURCE_DIR}/protos/helloworld.proto | ||
) | ||
|
||
set(PROTO_SRC_DIR ${CMAKE_CURRENT_BINARY_DIR}/proto-src) | ||
file(MAKE_DIRECTORY ${PROTO_SRC_DIR}) | ||
include_directories(${PROTO_SRC_DIR}) | ||
|
||
protobuf_generate_cpp(PROTO_SRCS PROTO_HDRS ${PROTO_SRC_DIR} ${PROTOS}) | ||
grpc_generate_cpp(GRPC_SRCS GRPC_HDRS ${PROTO_SRC_DIR} ${PROTOS}) | ||
|
||
# Building server | ||
add_executable(greeter_server | ||
${CMAKE_CURRENT_SOURCE_DIR}/greeter_server.cc | ||
${PROTO_SRCS} | ||
${GRPC_SRCS} | ||
) | ||
target_link_libraries(greeter_server | ||
gRPC::grpc++_reflection | ||
protobuf::libprotobuf | ||
) | ||
|
||
# Building client | ||
add_executable(greeter_client | ||
${CMAKE_CURRENT_SOURCE_DIR}/greeter_client.cc | ||
${PROTO_SRCS} | ||
${GRPC_SRCS} | ||
) | ||
target_link_libraries(greeter_client | ||
gRPC::grpc++_reflection | ||
protobuf::libprotobuf | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
Tested on Ubuntu 16.04 LTS | ||
|
||
## How to build | ||
|
||
* Install Protocol Buffers. [Here's instruction](https://github.com/google/protobuf/blob/master/src/README.md) | ||
* Install gRPC. See [INSTALL](https://github.com/grpc/grpc/blob/master/INSTALL.md) for installation instructions | ||
* Change CMAKE_PREFIX_PATH variable in CMakeFiles.txt[10] according to your install prefixes | ||
* And build it | ||
```bash | ||
mkdir build | ||
cd build | ||
cmake .. | ||
make | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
# | ||
# 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 | ||
# | ||
|
||
# | ||
# 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: PROTOBUF_GENERATE_CPP() called without any proto files") | ||
return() | ||
endif() | ||
|
||
if(PROTOBUF_GENERATE_CPP_APPEND_PATH) | ||
# Create an include path for each file specified | ||
foreach(FIL ${ARGN}) | ||
get_filename_component(ABS_FIL ${FIL} ABSOLUTE) | ||
get_filename_component(ABS_PATH ${ABS_FIL} PATH) | ||
list(FIND _protobuf_include_path ${ABS_PATH} _contains_already) | ||
if(${_contains_already} EQUAL -1) | ||
list(APPEND _protobuf_include_path -I ${ABS_PATH}) | ||
endif() | ||
endforeach() | ||
else() | ||
set(_protobuf_include_path -I ${CMAKE_CURRENT_SOURCE_DIR}) | ||
endif() | ||
|
||
if(DEFINED PROTOBUF_IMPORT_DIRS) | ||
foreach(DIR ${PROTOBUF_IMPORT_DIRS}) | ||
get_filename_component(ABS_PATH ${DIR} ABSOLUTE) | ||
list(FIND _protobuf_include_path ${ABS_PATH} _contains_already) | ||
if(${_contains_already} EQUAL -1) | ||
list(APPEND _protobuf_include_path -I ${ABS_PATH}) | ||
endif() | ||
endforeach() | ||
endif() | ||
|
||
set(${SRCS}) | ||
set(${HDRS}) | ||
foreach(FIL ${ARGN}) | ||
get_filename_component(ABS_FIL ${FIL} ABSOLUTE) | ||
get_filename_component(FIL_WE ${FIL} NAME_WE) | ||
|
||
list(APPEND ${SRCS} "${DEST}/${FIL_WE}.grpc.pb.cc") | ||
list(APPEND ${HDRS} "${DEST}/${FIL_WE}.grpc.pb.h") | ||
|
||
add_custom_command( | ||
OUTPUT "${DEST}/${FIL_WE}.grpc.pb.cc" | ||
"${DEST}/${FIL_WE}.grpc.pb.h" | ||
COMMAND protobuf::protoc | ||
ARGS --grpc_out ${DEST} ${_protobuf_include_path} --plugin=protoc-gen-grpc=${GRPC_CPP_PLUGIN} ${ABS_FIL} | ||
DEPENDS ${ABS_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() | ||
|
||
# By default have PROTOBUF_GENERATE_CPP macro pass -I to protoc | ||
# for each directory where a proto file is referenced. | ||
if(NOT DEFINED PROTOBUF_GENERATE_CPP_APPEND_PATH) | ||
set(PROTOBUF_GENERATE_CPP_APPEND_PATH TRUE) | ||
endif() | ||
|
||
# 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(Protobuf DEFAULT_MSG | ||
GRPC_LIBRARY GRPC_INCLUDE_DIR GRPC_GRPC++_REFLECTION_LIBRARY GRPC_CPP_PLUGIN) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
# | ||
# 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() | ||
|
||
if(PROTOBUF_GENERATE_CPP_APPEND_PATH) | ||
# Create an include path for each file specified | ||
foreach(FIL ${ARGN}) | ||
get_filename_component(ABS_FIL ${FIL} ABSOLUTE) | ||
get_filename_component(ABS_PATH ${ABS_FIL} PATH) | ||
list(FIND _protobuf_include_path ${ABS_PATH} _contains_already) | ||
if(${_contains_already} EQUAL -1) | ||
list(APPEND _protobuf_include_path -I ${ABS_PATH}) | ||
endif() | ||
endforeach() | ||
else() | ||
set(_protobuf_include_path -I ${CMAKE_CURRENT_SOURCE_DIR}) | ||
endif() | ||
|
||
if(DEFINED PROTOBUF_IMPORT_DIRS) | ||
foreach(DIR ${PROTOBUF_IMPORT_DIRS}) | ||
get_filename_component(ABS_PATH ${DIR} ABSOLUTE) | ||
list(FIND _protobuf_include_path ${ABS_PATH} _contains_already) | ||
if(${_contains_already} EQUAL -1) | ||
list(APPEND _protobuf_include_path -I ${ABS_PATH}) | ||
endif() | ||
endforeach() | ||
endif() | ||
|
||
set(${SRCS}) | ||
set(${HDRS}) | ||
foreach(FIL ${ARGN}) | ||
get_filename_component(ABS_FIL ${FIL} ABSOLUTE) | ||
get_filename_component(FIL_WE ${FIL} NAME_WE) | ||
|
||
list(APPEND ${SRCS} "${DEST}/${FIL_WE}.pb.cc") | ||
list(APPEND ${HDRS} "${DEST}/${FIL_WE}.pb.h") | ||
|
||
add_custom_command( | ||
OUTPUT "${DEST}/${FIL_WE}.pb.cc" | ||
"${DEST}/${FIL_WE}.pb.h" | ||
COMMAND protobuf::protoc | ||
ARGS --cpp_out ${DEST} ${_protobuf_include_path} ${ABS_FIL} | ||
DEPENDS ${ABS_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() | ||
|
||
# By default have PROTOBUF_GENERATE_CPP macro pass -I to protoc | ||
# for each directory where a proto file is referenced. | ||
if(NOT DEFINED PROTOBUF_GENERATE_CPP_APPEND_PATH) | ||
set(PROTOBUF_GENERATE_CPP_APPEND_PATH TRUE) | ||
endif() | ||
|
||
# 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 | ||
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} | ||
) | ||
|
||
include(${CMAKE_ROOT}/Modules/FindPackageHandleStandardArgs.cmake) | ||
FIND_PACKAGE_HANDLE_STANDARD_ARGS(Protobuf DEFAULT_MSG | ||
PROTOBUF_LIBRARY PROTOBUF_INCLUDE_DIR PROTOBUF_PROTOC_EXECUTABLE) |
Oops, something went wrong.