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:
8 Locate and configure the Google Protocol Buffers library.
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.
32 ``Protobuf_USE_STATIC_LIBS``
35 Set to ON to force the use of the static libraries.
38 Defines the following variables:
41 Found the Google Protocol Buffers library
42 (libprotobuf & header files)
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``
53 ``Protobuf_LITE_LIBRARIES``
54 The protobuf-lite libraries
57 The following :prop_tgt:`IMPORTED` targets are also defined:
59 ``protobuf::libprotobuf``
61 ``protobuf::libprotobuf-lite``
62 The protobuf lite library.
63 ``protobuf::libprotoc``
66 .. versionadded:: 3.10
69 The following cache variables are also available to set or use:
73 ``Protobuf_PROTOC_LIBRARY``
75 ``Protobuf_INCLUDE_DIR``
76 The include directory for protocol buffers
77 ``Protobuf_PROTOC_EXECUTABLE``
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)
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})
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>
116 [EXPORT_MACRO <macro>]
120 Variable to define with autogenerated source files
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.
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>...])
147 Variable to define with autogenerated Python 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
164 [EXPORT_MACRO <macro>]
165 [PROTOC_OUT_DIR <dir>]
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>]
177 A flag that causes the base path of all proto schema files to be added to
181 A single value: cpp or python. Determines what kind of source files are
182 being generated. Defaults to cpp.
185 Name of a CMake variable that will be filled with the paths to the generated
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``.
196 .. versionadded:: 3.21
198 An optional plugin executable. This could, for example, be the path to
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``
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.
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)
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 #]=======================================================================]
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)
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")
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")
279 if(NOT protobuf_generate_LANGUAGE)
280 set(protobuf_generate_LANGUAGE cpp)
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})
288 if(protobuf_generate_EXPORT_MACRO AND protobuf_generate_LANGUAGE STREQUAL cpp)
289 set(_dll_export_decl "dllexport_decl=${protobuf_generate_EXPORT_MACRO}")
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
296 set( _plugin_options "${_plugin_options},")
298 set(_plugin_options "${_plugin_options}${_option}")
301 if(protobuf_generate_PLUGIN)
302 set(_plugin "--plugin=${protobuf_generate_PLUGIN}")
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)
311 message(SEND_ERROR "Error: protobuf_generate given unknown Language ${LANGUAGE}, please provide a value for GENERATE_EXTENSIONS")
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})
325 if(NOT protobuf_generate_PROTOS)
326 message(SEND_ERROR "Error: protobuf_generate could not find any .proto files")
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.")
336 # Default to using the CMake executable
337 set(protobuf_generate_PROTOC_EXE protobuf::protoc)
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})
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})
360 if(NOT protobuf_generate_APPEND_PATH)
361 list(APPEND _protobuf_include_path -I ${CMAKE_CURRENT_SOURCE_DIR})
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)
379 string(FIND "${_rel_dir}" "../" _is_in_parent_folder)
380 if (NOT ${_is_in_parent_folder} EQUAL 0)
385 set(_possible_rel_dir ${_rel_dir}/)
389 foreach(_ext ${protobuf_generate_GENERATE_EXTENSIONS})
390 list(APPEND _generated_srcs "${protobuf_generate_PROTOC_OUT_DIR}/${_possible_rel_dir}${_basename}${_ext}")
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})
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}")
405 set(_comment "${_comment}, plugin-options: ${_plugin_options}")
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}
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)
421 if(protobuf_generate_TARGET)
422 target_sources(${protobuf_generate_TARGET} PRIVATE ${_generated_srcs_all})
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}")
431 message(SEND_ERROR "Error: PROTOBUF_GENERATE_CPP() called without any proto files")
435 if(PROTOBUF_GENERATE_CPP_APPEND_PATH)
436 set(_append_arg APPEND_PATH)
439 if(protobuf_generate_cpp_DESCRIPTORS)
440 set(_descriptors DESCRIPTORS)
443 if(DEFINED PROTOBUF_IMPORT_DIRS AND NOT DEFINED Protobuf_IMPORT_DIRS)
444 set(Protobuf_IMPORT_DIRS "${PROTOBUF_IMPORT_DIRS}")
447 if(DEFINED Protobuf_IMPORT_DIRS)
448 set(_import_arg IMPORT_DIRS ${Protobuf_IMPORT_DIRS})
452 protobuf_generate(${_append_arg} ${_descriptors} LANGUAGE cpp EXPORT_MACRO ${protobuf_generate_cpp_EXPORT_MACRO} OUT_VAR _outvar ${_import_arg} PROTOS ${_proto_files})
456 if(protobuf_generate_cpp_DESCRIPTORS)
457 set(${protobuf_generate_cpp_DESCRIPTORS})
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})
466 list(APPEND ${HDRS} ${_file})
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)
476 function(PROTOBUF_GENERATE_PYTHON SRCS)
478 message(SEND_ERROR "Error: PROTOBUF_GENERATE_PYTHON() called without any proto files")
482 if(PROTOBUF_GENERATE_CPP_APPEND_PATH)
483 set(_append_arg APPEND_PATH)
486 if(DEFINED PROTOBUF_IMPORT_DIRS AND NOT DEFINED Protobuf_IMPORT_DIRS)
487 set(Protobuf_IMPORT_DIRS "${PROTOBUF_IMPORT_DIRS}")
490 if(DEFINED Protobuf_IMPORT_DIRS)
491 set(_import_arg IMPORT_DIRS ${Protobuf_IMPORT_DIRS})
495 protobuf_generate(${_append_arg} LANGUAGE python OUT_VAR _outvar ${_import_arg} PROTOS ${ARGN})
496 set(${SRCS} ${_outvar} PARENT_SCOPE)
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}")
507 # Backwards compatibility
508 # Define camel case versions of input variables
510 PROTOBUF_SRC_ROOT_FOLDER
514 PROTOBUF_PROTOC_LIBRARY
516 PROTOBUF_PROTOC_EXECUTABLE
517 PROTOBUF_LIBRARY_DEBUG
518 PROTOBUF_PROTOC_LIBRARY_DEBUG
519 PROTOBUF_LITE_LIBRARY
520 PROTOBUF_LITE_LIBRARY_DEBUG
522 if (DEFINED ${UPPER})
523 string(REPLACE "PROTOBUF_" "Protobuf_" Camel ${UPPER})
524 if (NOT DEFINED ${Camel})
525 set(${Camel} ${${UPPER}})
530 if(CMAKE_SIZEOF_VOID_P EQUAL 8)
531 set(_PROTOBUF_ARCH_DIR x64/)
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})
539 set(CMAKE_FIND_LIBRARY_SUFFIXES .lib .a ${CMAKE_FIND_LIBRARY_SUFFIXES})
541 set(CMAKE_FIND_LIBRARY_SUFFIXES .a )
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.
554 elseif(${name}_LIBRARY)
555 # Honor cache entry used by CMake 3.5 and lower.
556 set(${name}_LIBRARIES "${${name}_LIBRARY}" PARENT_SCOPE)
558 find_library(${name}_LIBRARY_RELEASE
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}
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})
576 set(${name}_LIBRARY "${${name}_LIBRARY}" PARENT_SCOPE)
577 set(${name}_LIBRARIES "${${name}_LIBRARIES}" PARENT_SCOPE)
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)
592 # Google's provided vcproj files generate libraries with a "lib"
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)
602 # Protobuf headers may depend on threading.
603 find_package(Threads QUIET)
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
617 set(CMAKE_FIND_LIBRARY_PREFIXES "${Protobuf_ORIG_FIND_LIBRARY_PREFIXES}")
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
630 DOC "The Google Protocol Buffers Compiler"
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)
638 message(STATUS "[ ${CMAKE_CURRENT_LIST_FILE}:${CMAKE_CURRENT_LIST_LINE} ] "
639 "requested version of Google Protobuf is ${Protobuf_FIND_VERSION}")
642 if(Protobuf_INCLUDE_DIR)
643 set(_PROTOBUF_COMMON_HEADER ${Protobuf_INCLUDE_DIR}/google/protobuf/stubs/common.h)
646 message(STATUS "[ ${CMAKE_CURRENT_LIST_FILE}:${CMAKE_CURRENT_LIST_LINE} ] "
647 "location of common.h: ${_PROTOBUF_COMMON_HEADER}")
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}")
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}")
664 message(STATUS "[ ${CMAKE_CURRENT_LIST_FILE}:${CMAKE_CURRENT_LIST_LINE} ] "
665 "${_PROTOBUF_COMMON_HEADER} reveals protobuf ${Protobuf_VERSION}")
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}")
678 message(STATUS "[ ${CMAKE_CURRENT_LIST_FILE}:${CMAKE_CURRENT_LIST_LINE} ] "
679 "${Protobuf_PROTOC_EXECUTABLE} reveals version ${_PROTOBUF_PROTOC_EXECUTABLE_VERSION}")
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}")
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}")
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}")
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}")
711 if (Protobuf_VERSION VERSION_GREATER_EQUAL "3.6")
712 set_property(TARGET protobuf::libprotobuf APPEND PROPERTY
713 INTERFACE_COMPILE_FEATURES cxx_std_11
716 if (WIN32 AND NOT Protobuf_USE_STATIC_LIBS)
717 set_property(TARGET protobuf::libprotobuf APPEND PROPERTY
718 INTERFACE_COMPILE_DEFINITIONS "PROTOBUF_USE_DLLS"
721 if(UNIX AND TARGET Threads::Threads)
722 set_property(TARGET protobuf::libprotobuf APPEND PROPERTY
723 INTERFACE_LINK_LIBRARIES Threads::Threads)
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}")
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}")
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}")
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"
754 if(UNIX AND TARGET Threads::Threads)
755 set_property(TARGET protobuf::libprotobuf-lite APPEND PROPERTY
756 INTERFACE_LINK_LIBRARIES Threads::Threads)
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}")
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}")
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}")
782 if (Protobuf_VERSION VERSION_GREATER_EQUAL "3.6")
783 set_property(TARGET protobuf::libprotoc APPEND PROPERTY
784 INTERFACE_COMPILE_FEATURES cxx_std_11
787 if (WIN32 AND NOT Protobuf_USE_STATIC_LIBS)
788 set_property(TARGET protobuf::libprotoc APPEND PROPERTY
789 INTERFACE_COMPILE_DEFINITIONS "PROTOBUF_USE_DLLS"
792 if(UNIX AND TARGET Threads::Threads)
793 set_property(TARGET protobuf::libprotoc APPEND PROPERTY
794 INTERFACE_LINK_LIBRARIES Threads::Threads)
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}")
810 include(FindPackageHandleStandardArgs)
811 find_package_handle_standard_args(Protobuf
812 REQUIRED_VARS Protobuf_LIBRARIES Protobuf_INCLUDE_DIR
813 VERSION_VAR Protobuf_VERSION
817 set(Protobuf_INCLUDE_DIRS ${Protobuf_INCLUDE_DIR})
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})
825 # Backwards compatibility
826 # Define upper case versions of output variables
828 Protobuf_SRC_ROOT_FOLDER
831 Protobuf_INCLUDE_DIRS
833 Protobuf_PROTOC_LIBRARIES
834 Protobuf_LITE_LIBRARIES
836 Protobuf_PROTOC_LIBRARY
838 Protobuf_PROTOC_EXECUTABLE
839 Protobuf_LIBRARY_DEBUG
840 Protobuf_PROTOC_LIBRARY_DEBUG
841 Protobuf_LITE_LIBRARY
842 Protobuf_LITE_LIBRARY_DEBUG
844 string(TOUPPER ${Camel} UPPER)
845 set(${UPPER} ${${Camel}})