blob: c3c636c6af7db733bd1ec7e298735553ce7b8b38 [file] [log] [blame]
Tamas Berghammerd798a7d2016-06-21 13:58:18 +01001%YAML 1.2
2--- |
3 # GRPC global cmake file
4 # This currently builds C and C++ code.
5 # This file has been automatically generated from a template file.
6 # Please look at the templates directory instead.
7 # This file can be regenerated from the template by running
8 # tools/buildgen/generate_projects.sh
Nicolas "Pixel" Nobleb360c8a2016-07-02 00:54:28 +02009 #
10 # Additionally, this is currently very experimental, and unsupported.
11 # Further work will happen on that file.
12 #
Tamas Berghammerd798a7d2016-06-21 13:58:18 +010013 # Copyright 2015, Google Inc.
14 # All rights reserved.
15 #
16 # Redistribution and use in source and binary forms, with or without
17 # modification, are permitted provided that the following conditions are
18 # met:
19 #
20 # * Redistributions of source code must retain the above copyright
21 # notice, this list of conditions and the following disclaimer.
22 # * Redistributions in binary form must reproduce the above
23 # copyright notice, this list of conditions and the following disclaimer
24 # in the documentation and/or other materials provided with the
25 # distribution.
26 # * Neither the name of Google Inc. nor the names of its
27 # contributors may be used to endorse or promote products derived from
28 # this software without specific prior written permission.
29 #
30 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
31 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
32 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
33 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
34 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
35 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
36 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
37 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
38 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
39 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
40 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
41
42 <%!
43 def get_deps(target_dict):
44 deps = []
Konstantin Podsvirov5baf7252016-07-27 16:25:37 +030045 if target_dict.get('baselib', False):
46 deps.append("${_gRPC_BASELIB_LIBRARIES}")
Tamas Berghammerd798a7d2016-06-21 13:58:18 +010047 if target_dict.get('build', None) in ['protoc']:
Konstantin Podsvirov5baf7252016-07-27 16:25:37 +030048 deps.append("${_gRPC_PROTOBUF_PROTOC_LIBRARIES}")
Tamas Berghammerd798a7d2016-06-21 13:58:18 +010049 if target_dict.get('secure', False):
Konstantin Podsvirov5baf7252016-07-27 16:25:37 +030050 deps.append("${_gRPC_SSL_LIBRARIES}")
Tamas Berghammerd798a7d2016-06-21 13:58:18 +010051 if target_dict['name'] in ['grpc++', 'grpc++_unsecure', 'grpc++_codegen_lib']:
Konstantin Podsvirov5baf7252016-07-27 16:25:37 +030052 deps.append("${_gRPC_PROTOBUF_LIBRARIES}")
Tamas Berghammerdf6a44c2016-06-22 13:38:55 +010053 elif target_dict['name'] in ['grpc']:
Konstantin Podsvirov5baf7252016-07-27 16:25:37 +030054 deps.append("${_gRPC_ZLIB_LIBRARIES}")
Tamas Berghammerd798a7d2016-06-21 13:58:18 +010055 for d in target_dict.get('deps', []):
56 deps.append(d)
57 return deps
58 %>
59
60 cmake_minimum_required(VERSION 2.8)
61
Tamas Berghammerdf6a44c2016-06-22 13:38:55 +010062 set(PACKAGE_NAME "grpc")
Craig Tiller83c47892016-09-13 11:14:43 -070063 set(PACKAGE_VERSION "${settings.cpp_version}")
Tamas Berghammerdf6a44c2016-06-22 13:38:55 +010064 set(PACKAGE_STRING "<%text>${PACKAGE_NAME} ${PACKAGE_VERSION}</%text>")
65 set(PACKAGE_TARNAME "<%text>${PACKAGE_NAME}-${PACKAGE_VERSION}</%text>")
66 set(PACKAGE_BUGREPORT "https://github.com/grpc/grpc/issues/")
67 project(<%text>${PACKAGE_NAME}</%text> C CXX)
68
Arkadiy Shapkin40beb372016-08-17 14:42:36 +030069 if (NOT MSVC)
70 set(gRPC_INSTALL ON CACHE BOOL "Generate installation target")
71 else()
72 set(gRPC_INSTALL OFF CACHE BOOL "Generate installation target")
73 endif()
74
Konstantin Podsvirov5baf7252016-07-27 16:25:37 +030075 set(gRPC_ZLIB_PROVIDER "module" CACHE STRING "Provider of zlib library")
76 set_property(CACHE gRPC_ZLIB_PROVIDER PROPERTY STRINGS "module" "package")
Tamas Berghammerd798a7d2016-06-21 13:58:18 +010077
Konstantin Podsvirov5baf7252016-07-27 16:25:37 +030078 set(gRPC_SSL_PROVIDER "module" CACHE STRING "Provider of ssl library")
79 set_property(CACHE gRPC_SSL_PROVIDER PROPERTY STRINGS "module" "package")
Tamas Berghammera72712e2016-07-06 10:30:25 +010080
Konstantin Podsvirov5baf7252016-07-27 16:25:37 +030081 set(gRPC_PROTOBUF_PROVIDER "module" CACHE STRING "Provider of protobuf library")
82 set_property(CACHE gRPC_PROTOBUF_PROVIDER PROPERTY STRINGS "module" "package")
Tamas Berghammerd798a7d2016-06-21 13:58:18 +010083
Konstantin Podsvirov5baf7252016-07-27 16:25:37 +030084 set(gRPC_USE_PROTO_LITE OFF CACHE BOOL "Use the protobuf-lite library")
Tamas Berghammerd798a7d2016-06-21 13:58:18 +010085
Arkadiy Shapkin40beb372016-08-17 14:42:36 +030086 if (MSVC)
87 add_definitions( -D_WIN32_WINNT=0x600 )
88 endif()
89
Konstantin Podsvirov5baf7252016-07-27 16:25:37 +030090 if (gRPC_USE_PROTO_LITE)
91 set(_gRPC_PROTOBUF_LIBRARY_NAME "libprotobuf-lite")
Tamas Berghammer23911562016-07-27 15:24:05 +010092 add_definitions("-DGRPC_USE_PROTO_LITE")
93 else()
Konstantin Podsvirov5baf7252016-07-27 16:25:37 +030094 set(_gRPC_PROTOBUF_LIBRARY_NAME "libprotobuf")
95 endif()
96
97 if("<%text>${gRPC_ZLIB_PROVIDER}</%text>" STREQUAL "module")
98 if(NOT ZLIB_ROOT_DIR)
99 set(ZLIB_ROOT_DIR <%text>${CMAKE_CURRENT_SOURCE_DIR}</%text>/third_party/zlib)
100 endif()
101 set(ZLIB_INCLUDE_DIR "<%text>${ZLIB_ROOT_DIR}</%text>")
102 if(EXISTS "<%text>${ZLIB_ROOT_DIR}</%text>/CMakeLists.txt")
103 add_subdirectory(<%text>${ZLIB_ROOT_DIR}</%text> third_party/zlib)
104 if(TARGET zlibstatic)
105 set(_gRPC_ZLIB_LIBRARIES zlibstatic)
106 endif()
107 else()
108 message(WARNING "gRPC_ZLIB_PROVIDER is \"module\" but ZLIB_ROOT_DIR is wrong")
109 endif()
110 elseif("<%text>${gRPC_ZLIB_PROVIDER}</%text>" STREQUAL "package")
111 find_package(ZLIB)
112 if(TARGET ZLIB::ZLIB)
113 set(_gRPC_ZLIB_LIBRARIES ZLIB::ZLIB)
114 endif()
Konstantin Podsvirovdd331482016-07-28 00:00:25 +0300115 set(_gRPC_FIND_ZLIB "if(NOT ZLIB_FOUND)\n find_package(ZLIB)\nendif()")
Konstantin Podsvirov5baf7252016-07-27 16:25:37 +0300116 endif()
117
118 if("<%text>${gRPC_PROTOBUF_PROVIDER}</%text>" STREQUAL "module")
119 # Building the protobuf tests require gmock what is not part of a standard protobuf checkout.
120 # Disable them unless they are explicitly requested from the cmake command line (when we assume
121 # gmock is downloaded to the right location inside protobuf).
122 if(NOT protobuf_BUILD_TESTS)
123 set(protobuf_BUILD_TESTS OFF CACHE BOOL "Build protobuf tests")
124 endif()
125 if(NOT PROTOBUF_ROOT_DIR)
126 set(PROTOBUF_ROOT_DIR <%text>${CMAKE_CURRENT_SOURCE_DIR}</%text>/third_party/protobuf)
127 endif()
128 if(EXISTS "<%text>${PROTOBUF_ROOT_DIR}</%text>/cmake/CMakeLists.txt")
Arkadiy Shapkin40beb372016-08-17 14:42:36 +0300129 set(protobuf_MSVC_STATIC_RUNTIME OFF CACHE BOOL "Link static runtime libraries")
Konstantin Podsvirov5baf7252016-07-27 16:25:37 +0300130 add_subdirectory(<%text>${PROTOBUF_ROOT_DIR}</%text>/cmake third_party/protobuf)
131 if(TARGET <%text>${_gRPC_PROTOBUF_LIBRARY_NAME}</%text>)
132 set(_gRPC_PROTOBUF_LIBRARIES <%text>${_gRPC_PROTOBUF_LIBRARY_NAME}</%text>)
133 endif()
134 if(TARGET libprotoc)
135 set(_gRPC_PROTOBUF_PROTOC_LIBRARIES libprotoc)
136 endif()
137 else()
138 message(WARNING "gRPC_PROTOBUF_PROVIDER is \"module\" but PROTOBUF_ROOT_DIR is wrong")
139 endif()
140 elseif("<%text>${gRPC_PROTOBUF_PROVIDER}</%text>" STREQUAL "package")
141 find_package(protobuf CONFIG)
142 if(protobuf_FOUND)
143 if(TARGET protobuf::<%text>${_gRPC_PROTOBUF_LIBRARY_NAME}</%text>)
144 set(_gRPC_PROTOBUF_LIBRARIES protobuf::<%text>${_gRPC_PROTOBUF_LIBRARY_NAME}</%text>)
145 endif()
146 if(TARGET protobuf::libprotoc)
147 set(_gRPC_PROTOBUF_PROTOC_LIBRARIES protobuf::libprotoc)
148 endif()
Konstantin Podsvirovdd331482016-07-28 00:00:25 +0300149 set(_gRPC_FIND_PROTOBUF "if(NOT protobuf_FOUND)\n find_package(protobuf CONFIG)\nendif()")
Konstantin Podsvirov5baf7252016-07-27 16:25:37 +0300150 else()
151 find_package(Protobuf MODULE)
Konstantin Podsvirovdd331482016-07-28 00:00:25 +0300152 set(_gRPC_FIND_PROTOBUF "if(NOT Protobuf_FOUND)\n find_package(Protobuf)\nendif()")
Konstantin Podsvirov5baf7252016-07-27 16:25:37 +0300153 endif()
154 endif()
155
156 if("<%text>${gRPC_SSL_PROVIDER}</%text>" STREQUAL "module")
157 if(NOT BORINGSSL_ROOT_DIR)
158 set(BORINGSSL_ROOT_DIR <%text>${CMAKE_CURRENT_SOURCE_DIR}</%text>/third_party/boringssl)
159 endif()
160 if(EXISTS "<%text>${BORINGSSL_ROOT_DIR}</%text>/CMakeLists.txt")
161 add_subdirectory(<%text>${BORINGSSL_ROOT_DIR}</%text> third_party/boringssl)
162 if(TARGET ssl)
163 set(_gRPC_SSL_LIBRARIES ssl)
164 endif()
165 else()
166 message(WARNING "gRPC_SSL_PROVIDER is \"module\" but BORINGSSL_ROOT_DIR is wrong")
167 endif()
168 elseif("<%text>${gRPC_SSL_PROVIDER}</%text>" STREQUAL "package")
169 find_package(OpenSSL)
170 if(TARGET OpenSSL::SSL)
171 set(_gRPC_SSL_LIBRARIES OpenSSL::SSL)
172 endif()
Konstantin Podsvirovdd331482016-07-28 00:00:25 +0300173 set(_gRPC_FIND_SSL "if(NOT OpenSSL_FOUND)\n find_package(OpenSSL)\nendif()")
Konstantin Podsvirov5baf7252016-07-27 16:25:37 +0300174 endif()
175
176 if(NOT MSVC)
177 set(CMAKE_C_FLAGS "<%text>${CMAKE_C_FLAGS}</%text> -std=c11")
178 set(CMAKE_CXX_FLAGS "<%text>${CMAKE_CXX_FLAGS}</%text> -std=c++11")
179 endif()
180
181 if(WIN32 AND MSVC)
182 set(_gRPC_BASELIB_LIBRARIES wsock32 ws2_32)
Tamas Berghammer23911562016-07-27 15:24:05 +0100183 endif()
184
Konstantin Podsvirovd2b88302016-07-27 23:43:47 +0300185 include(GNUInstallDirs)
186 if(NOT DEFINED CMAKE_INSTALL_CMAKEDIR)
187 set(CMAKE_INSTALL_CMAKEDIR "<%text>${CMAKE_INSTALL_LIBDIR}</%text>/cmake/gRPC")
188 endif()
189
Tamas Berghammerd798a7d2016-06-21 13:58:18 +0100190 % for lib in libs:
Tamas Berghammerdf6a44c2016-06-22 13:38:55 +0100191 % if lib.build in ["all", "protoc", "tool"]:
Tamas Berghammerd798a7d2016-06-21 13:58:18 +0100192 ${cc_library(lib)}
Konstantin Podsvirovd2b88302016-07-27 23:43:47 +0300193 ${cc_install(lib)}
Tamas Berghammerd798a7d2016-06-21 13:58:18 +0100194 % endif
195 % endfor
196
197 % for tgt in targets:
Tamas Berghammerdf6a44c2016-06-22 13:38:55 +0100198 % if tgt.build in ["all", "protoc", "tool"]:
Tamas Berghammerd798a7d2016-06-21 13:58:18 +0100199 ${cc_binary(tgt)}
Konstantin Podsvirovd2b88302016-07-27 23:43:47 +0300200 ${cc_install(tgt)}
Tamas Berghammerd798a7d2016-06-21 13:58:18 +0100201 % endif
202 % endfor
203
204 <%def name="cc_library(lib)">
205 add_library(${lib.name}
206 % for src in lib.src:
207 ${src}
208 % endfor
209 )
210
211 target_include_directories(${lib.name}
212 PRIVATE <%text>${CMAKE_CURRENT_SOURCE_DIR}</%text>
213 PRIVATE <%text>${CMAKE_CURRENT_SOURCE_DIR}</%text>/include
214 PRIVATE <%text>${BORINGSSL_ROOT_DIR}</%text>/include
215 PRIVATE <%text>${PROTOBUF_ROOT_DIR}</%text>/src
Konstantin Podsvirov5baf7252016-07-27 16:25:37 +0300216 PRIVATE <%text>${ZLIB_INCLUDE_DIR}</%text>
Tamas Berghammerd798a7d2016-06-21 13:58:18 +0100217 PRIVATE <%text>${CMAKE_CURRENT_BINARY_DIR}</%text>/third_party/zlib
218 )
219
220 % if len(get_deps(lib)) > 0:
221 target_link_libraries(${lib.name}
222 % for dep in get_deps(lib):
223 ${dep}
224 % endfor
225 )
226 % endif
Konstantin Podsvirovd2b88302016-07-27 23:43:47 +0300227
228 % if len(lib.get('public_headers', [])) > 0:
229 foreach(_hdr
230 % for hdr in lib.get('public_headers', []):
231 ${hdr}
232 % endfor
233 )
234 string(REPLACE "include/" "" _path <%text>${_hdr}</%text>)
235 get_filename_component(_path <%text>${_path}</%text> PATH)
236 install(FILES <%text>${_hdr}</%text>
237 DESTINATION "<%text>${CMAKE_INSTALL_INCLUDEDIR}/${_path}</%text>"
238 )
239 endforeach()
240 % endif
Tamas Berghammerd798a7d2016-06-21 13:58:18 +0100241 </%def>
242
243 <%def name="cc_binary(tgt)">
244 add_executable(${tgt.name}
245 % for src in tgt.src:
246 ${src}
247 % endfor
248 )
249
250 target_include_directories(${tgt.name}
251 PRIVATE <%text>${CMAKE_CURRENT_SOURCE_DIR}</%text>
252 PRIVATE <%text>${CMAKE_CURRENT_SOURCE_DIR}</%text>/include
253 PRIVATE <%text>${BORINGSSL_ROOT_DIR}</%text>/include
254 PRIVATE <%text>${PROTOBUF_ROOT_DIR}</%text>/src
255 PRIVATE <%text>${ZLIB_ROOT_DIR}</%text>
256 PRIVATE <%text>${CMAKE_CURRENT_BINARY_DIR}</%text>/third_party/zlib
257 )
258
259 % if len(get_deps(tgt)) > 0:
260 target_link_libraries(${tgt.name}
261 % for dep in get_deps(tgt):
262 ${dep}
263 % endfor
264 )
265 % endif
266 </%def>
267
Konstantin Podsvirovd2b88302016-07-27 23:43:47 +0300268 <%def name="cc_install(tgt)">
Arkadiy Shapkin40beb372016-08-17 14:42:36 +0300269 if (gRPC_INSTALL)
270 install(TARGETS ${tgt.name} EXPORT gRPCTargets
271 RUNTIME DESTINATION <%text>${CMAKE_INSTALL_BINDIR}</%text>
272 LIBRARY DESTINATION <%text>${CMAKE_INSTALL_LIBDIR}</%text>
273 ARCHIVE DESTINATION <%text>${CMAKE_INSTALL_LIBDIR}</%text>
274 )
275 endif()
Konstantin Podsvirovd2b88302016-07-27 23:43:47 +0300276 </%def>
277
Arkadiy Shapkin40beb372016-08-17 14:42:36 +0300278 if (gRPC_INSTALL)
279 install(EXPORT gRPCTargets
280 DESTINATION <%text>${CMAKE_INSTALL_CMAKEDIR}</%text>
281 NAMESPACE gRPC::
282 )
283 endif()
Konstantin Podsvirovd2b88302016-07-27 23:43:47 +0300284
285 foreach(_config gRPCConfig gRPCConfigVersion)
286 configure_file(tools/cmake/<%text>${_config}</%text>.cmake.in
287 <%text>${_config}</%text>.cmake @ONLY)
288 install(FILES <%text>${CMAKE_CURRENT_BINARY_DIR}/${_config}</%text>.cmake
289 DESTINATION <%text>${CMAKE_INSTALL_CMAKEDIR}</%text>
290 )
291 endforeach()