Home | History | Annotate | Download | only in helloworld
      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 
     20 cmake_minimum_required(VERSION 2.8)
     21 
     22 project(HelloWorld C CXX)
     23 
     24 if(NOT MSVC)
     25   set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
     26 else()
     27   add_definitions(-D_WIN32_WINNT=0x600)
     28 endif()
     29 
     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().
     61 
     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}")
     67 
     68   set(_PROTOBUF_LIBPROTOBUF protobuf::libprotobuf)
     69   set(_PROTOBUF_PROTOC $<TARGET_FILE:protobuf::protoc>)
     70 
     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()
     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
     85 set(hw_proto_srcs "${CMAKE_CURRENT_BINARY_DIR}/helloworld.pb.cc")
     86 set(hw_proto_hdrs "${CMAKE_CURRENT_BINARY_DIR}/helloworld.pb.h")
     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(
     90       OUTPUT "${hw_proto_srcs}" "${hw_proto_hdrs}" "${hw_grpc_srcs}" "${hw_grpc_hdrs}"
     91       COMMAND ${_PROTOBUF_PROTOC}
     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}"
     96         "${hw_proto}"
     97       DEPENDS "${hw_proto}")
     98 
     99 # Include generated *.pb.h files
    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}
    110     ${_GRPC_GRPCPP_UNSECURE}
    111     ${_PROTOBUF_LIBPROTOBUF})
    112 endforeach()
    113