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