]>
Commit | Line | Data |
---|---|---|
11fdf7f2 TL |
1 | # Distributed under the OSI-approved BSD 3-Clause License. See accompanying |
2 | # file Copyright.txt or https://cmake.org/licensing for details. | |
3 | ||
4 | #.rst: | |
5 | # FindProtobuf | |
6 | # ------------ | |
7 | # | |
8 | # Locate and configure the Google Protocol Buffers library. | |
9 | # | |
10 | # The following variables can be set and are optional: | |
11 | # | |
12 | # ``Protobuf_SRC_ROOT_FOLDER`` | |
13 | # When compiling with MSVC, if this cache variable is set | |
14 | # the protobuf-default VS project build locations | |
15 | # (vsprojects/Debug and vsprojects/Release | |
16 | # or vsprojects/x64/Debug and vsprojects/x64/Release) | |
17 | # will be searched for libraries and binaries. | |
18 | # ``Protobuf_IMPORT_DIRS`` | |
19 | # List of additional directories to be searched for | |
20 | # imported .proto files. | |
21 | # ``Protobuf_DEBUG`` | |
22 | # Show debug messages. | |
23 | # ``Protobuf_USE_STATIC_LIBS`` | |
24 | # Set to ON to force the use of the static libraries. | |
25 | # Default is OFF. | |
26 | # | |
27 | # Defines the following variables: | |
28 | # | |
29 | # ``Protobuf_FOUND`` | |
30 | # Found the Google Protocol Buffers library | |
31 | # (libprotobuf & header files) | |
32 | # ``Protobuf_VERSION`` | |
33 | # Version of package found. | |
34 | # ``Protobuf_INCLUDE_DIRS`` | |
35 | # Include directories for Google Protocol Buffers | |
36 | # ``Protobuf_LIBRARIES`` | |
37 | # The protobuf libraries | |
38 | # ``Protobuf_PROTOC_LIBRARIES`` | |
39 | # The protoc libraries | |
40 | # ``Protobuf_LITE_LIBRARIES`` | |
41 | # The protobuf-lite libraries | |
42 | # | |
43 | # The following :prop_tgt:`IMPORTED` targets are also defined: | |
44 | # | |
45 | # ``protobuf::libprotobuf`` | |
46 | # The protobuf library. | |
47 | # ``protobuf::libprotobuf-lite`` | |
48 | # The protobuf lite library. | |
49 | # ``protobuf::libprotoc`` | |
50 | # The protoc library. | |
51 | # ``protobuf::protoc`` | |
52 | # The protoc compiler. | |
53 | # | |
54 | # The following cache variables are also available to set or use: | |
55 | # | |
56 | # ``Protobuf_LIBRARY`` | |
57 | # The protobuf library | |
58 | # ``Protobuf_PROTOC_LIBRARY`` | |
59 | # The protoc library | |
60 | # ``Protobuf_INCLUDE_DIR`` | |
61 | # The include directory for protocol buffers | |
62 | # ``Protobuf_PROTOC_EXECUTABLE`` | |
63 | # The protoc compiler | |
64 | # ``Protobuf_LIBRARY_DEBUG`` | |
65 | # The protobuf library (debug) | |
66 | # ``Protobuf_PROTOC_LIBRARY_DEBUG`` | |
67 | # The protoc library (debug) | |
68 | # ``Protobuf_LITE_LIBRARY`` | |
69 | # The protobuf lite library | |
70 | # ``Protobuf_LITE_LIBRARY_DEBUG`` | |
71 | # The protobuf lite library (debug) | |
72 | # | |
73 | # Example: | |
74 | # | |
75 | # .. code-block:: cmake | |
76 | # | |
77 | # find_package(Protobuf REQUIRED) | |
78 | # include_directories(${Protobuf_INCLUDE_DIRS}) | |
79 | # include_directories(${CMAKE_CURRENT_BINARY_DIR}) | |
80 | # protobuf_generate_cpp(PROTO_SRCS PROTO_HDRS foo.proto) | |
81 | # protobuf_generate_cpp(PROTO_SRCS PROTO_HDRS EXPORT_MACRO DLL_EXPORT foo.proto) | |
82 | # protobuf_generate_cpp(PROTO_SRCS PROTO_HDRS DESCRIPTORS PROTO_DESCS foo.proto) | |
83 | # protobuf_generate_python(PROTO_PY foo.proto) | |
84 | # add_executable(bar bar.cc ${PROTO_SRCS} ${PROTO_HDRS}) | |
85 | # target_link_libraries(bar ${Protobuf_LIBRARIES}) | |
86 | # | |
87 | # .. note:: | |
88 | # The ``protobuf_generate_cpp`` and ``protobuf_generate_python`` | |
89 | # functions and :command:`add_executable` or :command:`add_library` | |
90 | # calls only work properly within the same directory. | |
91 | # | |
92 | # .. command:: protobuf_generate_cpp | |
93 | # | |
94 | # Add custom commands to process ``.proto`` files to C++:: | |
95 | # | |
96 | # protobuf_generate_cpp (<SRCS> <HDRS> | |
97 | # [DESCRIPTORS <DESC>] [EXPORT_MACRO <MACRO>] [<ARGN>...]) | |
98 | # | |
99 | # ``SRCS`` | |
100 | # Variable to define with autogenerated source files | |
101 | # ``HDRS`` | |
102 | # Variable to define with autogenerated header files | |
103 | # ``DESCRIPTORS`` | |
104 | # Variable to define with autogenerated descriptor files, if requested. | |
105 | # ``EXPORT_MACRO`` | |
106 | # is a macro which should expand to ``__declspec(dllexport)`` or | |
107 | # ``__declspec(dllimport)`` depending on what is being compiled. | |
108 | # ``ARGN`` | |
109 | # ``.proto`` files | |
110 | # | |
111 | # .. command:: protobuf_generate_python | |
112 | # | |
113 | # Add custom commands to process ``.proto`` files to Python:: | |
114 | # | |
115 | # protobuf_generate_python (<PY> [<ARGN>...]) | |
116 | # | |
117 | # ``PY`` | |
118 | # Variable to define with autogenerated Python files | |
119 | # ``ARGN`` | |
120 | # ``.proto`` filess | |
121 | ||
122 | function(PROTOBUF_GENERATE_CPP SRCS HDRS) | |
123 | cmake_parse_arguments(protobuf "" "EXPORT_MACRO;DESCRIPTORS" "" ${ARGN}) | |
124 | ||
125 | set(PROTO_FILES "${protobuf_UNPARSED_ARGUMENTS}") | |
126 | if(NOT PROTO_FILES) | |
127 | message(SEND_ERROR "Error: PROTOBUF_GENERATE_CPP() called without any proto files") | |
128 | return() | |
129 | endif() | |
130 | ||
131 | if(protobuf_EXPORT_MACRO) | |
132 | set(DLL_EXPORT_DECL "dllexport_decl=${protobuf_EXPORT_MACRO}:") | |
133 | endif() | |
134 | ||
135 | if(PROTOBUF_GENERATE_CPP_APPEND_PATH) | |
136 | # Create an include path for each file specified | |
137 | foreach(FIL ${PROTO_FILES}) | |
138 | get_filename_component(ABS_FIL ${FIL} ABSOLUTE) | |
139 | get_filename_component(ABS_PATH ${ABS_FIL} PATH) | |
140 | list(FIND _protobuf_include_path ${ABS_PATH} _contains_already) | |
141 | if(${_contains_already} EQUAL -1) | |
142 | list(APPEND _protobuf_include_path -I ${ABS_PATH}) | |
143 | endif() | |
144 | endforeach() | |
145 | else() | |
146 | set(_protobuf_include_path -I ${CMAKE_CURRENT_SOURCE_DIR}) | |
147 | endif() | |
148 | ||
149 | if(DEFINED PROTOBUF_IMPORT_DIRS AND NOT DEFINED Protobuf_IMPORT_DIRS) | |
150 | set(Protobuf_IMPORT_DIRS "${PROTOBUF_IMPORT_DIRS}") | |
151 | endif() | |
152 | ||
153 | if(DEFINED Protobuf_IMPORT_DIRS) | |
154 | foreach(DIR ${Protobuf_IMPORT_DIRS}) | |
155 | get_filename_component(ABS_PATH ${DIR} ABSOLUTE) | |
156 | list(FIND _protobuf_include_path ${ABS_PATH} _contains_already) | |
157 | if(${_contains_already} EQUAL -1) | |
158 | list(APPEND _protobuf_include_path -I ${ABS_PATH}) | |
159 | endif() | |
160 | endforeach() | |
161 | endif() | |
162 | ||
163 | set(${SRCS}) | |
164 | set(${HDRS}) | |
165 | if (protobuf_DESCRIPTORS) | |
166 | set(${protobuf_DESCRIPTORS}) | |
167 | endif() | |
168 | ||
169 | foreach(FIL ${PROTO_FILES}) | |
170 | get_filename_component(ABS_FIL ${FIL} ABSOLUTE) | |
171 | get_filename_component(FIL_WE ${FIL} NAME_WE) | |
172 | if(NOT PROTOBUF_GENERATE_CPP_APPEND_PATH) | |
173 | get_filename_component(FIL_DIR ${FIL} DIRECTORY) | |
174 | if(FIL_DIR) | |
175 | set(FIL_WE "${FIL_DIR}/${FIL_WE}") | |
176 | endif() | |
177 | endif() | |
178 | ||
179 | set(_protobuf_protoc_src "${CMAKE_CURRENT_BINARY_DIR}/${FIL_WE}.pb.cc") | |
180 | set(_protobuf_protoc_hdr "${CMAKE_CURRENT_BINARY_DIR}/${FIL_WE}.pb.h") | |
181 | list(APPEND ${SRCS} "${_protobuf_protoc_src}") | |
182 | list(APPEND ${HDRS} "${_protobuf_protoc_hdr}") | |
183 | ||
184 | if(protobuf_DESCRIPTORS) | |
185 | set(_protobuf_protoc_desc "${CMAKE_CURRENT_BINARY_DIR}/${FIL_WE}.desc") | |
186 | set(_protobuf_protoc_flags "--descriptor_set_out=${_protobuf_protoc_desc}") | |
187 | list(APPEND ${protobuf_DESCRIPTORS} "${_protobuf_protoc_desc}") | |
188 | else() | |
189 | set(_protobuf_protoc_desc "") | |
190 | set(_protobuf_protoc_flags "") | |
191 | endif() | |
192 | ||
193 | add_custom_command( | |
194 | OUTPUT "${_protobuf_protoc_src}" | |
195 | "${_protobuf_protoc_hdr}" | |
196 | ${_protobuf_protoc_desc} | |
197 | COMMAND protobuf::protoc | |
198 | "--cpp_out=${DLL_EXPORT_DECL}${CMAKE_CURRENT_BINARY_DIR}" | |
199 | ${_protobuf_protoc_flags} | |
200 | ${_protobuf_include_path} ${ABS_FIL} | |
201 | DEPENDS ${ABS_FIL} protobuf::protoc | |
202 | COMMENT "Running C++ protocol buffer compiler on ${FIL}" | |
203 | VERBATIM ) | |
204 | endforeach() | |
205 | ||
206 | set(${SRCS} "${${SRCS}}" PARENT_SCOPE) | |
207 | set(${HDRS} "${${HDRS}}" PARENT_SCOPE) | |
208 | if(protobuf_DESCRIPTORS) | |
209 | set(${protobuf_DESCRIPTORS} "${${protobuf_DESCRIPTORS}}" PARENT_SCOPE) | |
210 | endif() | |
211 | endfunction() | |
212 | ||
213 | function(PROTOBUF_GENERATE_PYTHON SRCS) | |
214 | if(NOT ARGN) | |
215 | message(SEND_ERROR "Error: PROTOBUF_GENERATE_PYTHON() called without any proto files") | |
216 | return() | |
217 | endif() | |
218 | ||
219 | if(PROTOBUF_GENERATE_CPP_APPEND_PATH) | |
220 | # Create an include path for each file specified | |
221 | foreach(FIL ${ARGN}) | |
222 | get_filename_component(ABS_FIL ${FIL} ABSOLUTE) | |
223 | get_filename_component(ABS_PATH ${ABS_FIL} PATH) | |
224 | list(FIND _protobuf_include_path ${ABS_PATH} _contains_already) | |
225 | if(${_contains_already} EQUAL -1) | |
226 | list(APPEND _protobuf_include_path -I ${ABS_PATH}) | |
227 | endif() | |
228 | endforeach() | |
229 | else() | |
230 | set(_protobuf_include_path -I ${CMAKE_CURRENT_SOURCE_DIR}) | |
231 | endif() | |
232 | ||
233 | if(DEFINED PROTOBUF_IMPORT_DIRS AND NOT DEFINED Protobuf_IMPORT_DIRS) | |
234 | set(Protobuf_IMPORT_DIRS "${PROTOBUF_IMPORT_DIRS}") | |
235 | endif() | |
236 | ||
237 | if(DEFINED Protobuf_IMPORT_DIRS) | |
238 | foreach(DIR ${Protobuf_IMPORT_DIRS}) | |
239 | get_filename_component(ABS_PATH ${DIR} ABSOLUTE) | |
240 | list(FIND _protobuf_include_path ${ABS_PATH} _contains_already) | |
241 | if(${_contains_already} EQUAL -1) | |
242 | list(APPEND _protobuf_include_path -I ${ABS_PATH}) | |
243 | endif() | |
244 | endforeach() | |
245 | endif() | |
246 | ||
247 | set(${SRCS}) | |
248 | foreach(FIL ${ARGN}) | |
249 | get_filename_component(ABS_FIL ${FIL} ABSOLUTE) | |
250 | get_filename_component(FIL_WE ${FIL} NAME_WE) | |
251 | if(NOT PROTOBUF_GENERATE_CPP_APPEND_PATH) | |
252 | get_filename_component(FIL_DIR ${FIL} DIRECTORY) | |
253 | if(FIL_DIR) | |
254 | set(FIL_WE "${FIL_DIR}/${FIL_WE}") | |
255 | endif() | |
256 | endif() | |
257 | ||
258 | list(APPEND ${SRCS} "${CMAKE_CURRENT_BINARY_DIR}/${FIL_WE}_pb2.py") | |
259 | add_custom_command( | |
260 | OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/${FIL_WE}_pb2.py" | |
261 | COMMAND protobuf::protoc --python_out ${CMAKE_CURRENT_BINARY_DIR} ${_protobuf_include_path} ${ABS_FIL} | |
262 | DEPENDS ${ABS_FIL} protobuf::protoc | |
263 | COMMENT "Running Python protocol buffer compiler on ${FIL}" | |
264 | VERBATIM ) | |
265 | endforeach() | |
266 | ||
267 | set(${SRCS} ${${SRCS}} PARENT_SCOPE) | |
268 | endfunction() | |
269 | ||
270 | ||
271 | if(Protobuf_DEBUG) | |
272 | # Output some of their choices | |
273 | message(STATUS "[ ${CMAKE_CURRENT_LIST_FILE}:${CMAKE_CURRENT_LIST_LINE} ] " | |
274 | "Protobuf_USE_STATIC_LIBS = ${Protobuf_USE_STATIC_LIBS}") | |
275 | endif() | |
276 | ||
277 | ||
278 | # Backwards compatibility | |
279 | # Define camel case versions of input variables | |
280 | foreach(UPPER | |
281 | PROTOBUF_SRC_ROOT_FOLDER | |
282 | PROTOBUF_IMPORT_DIRS | |
283 | PROTOBUF_DEBUG | |
284 | PROTOBUF_LIBRARY | |
285 | PROTOBUF_PROTOC_LIBRARY | |
286 | PROTOBUF_INCLUDE_DIR | |
287 | PROTOBUF_PROTOC_EXECUTABLE | |
288 | PROTOBUF_LIBRARY_DEBUG | |
289 | PROTOBUF_PROTOC_LIBRARY_DEBUG | |
290 | PROTOBUF_LITE_LIBRARY | |
291 | PROTOBUF_LITE_LIBRARY_DEBUG | |
292 | ) | |
293 | if (DEFINED ${UPPER}) | |
294 | string(REPLACE "PROTOBUF_" "Protobuf_" Camel ${UPPER}) | |
295 | if (NOT DEFINED ${Camel}) | |
296 | set(${Camel} ${${UPPER}}) | |
297 | endif() | |
298 | endif() | |
299 | endforeach() | |
300 | ||
301 | if(CMAKE_SIZEOF_VOID_P EQUAL 8) | |
302 | set(_PROTOBUF_ARCH_DIR x64/) | |
303 | endif() | |
304 | ||
305 | ||
306 | # Support preference of static libs by adjusting CMAKE_FIND_LIBRARY_SUFFIXES | |
307 | if( Protobuf_USE_STATIC_LIBS ) | |
308 | set( _protobuf_ORIG_CMAKE_FIND_LIBRARY_SUFFIXES ${CMAKE_FIND_LIBRARY_SUFFIXES}) | |
309 | if(WIN32) | |
310 | set(CMAKE_FIND_LIBRARY_SUFFIXES .lib .a ${CMAKE_FIND_LIBRARY_SUFFIXES}) | |
311 | else() | |
312 | set(CMAKE_FIND_LIBRARY_SUFFIXES .a ) | |
313 | endif() | |
314 | endif() | |
315 | ||
316 | include(SelectLibraryConfigurations) | |
317 | ||
318 | # Internal function: search for normal library as well as a debug one | |
319 | # if the debug one is specified also include debug/optimized keywords | |
320 | # in *_LIBRARIES variable | |
321 | function(_protobuf_find_libraries name filename) | |
322 | if(${name}_LIBRARIES) | |
323 | # Use result recorded by a previous call. | |
324 | return() | |
325 | elseif(${name}_LIBRARY) | |
326 | # Honor cache entry used by CMake 3.5 and lower. | |
327 | set(${name}_LIBRARIES "${${name}_LIBRARY}" PARENT_SCOPE) | |
328 | else() | |
329 | find_library(${name}_LIBRARY_RELEASE | |
330 | NAMES ${filename} | |
331 | PATHS ${Protobuf_SRC_ROOT_FOLDER}/vsprojects/${_PROTOBUF_ARCH_DIR}Release) | |
332 | mark_as_advanced(${name}_LIBRARY_RELEASE) | |
333 | ||
334 | find_library(${name}_LIBRARY_DEBUG | |
335 | NAMES ${filename}d ${filename} | |
336 | PATHS ${Protobuf_SRC_ROOT_FOLDER}/vsprojects/${_PROTOBUF_ARCH_DIR}Debug) | |
337 | mark_as_advanced(${name}_LIBRARY_DEBUG) | |
338 | ||
339 | select_library_configurations(${name}) | |
340 | set(${name}_LIBRARY "${${name}_LIBRARY}" PARENT_SCOPE) | |
341 | set(${name}_LIBRARIES "${${name}_LIBRARIES}" PARENT_SCOPE) | |
342 | endif() | |
343 | endfunction() | |
344 | ||
345 | # Internal function: find threads library | |
346 | function(_protobuf_find_threads) | |
347 | set(CMAKE_THREAD_PREFER_PTHREAD TRUE) | |
348 | find_package(Threads) | |
349 | if(Threads_FOUND) | |
350 | list(APPEND Protobuf_LIBRARIES ${CMAKE_THREAD_LIBS_INIT}) | |
351 | set(Protobuf_LIBRARIES "${Protobuf_LIBRARIES}" PARENT_SCOPE) | |
352 | endif() | |
353 | endfunction() | |
354 | ||
355 | # | |
356 | # Main. | |
357 | # | |
358 | ||
359 | # By default have PROTOBUF_GENERATE_CPP macro pass -I to protoc | |
360 | # for each directory where a proto file is referenced. | |
361 | if(NOT DEFINED PROTOBUF_GENERATE_CPP_APPEND_PATH) | |
362 | set(PROTOBUF_GENERATE_CPP_APPEND_PATH TRUE) | |
363 | endif() | |
364 | ||
365 | ||
366 | # Google's provided vcproj files generate libraries with a "lib" | |
367 | # prefix on Windows | |
368 | if(MSVC) | |
369 | set(Protobuf_ORIG_FIND_LIBRARY_PREFIXES "${CMAKE_FIND_LIBRARY_PREFIXES}") | |
370 | set(CMAKE_FIND_LIBRARY_PREFIXES "lib" "") | |
371 | ||
372 | find_path(Protobuf_SRC_ROOT_FOLDER protobuf.pc.in) | |
373 | endif() | |
374 | ||
375 | # The Protobuf library | |
376 | _protobuf_find_libraries(Protobuf protobuf) | |
377 | #DOC "The Google Protocol Buffers RELEASE Library" | |
378 | ||
379 | _protobuf_find_libraries(Protobuf_LITE protobuf-lite) | |
380 | ||
381 | # The Protobuf Protoc Library | |
382 | _protobuf_find_libraries(Protobuf_PROTOC protoc) | |
383 | ||
384 | # Restore original find library prefixes | |
385 | if(MSVC) | |
386 | set(CMAKE_FIND_LIBRARY_PREFIXES "${Protobuf_ORIG_FIND_LIBRARY_PREFIXES}") | |
387 | endif() | |
388 | ||
389 | if(UNIX) | |
390 | _protobuf_find_threads() | |
391 | endif() | |
392 | ||
393 | # Find the include directory | |
394 | find_path(Protobuf_INCLUDE_DIR | |
395 | google/protobuf/service.h | |
396 | PATHS ${Protobuf_SRC_ROOT_FOLDER}/src | |
397 | ) | |
398 | mark_as_advanced(Protobuf_INCLUDE_DIR) | |
399 | ||
400 | # Find the protoc Executable | |
401 | find_program(Protobuf_PROTOC_EXECUTABLE | |
402 | NAMES protoc | |
403 | DOC "The Google Protocol Buffers Compiler" | |
404 | PATHS | |
405 | ${Protobuf_SRC_ROOT_FOLDER}/vsprojects/${_PROTOBUF_ARCH_DIR}Release | |
406 | ${Protobuf_SRC_ROOT_FOLDER}/vsprojects/${_PROTOBUF_ARCH_DIR}Debug | |
407 | ) | |
408 | mark_as_advanced(Protobuf_PROTOC_EXECUTABLE) | |
409 | ||
410 | if(Protobuf_DEBUG) | |
411 | message(STATUS "[ ${CMAKE_CURRENT_LIST_FILE}:${CMAKE_CURRENT_LIST_LINE} ] " | |
412 | "requested version of Google Protobuf is ${Protobuf_FIND_VERSION}") | |
413 | endif() | |
414 | ||
415 | if(Protobuf_INCLUDE_DIR) | |
416 | set(_PROTOBUF_COMMON_HEADER ${Protobuf_INCLUDE_DIR}/google/protobuf/stubs/common.h) | |
417 | ||
418 | if(Protobuf_DEBUG) | |
419 | message(STATUS "[ ${CMAKE_CURRENT_LIST_FILE}:${CMAKE_CURRENT_LIST_LINE} ] " | |
420 | "location of common.h: ${_PROTOBUF_COMMON_HEADER}") | |
421 | endif() | |
422 | ||
423 | set(Protobuf_VERSION "") | |
424 | set(Protobuf_LIB_VERSION "") | |
425 | file(STRINGS ${_PROTOBUF_COMMON_HEADER} _PROTOBUF_COMMON_H_CONTENTS REGEX "#define[ \t]+GOOGLE_PROTOBUF_VERSION[ \t]+") | |
426 | if(_PROTOBUF_COMMON_H_CONTENTS MATCHES "#define[ \t]+GOOGLE_PROTOBUF_VERSION[ \t]+([0-9]+)") | |
427 | set(Protobuf_LIB_VERSION "${CMAKE_MATCH_1}") | |
428 | endif() | |
429 | unset(_PROTOBUF_COMMON_H_CONTENTS) | |
430 | ||
431 | math(EXPR _PROTOBUF_MAJOR_VERSION "${Protobuf_LIB_VERSION} / 1000000") | |
432 | math(EXPR _PROTOBUF_MINOR_VERSION "${Protobuf_LIB_VERSION} / 1000 % 1000") | |
433 | math(EXPR _PROTOBUF_SUBMINOR_VERSION "${Protobuf_LIB_VERSION} % 1000") | |
434 | set(Protobuf_VERSION "${_PROTOBUF_MAJOR_VERSION}.${_PROTOBUF_MINOR_VERSION}.${_PROTOBUF_SUBMINOR_VERSION}") | |
435 | ||
436 | if(Protobuf_DEBUG) | |
437 | message(STATUS "[ ${CMAKE_CURRENT_LIST_FILE}:${CMAKE_CURRENT_LIST_LINE} ] " | |
438 | "${_PROTOBUF_COMMON_HEADER} reveals protobuf ${Protobuf_VERSION}") | |
439 | endif() | |
440 | ||
441 | # Check Protobuf compiler version to be aligned with libraries version | |
442 | execute_process(COMMAND ${Protobuf_PROTOC_EXECUTABLE} --version | |
443 | OUTPUT_VARIABLE _PROTOBUF_PROTOC_EXECUTABLE_VERSION) | |
444 | ||
445 | if("${_PROTOBUF_PROTOC_EXECUTABLE_VERSION}" MATCHES "libprotoc ([0-9.]+)") | |
446 | set(_PROTOBUF_PROTOC_EXECUTABLE_VERSION "${CMAKE_MATCH_1}") | |
447 | endif() | |
448 | ||
449 | if(Protobuf_DEBUG) | |
450 | message(STATUS "[ ${CMAKE_CURRENT_LIST_FILE}:${CMAKE_CURRENT_LIST_LINE} ] " | |
451 | "${Protobuf_PROTOC_EXECUTABLE} reveals version ${_PROTOBUF_PROTOC_EXECUTABLE_VERSION}") | |
452 | endif() | |
453 | ||
454 | if(NOT "${_PROTOBUF_PROTOC_EXECUTABLE_VERSION}" VERSION_EQUAL "${Protobuf_VERSION}") | |
455 | message(WARNING "Protobuf compiler version ${_PROTOBUF_PROTOC_EXECUTABLE_VERSION}" | |
456 | " doesn't match library version ${Protobuf_VERSION}") | |
457 | endif() | |
458 | ||
459 | if(Protobuf_LIBRARY) | |
460 | if(NOT TARGET protobuf::libprotobuf) | |
461 | add_library(protobuf::libprotobuf UNKNOWN IMPORTED) | |
462 | set_target_properties(protobuf::libprotobuf PROPERTIES | |
463 | INTERFACE_INCLUDE_DIRECTORIES "${Protobuf_INCLUDE_DIR}") | |
464 | if(EXISTS "${Protobuf_LIBRARY}") | |
465 | set_target_properties(protobuf::libprotobuf PROPERTIES | |
466 | IMPORTED_LOCATION "${Protobuf_LIBRARY}") | |
467 | endif() | |
468 | if(EXISTS "${Protobuf_LIBRARY_RELEASE}") | |
469 | set_property(TARGET protobuf::libprotobuf APPEND PROPERTY | |
470 | IMPORTED_CONFIGURATIONS RELEASE) | |
471 | set_target_properties(protobuf::libprotobuf PROPERTIES | |
472 | IMPORTED_LOCATION_RELEASE "${Protobuf_LIBRARY_RELEASE}") | |
473 | endif() | |
474 | if(EXISTS "${Protobuf_LIBRARY_DEBUG}") | |
475 | set_property(TARGET protobuf::libprotobuf APPEND PROPERTY | |
476 | IMPORTED_CONFIGURATIONS DEBUG) | |
477 | set_target_properties(protobuf::libprotobuf PROPERTIES | |
478 | IMPORTED_LOCATION_DEBUG "${Protobuf_LIBRARY_DEBUG}") | |
479 | endif() | |
480 | endif() | |
481 | endif() | |
482 | ||
483 | if(Protobuf_LITE_LIBRARY) | |
484 | if(NOT TARGET protobuf::libprotobuf-lite) | |
485 | add_library(protobuf::libprotobuf-lite UNKNOWN IMPORTED) | |
486 | set_target_properties(protobuf::libprotobuf-lite PROPERTIES | |
487 | INTERFACE_INCLUDE_DIRECTORIES "${Protobuf_INCLUDE_DIR}") | |
488 | if(EXISTS "${Protobuf_LITE_LIBRARY}") | |
489 | set_target_properties(protobuf::libprotobuf-lite PROPERTIES | |
490 | IMPORTED_LOCATION "${Protobuf_LITE_LIBRARY}") | |
491 | endif() | |
492 | if(EXISTS "${Protobuf_LITE_LIBRARY_RELEASE}") | |
493 | set_property(TARGET protobuf::libprotobuf-lite APPEND PROPERTY | |
494 | IMPORTED_CONFIGURATIONS RELEASE) | |
495 | set_target_properties(protobuf::libprotobuf-lite PROPERTIES | |
496 | IMPORTED_LOCATION_RELEASE "${Protobuf_LITE_LIBRARY_RELEASE}") | |
497 | endif() | |
498 | if(EXISTS "${Protobuf_LITE_LIBRARY_DEBUG}") | |
499 | set_property(TARGET protobuf::libprotobuf-lite APPEND PROPERTY | |
500 | IMPORTED_CONFIGURATIONS DEBUG) | |
501 | set_target_properties(protobuf::libprotobuf-lite PROPERTIES | |
502 | IMPORTED_LOCATION_DEBUG "${Protobuf_LITE_LIBRARY_DEBUG}") | |
503 | endif() | |
504 | endif() | |
505 | endif() | |
506 | ||
507 | if(Protobuf_PROTOC_LIBRARY) | |
508 | if(NOT TARGET protobuf::libprotoc) | |
509 | add_library(protobuf::libprotoc UNKNOWN IMPORTED) | |
510 | set_target_properties(protobuf::libprotoc PROPERTIES | |
511 | INTERFACE_INCLUDE_DIRECTORIES "${Protobuf_INCLUDE_DIR}") | |
512 | if(EXISTS "${Protobuf_PROTOC_LIBRARY}") | |
513 | set_target_properties(protobuf::libprotoc PROPERTIES | |
514 | IMPORTED_LOCATION "${Protobuf_PROTOC_LIBRARY}") | |
515 | endif() | |
516 | if(EXISTS "${Protobuf_PROTOC_LIBRARY_RELEASE}") | |
517 | set_property(TARGET protobuf::libprotoc APPEND PROPERTY | |
518 | IMPORTED_CONFIGURATIONS RELEASE) | |
519 | set_target_properties(protobuf::libprotoc PROPERTIES | |
520 | IMPORTED_LOCATION_RELEASE "${Protobuf_PROTOC_LIBRARY_RELEASE}") | |
521 | endif() | |
522 | if(EXISTS "${Protobuf_PROTOC_LIBRARY_DEBUG}") | |
523 | set_property(TARGET protobuf::libprotoc APPEND PROPERTY | |
524 | IMPORTED_CONFIGURATIONS DEBUG) | |
525 | set_target_properties(protobuf::libprotoc PROPERTIES | |
526 | IMPORTED_LOCATION_DEBUG "${Protobuf_PROTOC_LIBRARY_DEBUG}") | |
527 | endif() | |
528 | endif() | |
529 | endif() | |
530 | ||
531 | if(Protobuf_PROTOC_EXECUTABLE) | |
532 | if(NOT TARGET protobuf::protoc) | |
533 | add_executable(protobuf::protoc IMPORTED) | |
534 | if(EXISTS "${Protobuf_PROTOC_EXECUTABLE}") | |
535 | set_target_properties(protobuf::protoc PROPERTIES | |
536 | IMPORTED_LOCATION "${Protobuf_PROTOC_EXECUTABLE}") | |
537 | endif() | |
538 | endif() | |
539 | endif() | |
540 | endif() | |
541 | ||
542 | include(FindPackageHandleStandardArgs) | |
543 | FIND_PACKAGE_HANDLE_STANDARD_ARGS(Protobuf | |
544 | REQUIRED_VARS Protobuf_LIBRARIES Protobuf_INCLUDE_DIR | |
545 | VERSION_VAR Protobuf_VERSION | |
546 | ) | |
547 | ||
548 | if(Protobuf_FOUND) | |
549 | set(Protobuf_INCLUDE_DIRS ${Protobuf_INCLUDE_DIR}) | |
550 | endif() | |
551 | ||
552 | # Restore the original find library ordering | |
553 | if( Protobuf_USE_STATIC_LIBS ) | |
554 | set(CMAKE_FIND_LIBRARY_SUFFIXES ${_protobuf_ORIG_CMAKE_FIND_LIBRARY_SUFFIXES}) | |
555 | endif() | |
556 | ||
557 | # Backwards compatibility | |
558 | # Define upper case versions of output variables | |
559 | foreach(Camel | |
560 | Protobuf_SRC_ROOT_FOLDER | |
561 | Protobuf_IMPORT_DIRS | |
562 | Protobuf_DEBUG | |
563 | Protobuf_INCLUDE_DIRS | |
564 | Protobuf_LIBRARIES | |
565 | Protobuf_PROTOC_LIBRARIES | |
566 | Protobuf_LITE_LIBRARIES | |
567 | Protobuf_LIBRARY | |
568 | Protobuf_PROTOC_LIBRARY | |
569 | Protobuf_INCLUDE_DIR | |
570 | Protobuf_PROTOC_EXECUTABLE | |
571 | Protobuf_LIBRARY_DEBUG | |
572 | Protobuf_PROTOC_LIBRARY_DEBUG | |
573 | Protobuf_LITE_LIBRARY | |
574 | Protobuf_LITE_LIBRARY_DEBUG | |
575 | ) | |
576 | string(TOUPPER ${Camel} UPPER) | |
577 | set(${UPPER} ${${Camel}}) | |
578 | endforeach() |