blob: 358285b6200a4e0e7ac60e0183c69787e8a2369b (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
|
# ~~~
# Copyright 2019 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ~~~
#[=======================================================================[.rst:
FindgRPC
--------
Locate and configure the gRPC library.
The following variables can be set and are optional:
``gRPC_DEBUG``
Show debug messages.
``gRPC_USE_STATIC_LIBS``
Set to ON to force the use of the static libraries.
Default is OFF.
Defines the following variables:
``gRPC_FOUND``
Found the gRPC library
``gRPC_VERSION``
Version of package found.
The following :prop_tgt:`IMPORTED` targets are also defined:
``gRPC::grpc++``
The gRPC C++ library.
``gRPC::grpc``
The gRPC C core library.
``gRPC::cpp_plugin``
The C++ plugin for the Protobuf protoc compiler.
The following cache variables are also available to set or use:
Example:
.. code-block:: cmake
find_package(gRPC REQUIRED)
add_executable(bar bar.cc)
target_link_libraries(bar PRIVATE gRPC::grpc++)
#]=======================================================================]
if (gRPC_DEBUG)
message(STATUS "[ ${CMAKE_CURRENT_LIST_FILE}:${CMAKE_CURRENT_LIST_LINE} ] "
"gRPC_USE_STATIC_LIBS = ${gRPC_USE_STATIC_LIBS}"
" gRPC_FOUND = ${gRPC_FOUND}")
endif ()
# gRPC always requires Thread support.
find_package(Threads REQUIRED)
# Load the module to find protobuf with proper targets. Do not use
# `find_package()` because we (have to) install this module in non-standard
# locations.
include(${CMAKE_CURRENT_LIST_DIR}/FindProtobufTargets.cmake)
# The gRPC::grpc_cpp_plugin target is sometimes defined, but without a
# IMPORTED_LOCATION
function (_grpc_fix_grpc_cpp_plugin_target)
# The target may already exist, do not create it again if it does.
if (NOT TARGET gRPC::grpc_cpp_plugin)
add_executable(gRPC::grpc_cpp_plugin IMPORTED)
endif ()
get_target_property(_gRPC_CPP_PLUGIN_EXECUTABLE gRPC::grpc_cpp_plugin
IMPORTED_LOCATION)
if (gRPC_DEBUG)
message(
STATUS "[ ${CMAKE_CURRENT_LIST_FILE}:${CMAKE_CURRENT_LIST_LINE} ] "
"LOCATION=${_gRPC_CPP_PLUGIN_EXECUTABLE}")
endif ()
# Even if the target exists, gRPC CMake support files do not define the
# executable for the imported target (at least they do not in v1.19.1), so
# we need to define it ourselves.
if (NOT _gRPC_CPP_PLUGIN_EXECUTABLE)
find_program(_gRPC_CPP_PLUGIN_EXECUTABLE grpc_cpp_plugin
DOC "The gRPC C++ plugin for protoc")
mark_as_advanced(_gRPC_CPP_PLUGIN_EXECUTABLE)
if (_gRPC_CPP_PLUGIN_EXECUTABLE)
set_property(
TARGET gRPC::grpc_cpp_plugin
PROPERTY IMPORTED_LOCATION ${_gRPC_CPP_PLUGIN_EXECUTABLE})
else ()
set(gRPC_FOUND "grpc_cpp_plugin-NOTFOUND")
endif ()
endif ()
endfunction ()
# The gRPC::* targets sometimes lack the right definitions to compile cleanly on
# WIN32
function (_grpc_fix_grpc_target_definitions)
# Including gRPC headers without this definition results in a build error.
if (WIN32)
set_property(TARGET gRPC::grpc APPEND
PROPERTY INTERFACE_COMPILE_DEFINITIONS _WIN32_WINNT=0x600)
set_property(TARGET gRPC::grpc++ APPEND
PROPERTY INTERFACE_COMPILE_DEFINITIONS _WIN32_WINNT=0x600)
endif ()
endfunction ()
# First try to use the `gRPCConfig.cmake` or `grpc-config.cmake` file if it was
# installed. This is common on systems (or package managers) where gRPC was
# compiled and installed with `CMake`.
find_package(gRPC NO_MODULE QUIET)
if (gRPC_DEBUG)
message(STATUS "[ ${CMAKE_CURRENT_LIST_FILE}:${CMAKE_CURRENT_LIST_LINE} ] "
"NO_MODULE result gRPC_FOUND = ${gRPC_FOUND}")
endif ()
if (gRPC_FOUND)
_grpc_fix_grpc_cpp_plugin_target()
_grpc_fix_grpc_target_definitions()
return()
endif ()
include(SelectLibraryConfigurations)
# Internal function: search for normal library as well as a debug one if the
# debug one is specified also include debug/optimized keywords in *_LIBRARIES
# variable
function (_gRPC_find_library name filename)
if (${name}_LIBRARY)
# Use result recorded by a previous call.
return()
else ()
find_library(${name}_LIBRARY_RELEASE NAMES ${filename})
mark_as_advanced(${name}_LIBRARY_RELEASE)
find_library(${name}_LIBRARY_DEBUG NAMES ${filename}d ${filename})
mark_as_advanced(${name}_LIBRARY_DEBUG)
select_library_configurations(${name})
if (gRPC_DEBUG)
message(
STATUS
"[ ${CMAKE_CURRENT_LIST_FILE}:${CMAKE_CURRENT_LIST_LINE} ] "
"${name} ${filename} RELEASE=${${name}_LIBRARY}"
" DEBUG=${${name}_LIBRARY_DEBUG} DEFAULT=${${name}_LIBRARY}"
)
endif ()
set(${name}_LIBRARY
"${${name}_LIBRARY}"
PARENT_SCOPE)
endif ()
endfunction ()
#
# Main
#
# Support preference of static libs by adjusting CMAKE_FIND_LIBRARY_SUFFIXES
if (_gRPC_USE_STATIC_LIBS)
set(_gRPC_ORIG_CMAKE_FIND_LIBRARY_SUFFIXES ${CMAKE_FIND_LIBRARY_SUFFIXES})
if (WIN32)
set(CMAKE_FIND_LIBRARY_SUFFIXES .lib .a ${CMAKE_FIND_LIBRARY_SUFFIXES})
else ()
set(CMAKE_FIND_LIBRARY_SUFFIXES .a)
endif ()
endif ()
_grpc_find_library(_gRPC_grpc grpc)
_grpc_find_library(_gRPC_grpc++ grpc++)
if (NOT _gRPC_INCLUDE_DIR)
find_path(_gRPC_INCLUDE_DIR grpcpp/grpcpp.h)
mark_as_advanced(_gRPC_INCLUDE_DIR)
endif ()
if (gRPC_DEBUG)
message(STATUS "[ ${CMAKE_CURRENT_LIST_FILE}:${CMAKE_CURRENT_LIST_LINE} ] "
" _gRPC_grpc_LIBRARY = ${_gRPC_grpc_LIBRARY}")
message(STATUS "[ ${CMAKE_CURRENT_LIST_FILE}:${CMAKE_CURRENT_LIST_LINE} ] "
" _gRPC_grpc++_LIBRARY = ${_gRPC_grpc++_LIBRARY}")
message(STATUS "[ ${CMAKE_CURRENT_LIST_FILE}:${CMAKE_CURRENT_LIST_LINE} ] "
" _gRPC_INCLUDE_DIR = ${_gRPC_INCLUDE_DIR}")
endif ()
if (_gRPC_grpc_LIBRARY)
if (NOT TARGET gRPC::grpc)
add_library(gRPC::grpc IMPORTED UNKNOWN)
set_target_properties(
gRPC::grpc PROPERTIES INTERFACE_INCLUDE_DIRECTORIES
"${_gRPC_INCLUDE_DIR}")
if (EXISTS "${_gRPC_grpc_LIBRARY}")
set_target_properties(gRPC::grpc PROPERTIES IMPORTED_LOCATION
"${_gRPC_grpc_LIBRARY}")
endif ()
if (EXISTS "${_gRPC_grpc_LIBRARY_RELEASE}")
set_property(TARGET gRPC::grpc APPEND
PROPERTY IMPORTED_CONFIGURATIONS RELEASE)
set_target_properties(
gRPC::grpc PROPERTIES IMPORTED_LOCATION_RELEASE
"${_gRPC_grpc_LIBRARY_RELEASE}")
endif ()
if (EXISTS "${_gRPC_grpc_LIBRARY_DEBUG}")
set_property(TARGET gRPC::grpc APPEND
PROPERTY IMPORTED_CONFIGURATIONS DEBUG)
set_target_properties(
gRPC::grpc PROPERTIES IMPORTED_LOCATION_DEBUG
"${_gRPC_grpc_LIBRARY_DEBUG}")
endif ()
set_property(
TARGET gRPC::grpc APPEND
PROPERTY INTERFACE_LINK_LIBRARIES protobuf::libprotobuf
Threads::Threads)
endif ()
endif ()
if (_gRPC_grpc++_LIBRARY)
if (NOT TARGET gRPC::grpc++)
add_library(gRPC::grpc++ IMPORTED UNKNOWN)
set_target_properties(
gRPC::grpc++ PROPERTIES INTERFACE_INCLUDE_DIRECTORIES
"${_gRPC++_INCLUDE_DIR}")
if (EXISTS "${_gRPC_grpc++_LIBRARY}")
set_target_properties(
gRPC::grpc++ PROPERTIES IMPORTED_LOCATION
"${_gRPC_grpc++_LIBRARY}")
endif ()
if (EXISTS "${_gRPC_grpc++_LIBRARY_RELEASE}")
set_property(TARGET gRPC::grpc++ APPEND
PROPERTY IMPORTED_CONFIGURATIONS RELEASE)
set_target_properties(
gRPC::grpc++ PROPERTIES IMPORTED_LOCATION_RELEASE
"${_gRPC_grpc++_LIBRARY_RELEASE}")
endif ()
if (EXISTS "${_gRPC_grpc++_LIBRARY_DEBUG}")
set_property(TARGET gRPC::grpc++ APPEND
PROPERTY IMPORTED_CONFIGURATIONS DEBUG)
set_target_properties(
gRPC::grpc++ PROPERTIES IMPORTED_LOCATION_DEBUG
"${_gRPC_grpc++_LIBRARY_DEBUG}")
endif ()
set_property(
TARGET gRPC::grpc++ APPEND
PROPERTY INTERFACE_LINK_LIBRARIES gRPC::grpc protobuf::libprotobuf
Threads::Threads)
if (CMAKE_VERSION VERSION_GREATER 3.8)
# gRPC++ requires C++11, but only CMake-3.8 introduced a target
# compiler feature to meet that requirement.
set_property(TARGET gRPC::grpc++ APPEND
PROPERTY INTERFACE_COMPILE_FEATURES cxx_std_11)
elseif ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU")
# CMake 3.5 is still alive and kicking in some older distros, use
# the compiler-specific versions in these cases.
set_property(TARGET gRPC::grpc++ APPEND
PROPERTY INTERFACE_COMPILE_OPTIONS "-std=c++11")
elseif ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang")
set_property(TARGET gRPC::grpc++ APPEND
PROPERTY INTERFACE_COMPILE_OPTIONS "-std=c++11")
else ()
message(
WARNING
"gRPC::grpc++ requires C++11, but this module"
" (${CMAKE_CURRENT_LIST_FILE})"
" cannot enable it for the library target in your CMake and"
" compiler versions. You need to enable C++11 in the"
" CMakeLists.txt for your project. Consider filing a bug"
" so we can fix this problem.")
endif ()
endif ()
endif ()
# Restore original find library prefixes
if (_gRPC_USE_STATIC_LIBS)
set(CMAKE_FIND_LIBRARY_PREFIXES "${_gRPC_ORIG_FIND_LIBRARY_PREFIXES}")
endif ()
file(
WRITE "${CMAKE_BINARY_DIR}/get_gRPC_version.cc"
[====[
#include <grpcpp/grpcpp.h>
#include <iostream>
int main() {
std::cout << grpc::Version(); // no newline to simplify CMake module
return 0;
}
]====])
try_run(
_gRPC_GET_VERSION_STATUS
_gRPC_GET_VERSION_COMPILE_STATUS
"${CMAKE_BINARY_DIR}"
"${CMAKE_BINARY_DIR}/get_gRPC_version.cc"
LINK_LIBRARIES
gRPC::grpc++
gRPC::grpc
COMPILE_OUTPUT_VARIABLE _gRPC_GET_VERSION_COMPILE_OUTPUT
RUN_OUTPUT_VARIABLE gRPC_VERSION)
file(REMOVE "${CMAKE_BINARY_DIR}/get_gRPC_version.cc")
_grpc_fix_grpc_cpp_plugin_target()
if (gRPC_DEBUG)
foreach (
_var
_gRPC_CPP_PLUGIN_EXECUTABLE
_gRPC_VERSION_RAW
_gRPC_GET_VERSION_STATUS
_gRPC_GET_VERSION_COMPILE_STATUS
_gRPC_GET_VERSION_COMPILE_OUTPUT
_gRPC_grpc_LIBRARY
_gRPC_grpc++_LIBRARY
_gRPC_INCLUDE_DIR)
message(
STATUS "[ ${CMAKE_CURRENT_LIST_FILE}:${CMAKE_CURRENT_LIST_LINE} ] "
"${_var} = ${${_var}}")
endforeach ()
unset(_var)
endif ()
include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(gRPC REQUIRED_VARS _gRPC_grpc_LIBRARY
_gRPC_INCLUDE_DIR VERSION_VAR gRPC_VERSION)
|