Jan Tattermusch | 2286c63 | 2018-03-27 09:45:47 +0200 | [diff] [blame] | 1 | # Copyright 2018 gRPC authors.
|
| 2 | #
|
| 3 | # Licensed under the Apache License, Version 2.0 (the "License");
|
| 4 | # you may not use this file except in compliance with the License.
|
| 5 | # You may obtain a copy of the License at
|
| 6 | #
|
| 7 | # http://www.apache.org/licenses/LICENSE-2.0
|
| 8 | #
|
| 9 | # Unless required by applicable law or agreed to in writing, software
|
| 10 | # distributed under the License is distributed on an "AS IS" BASIS,
|
| 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
| 12 | # See the License for the specific language governing permissions and
|
| 13 | # limitations under the License.
|
| 14 | #
|
| 15 | # cmake build file for C++ helloworld example.
|
| 16 | # Assumes protobuf and gRPC have been installed using cmake.
|
| 17 | # See cmake_externalproject/CMakeLists.txt for all-in-one cmake build
|
| 18 | # that automatically builds all the dependencies before building helloworld.
|
| 19 |
|
Konstantin Podsvirov | 13016cc | 2016-07-27 23:44:50 +0300 | [diff] [blame] | 20 | cmake_minimum_required(VERSION 2.8)
|
| 21 |
|
Jan Tattermusch | 4caadb9 | 2017-08-25 12:59:11 +0200 | [diff] [blame] | 22 | project(HelloWorld C CXX)
|
| 23 |
|
| 24 | if(NOT MSVC)
|
| 25 | set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
|
Jan Tattermusch | 06251f8 | 2017-11-09 11:09:29 +0100 | [diff] [blame] | 26 | else()
|
| 27 | add_definitions(-D_WIN32_WINNT=0x600)
|
Jan Tattermusch | 4caadb9 | 2017-08-25 12:59:11 +0200 | [diff] [blame] | 28 | endif()
|
Konstantin Podsvirov | 13016cc | 2016-07-27 23:44:50 +0300 | [diff] [blame] | 29 |
|
Jan Tattermusch | 4a51096 | 2018-03-29 14:07:20 +0200 | [diff] [blame] | 30 | if(GRPC_AS_SUBMODULE)
|
| 31 | # One way to build a projects that uses gRPC is to just include the
|
| 32 | # entire gRPC project tree via "add_subdirectory".
|
| 33 | # This approach is very simple to use, but the are some potential
|
| 34 | # disadvantages:
|
| 35 | # * it includes gRPC's CMakeLists.txt directly into your build script
|
| 36 | # without and that can make gRPC's internal setting interfere with your
|
| 37 | # own build.
|
| 38 | # * depending on what's installed on your system, the contents of submodules
|
| 39 | # in gRPC's third_party/* might need to be available (and there might be
|
| 40 | # additional prerequisites required to build them). Consider using
|
| 41 | # the gRPC_*_PROVIDER options to fine-tune the expected behavior.
|
| 42 | #
|
| 43 | # A more robust approach to add dependency on gRPC is using
|
| 44 | # cmake's ExternalProject_Add (see cmake_externalproject/CMakeLists.txt).
|
| 45 |
|
| 46 | # Include the gRPC's cmake build (normally grpc source code would live
|
| 47 | # in a git submodule called "third_party/grpc", but this example lives in
|
| 48 | # the same repository as gRPC sources, so we just look a few directories up)
|
| 49 | add_subdirectory(../../.. ${CMAKE_CURRENT_BINARY_DIR}/grpc EXCLUDE_FROM_ALL)
|
| 50 | message(STATUS "Using gRPC via add_subdirectory.")
|
| 51 |
|
| 52 | # After using add_subdirectory, we can now use the grpc targets directly from
|
| 53 | # this build.
|
| 54 | set(_PROTOBUF_LIBPROTOBUF libprotobuf)
|
| 55 | set(_PROTOBUF_PROTOC $<TARGET_FILE:protoc>)
|
| 56 | set(_GRPC_GRPCPP_UNSECURE grpc++_unsecure)
|
| 57 | set(_GRPC_CPP_PLUGIN_EXECUTABLE $<TARGET_FILE:grpc_cpp_plugin>)
|
| 58 | else()
|
| 59 | # This branch assumes that gRPC and all its dependencies are already installed
|
| 60 | # on this system, so they can be located by find_package().
|
Konstantin Podsvirov | 13016cc | 2016-07-27 23:44:50 +0300 | [diff] [blame] | 61 |
|
Jan Tattermusch | 4a51096 | 2018-03-29 14:07:20 +0200 | [diff] [blame] | 62 | # Find Protobuf installation
|
| 63 | # Looks for protobuf-config.cmake file installed by Protobuf's cmake installation.
|
| 64 | set(protobuf_MODULE_COMPATIBLE TRUE)
|
| 65 | find_package(Protobuf CONFIG REQUIRED)
|
| 66 | message(STATUS "Using protobuf ${protobuf_VERSION}")
|
Jan Tattermusch | 06251f8 | 2017-11-09 11:09:29 +0100 | [diff] [blame] | 67 |
|
Jan Tattermusch | 4a51096 | 2018-03-29 14:07:20 +0200 | [diff] [blame] | 68 | set(_PROTOBUF_LIBPROTOBUF protobuf::libprotobuf)
|
| 69 | set(_PROTOBUF_PROTOC $<TARGET_FILE:protobuf::protoc>)
|
Konstantin Podsvirov | 13016cc | 2016-07-27 23:44:50 +0300 | [diff] [blame] | 70 |
|
Jan Tattermusch | 4a51096 | 2018-03-29 14:07:20 +0200 | [diff] [blame] | 71 | # Find gRPC installation
|
| 72 | # Looks for gRPCConfig.cmake file installed by gRPC's cmake installation.
|
| 73 | find_package(gRPC CONFIG REQUIRED)
|
| 74 | message(STATUS "Using gRPC ${gRPC_VERSION}")
|
| 75 |
|
| 76 | set(_GRPC_GRPCPP_UNSECURE gRPC::grpc++_unsecure)
|
| 77 | set(_GRPC_CPP_PLUGIN_EXECUTABLE $<TARGET_FILE:gRPC::grpc_cpp_plugin>)
|
| 78 | endif()
|
Konstantin Podsvirov | 13016cc | 2016-07-27 23:44:50 +0300 | [diff] [blame] | 79 |
|
| 80 | # Proto file
|
| 81 | get_filename_component(hw_proto "../../protos/helloworld.proto" ABSOLUTE)
|
| 82 | get_filename_component(hw_proto_path "${hw_proto}" PATH)
|
| 83 |
|
| 84 | # Generated sources
|
Jan Tattermusch | 2286c63 | 2018-03-27 09:45:47 +0200 | [diff] [blame] | 85 | set(hw_proto_srcs "${CMAKE_CURRENT_BINARY_DIR}/helloworld.pb.cc")
|
| 86 | set(hw_proto_hdrs "${CMAKE_CURRENT_BINARY_DIR}/helloworld.pb.h")
|
Konstantin Podsvirov | 13016cc | 2016-07-27 23:44:50 +0300 | [diff] [blame] | 87 | set(hw_grpc_srcs "${CMAKE_CURRENT_BINARY_DIR}/helloworld.grpc.pb.cc")
|
| 88 | set(hw_grpc_hdrs "${CMAKE_CURRENT_BINARY_DIR}/helloworld.grpc.pb.h")
|
| 89 | add_custom_command(
|
Jan Tattermusch | 2286c63 | 2018-03-27 09:45:47 +0200 | [diff] [blame] | 90 | OUTPUT "${hw_proto_srcs}" "${hw_proto_hdrs}" "${hw_grpc_srcs}" "${hw_grpc_hdrs}"
|
Jan Tattermusch | 06251f8 | 2017-11-09 11:09:29 +0100 | [diff] [blame] | 91 | COMMAND ${_PROTOBUF_PROTOC}
|
Jan Tattermusch | 2286c63 | 2018-03-27 09:45:47 +0200 | [diff] [blame] | 92 | ARGS --grpc_out "${CMAKE_CURRENT_BINARY_DIR}"
|
| 93 | --cpp_out "${CMAKE_CURRENT_BINARY_DIR}"
|
| 94 | -I "${hw_proto_path}"
|
| 95 | --plugin=protoc-gen-grpc="${_GRPC_CPP_PLUGIN_EXECUTABLE}"
|
Konstantin Podsvirov | 13016cc | 2016-07-27 23:44:50 +0300 | [diff] [blame] | 96 | "${hw_proto}"
|
| 97 | DEPENDS "${hw_proto}")
|
| 98 |
|
Jan Tattermusch | 2286c63 | 2018-03-27 09:45:47 +0200 | [diff] [blame] | 99 | # Include generated *.pb.h files
|
Konstantin Podsvirov | 13016cc | 2016-07-27 23:44:50 +0300 | [diff] [blame] | 100 | include_directories("${CMAKE_CURRENT_BINARY_DIR}")
|
| 101 |
|
| 102 | # Targets greeter_[async_](client|server)
|
| 103 | foreach(_target
|
| 104 | greeter_client greeter_server
|
| 105 | greeter_async_client greeter_async_server)
|
| 106 | add_executable(${_target} "${_target}.cc"
|
| 107 | ${hw_proto_srcs}
|
| 108 | ${hw_grpc_srcs})
|
| 109 | target_link_libraries(${_target}
|
Jan Tattermusch | 4a51096 | 2018-03-29 14:07:20 +0200 | [diff] [blame] | 110 | ${_GRPC_GRPCPP_UNSECURE}
|
| 111 | ${_PROTOBUF_LIBPROTOBUF})
|
Konstantin Podsvirov | 13016cc | 2016-07-27 23:44:50 +0300 | [diff] [blame] | 112 | endforeach()
|