• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "superbuild" file for C++ helloworld example.
16# This build file demonstrates how to build the helloworld project
17# and all its dependencies in a single cmake build (hence "superbuild")
18# that is easy to build and maintain.
19# cmake's ExternalProject_Add() is used to import all the sub-projects,
20# including the "helloworld" project itself.
21# See https://blog.kitware.com/cmake-superbuilds-git-submodules/
22
23cmake_minimum_required(VERSION 2.8)
24
25# Project
26project(HelloWorld-SuperBuild C CXX)
27
28include(ExternalProject)
29
30# Builds c-ares project from the git submodule.
31# Note: For all external projects, instead of using checked-out code, one could
32# specify GIT_REPOSITORY and GIT_TAG to have cmake download the dependency directly,
33# without needing to add a submodule to your project.
34ExternalProject_Add(c-ares
35  PREFIX c-ares
36  SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../../../../third_party/cares/cares"
37  CMAKE_CACHE_ARGS
38        -DCARES_SHARED:BOOL=OFF
39        -DCARES_STATIC:BOOL=ON
40        -DCARES_STATIC_PIC:BOOL=ON
41        -DCMAKE_INSTALL_PREFIX:PATH=${CMAKE_CURRENT_BINARY_DIR}/c-ares
42)
43
44# Builds protobuf project from the git submodule.
45ExternalProject_Add(protobuf
46  PREFIX protobuf
47  SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../../../../third_party/protobuf/cmake"
48  CMAKE_CACHE_ARGS
49        -Dprotobuf_BUILD_TESTS:BOOL=OFF
50        -Dprotobuf_WITH_ZLIB:BOOL=OFF
51        -Dprotobuf_MSVC_STATIC_RUNTIME:BOOL=OFF
52        -DCMAKE_INSTALL_PREFIX:PATH=${CMAKE_CURRENT_BINARY_DIR}/protobuf
53)
54
55# Builds zlib project from the git submodule.
56ExternalProject_Add(zlib
57  PREFIX zlib
58  SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../../../../third_party/zlib"
59  CMAKE_CACHE_ARGS
60        -DCMAKE_INSTALL_PREFIX:PATH=${CMAKE_CURRENT_BINARY_DIR}/zlib
61)
62
63# the location where protobuf-config.cmake will be installed varies by platform
64if (WIN32)
65  set(_FINDPACKAGE_PROTOBUF_CONFIG_DIR "${CMAKE_CURRENT_BINARY_DIR}/protobuf/cmake")
66else()
67  set(_FINDPACKAGE_PROTOBUF_CONFIG_DIR "${CMAKE_CURRENT_BINARY_DIR}/protobuf/lib/cmake/protobuf")
68endif()
69
70# if OPENSSL_ROOT_DIR is set, propagate that hint path to the external projects with OpenSSL dependency.
71set(_CMAKE_ARGS_OPENSSL_ROOT_DIR "")
72if (OPENSSL_ROOT_DIR)
73  set(_CMAKE_ARGS_OPENSSL_ROOT_DIR "-DOPENSSL_ROOT_DIR:PATH=${OPENSSL_ROOT_DIR}")
74endif()
75
76# Builds gRPC based on locally checked-out sources and set arguments so that all the dependencies
77# are correctly located.
78ExternalProject_Add(grpc
79  PREFIX grpc
80  SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../../../.."
81  CMAKE_CACHE_ARGS
82        -DgRPC_INSTALL:BOOL=ON
83        -DgRPC_BUILD_TESTS:BOOL=OFF
84        -DgRPC_PROTOBUF_PROVIDER:STRING=package
85        -DgRPC_PROTOBUF_PACKAGE_TYPE:STRING=CONFIG
86        -DProtobuf_DIR:PATH=${_FINDPACKAGE_PROTOBUF_CONFIG_DIR}
87        -DgRPC_ZLIB_PROVIDER:STRING=package
88        -DZLIB_ROOT:STRING=${CMAKE_CURRENT_BINARY_DIR}/zlib
89        -DgRPC_CARES_PROVIDER:STRING=package
90        -Dc-ares_DIR:PATH=${CMAKE_CURRENT_BINARY_DIR}/c-ares/lib/cmake/c-ares
91        -DgRPC_SSL_PROVIDER:STRING=package
92        ${_CMAKE_ARGS_OPENSSL_ROOT_DIR}
93        -DCMAKE_INSTALL_PREFIX:PATH=${CMAKE_CURRENT_BINARY_DIR}/grpc
94  DEPENDS c-ares protobuf zlib
95)
96
97# Build the helloworld projects itself using a CMakeLists.txt that assumes all the dependencies
98# have already been installed.
99# Even though helloworld is not really an "external project" from perspective of this build,
100# we are still importing it using ExternalProject_Add because that allows us to use find_package()
101# to locate all the dependencies (if we were building helloworld directly in this build we,
102# we would have needed to manually import the libraries as opposed to reusing targets exported by
103# gRPC and protobuf).
104ExternalProject_Add(helloworld
105  PREFIX helloworld
106  SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/.."
107  BINARY_DIR "${CMAKE_CURRENT_BINARY_DIR}/helloworld"
108  INSTALL_COMMAND ""
109  CMAKE_CACHE_ARGS
110        -DProtobuf_DIR:PATH=${_FINDPACKAGE_PROTOBUF_CONFIG_DIR}
111        -Dc-ares_DIR:PATH=${CMAKE_CURRENT_BINARY_DIR}/c-ares/lib/cmake/c-ares
112        -DZLIB_ROOT:STRING=${CMAKE_CURRENT_BINARY_DIR}/zlib
113        ${_CMAKE_ARGS_OPENSSL_ROOT_DIR}
114        -DgRPC_DIR:PATH=${CMAKE_CURRENT_BINARY_DIR}/grpc/lib/cmake/grpc
115  DEPENDS protobuf grpc
116)
117