• 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 3.5.1)
24
25# Project
26project(HelloWorld-SuperBuild C CXX)
27
28include(ExternalProject)
29
30# Note: For all external projects, instead of using checked-out code, one could
31# specify GIT_REPOSITORY and GIT_TAG to have cmake download the dependency directly,
32# without needing to add a submodule to your project.
33
34# Builds absl project from the git submodule.
35ExternalProject_Add(absl
36  PREFIX absl
37  SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../../../../third_party/abseil-cpp"
38  CMAKE_CACHE_ARGS
39        -DCMAKE_POSITION_INDEPENDENT_CODE:BOOL=TRUE
40        -DCMAKE_INSTALL_PREFIX:PATH=${CMAKE_CURRENT_BINARY_DIR}/absl
41)
42
43# Builds c-ares project from the git submodule.
44ExternalProject_Add(c-ares
45  PREFIX c-ares
46  SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../../../../third_party/cares/cares"
47  CMAKE_CACHE_ARGS
48        -DCARES_SHARED:BOOL=OFF
49        -DCARES_STATIC:BOOL=ON
50        -DCARES_STATIC_PIC:BOOL=ON
51        -DCMAKE_INSTALL_PREFIX:PATH=${CMAKE_CURRENT_BINARY_DIR}/c-ares
52)
53
54# Builds protobuf project from the git submodule.
55ExternalProject_Add(protobuf
56  PREFIX protobuf
57  SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../../../../third_party/protobuf/cmake"
58  CMAKE_CACHE_ARGS
59        -Dprotobuf_BUILD_TESTS:BOOL=OFF
60        -Dprotobuf_WITH_ZLIB:BOOL=OFF
61        -Dprotobuf_MSVC_STATIC_RUNTIME:BOOL=OFF
62        -DCMAKE_INSTALL_PREFIX:PATH=${CMAKE_CURRENT_BINARY_DIR}/protobuf
63)
64
65# Builds re2 project from the git submodule.
66ExternalProject_Add(re2
67  PREFIX re2
68  SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../../../../third_party/re2"
69  CMAKE_CACHE_ARGS
70        -DCMAKE_POSITION_INDEPENDENT_CODE:BOOL=TRUE
71        -DCMAKE_INSTALL_PREFIX:PATH=${CMAKE_CURRENT_BINARY_DIR}/re2
72)
73
74# Builds zlib project from the git submodule.
75ExternalProject_Add(zlib
76  PREFIX zlib
77  SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../../../../third_party/zlib"
78  CMAKE_CACHE_ARGS
79        -DCMAKE_INSTALL_PREFIX:PATH=${CMAKE_CURRENT_BINARY_DIR}/zlib
80)
81
82# the location where protobuf-config.cmake will be installed varies by platform
83if (WIN32)
84  set(_FINDPACKAGE_PROTOBUF_CONFIG_DIR "${CMAKE_CURRENT_BINARY_DIR}/protobuf/cmake")
85else()
86  set(_FINDPACKAGE_PROTOBUF_CONFIG_DIR "${CMAKE_CURRENT_BINARY_DIR}/protobuf/lib/cmake/protobuf")
87endif()
88
89# if OPENSSL_ROOT_DIR is set, propagate that hint path to the external projects with OpenSSL dependency.
90set(_CMAKE_ARGS_OPENSSL_ROOT_DIR "")
91if (OPENSSL_ROOT_DIR)
92  set(_CMAKE_ARGS_OPENSSL_ROOT_DIR "-DOPENSSL_ROOT_DIR:PATH=${OPENSSL_ROOT_DIR}")
93endif()
94
95# Builds gRPC based on locally checked-out sources and set arguments so that all the dependencies
96# are correctly located.
97ExternalProject_Add(grpc
98  PREFIX grpc
99  SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../../../.."
100  CMAKE_CACHE_ARGS
101        -DgRPC_INSTALL:BOOL=ON
102        -DgRPC_BUILD_TESTS:BOOL=OFF
103        -DgRPC_PROTOBUF_PROVIDER:STRING=package
104        -DgRPC_PROTOBUF_PACKAGE_TYPE:STRING=CONFIG
105        -DProtobuf_DIR:PATH=${_FINDPACKAGE_PROTOBUF_CONFIG_DIR}
106        -DgRPC_RE2_PROVIDER:STRING=package
107        -Dre2_DIR:STRING=${CMAKE_CURRENT_BINARY_DIR}/re2/lib/cmake/re2
108        -DgRPC_ZLIB_PROVIDER:STRING=package
109        -DZLIB_ROOT:STRING=${CMAKE_CURRENT_BINARY_DIR}/zlib
110        -DgRPC_ABSL_PROVIDER:STRING=package
111        -Dabsl_DIR:STRING=${CMAKE_CURRENT_BINARY_DIR}/absl/lib/cmake/absl
112        -DgRPC_CARES_PROVIDER:STRING=package
113        -Dc-ares_DIR:PATH=${CMAKE_CURRENT_BINARY_DIR}/c-ares/lib/cmake/c-ares
114        -DgRPC_SSL_PROVIDER:STRING=package
115        ${_CMAKE_ARGS_OPENSSL_ROOT_DIR}
116        -DCMAKE_INSTALL_PREFIX:PATH=${CMAKE_CURRENT_BINARY_DIR}/grpc
117  DEPENDS c-ares protobuf re2 zlib absl
118)
119
120# Build the helloworld projects itself using a CMakeLists.txt that assumes all the dependencies
121# have already been installed.
122# Even though helloworld is not really an "external project" from perspective of this build,
123# we are still importing it using ExternalProject_Add because that allows us to use find_package()
124# to locate all the dependencies (if we were building helloworld directly in this build we,
125# we would have needed to manually import the libraries as opposed to reusing targets exported by
126# gRPC and protobuf).
127ExternalProject_Add(helloworld
128  PREFIX helloworld
129  SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/.."
130  BINARY_DIR "${CMAKE_CURRENT_BINARY_DIR}/helloworld"
131  INSTALL_COMMAND ""
132  CMAKE_CACHE_ARGS
133        -DProtobuf_DIR:PATH=${_FINDPACKAGE_PROTOBUF_CONFIG_DIR}
134        -Dc-ares_DIR:PATH=${CMAKE_CURRENT_BINARY_DIR}/c-ares/lib/cmake/c-ares
135        -Dre2_DIR:STRING=${CMAKE_CURRENT_BINARY_DIR}/re2/lib/cmake/re2
136        -DZLIB_ROOT:STRING=${CMAKE_CURRENT_BINARY_DIR}/zlib
137        -Dabsl_DIR:STRING=${CMAKE_CURRENT_BINARY_DIR}/absl/lib/cmake/absl
138        ${_CMAKE_ARGS_OPENSSL_ROOT_DIR}
139        -DgRPC_DIR:PATH=${CMAKE_CURRENT_BINARY_DIR}/grpc/lib/cmake/grpc
140  DEPENDS protobuf grpc
141)
142