xref: /aosp_15_r20/external/grpc-grpc/examples/cpp/helloworld/cmake_externalproject/CMakeLists.txt (revision cc02d7e222339f7a4f6ba5f422e6413f4bd931f2)
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.8)
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 absl project from the git submodule.
44ExternalProject_Add(utf8_range
45  PREFIX utf8_range
46  SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../../../../third_party/utf8_range"
47  CMAKE_CACHE_ARGS
48        -DCMAKE_POSITION_INDEPENDENT_CODE:BOOL=TRUE
49        -Dutf8_range_ENABLE_TESTS:BOOL=OFF
50        -Dabsl_DIR:STRING=${CMAKE_CURRENT_BINARY_DIR}/absl/lib/cmake/absl
51        -DCMAKE_INSTALL_PREFIX:PATH=${CMAKE_CURRENT_BINARY_DIR}/utf8_range
52  DEPENDS absl
53)
54
55# Builds c-ares project from the git submodule.
56ExternalProject_Add(c-ares
57  PREFIX c-ares
58  SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../../../../third_party/cares/cares"
59  CMAKE_CACHE_ARGS
60        -DCARES_SHARED:BOOL=OFF
61        -DCARES_STATIC:BOOL=ON
62        -DCARES_STATIC_PIC:BOOL=ON
63        -DCMAKE_INSTALL_PREFIX:PATH=${CMAKE_CURRENT_BINARY_DIR}/c-ares
64)
65
66# Builds protobuf project from the git submodule.
67ExternalProject_Add(protobuf
68  PREFIX protobuf
69  SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../../../../third_party/protobuf"
70  CMAKE_CACHE_ARGS
71        -Dprotobuf_BUILD_TESTS:BOOL=OFF
72        -Dprotobuf_WITH_ZLIB:BOOL=OFF
73        -Dprotobuf_ABSL_PROVIDER:STRING=package
74        -Dabsl_DIR:STRING=${CMAKE_CURRENT_BINARY_DIR}/absl/lib/cmake/absl
75        -Dutf8_range_DIR:STRING=${CMAKE_CURRENT_BINARY_DIR}/utf8_range/lib/cmake/utf8_range
76        -Dprotobuf_MSVC_STATIC_RUNTIME:BOOL=OFF
77        -DCMAKE_INSTALL_PREFIX:PATH=${CMAKE_CURRENT_BINARY_DIR}/protobuf
78  DEPENDS absl utf8_range
79)
80
81# Builds re2 project from the git submodule.
82ExternalProject_Add(re2
83  PREFIX re2
84  SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../../../../third_party/re2"
85  CMAKE_CACHE_ARGS
86        -DCMAKE_POSITION_INDEPENDENT_CODE:BOOL=TRUE
87        -DCMAKE_INSTALL_PREFIX:PATH=${CMAKE_CURRENT_BINARY_DIR}/re2
88)
89
90# Builds zlib project from the git submodule.
91ExternalProject_Add(zlib
92  PREFIX zlib
93  SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../../../../third_party/zlib"
94  CMAKE_CACHE_ARGS
95        -DCMAKE_INSTALL_PREFIX:PATH=${CMAKE_CURRENT_BINARY_DIR}/zlib
96)
97
98# the location where protobuf-config.cmake will be installed varies by platform
99if (WIN32)
100  set(_FINDPACKAGE_PROTOBUF_CONFIG_DIR "${CMAKE_CURRENT_BINARY_DIR}/protobuf/cmake")
101else()
102  set(_FINDPACKAGE_PROTOBUF_CONFIG_DIR "${CMAKE_CURRENT_BINARY_DIR}/protobuf/lib/cmake/protobuf")
103endif()
104
105# if OPENSSL_ROOT_DIR is set, propagate that hint path to the external projects with OpenSSL dependency.
106set(_CMAKE_ARGS_OPENSSL_ROOT_DIR "")
107if (OPENSSL_ROOT_DIR)
108  set(_CMAKE_ARGS_OPENSSL_ROOT_DIR "-DOPENSSL_ROOT_DIR:PATH=${OPENSSL_ROOT_DIR}")
109endif()
110
111# Builds gRPC based on locally checked-out sources and set arguments so that all the dependencies
112# are correctly located.
113ExternalProject_Add(grpc
114  PREFIX grpc
115  SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../../../.."
116  CMAKE_CACHE_ARGS
117        -DgRPC_INSTALL:BOOL=ON
118        -DgRPC_BUILD_TESTS:BOOL=OFF
119        -DgRPC_BUILD_MSVC_MP_COUNT:STRING=-1
120        -Dutf8_range_DIR:STRING=${CMAKE_CURRENT_BINARY_DIR}/utf8_range/lib/cmake/utf8_range
121        -DgRPC_PROTOBUF_PROVIDER:STRING=package
122        -DProtobuf_DIR:PATH=${_FINDPACKAGE_PROTOBUF_CONFIG_DIR}
123        -DgRPC_RE2_PROVIDER:STRING=package
124        -Dre2_DIR:STRING=${CMAKE_CURRENT_BINARY_DIR}/re2/lib/cmake/re2
125        -DgRPC_ZLIB_PROVIDER:STRING=package
126        -DZLIB_ROOT:STRING=${CMAKE_CURRENT_BINARY_DIR}/zlib
127        -DgRPC_ABSL_PROVIDER:STRING=package
128        -Dabsl_DIR:STRING=${CMAKE_CURRENT_BINARY_DIR}/absl/lib/cmake/absl
129        -DgRPC_CARES_PROVIDER:STRING=package
130        -Dc-ares_DIR:PATH=${CMAKE_CURRENT_BINARY_DIR}/c-ares/lib/cmake/c-ares
131        -DgRPC_SSL_PROVIDER:STRING=package
132        ${_CMAKE_ARGS_OPENSSL_ROOT_DIR}
133        -DCMAKE_INSTALL_PREFIX:PATH=${CMAKE_CURRENT_BINARY_DIR}/grpc
134  DEPENDS c-ares protobuf re2 zlib absl
135)
136
137# Build the helloworld projects itself using a CMakeLists.txt that assumes all the dependencies
138# have already been installed.
139# Even though helloworld is not really an "external project" from perspective of this build,
140# we are still importing it using ExternalProject_Add because that allows us to use find_package()
141# to locate all the dependencies (if we were building helloworld directly in this build we,
142# we would have needed to manually import the libraries as opposed to reusing targets exported by
143# gRPC and protobuf).
144ExternalProject_Add(helloworld
145  PREFIX helloworld
146  SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/.."
147  BINARY_DIR "${CMAKE_CURRENT_BINARY_DIR}/helloworld"
148  INSTALL_COMMAND ""
149  CMAKE_CACHE_ARGS
150        -DProtobuf_DIR:PATH=${_FINDPACKAGE_PROTOBUF_CONFIG_DIR}
151        -Dc-ares_DIR:PATH=${CMAKE_CURRENT_BINARY_DIR}/c-ares/lib/cmake/c-ares
152        -Dre2_DIR:STRING=${CMAKE_CURRENT_BINARY_DIR}/re2/lib/cmake/re2
153        -Dutf8_range_DIR:STRING=${CMAKE_CURRENT_BINARY_DIR}/utf8_range/lib/cmake/utf8_range
154        -DZLIB_ROOT:STRING=${CMAKE_CURRENT_BINARY_DIR}/zlib
155        -Dabsl_DIR:STRING=${CMAKE_CURRENT_BINARY_DIR}/absl/lib/cmake/absl
156        ${_CMAKE_ARGS_OPENSSL_ROOT_DIR}
157        -DgRPC_DIR:PATH=${CMAKE_CURRENT_BINARY_DIR}/grpc/lib/cmake/grpc
158  DEPENDS protobuf grpc
159)
160