blob: d0f705f6d99bcdb32378844f2d944911a287a7a2 [file] [log] [blame]
Jan Tattermusch2286c632018-03-27 09:45:47 +02001# 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 Podsvirov13016cc2016-07-27 23:44:50 +030020cmake_minimum_required(VERSION 2.8)
21
Jan Tattermusch4caadb92017-08-25 12:59:11 +020022project(HelloWorld C CXX)
23
24if(NOT MSVC)
25 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
Jan Tattermusch06251f82017-11-09 11:09:29 +010026else()
27 add_definitions(-D_WIN32_WINNT=0x600)
Jan Tattermusch4caadb92017-08-25 12:59:11 +020028endif()
Konstantin Podsvirov13016cc2016-07-27 23:44:50 +030029
Jan Tattermusch4a510962018-03-29 14:07:20 +020030if(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>)
58else()
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 Podsvirov13016cc2016-07-27 23:44:50 +030061
Jan Tattermusch4a510962018-03-29 14:07:20 +020062 # 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 Tattermusch06251f82017-11-09 11:09:29 +010067
Jan Tattermusch4a510962018-03-29 14:07:20 +020068 set(_PROTOBUF_LIBPROTOBUF protobuf::libprotobuf)
69 set(_PROTOBUF_PROTOC $<TARGET_FILE:protobuf::protoc>)
Konstantin Podsvirov13016cc2016-07-27 23:44:50 +030070
Jan Tattermusch4a510962018-03-29 14:07:20 +020071 # 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>)
78endif()
Konstantin Podsvirov13016cc2016-07-27 23:44:50 +030079
80# Proto file
81get_filename_component(hw_proto "../../protos/helloworld.proto" ABSOLUTE)
82get_filename_component(hw_proto_path "${hw_proto}" PATH)
83
84# Generated sources
Jan Tattermusch2286c632018-03-27 09:45:47 +020085set(hw_proto_srcs "${CMAKE_CURRENT_BINARY_DIR}/helloworld.pb.cc")
86set(hw_proto_hdrs "${CMAKE_CURRENT_BINARY_DIR}/helloworld.pb.h")
Konstantin Podsvirov13016cc2016-07-27 23:44:50 +030087set(hw_grpc_srcs "${CMAKE_CURRENT_BINARY_DIR}/helloworld.grpc.pb.cc")
88set(hw_grpc_hdrs "${CMAKE_CURRENT_BINARY_DIR}/helloworld.grpc.pb.h")
89add_custom_command(
Jan Tattermusch2286c632018-03-27 09:45:47 +020090 OUTPUT "${hw_proto_srcs}" "${hw_proto_hdrs}" "${hw_grpc_srcs}" "${hw_grpc_hdrs}"
Jan Tattermusch06251f82017-11-09 11:09:29 +010091 COMMAND ${_PROTOBUF_PROTOC}
Jan Tattermusch2286c632018-03-27 09:45:47 +020092 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 Podsvirov13016cc2016-07-27 23:44:50 +030096 "${hw_proto}"
97 DEPENDS "${hw_proto}")
98
Jan Tattermusch2286c632018-03-27 09:45:47 +020099# Include generated *.pb.h files
Konstantin Podsvirov13016cc2016-07-27 23:44:50 +0300100include_directories("${CMAKE_CURRENT_BINARY_DIR}")
101
102# Targets greeter_[async_](client|server)
103foreach(_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 Tattermusch4a510962018-03-29 14:07:20 +0200110 ${_GRPC_GRPCPP_UNSECURE}
111 ${_PROTOBUF_LIBPROTOBUF})
Konstantin Podsvirov13016cc2016-07-27 23:44:50 +0300112endforeach()