Merge branch 'release-4.0'
[kiteware-cmake.git] / Modules / FindProtobuf.cmake
blob665190cd7f09603f36cb6d507cf39d48c0d19da5
1 # Distributed under the OSI-approved BSD 3-Clause License.  See accompanying
2 # file Copyright.txt or https://cmake.org/licensing for details.
4 #[=======================================================================[.rst:
5 FindProtobuf
6 ------------
8 Locate and configure the Google Protocol Buffers library.
10 .. versionadded:: 3.6
11   Support for :command:`find_package` version checks.
13 .. versionchanged:: 3.6
14   All input and output variables use the ``Protobuf_`` prefix.
15   Variables with ``PROTOBUF_`` prefix are still supported for compatibility.
17 The following variables can be set and are optional:
19 ``Protobuf_SRC_ROOT_FOLDER``
20   When compiling with MSVC, if this cache variable is set
21   the protobuf-default VS project build locations
22   (vsprojects/Debug and vsprojects/Release
23   or vsprojects/x64/Debug and vsprojects/x64/Release)
24   will be searched for libraries and binaries.
25 ``Protobuf_IMPORT_DIRS``
26   List of additional directories to be searched for
27   imported .proto files.
28 ``Protobuf_DEBUG``
29   .. versionadded:: 3.6
31   Show debug messages.
32 ``Protobuf_USE_STATIC_LIBS``
33   .. versionadded:: 3.9
35   Set to ON to force the use of the static libraries.
36   Default is OFF.
38 Defines the following variables:
40 ``Protobuf_FOUND``
41   Found the Google Protocol Buffers library
42   (libprotobuf & header files)
43 ``Protobuf_VERSION``
44   .. versionadded:: 3.6
46   Version of package found.
47 ``Protobuf_INCLUDE_DIRS``
48   Include directories for Google Protocol Buffers
49 ``Protobuf_LIBRARIES``
50   The protobuf libraries
51 ``Protobuf_PROTOC_LIBRARIES``
52   The protoc libraries
53 ``Protobuf_LITE_LIBRARIES``
54   The protobuf-lite libraries
56 .. versionadded:: 3.9
57   The following :prop_tgt:`IMPORTED` targets are also defined:
59 ``protobuf::libprotobuf``
60   The protobuf library.
61 ``protobuf::libprotobuf-lite``
62   The protobuf lite library.
63 ``protobuf::libprotoc``
64   The protoc library.
65 ``protobuf::protoc``
66   .. versionadded:: 3.10
67     The protoc compiler.
69 The following cache variables are also available to set or use:
71 ``Protobuf_LIBRARY``
72   The protobuf library
73 ``Protobuf_PROTOC_LIBRARY``
74   The protoc library
75 ``Protobuf_INCLUDE_DIR``
76   The include directory for protocol buffers
77 ``Protobuf_PROTOC_EXECUTABLE``
78   The protoc compiler
79 ``Protobuf_LIBRARY_DEBUG``
80   The protobuf library (debug)
81 ``Protobuf_PROTOC_LIBRARY_DEBUG``
82   The protoc library (debug)
83 ``Protobuf_LITE_LIBRARY``
84   The protobuf lite library
85 ``Protobuf_LITE_LIBRARY_DEBUG``
86   The protobuf lite library (debug)
88 Example:
90 .. code-block:: cmake
92   find_package(Protobuf REQUIRED)
93   include_directories(${Protobuf_INCLUDE_DIRS})
94   include_directories(${CMAKE_CURRENT_BINARY_DIR})
95   protobuf_generate_cpp(PROTO_SRCS PROTO_HDRS foo.proto)
96   protobuf_generate_cpp(PROTO_SRCS PROTO_HDRS EXPORT_MACRO DLL_EXPORT foo.proto)
97   protobuf_generate_cpp(PROTO_SRCS PROTO_HDRS DESCRIPTORS PROTO_DESCS foo.proto)
98   protobuf_generate_python(PROTO_PY foo.proto)
99   add_executable(bar bar.cc ${PROTO_SRCS} ${PROTO_HDRS})
100   target_link_libraries(bar ${Protobuf_LIBRARIES})
102 .. note::
103   The ``protobuf_generate_cpp`` and ``protobuf_generate_python``
104   functions and :command:`add_executable` or :command:`add_library`
105   calls only work properly within the same directory.
107 .. command:: protobuf_generate_cpp
109   Add custom commands to process ``.proto`` files to C++:
111   .. code-block:: cmake
113     protobuf_generate_cpp (
114       <srcs-var> <hdrs-var>
115       [DESCRIPTORS <var>]
116       [EXPORT_MACRO <macro>]
117       [<proto-file>...])
119   ``<srcs-var>``
120     Variable to define with autogenerated source files
122   ``<hdrs-var>``
123     Variable to define with autogenerated header files
125   ``DESCRIPTORS <var>``
126     .. versionadded:: 3.10
127       Variable to define with autogenerated descriptor files, if requested.
129   ``EXPORT_MACRO <macro>``
130     is a macro which should expand to ``__declspec(dllexport)`` or
131     ``__declspec(dllimport)`` depending on what is being compiled.
133   ``<proto-file>...``
134     ``.proto`` files
136 .. command:: protobuf_generate_python
138   .. versionadded:: 3.4
140   Add custom commands to process ``.proto`` files to Python:
142   .. code-block:: cmake
144     protobuf_generate_python (<py-srcs-var> [<proto-file>...])
146   ``<py-srcs-var>``
147     Variable to define with autogenerated Python files
149   ``<proto-file>...``
150     ``.proto`` files
152 .. command:: protobuf_generate
154   .. versionadded:: 3.13
156   Automatically generate source files from ``.proto`` schema files at build time:
158   .. code-block:: cmake
160     protobuf_generate (
161         TARGET <target>
162         [LANGUAGE <lang>]
163         [OUT_VAR <var>]
164         [EXPORT_MACRO <macro>]
165         [PROTOC_OUT_DIR <dir>]
166         [PLUGIN <plugin>]
167         [PLUGIN_OPTIONS <plugin-options>]
168         [DEPENDENCIES <dependencies>]
169         [PROTOS <proto-file>...]
170         [IMPORT_DIRS <dir>...]
171         [GENERATE_EXTENSIONS <extension>...]
172         [PROTOC_OPTIONS <option>...]
173         [PROTOC_EXE <executable>]
174         [APPEND_PATH])
176   ``APPEND_PATH``
177     A flag that causes the base path of all proto schema files to be added to
178     ``IMPORT_DIRS``.
180   ``LANGUAGE <lang>``
181     A single value: cpp or python. Determines what kind of source files are
182     being generated. Defaults to cpp.
184   ``OUT_VAR <var>``
185     Name of a CMake variable that will be filled with the paths to the generated
186     source files.
188   ``EXPORT_MACRO <macro>``
189     Name of a macro that is applied to all generated Protobuf message classes
190     and extern variables. It can, for example, be used to declare DLL exports.
192   ``PROTOC_OUT_DIR <dir>``
193     Output directory of generated source files. Defaults to ``CMAKE_CURRENT_BINARY_DIR``.
195   ``PLUGIN <plugin>``
196     .. versionadded:: 3.21
198     An optional plugin executable. This could, for example, be the path to
199     ``grpc_cpp_plugin``.
201   ``PLUGIN_OPTIONS <plugin-options>``
202     .. versionadded:: 3.28
204     Additional options provided to the plugin, such as ``generate_mock_code=true``
205     for the gRPC cpp plugin.
207   ``DEPENDENCIES <dependencies>``
208     .. versionadded:: 3.28
210     Arguments forwarded to the ``DEPENDS`` of the underlying ``add_custom_command``
211     invocation.
213   ``TARGET <target>``
214     CMake target that will have the generated files added as sources.
216   ``PROTOS <proto-file>...``
217     List of proto schema files. If omitted, then every source file ending in *proto* of ``TARGET`` will be used.
219   ``IMPORT_DIRS <dir>...``
220     A common parent directory for the schema files. For example, if the schema file is
221     ``proto/helloworld/helloworld.proto`` and the import directory ``proto/`` then the
222     generated files are ``${PROTOC_OUT_DIR}/helloworld/helloworld.pb.h`` and
223     ``${PROTOC_OUT_DIR}/helloworld/helloworld.pb.cc``.
225   ``GENERATE_EXTENSIONS <extension>...``
226     If LANGUAGE is omitted then this must be set to the extensions that protoc generates.
228   ``PROTOC_OPTIONS <option>...``
229     .. versionadded:: 3.28
231     Additional arguments that are forwarded to protoc.
233   ``PROTOC_EXE <executable>``
234     .. versionadded:: 4.0
236     Command name, path, or CMake executable used to generate protobuf bindings.
237     If omitted, ``protobuf::protoc`` is used.
239   Example:
241   .. code-block:: cmake
243     find_package(gRPC CONFIG REQUIRED)
244     find_package(Protobuf REQUIRED)
245     add_library(ProtoTest Test.proto)
246     target_link_libraries(ProtoTest PUBLIC gRPC::grpc++)
247     protobuf_generate(TARGET ProtoTest)
248     protobuf_generate(
249         TARGET ProtoTest
250         LANGUAGE grpc
251         PLUGIN protoc-gen-grpc=$<TARGET_FILE:gRPC::grpc_cpp_plugin>
252         PLUGIN_OPTIONS generate_mock_code=true
253         GENERATE_EXTENSIONS .grpc.pb.h .grpc.pb.cc)
254 #]=======================================================================]
256 cmake_policy(PUSH)
257 cmake_policy(SET CMP0159 NEW) # file(STRINGS) with REGEX updates CMAKE_MATCH_<n>
259 function(protobuf_generate)
260   set(_options APPEND_PATH DESCRIPTORS)
261   set(_singleargs LANGUAGE OUT_VAR EXPORT_MACRO PROTOC_OUT_DIR PLUGIN PLUGIN_OPTIONS DEPENDENCIES PROTOC_EXE)
262   if(COMMAND target_sources)
263     list(APPEND _singleargs TARGET)
264   endif()
265   set(_multiargs PROTOS IMPORT_DIRS GENERATE_EXTENSIONS PROTOC_OPTIONS)
267   cmake_parse_arguments(protobuf_generate "${_options}" "${_singleargs}" "${_multiargs}" "${ARGN}")
269   if(NOT protobuf_generate_PROTOS AND NOT protobuf_generate_TARGET)
270     message(SEND_ERROR "Error: protobuf_generate called without any targets or source files")
271     return()
272   endif()
274   if(NOT protobuf_generate_OUT_VAR AND NOT protobuf_generate_TARGET)
275     message(SEND_ERROR "Error: protobuf_generate called without a target or output variable")
276     return()
277   endif()
279   if(NOT protobuf_generate_LANGUAGE)
280     set(protobuf_generate_LANGUAGE cpp)
281   endif()
282   string(TOLOWER ${protobuf_generate_LANGUAGE} protobuf_generate_LANGUAGE)
284   if(NOT protobuf_generate_PROTOC_OUT_DIR)
285     set(protobuf_generate_PROTOC_OUT_DIR ${CMAKE_CURRENT_BINARY_DIR})
286   endif()
288   if(protobuf_generate_EXPORT_MACRO AND protobuf_generate_LANGUAGE STREQUAL cpp)
289     set(_dll_export_decl "dllexport_decl=${protobuf_generate_EXPORT_MACRO}")
290   endif()
292   foreach(_option ${_dll_export_decl} ${protobuf_generate_PLUGIN_OPTIONS})
293     # append comma - not using CMake lists and string replacement as users
294     # might have semicolons in options
295     if(_plugin_options)
296       set( _plugin_options "${_plugin_options},")
297     endif()
298     set(_plugin_options "${_plugin_options}${_option}")
299   endforeach()
301   if(protobuf_generate_PLUGIN)
302     set(_plugin "--plugin=${protobuf_generate_PLUGIN}")
303   endif()
305   if(NOT protobuf_generate_GENERATE_EXTENSIONS)
306     if(protobuf_generate_LANGUAGE STREQUAL cpp)
307       set(protobuf_generate_GENERATE_EXTENSIONS .pb.h .pb.cc)
308     elseif(protobuf_generate_LANGUAGE STREQUAL python)
309       set(protobuf_generate_GENERATE_EXTENSIONS _pb2.py)
310     else()
311       message(SEND_ERROR "Error: protobuf_generate given unknown Language ${LANGUAGE}, please provide a value for GENERATE_EXTENSIONS")
312       return()
313     endif()
314   endif()
316   if(protobuf_generate_TARGET)
317     get_target_property(_source_list ${protobuf_generate_TARGET} SOURCES)
318     foreach(_file ${_source_list})
319       if(_file MATCHES "proto$")
320         list(APPEND protobuf_generate_PROTOS ${_file})
321       endif()
322     endforeach()
323   endif()
325   if(NOT protobuf_generate_PROTOS)
326     message(SEND_ERROR "Error: protobuf_generate could not find any .proto files")
327     return()
328   endif()
330   if(NOT protobuf_generate_PROTOC_EXE)
331     if(NOT TARGET protobuf::protoc)
332       message(SEND_ERROR "protoc executable not found. "
333         "Please define the Protobuf_PROTOC_EXECUTABLE variable, or pass PROTOC_EXE to protobuf_generate, or ensure that protoc is in CMake's search path.")
334       return()
335     endif()
336     # Default to using the CMake executable
337     set(protobuf_generate_PROTOC_EXE protobuf::protoc)
338   endif()
340   if(protobuf_generate_APPEND_PATH)
341     # Create an include path for each file specified
342     foreach(_file ${protobuf_generate_PROTOS})
343       get_filename_component(_abs_file ${_file} ABSOLUTE)
344       get_filename_component(_abs_dir ${_abs_file} DIRECTORY)
345       list(FIND _protobuf_include_path ${_abs_dir} _contains_already)
346       if(${_contains_already} EQUAL -1)
347           list(APPEND _protobuf_include_path -I ${_abs_dir})
348       endif()
349     endforeach()
350   endif()
352   foreach(DIR ${protobuf_generate_IMPORT_DIRS})
353     get_filename_component(ABS_PATH ${DIR} ABSOLUTE)
354     list(FIND _protobuf_include_path ${ABS_PATH} _contains_already)
355     if(${_contains_already} EQUAL -1)
356         list(APPEND _protobuf_include_path -I ${ABS_PATH})
357     endif()
358   endforeach()
360   if(NOT protobuf_generate_APPEND_PATH)
361     list(APPEND _protobuf_include_path -I ${CMAKE_CURRENT_SOURCE_DIR})
362   endif()
364   set(_generated_srcs_all)
365   foreach(_proto ${protobuf_generate_PROTOS})
366     get_filename_component(_abs_file ${_proto} ABSOLUTE)
367     get_filename_component(_abs_dir ${_abs_file} DIRECTORY)
368     get_filename_component(_basename ${_proto} NAME_WLE)
369     file(RELATIVE_PATH _rel_dir ${CMAKE_CURRENT_SOURCE_DIR} ${_abs_dir})
371     set(_possible_rel_dir)
372     if (NOT protobuf_generate_APPEND_PATH)
373       foreach(DIR ${_protobuf_include_path})
374         if(NOT DIR STREQUAL "-I")
375           file(RELATIVE_PATH _rel_dir ${DIR} ${_abs_dir})
376           if(_rel_dir STREQUAL _abs_dir)
377             continue()
378           endif()
379           string(FIND "${_rel_dir}" "../" _is_in_parent_folder)
380           if (NOT ${_is_in_parent_folder} EQUAL 0)
381             break()
382           endif()
383         endif()
384       endforeach()
385       set(_possible_rel_dir ${_rel_dir}/)
386     endif()
388     set(_generated_srcs)
389     foreach(_ext ${protobuf_generate_GENERATE_EXTENSIONS})
390       list(APPEND _generated_srcs "${protobuf_generate_PROTOC_OUT_DIR}/${_possible_rel_dir}${_basename}${_ext}")
391     endforeach()
393     if(protobuf_generate_DESCRIPTORS AND protobuf_generate_LANGUAGE STREQUAL cpp)
394       set(_descriptor_file "${CMAKE_CURRENT_BINARY_DIR}/${_basename}.desc")
395       set(_dll_desc_out "--descriptor_set_out=${_descriptor_file}")
396       list(APPEND _generated_srcs ${_descriptor_file})
397     endif()
398     list(APPEND _generated_srcs_all ${_generated_srcs})
400     set(_comment "Running ${protobuf_generate_LANGUAGE} protocol buffer compiler on ${_proto}")
401     if(protobuf_generate_PROTOC_OPTIONS)
402       set(_comment "${_comment}, protoc-options: ${protobuf_generate_PROTOC_OPTIONS}")
403     endif()
404     if(_plugin_options)
405       set(_comment "${_comment}, plugin-options: ${_plugin_options}")
406     endif()
408     add_custom_command(
409       OUTPUT ${_generated_srcs}
410       COMMAND ${protobuf_generate_PROTOC_EXE}
411       ARGS ${protobuf_generate_PROTOC_OPTIONS} --${protobuf_generate_LANGUAGE}_out ${_plugin_options}:${protobuf_generate_PROTOC_OUT_DIR} ${_plugin} ${_dll_desc_out} ${_protobuf_include_path} ${_abs_file}
412       DEPENDS ${_abs_file} protobuf::protoc ${protobuf_generate_DEPENDENCIES}
413       COMMENT ${_comment}
414       VERBATIM )
415   endforeach()
417   set_source_files_properties(${_generated_srcs_all} PROPERTIES GENERATED TRUE)
418   if(protobuf_generate_OUT_VAR)
419     set(${protobuf_generate_OUT_VAR} ${_generated_srcs_all} PARENT_SCOPE)
420   endif()
421   if(protobuf_generate_TARGET)
422     target_sources(${protobuf_generate_TARGET} PRIVATE ${_generated_srcs_all})
423   endif()
424 endfunction()
426 function(PROTOBUF_GENERATE_CPP SRCS HDRS)
427   cmake_parse_arguments(protobuf_generate_cpp "" "EXPORT_MACRO;DESCRIPTORS" "" ${ARGN})
429   set(_proto_files "${protobuf_generate_cpp_UNPARSED_ARGUMENTS}")
430   if(NOT _proto_files)
431     message(SEND_ERROR "Error: PROTOBUF_GENERATE_CPP() called without any proto files")
432     return()
433   endif()
435   if(PROTOBUF_GENERATE_CPP_APPEND_PATH)
436     set(_append_arg APPEND_PATH)
437   endif()
439   if(protobuf_generate_cpp_DESCRIPTORS)
440     set(_descriptors DESCRIPTORS)
441   endif()
443   if(DEFINED PROTOBUF_IMPORT_DIRS AND NOT DEFINED Protobuf_IMPORT_DIRS)
444     set(Protobuf_IMPORT_DIRS "${PROTOBUF_IMPORT_DIRS}")
445   endif()
447   if(DEFINED Protobuf_IMPORT_DIRS)
448     set(_import_arg IMPORT_DIRS ${Protobuf_IMPORT_DIRS})
449   endif()
451   set(_outvar)
452   protobuf_generate(${_append_arg} ${_descriptors} LANGUAGE cpp EXPORT_MACRO ${protobuf_generate_cpp_EXPORT_MACRO} OUT_VAR _outvar ${_import_arg} PROTOS ${_proto_files})
454   set(${SRCS})
455   set(${HDRS})
456   if(protobuf_generate_cpp_DESCRIPTORS)
457     set(${protobuf_generate_cpp_DESCRIPTORS})
458   endif()
460   foreach(_file ${_outvar})
461     if(_file MATCHES "cc$")
462       list(APPEND ${SRCS} ${_file})
463     elseif(_file MATCHES "desc$")
464       list(APPEND ${protobuf_generate_cpp_DESCRIPTORS} ${_file})
465     else()
466       list(APPEND ${HDRS} ${_file})
467     endif()
468   endforeach()
469   set(${SRCS} ${${SRCS}} PARENT_SCOPE)
470   set(${HDRS} ${${HDRS}} PARENT_SCOPE)
471   if(protobuf_generate_cpp_DESCRIPTORS)
472     set(${protobuf_generate_cpp_DESCRIPTORS} "${${protobuf_generate_cpp_DESCRIPTORS}}" PARENT_SCOPE)
473   endif()
474 endfunction()
476 function(PROTOBUF_GENERATE_PYTHON SRCS)
477   if(NOT ARGN)
478     message(SEND_ERROR "Error: PROTOBUF_GENERATE_PYTHON() called without any proto files")
479     return()
480   endif()
482   if(PROTOBUF_GENERATE_CPP_APPEND_PATH)
483     set(_append_arg APPEND_PATH)
484   endif()
486   if(DEFINED PROTOBUF_IMPORT_DIRS AND NOT DEFINED Protobuf_IMPORT_DIRS)
487     set(Protobuf_IMPORT_DIRS "${PROTOBUF_IMPORT_DIRS}")
488   endif()
490   if(DEFINED Protobuf_IMPORT_DIRS)
491     set(_import_arg IMPORT_DIRS ${Protobuf_IMPORT_DIRS})
492   endif()
494   set(_outvar)
495   protobuf_generate(${_append_arg} LANGUAGE python OUT_VAR _outvar ${_import_arg} PROTOS ${ARGN})
496   set(${SRCS} ${_outvar} PARENT_SCOPE)
497 endfunction()
500 if(Protobuf_DEBUG)
501   # Output some of their choices
502   message(STATUS "[ ${CMAKE_CURRENT_LIST_FILE}:${CMAKE_CURRENT_LIST_LINE} ] "
503                  "Protobuf_USE_STATIC_LIBS = ${Protobuf_USE_STATIC_LIBS}")
504 endif()
507 # Backwards compatibility
508 # Define camel case versions of input variables
509 foreach(UPPER
510     PROTOBUF_SRC_ROOT_FOLDER
511     PROTOBUF_IMPORT_DIRS
512     PROTOBUF_DEBUG
513     PROTOBUF_LIBRARY
514     PROTOBUF_PROTOC_LIBRARY
515     PROTOBUF_INCLUDE_DIR
516     PROTOBUF_PROTOC_EXECUTABLE
517     PROTOBUF_LIBRARY_DEBUG
518     PROTOBUF_PROTOC_LIBRARY_DEBUG
519     PROTOBUF_LITE_LIBRARY
520     PROTOBUF_LITE_LIBRARY_DEBUG
521     )
522     if (DEFINED ${UPPER})
523         string(REPLACE "PROTOBUF_" "Protobuf_" Camel ${UPPER})
524         if (NOT DEFINED ${Camel})
525             set(${Camel} ${${UPPER}})
526         endif()
527     endif()
528 endforeach()
530 if(CMAKE_SIZEOF_VOID_P EQUAL 8)
531   set(_PROTOBUF_ARCH_DIR x64/)
532 endif()
535 # Support preference of static libs by adjusting CMAKE_FIND_LIBRARY_SUFFIXES
536 if( Protobuf_USE_STATIC_LIBS )
537   set( _protobuf_ORIG_CMAKE_FIND_LIBRARY_SUFFIXES ${CMAKE_FIND_LIBRARY_SUFFIXES})
538   if(WIN32)
539     set(CMAKE_FIND_LIBRARY_SUFFIXES .lib .a ${CMAKE_FIND_LIBRARY_SUFFIXES})
540   else()
541     set(CMAKE_FIND_LIBRARY_SUFFIXES .a )
542   endif()
543 endif()
545 include(${CMAKE_CURRENT_LIST_DIR}/SelectLibraryConfigurations.cmake)
547 # Internal function: search for normal library as well as a debug one
548 #    if the debug one is specified also include debug/optimized keywords
549 #    in *_LIBRARIES variable
550 function(_protobuf_find_libraries name filename)
551   if(${name}_LIBRARIES)
552     # Use result recorded by a previous call.
553     return()
554   elseif(${name}_LIBRARY)
555     # Honor cache entry used by CMake 3.5 and lower.
556     set(${name}_LIBRARIES "${${name}_LIBRARY}" PARENT_SCOPE)
557   else()
558     find_library(${name}_LIBRARY_RELEASE
559       NAMES ${filename}
560       NAMES_PER_DIR
561       PATHS ${Protobuf_SRC_ROOT_FOLDER}/vsprojects/${_PROTOBUF_ARCH_DIR}Release)
562     mark_as_advanced(${name}_LIBRARY_RELEASE)
564     find_library(${name}_LIBRARY_DEBUG
565       NAMES ${filename}d ${filename}
566       NAMES_PER_DIR
567       PATHS ${Protobuf_SRC_ROOT_FOLDER}/vsprojects/${_PROTOBUF_ARCH_DIR}Debug)
568     mark_as_advanced(${name}_LIBRARY_DEBUG)
570     select_library_configurations(${name})
572     if(UNIX AND Threads_FOUND AND ${name}_LIBRARY)
573       list(APPEND ${name}_LIBRARIES ${CMAKE_THREAD_LIBS_INIT})
574     endif()
576     set(${name}_LIBRARY "${${name}_LIBRARY}" PARENT_SCOPE)
577     set(${name}_LIBRARIES "${${name}_LIBRARIES}" PARENT_SCOPE)
578   endif()
579 endfunction()
582 # Main.
585 # By default have PROTOBUF_GENERATE_CPP macro pass -I to protoc
586 # for each directory where a proto file is referenced.
587 if(NOT DEFINED PROTOBUF_GENERATE_CPP_APPEND_PATH)
588   set(PROTOBUF_GENERATE_CPP_APPEND_PATH TRUE)
589 endif()
592 # Google's provided vcproj files generate libraries with a "lib"
593 # prefix on Windows
594 if(MSVC)
595     set(Protobuf_ORIG_FIND_LIBRARY_PREFIXES "${CMAKE_FIND_LIBRARY_PREFIXES}")
596     set(CMAKE_FIND_LIBRARY_PREFIXES "lib" "")
598     find_path(Protobuf_SRC_ROOT_FOLDER protobuf.pc.in)
599 endif()
601 if(UNIX)
602   # Protobuf headers may depend on threading.
603   find_package(Threads QUIET)
604 endif()
606 # The Protobuf library
607 _protobuf_find_libraries(Protobuf protobuf)
608 #DOC "The Google Protocol Buffers RELEASE Library"
610 _protobuf_find_libraries(Protobuf_LITE protobuf-lite)
612 # The Protobuf Protoc Library
613 _protobuf_find_libraries(Protobuf_PROTOC protoc)
615 # Restore original find library prefixes
616 if(MSVC)
617     set(CMAKE_FIND_LIBRARY_PREFIXES "${Protobuf_ORIG_FIND_LIBRARY_PREFIXES}")
618 endif()
620 # Find the include directory
621 find_path(Protobuf_INCLUDE_DIR
622     google/protobuf/service.h
623     PATHS ${Protobuf_SRC_ROOT_FOLDER}/src
625 mark_as_advanced(Protobuf_INCLUDE_DIR)
627 # Find the protoc Executable
628 find_program(Protobuf_PROTOC_EXECUTABLE
629     NAMES protoc
630     DOC "The Google Protocol Buffers Compiler"
631     PATHS
632     ${Protobuf_SRC_ROOT_FOLDER}/vsprojects/${_PROTOBUF_ARCH_DIR}Release
633     ${Protobuf_SRC_ROOT_FOLDER}/vsprojects/${_PROTOBUF_ARCH_DIR}Debug
635 mark_as_advanced(Protobuf_PROTOC_EXECUTABLE)
637 if(Protobuf_DEBUG)
638     message(STATUS "[ ${CMAKE_CURRENT_LIST_FILE}:${CMAKE_CURRENT_LIST_LINE} ] "
639         "requested version of Google Protobuf is ${Protobuf_FIND_VERSION}")
640 endif()
642 if(Protobuf_INCLUDE_DIR)
643   set(_PROTOBUF_COMMON_HEADER ${Protobuf_INCLUDE_DIR}/google/protobuf/stubs/common.h)
645   if(Protobuf_DEBUG)
646     message(STATUS "[ ${CMAKE_CURRENT_LIST_FILE}:${CMAKE_CURRENT_LIST_LINE} ] "
647                    "location of common.h: ${_PROTOBUF_COMMON_HEADER}")
648   endif()
650   set(Protobuf_VERSION "")
651   set(Protobuf_LIB_VERSION "")
652   file(STRINGS ${_PROTOBUF_COMMON_HEADER} _PROTOBUF_COMMON_H_CONTENTS REGEX "#define[ \t]+GOOGLE_PROTOBUF_VERSION[ \t]+")
653   if(_PROTOBUF_COMMON_H_CONTENTS MATCHES "#define[ \t]+GOOGLE_PROTOBUF_VERSION[ \t]+([0-9]+)")
654       set(Protobuf_LIB_VERSION "${CMAKE_MATCH_1}")
655   endif()
656   unset(_PROTOBUF_COMMON_H_CONTENTS)
658   math(EXPR _PROTOBUF_MAJOR_VERSION "${Protobuf_LIB_VERSION} / 1000000")
659   math(EXPR _PROTOBUF_MINOR_VERSION "${Protobuf_LIB_VERSION} / 1000 % 1000")
660   math(EXPR _PROTOBUF_SUBMINOR_VERSION "${Protobuf_LIB_VERSION} % 1000")
661   set(Protobuf_VERSION "${_PROTOBUF_MAJOR_VERSION}.${_PROTOBUF_MINOR_VERSION}.${_PROTOBUF_SUBMINOR_VERSION}")
663   if(Protobuf_DEBUG)
664     message(STATUS "[ ${CMAKE_CURRENT_LIST_FILE}:${CMAKE_CURRENT_LIST_LINE} ] "
665         "${_PROTOBUF_COMMON_HEADER} reveals protobuf ${Protobuf_VERSION}")
666   endif()
668   if(Protobuf_PROTOC_EXECUTABLE)
669     # Check Protobuf compiler version to be aligned with libraries version
670     execute_process(COMMAND ${Protobuf_PROTOC_EXECUTABLE} --version
671                     OUTPUT_VARIABLE _PROTOBUF_PROTOC_EXECUTABLE_VERSION)
673     if("${_PROTOBUF_PROTOC_EXECUTABLE_VERSION}" MATCHES "libprotoc ([0-9.]+)")
674       set(_PROTOBUF_PROTOC_EXECUTABLE_VERSION "${CMAKE_MATCH_1}")
675     endif()
677     if(Protobuf_DEBUG)
678       message(STATUS "[ ${CMAKE_CURRENT_LIST_FILE}:${CMAKE_CURRENT_LIST_LINE} ] "
679           "${Protobuf_PROTOC_EXECUTABLE} reveals version ${_PROTOBUF_PROTOC_EXECUTABLE_VERSION}")
680     endif()
682     # protoc version 22 and up don't print the major version any more
683     if(NOT "${_PROTOBUF_PROTOC_EXECUTABLE_VERSION}" VERSION_EQUAL "${Protobuf_VERSION}" AND
684        NOT "${_PROTOBUF_PROTOC_EXECUTABLE_VERSION}" VERSION_EQUAL "${_PROTOBUF_MINOR_VERSION}.${_PROTOBUF_SUBMINOR_VERSION}")
685       message(WARNING "Protobuf compiler version ${_PROTOBUF_PROTOC_EXECUTABLE_VERSION}"
686         " doesn't match library version ${Protobuf_VERSION}")
687     endif()
688   endif()
690   if(Protobuf_LIBRARY)
691       if(NOT TARGET protobuf::libprotobuf)
692           add_library(protobuf::libprotobuf UNKNOWN IMPORTED)
693           set_target_properties(protobuf::libprotobuf PROPERTIES
694             INTERFACE_INCLUDE_DIRECTORIES "${Protobuf_INCLUDE_DIR}")
695           if(EXISTS "${Protobuf_LIBRARY}")
696             set_target_properties(protobuf::libprotobuf PROPERTIES
697               IMPORTED_LOCATION "${Protobuf_LIBRARY}")
698           endif()
699           if(EXISTS "${Protobuf_LIBRARY_RELEASE}")
700             set_property(TARGET protobuf::libprotobuf APPEND PROPERTY
701               IMPORTED_CONFIGURATIONS RELEASE)
702             set_target_properties(protobuf::libprotobuf PROPERTIES
703               IMPORTED_LOCATION_RELEASE "${Protobuf_LIBRARY_RELEASE}")
704           endif()
705           if(EXISTS "${Protobuf_LIBRARY_DEBUG}")
706             set_property(TARGET protobuf::libprotobuf APPEND PROPERTY
707               IMPORTED_CONFIGURATIONS DEBUG)
708             set_target_properties(protobuf::libprotobuf PROPERTIES
709               IMPORTED_LOCATION_DEBUG "${Protobuf_LIBRARY_DEBUG}")
710           endif()
711           if (Protobuf_VERSION VERSION_GREATER_EQUAL "3.6")
712             set_property(TARGET protobuf::libprotobuf APPEND PROPERTY
713               INTERFACE_COMPILE_FEATURES cxx_std_11
714             )
715           endif()
716           if (WIN32 AND NOT Protobuf_USE_STATIC_LIBS)
717             set_property(TARGET protobuf::libprotobuf APPEND PROPERTY
718               INTERFACE_COMPILE_DEFINITIONS "PROTOBUF_USE_DLLS"
719             )
720           endif()
721           if(UNIX AND TARGET Threads::Threads)
722             set_property(TARGET protobuf::libprotobuf APPEND PROPERTY
723                 INTERFACE_LINK_LIBRARIES Threads::Threads)
724           endif()
725       endif()
726   endif()
728   if(Protobuf_LITE_LIBRARY)
729       if(NOT TARGET protobuf::libprotobuf-lite)
730           add_library(protobuf::libprotobuf-lite UNKNOWN IMPORTED)
731           set_target_properties(protobuf::libprotobuf-lite PROPERTIES
732             INTERFACE_INCLUDE_DIRECTORIES "${Protobuf_INCLUDE_DIR}")
733           if(EXISTS "${Protobuf_LITE_LIBRARY}")
734             set_target_properties(protobuf::libprotobuf-lite PROPERTIES
735               IMPORTED_LOCATION "${Protobuf_LITE_LIBRARY}")
736           endif()
737           if(EXISTS "${Protobuf_LITE_LIBRARY_RELEASE}")
738             set_property(TARGET protobuf::libprotobuf-lite APPEND PROPERTY
739               IMPORTED_CONFIGURATIONS RELEASE)
740             set_target_properties(protobuf::libprotobuf-lite PROPERTIES
741               IMPORTED_LOCATION_RELEASE "${Protobuf_LITE_LIBRARY_RELEASE}")
742           endif()
743           if(EXISTS "${Protobuf_LITE_LIBRARY_DEBUG}")
744             set_property(TARGET protobuf::libprotobuf-lite APPEND PROPERTY
745               IMPORTED_CONFIGURATIONS DEBUG)
746             set_target_properties(protobuf::libprotobuf-lite PROPERTIES
747               IMPORTED_LOCATION_DEBUG "${Protobuf_LITE_LIBRARY_DEBUG}")
748           endif()
749           if (WIN32 AND NOT Protobuf_USE_STATIC_LIBS)
750             set_property(TARGET protobuf::libprotobuf-lite APPEND PROPERTY
751               INTERFACE_COMPILE_DEFINITIONS "PROTOBUF_USE_DLLS"
752             )
753           endif()
754           if(UNIX AND TARGET Threads::Threads)
755             set_property(TARGET protobuf::libprotobuf-lite APPEND PROPERTY
756                 INTERFACE_LINK_LIBRARIES Threads::Threads)
757           endif()
758       endif()
759   endif()
761   if(Protobuf_PROTOC_LIBRARY)
762       if(NOT TARGET protobuf::libprotoc)
763           add_library(protobuf::libprotoc UNKNOWN IMPORTED)
764           set_target_properties(protobuf::libprotoc PROPERTIES
765             INTERFACE_INCLUDE_DIRECTORIES "${Protobuf_INCLUDE_DIR}")
766           if(EXISTS "${Protobuf_PROTOC_LIBRARY}")
767             set_target_properties(protobuf::libprotoc PROPERTIES
768               IMPORTED_LOCATION "${Protobuf_PROTOC_LIBRARY}")
769           endif()
770           if(EXISTS "${Protobuf_PROTOC_LIBRARY_RELEASE}")
771             set_property(TARGET protobuf::libprotoc APPEND PROPERTY
772               IMPORTED_CONFIGURATIONS RELEASE)
773             set_target_properties(protobuf::libprotoc PROPERTIES
774               IMPORTED_LOCATION_RELEASE "${Protobuf_PROTOC_LIBRARY_RELEASE}")
775           endif()
776           if(EXISTS "${Protobuf_PROTOC_LIBRARY_DEBUG}")
777             set_property(TARGET protobuf::libprotoc APPEND PROPERTY
778               IMPORTED_CONFIGURATIONS DEBUG)
779             set_target_properties(protobuf::libprotoc PROPERTIES
780               IMPORTED_LOCATION_DEBUG "${Protobuf_PROTOC_LIBRARY_DEBUG}")
781           endif()
782           if (Protobuf_VERSION VERSION_GREATER_EQUAL "3.6")
783             set_property(TARGET protobuf::libprotoc APPEND PROPERTY
784               INTERFACE_COMPILE_FEATURES cxx_std_11
785             )
786           endif()
787           if (WIN32 AND NOT Protobuf_USE_STATIC_LIBS)
788             set_property(TARGET protobuf::libprotoc APPEND PROPERTY
789               INTERFACE_COMPILE_DEFINITIONS "PROTOBUF_USE_DLLS"
790             )
791           endif()
792           if(UNIX AND TARGET Threads::Threads)
793             set_property(TARGET protobuf::libprotoc APPEND PROPERTY
794                 INTERFACE_LINK_LIBRARIES Threads::Threads)
795           endif()
796       endif()
797   endif()
799   if(Protobuf_PROTOC_EXECUTABLE)
800       if(NOT TARGET protobuf::protoc)
801           add_executable(protobuf::protoc IMPORTED)
802           if(EXISTS "${Protobuf_PROTOC_EXECUTABLE}")
803             set_target_properties(protobuf::protoc PROPERTIES
804               IMPORTED_LOCATION "${Protobuf_PROTOC_EXECUTABLE}")
805           endif()
806       endif()
807   endif()
808 endif()
810 include(FindPackageHandleStandardArgs)
811 find_package_handle_standard_args(Protobuf
812     REQUIRED_VARS Protobuf_LIBRARIES Protobuf_INCLUDE_DIR
813     VERSION_VAR Protobuf_VERSION
816 if(Protobuf_FOUND)
817     set(Protobuf_INCLUDE_DIRS ${Protobuf_INCLUDE_DIR})
818 endif()
820 # Restore the original find library ordering
821 if( Protobuf_USE_STATIC_LIBS )
822   set(CMAKE_FIND_LIBRARY_SUFFIXES ${_protobuf_ORIG_CMAKE_FIND_LIBRARY_SUFFIXES})
823 endif()
825 # Backwards compatibility
826 # Define upper case versions of output variables
827 foreach(Camel
828     Protobuf_SRC_ROOT_FOLDER
829     Protobuf_IMPORT_DIRS
830     Protobuf_DEBUG
831     Protobuf_INCLUDE_DIRS
832     Protobuf_LIBRARIES
833     Protobuf_PROTOC_LIBRARIES
834     Protobuf_LITE_LIBRARIES
835     Protobuf_LIBRARY
836     Protobuf_PROTOC_LIBRARY
837     Protobuf_INCLUDE_DIR
838     Protobuf_PROTOC_EXECUTABLE
839     Protobuf_LIBRARY_DEBUG
840     Protobuf_PROTOC_LIBRARY_DEBUG
841     Protobuf_LITE_LIBRARY
842     Protobuf_LITE_LIBRARY_DEBUG
843     )
844     string(TOUPPER ${Camel} UPPER)
845     set(${UPPER} ${${Camel}})
846 endforeach()
848 cmake_policy(POP)