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 protobuf_generate_cpp (<SRCS> <HDRS>
112 [DESCRIPTORS <DESC>] [EXPORT_MACRO <MACRO>] [<ARGN>...])
115 Variable to define with autogenerated source files
117 Variable to define with autogenerated header files
119 .. versionadded:: 3.10
120 Variable to define with autogenerated descriptor files, if requested.
122 is a macro which should expand to ``__declspec(dllexport)`` or
123 ``__declspec(dllimport)`` depending on what is being compiled.
127 .. command:: protobuf_generate_python
129 .. versionadded:: 3.4
131 Add custom commands to process ``.proto`` files to Python::
133 protobuf_generate_python (<PY> [<ARGN>...])
136 Variable to define with autogenerated Python files
140 .. command:: protobuf_generate
142 .. versionadded:: 3.13
144 Automatically generate source files from ``.proto`` schema files at build time::
150 [EXPORT_MACRO <macro>]
151 [PROTOC_OUT_DIR <dir>]
153 [PLUGIN_OPTIONS <plugin_options>]
154 [DEPENDENCIES <depends]
155 [PROTOS <protobuf_files>]
157 [GENERATE_EXTENSIONS <extensions>]
158 [PROTOC_OPTIONS <protoc_options>]
162 A flag that causes the base path of all proto schema files to be added to
165 A single value: cpp or python. Determines what kind of source files are
166 being generated. Defaults to cpp.
168 Name of a CMake variable that will be filled with the paths to the generated
171 Name of a macro that is applied to all generated Protobuf message classes
172 and extern variables. It can, for example, be used to declare DLL exports.
174 Output directory of generated source files. Defaults to ``CMAKE_CURRENT_BINARY_DIR``.
176 .. versionadded:: 3.21
178 An optional plugin executable. This could, for example, be the path to
181 .. versionadded:: 3.28
183 Additional options provided to the plugin, such as ``generate_mock_code=true``
184 for the gRPC cpp plugin.
186 .. versionadded:: 3.28
188 Arguments forwarded to the ``DEPENDS`` of the underlying ``add_custom_command``
191 CMake target that will have the generated files added as sources.
193 List of proto schema files. If omitted, then every source file ending in *proto* of ``TARGET`` will be used.
195 A common parent directory for the schema files. For example, if the schema file is
196 ``proto/helloworld/helloworld.proto`` and the import directory ``proto/`` then the
197 generated files are ``${PROTOC_OUT_DIR}/helloworld/helloworld.pb.h`` and
198 ``${PROTOC_OUT_DIR}/helloworld/helloworld.pb.cc``.
199 ``GENERATE_EXTENSIONS``
200 If LANGUAGE is omitted then this must be set to the extensions that protoc generates.
202 .. versionadded:: 3.28
204 Additional arguments that are forwarded to protoc.
208 find_package(gRPC CONFIG REQUIRED)
209 find_package(Protobuf REQUIRED)
210 add_library(ProtoTest Test.proto)
211 target_link_libraries(ProtoTest PUBLIC gRPC::grpc++)
212 protobuf_generate(TARGET ProtoTest)
216 PLUGIN protoc-gen-grpc=$<TARGET_FILE:gRPC::grpc_cpp_plugin>
217 PLUGIN_OPTIONS generate_mock_code=true
218 GENERATE_EXTENSIONS .grpc.pb.h .grpc.pb.cc)
219 #]=======================================================================]
222 cmake_policy(SET CMP0159 NEW) # file(STRINGS) with REGEX updates CMAKE_MATCH_<n>
224 function(protobuf_generate)
225 set(_options APPEND_PATH DESCRIPTORS)
226 set(_singleargs LANGUAGE OUT_VAR EXPORT_MACRO PROTOC_OUT_DIR PLUGIN PLUGIN_OPTIONS DEPENDENCIES)
227 if(COMMAND target_sources)
228 list(APPEND _singleargs TARGET)
230 set(_multiargs PROTOS IMPORT_DIRS GENERATE_EXTENSIONS PROTOC_OPTIONS)
232 cmake_parse_arguments(protobuf_generate "${_options}" "${_singleargs}" "${_multiargs}" "${ARGN}")
234 if(NOT protobuf_generate_PROTOS AND NOT protobuf_generate_TARGET)
235 message(SEND_ERROR "Error: protobuf_generate called without any targets or source files")
239 if(NOT protobuf_generate_OUT_VAR AND NOT protobuf_generate_TARGET)
240 message(SEND_ERROR "Error: protobuf_generate called without a target or output variable")
244 if(NOT protobuf_generate_LANGUAGE)
245 set(protobuf_generate_LANGUAGE cpp)
247 string(TOLOWER ${protobuf_generate_LANGUAGE} protobuf_generate_LANGUAGE)
249 if(NOT protobuf_generate_PROTOC_OUT_DIR)
250 set(protobuf_generate_PROTOC_OUT_DIR ${CMAKE_CURRENT_BINARY_DIR})
253 if(protobuf_generate_EXPORT_MACRO AND protobuf_generate_LANGUAGE STREQUAL cpp)
254 set(_dll_export_decl "dllexport_decl=${protobuf_generate_EXPORT_MACRO}")
257 foreach(_option ${_dll_export_decl} ${protobuf_generate_PLUGIN_OPTIONS})
258 # append comma - not using CMake lists and string replacement as users
259 # might have semicolons in options
261 set( _plugin_options "${_plugin_options},")
263 set(_plugin_options "${_plugin_options}${_option}")
266 if(protobuf_generate_PLUGIN)
267 set(_plugin "--plugin=${protobuf_generate_PLUGIN}")
270 if(NOT protobuf_generate_GENERATE_EXTENSIONS)
271 if(protobuf_generate_LANGUAGE STREQUAL cpp)
272 set(protobuf_generate_GENERATE_EXTENSIONS .pb.h .pb.cc)
273 elseif(protobuf_generate_LANGUAGE STREQUAL python)
274 set(protobuf_generate_GENERATE_EXTENSIONS _pb2.py)
276 message(SEND_ERROR "Error: protobuf_generate given unknown Language ${LANGUAGE}, please provide a value for GENERATE_EXTENSIONS")
281 if(protobuf_generate_TARGET)
282 get_target_property(_source_list ${protobuf_generate_TARGET} SOURCES)
283 foreach(_file ${_source_list})
284 if(_file MATCHES "proto$")
285 list(APPEND protobuf_generate_PROTOS ${_file})
290 if(NOT protobuf_generate_PROTOS)
291 message(SEND_ERROR "Error: protobuf_generate could not find any .proto files")
295 if(protobuf_generate_APPEND_PATH)
296 # Create an include path for each file specified
297 foreach(_file ${protobuf_generate_PROTOS})
298 get_filename_component(_abs_file ${_file} ABSOLUTE)
299 get_filename_component(_abs_dir ${_abs_file} DIRECTORY)
300 list(FIND _protobuf_include_path ${_abs_dir} _contains_already)
301 if(${_contains_already} EQUAL -1)
302 list(APPEND _protobuf_include_path -I ${_abs_dir})
306 set(_protobuf_include_path -I ${CMAKE_CURRENT_SOURCE_DIR})
309 foreach(DIR ${protobuf_generate_IMPORT_DIRS})
310 get_filename_component(ABS_PATH ${DIR} ABSOLUTE)
311 list(FIND _protobuf_include_path ${ABS_PATH} _contains_already)
312 if(${_contains_already} EQUAL -1)
313 list(APPEND _protobuf_include_path -I ${ABS_PATH})
317 set(_generated_srcs_all)
318 foreach(_proto ${protobuf_generate_PROTOS})
319 get_filename_component(_abs_file ${_proto} ABSOLUTE)
320 get_filename_component(_abs_dir ${_abs_file} DIRECTORY)
321 get_filename_component(_basename ${_proto} NAME_WLE)
322 file(RELATIVE_PATH _rel_dir ${CMAKE_CURRENT_SOURCE_DIR} ${_abs_dir})
324 set(_possible_rel_dir)
325 if (NOT protobuf_generate_APPEND_PATH)
326 set(_possible_rel_dir ${_rel_dir}/)
330 foreach(_ext ${protobuf_generate_GENERATE_EXTENSIONS})
331 list(APPEND _generated_srcs "${protobuf_generate_PROTOC_OUT_DIR}/${_possible_rel_dir}${_basename}${_ext}")
334 if(protobuf_generate_DESCRIPTORS AND protobuf_generate_LANGUAGE STREQUAL cpp)
335 set(_descriptor_file "${CMAKE_CURRENT_BINARY_DIR}/${_basename}.desc")
336 set(_dll_desc_out "--descriptor_set_out=${_descriptor_file}")
337 list(APPEND _generated_srcs ${_descriptor_file})
339 list(APPEND _generated_srcs_all ${_generated_srcs})
341 set(_comment "Running ${protobuf_generate_LANGUAGE} protocol buffer compiler on ${_proto}")
342 if(protobuf_generate_PROTOC_OPTIONS)
343 set(_comment "${_comment}, protoc-options: ${protobuf_generate_PROTOC_OPTIONS}")
346 set(_comment "${_comment}, plugin-options: ${_plugin_options}")
350 OUTPUT ${_generated_srcs}
351 COMMAND protobuf::protoc
352 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}
353 DEPENDS ${_abs_file} protobuf::protoc ${protobuf_generate_DEPENDENCIES}
358 set_source_files_properties(${_generated_srcs_all} PROPERTIES GENERATED TRUE)
359 if(protobuf_generate_OUT_VAR)
360 set(${protobuf_generate_OUT_VAR} ${_generated_srcs_all} PARENT_SCOPE)
362 if(protobuf_generate_TARGET)
363 target_sources(${protobuf_generate_TARGET} PRIVATE ${_generated_srcs_all})
367 function(PROTOBUF_GENERATE_CPP SRCS HDRS)
368 cmake_parse_arguments(protobuf_generate_cpp "" "EXPORT_MACRO;DESCRIPTORS" "" ${ARGN})
370 set(_proto_files "${protobuf_generate_cpp_UNPARSED_ARGUMENTS}")
372 message(SEND_ERROR "Error: PROTOBUF_GENERATE_CPP() called without any proto files")
376 if(PROTOBUF_GENERATE_CPP_APPEND_PATH)
377 set(_append_arg APPEND_PATH)
380 if(protobuf_generate_cpp_DESCRIPTORS)
381 set(_descriptors DESCRIPTORS)
384 if(DEFINED PROTOBUF_IMPORT_DIRS AND NOT DEFINED Protobuf_IMPORT_DIRS)
385 set(Protobuf_IMPORT_DIRS "${PROTOBUF_IMPORT_DIRS}")
388 if(DEFINED Protobuf_IMPORT_DIRS)
389 set(_import_arg IMPORT_DIRS ${Protobuf_IMPORT_DIRS})
393 protobuf_generate(${_append_arg} ${_descriptors} LANGUAGE cpp EXPORT_MACRO ${protobuf_generate_cpp_EXPORT_MACRO} OUT_VAR _outvar ${_import_arg} PROTOS ${_proto_files})
397 if(protobuf_generate_cpp_DESCRIPTORS)
398 set(${protobuf_generate_cpp_DESCRIPTORS})
401 foreach(_file ${_outvar})
402 if(_file MATCHES "cc$")
403 list(APPEND ${SRCS} ${_file})
404 elseif(_file MATCHES "desc$")
405 list(APPEND ${protobuf_generate_cpp_DESCRIPTORS} ${_file})
407 list(APPEND ${HDRS} ${_file})
410 set(${SRCS} ${${SRCS}} PARENT_SCOPE)
411 set(${HDRS} ${${HDRS}} PARENT_SCOPE)
412 if(protobuf_generate_cpp_DESCRIPTORS)
413 set(${protobuf_generate_cpp_DESCRIPTORS} "${${protobuf_generate_cpp_DESCRIPTORS}}" PARENT_SCOPE)
417 function(PROTOBUF_GENERATE_PYTHON SRCS)
419 message(SEND_ERROR "Error: PROTOBUF_GENERATE_PYTHON() called without any proto files")
423 if(PROTOBUF_GENERATE_CPP_APPEND_PATH)
424 set(_append_arg APPEND_PATH)
427 if(DEFINED PROTOBUF_IMPORT_DIRS AND NOT DEFINED Protobuf_IMPORT_DIRS)
428 set(Protobuf_IMPORT_DIRS "${PROTOBUF_IMPORT_DIRS}")
431 if(DEFINED Protobuf_IMPORT_DIRS)
432 set(_import_arg IMPORT_DIRS ${Protobuf_IMPORT_DIRS})
436 protobuf_generate(${_append_arg} LANGUAGE python OUT_VAR _outvar ${_import_arg} PROTOS ${ARGN})
437 set(${SRCS} ${_outvar} PARENT_SCOPE)
442 # Output some of their choices
443 message(STATUS "[ ${CMAKE_CURRENT_LIST_FILE}:${CMAKE_CURRENT_LIST_LINE} ] "
444 "Protobuf_USE_STATIC_LIBS = ${Protobuf_USE_STATIC_LIBS}")
448 # Backwards compatibility
449 # Define camel case versions of input variables
451 PROTOBUF_SRC_ROOT_FOLDER
455 PROTOBUF_PROTOC_LIBRARY
457 PROTOBUF_PROTOC_EXECUTABLE
458 PROTOBUF_LIBRARY_DEBUG
459 PROTOBUF_PROTOC_LIBRARY_DEBUG
460 PROTOBUF_LITE_LIBRARY
461 PROTOBUF_LITE_LIBRARY_DEBUG
463 if (DEFINED ${UPPER})
464 string(REPLACE "PROTOBUF_" "Protobuf_" Camel ${UPPER})
465 if (NOT DEFINED ${Camel})
466 set(${Camel} ${${UPPER}})
471 if(CMAKE_SIZEOF_VOID_P EQUAL 8)
472 set(_PROTOBUF_ARCH_DIR x64/)
476 # Support preference of static libs by adjusting CMAKE_FIND_LIBRARY_SUFFIXES
477 if( Protobuf_USE_STATIC_LIBS )
478 set( _protobuf_ORIG_CMAKE_FIND_LIBRARY_SUFFIXES ${CMAKE_FIND_LIBRARY_SUFFIXES})
480 set(CMAKE_FIND_LIBRARY_SUFFIXES .lib .a ${CMAKE_FIND_LIBRARY_SUFFIXES})
482 set(CMAKE_FIND_LIBRARY_SUFFIXES .a )
486 include(${CMAKE_CURRENT_LIST_DIR}/SelectLibraryConfigurations.cmake)
488 # Internal function: search for normal library as well as a debug one
489 # if the debug one is specified also include debug/optimized keywords
490 # in *_LIBRARIES variable
491 function(_protobuf_find_libraries name filename)
492 if(${name}_LIBRARIES)
493 # Use result recorded by a previous call.
495 elseif(${name}_LIBRARY)
496 # Honor cache entry used by CMake 3.5 and lower.
497 set(${name}_LIBRARIES "${${name}_LIBRARY}" PARENT_SCOPE)
499 find_library(${name}_LIBRARY_RELEASE
502 PATHS ${Protobuf_SRC_ROOT_FOLDER}/vsprojects/${_PROTOBUF_ARCH_DIR}Release)
503 mark_as_advanced(${name}_LIBRARY_RELEASE)
505 find_library(${name}_LIBRARY_DEBUG
506 NAMES ${filename}d ${filename}
508 PATHS ${Protobuf_SRC_ROOT_FOLDER}/vsprojects/${_PROTOBUF_ARCH_DIR}Debug)
509 mark_as_advanced(${name}_LIBRARY_DEBUG)
511 select_library_configurations(${name})
513 if(UNIX AND Threads_FOUND AND ${name}_LIBRARY)
514 list(APPEND ${name}_LIBRARIES ${CMAKE_THREAD_LIBS_INIT})
517 set(${name}_LIBRARY "${${name}_LIBRARY}" PARENT_SCOPE)
518 set(${name}_LIBRARIES "${${name}_LIBRARIES}" PARENT_SCOPE)
526 # By default have PROTOBUF_GENERATE_CPP macro pass -I to protoc
527 # for each directory where a proto file is referenced.
528 if(NOT DEFINED PROTOBUF_GENERATE_CPP_APPEND_PATH)
529 set(PROTOBUF_GENERATE_CPP_APPEND_PATH TRUE)
533 # Google's provided vcproj files generate libraries with a "lib"
536 set(Protobuf_ORIG_FIND_LIBRARY_PREFIXES "${CMAKE_FIND_LIBRARY_PREFIXES}")
537 set(CMAKE_FIND_LIBRARY_PREFIXES "lib" "")
539 find_path(Protobuf_SRC_ROOT_FOLDER protobuf.pc.in)
543 # Protobuf headers may depend on threading.
544 find_package(Threads QUIET)
547 # The Protobuf library
548 _protobuf_find_libraries(Protobuf protobuf)
549 #DOC "The Google Protocol Buffers RELEASE Library"
551 _protobuf_find_libraries(Protobuf_LITE protobuf-lite)
553 # The Protobuf Protoc Library
554 _protobuf_find_libraries(Protobuf_PROTOC protoc)
556 # Restore original find library prefixes
558 set(CMAKE_FIND_LIBRARY_PREFIXES "${Protobuf_ORIG_FIND_LIBRARY_PREFIXES}")
561 # Find the include directory
562 find_path(Protobuf_INCLUDE_DIR
563 google/protobuf/service.h
564 PATHS ${Protobuf_SRC_ROOT_FOLDER}/src
566 mark_as_advanced(Protobuf_INCLUDE_DIR)
568 # Find the protoc Executable
569 find_program(Protobuf_PROTOC_EXECUTABLE
571 DOC "The Google Protocol Buffers Compiler"
573 ${Protobuf_SRC_ROOT_FOLDER}/vsprojects/${_PROTOBUF_ARCH_DIR}Release
574 ${Protobuf_SRC_ROOT_FOLDER}/vsprojects/${_PROTOBUF_ARCH_DIR}Debug
576 mark_as_advanced(Protobuf_PROTOC_EXECUTABLE)
579 message(STATUS "[ ${CMAKE_CURRENT_LIST_FILE}:${CMAKE_CURRENT_LIST_LINE} ] "
580 "requested version of Google Protobuf is ${Protobuf_FIND_VERSION}")
583 if(Protobuf_INCLUDE_DIR)
584 set(_PROTOBUF_COMMON_HEADER ${Protobuf_INCLUDE_DIR}/google/protobuf/stubs/common.h)
587 message(STATUS "[ ${CMAKE_CURRENT_LIST_FILE}:${CMAKE_CURRENT_LIST_LINE} ] "
588 "location of common.h: ${_PROTOBUF_COMMON_HEADER}")
591 set(Protobuf_VERSION "")
592 set(Protobuf_LIB_VERSION "")
593 file(STRINGS ${_PROTOBUF_COMMON_HEADER} _PROTOBUF_COMMON_H_CONTENTS REGEX "#define[ \t]+GOOGLE_PROTOBUF_VERSION[ \t]+")
594 if(_PROTOBUF_COMMON_H_CONTENTS MATCHES "#define[ \t]+GOOGLE_PROTOBUF_VERSION[ \t]+([0-9]+)")
595 set(Protobuf_LIB_VERSION "${CMAKE_MATCH_1}")
597 unset(_PROTOBUF_COMMON_H_CONTENTS)
599 math(EXPR _PROTOBUF_MAJOR_VERSION "${Protobuf_LIB_VERSION} / 1000000")
600 math(EXPR _PROTOBUF_MINOR_VERSION "${Protobuf_LIB_VERSION} / 1000 % 1000")
601 math(EXPR _PROTOBUF_SUBMINOR_VERSION "${Protobuf_LIB_VERSION} % 1000")
602 set(Protobuf_VERSION "${_PROTOBUF_MAJOR_VERSION}.${_PROTOBUF_MINOR_VERSION}.${_PROTOBUF_SUBMINOR_VERSION}")
605 message(STATUS "[ ${CMAKE_CURRENT_LIST_FILE}:${CMAKE_CURRENT_LIST_LINE} ] "
606 "${_PROTOBUF_COMMON_HEADER} reveals protobuf ${Protobuf_VERSION}")
609 if(Protobuf_PROTOC_EXECUTABLE)
610 # Check Protobuf compiler version to be aligned with libraries version
611 execute_process(COMMAND ${Protobuf_PROTOC_EXECUTABLE} --version
612 OUTPUT_VARIABLE _PROTOBUF_PROTOC_EXECUTABLE_VERSION)
614 if("${_PROTOBUF_PROTOC_EXECUTABLE_VERSION}" MATCHES "libprotoc ([0-9.]+)")
615 set(_PROTOBUF_PROTOC_EXECUTABLE_VERSION "${CMAKE_MATCH_1}")
619 message(STATUS "[ ${CMAKE_CURRENT_LIST_FILE}:${CMAKE_CURRENT_LIST_LINE} ] "
620 "${Protobuf_PROTOC_EXECUTABLE} reveals version ${_PROTOBUF_PROTOC_EXECUTABLE_VERSION}")
623 # protoc version 22 and up don't print the major version any more
624 if(NOT "${_PROTOBUF_PROTOC_EXECUTABLE_VERSION}" VERSION_EQUAL "${Protobuf_VERSION}" AND
625 NOT "${_PROTOBUF_PROTOC_EXECUTABLE_VERSION}" VERSION_EQUAL "${_PROTOBUF_MINOR_VERSION}.${_PROTOBUF_SUBMINOR_VERSION}")
626 message(WARNING "Protobuf compiler version ${_PROTOBUF_PROTOC_EXECUTABLE_VERSION}"
627 " doesn't match library version ${Protobuf_VERSION}")
632 if(NOT TARGET protobuf::libprotobuf)
633 add_library(protobuf::libprotobuf UNKNOWN IMPORTED)
634 set_target_properties(protobuf::libprotobuf PROPERTIES
635 INTERFACE_INCLUDE_DIRECTORIES "${Protobuf_INCLUDE_DIR}")
636 if(EXISTS "${Protobuf_LIBRARY}")
637 set_target_properties(protobuf::libprotobuf PROPERTIES
638 IMPORTED_LOCATION "${Protobuf_LIBRARY}")
640 if(EXISTS "${Protobuf_LIBRARY_RELEASE}")
641 set_property(TARGET protobuf::libprotobuf APPEND PROPERTY
642 IMPORTED_CONFIGURATIONS RELEASE)
643 set_target_properties(protobuf::libprotobuf PROPERTIES
644 IMPORTED_LOCATION_RELEASE "${Protobuf_LIBRARY_RELEASE}")
646 if(EXISTS "${Protobuf_LIBRARY_DEBUG}")
647 set_property(TARGET protobuf::libprotobuf APPEND PROPERTY
648 IMPORTED_CONFIGURATIONS DEBUG)
649 set_target_properties(protobuf::libprotobuf PROPERTIES
650 IMPORTED_LOCATION_DEBUG "${Protobuf_LIBRARY_DEBUG}")
652 if (Protobuf_VERSION VERSION_GREATER_EQUAL "3.6")
653 set_property(TARGET protobuf::libprotobuf APPEND PROPERTY
654 INTERFACE_COMPILE_FEATURES cxx_std_11
657 if (WIN32 AND NOT Protobuf_USE_STATIC_LIBS)
658 set_property(TARGET protobuf::libprotobuf APPEND PROPERTY
659 INTERFACE_COMPILE_DEFINITIONS "PROTOBUF_USE_DLLS"
662 if(UNIX AND TARGET Threads::Threads)
663 set_property(TARGET protobuf::libprotobuf APPEND PROPERTY
664 INTERFACE_LINK_LIBRARIES Threads::Threads)
669 if(Protobuf_LITE_LIBRARY)
670 if(NOT TARGET protobuf::libprotobuf-lite)
671 add_library(protobuf::libprotobuf-lite UNKNOWN IMPORTED)
672 set_target_properties(protobuf::libprotobuf-lite PROPERTIES
673 INTERFACE_INCLUDE_DIRECTORIES "${Protobuf_INCLUDE_DIR}")
674 if(EXISTS "${Protobuf_LITE_LIBRARY}")
675 set_target_properties(protobuf::libprotobuf-lite PROPERTIES
676 IMPORTED_LOCATION "${Protobuf_LITE_LIBRARY}")
678 if(EXISTS "${Protobuf_LITE_LIBRARY_RELEASE}")
679 set_property(TARGET protobuf::libprotobuf-lite APPEND PROPERTY
680 IMPORTED_CONFIGURATIONS RELEASE)
681 set_target_properties(protobuf::libprotobuf-lite PROPERTIES
682 IMPORTED_LOCATION_RELEASE "${Protobuf_LITE_LIBRARY_RELEASE}")
684 if(EXISTS "${Protobuf_LITE_LIBRARY_DEBUG}")
685 set_property(TARGET protobuf::libprotobuf-lite APPEND PROPERTY
686 IMPORTED_CONFIGURATIONS DEBUG)
687 set_target_properties(protobuf::libprotobuf-lite PROPERTIES
688 IMPORTED_LOCATION_DEBUG "${Protobuf_LITE_LIBRARY_DEBUG}")
690 if (WIN32 AND NOT Protobuf_USE_STATIC_LIBS)
691 set_property(TARGET protobuf::libprotobuf-lite APPEND PROPERTY
692 INTERFACE_COMPILE_DEFINITIONS "PROTOBUF_USE_DLLS"
695 if(UNIX AND TARGET Threads::Threads)
696 set_property(TARGET protobuf::libprotobuf-lite APPEND PROPERTY
697 INTERFACE_LINK_LIBRARIES Threads::Threads)
702 if(Protobuf_PROTOC_LIBRARY)
703 if(NOT TARGET protobuf::libprotoc)
704 add_library(protobuf::libprotoc UNKNOWN IMPORTED)
705 set_target_properties(protobuf::libprotoc PROPERTIES
706 INTERFACE_INCLUDE_DIRECTORIES "${Protobuf_INCLUDE_DIR}")
707 if(EXISTS "${Protobuf_PROTOC_LIBRARY}")
708 set_target_properties(protobuf::libprotoc PROPERTIES
709 IMPORTED_LOCATION "${Protobuf_PROTOC_LIBRARY}")
711 if(EXISTS "${Protobuf_PROTOC_LIBRARY_RELEASE}")
712 set_property(TARGET protobuf::libprotoc APPEND PROPERTY
713 IMPORTED_CONFIGURATIONS RELEASE)
714 set_target_properties(protobuf::libprotoc PROPERTIES
715 IMPORTED_LOCATION_RELEASE "${Protobuf_PROTOC_LIBRARY_RELEASE}")
717 if(EXISTS "${Protobuf_PROTOC_LIBRARY_DEBUG}")
718 set_property(TARGET protobuf::libprotoc APPEND PROPERTY
719 IMPORTED_CONFIGURATIONS DEBUG)
720 set_target_properties(protobuf::libprotoc PROPERTIES
721 IMPORTED_LOCATION_DEBUG "${Protobuf_PROTOC_LIBRARY_DEBUG}")
723 if (Protobuf_VERSION VERSION_GREATER_EQUAL "3.6")
724 set_property(TARGET protobuf::libprotoc APPEND PROPERTY
725 INTERFACE_COMPILE_FEATURES cxx_std_11
728 if (WIN32 AND NOT Protobuf_USE_STATIC_LIBS)
729 set_property(TARGET protobuf::libprotoc APPEND PROPERTY
730 INTERFACE_COMPILE_DEFINITIONS "PROTOBUF_USE_DLLS"
733 if(UNIX AND TARGET Threads::Threads)
734 set_property(TARGET protobuf::libprotoc APPEND PROPERTY
735 INTERFACE_LINK_LIBRARIES Threads::Threads)
740 if(Protobuf_PROTOC_EXECUTABLE)
741 if(NOT TARGET protobuf::protoc)
742 add_executable(protobuf::protoc IMPORTED)
743 if(EXISTS "${Protobuf_PROTOC_EXECUTABLE}")
744 set_target_properties(protobuf::protoc PROPERTIES
745 IMPORTED_LOCATION "${Protobuf_PROTOC_EXECUTABLE}")
751 include(${CMAKE_CURRENT_LIST_DIR}/FindPackageHandleStandardArgs.cmake)
752 FIND_PACKAGE_HANDLE_STANDARD_ARGS(Protobuf
753 REQUIRED_VARS Protobuf_LIBRARIES Protobuf_INCLUDE_DIR
754 VERSION_VAR Protobuf_VERSION
758 set(Protobuf_INCLUDE_DIRS ${Protobuf_INCLUDE_DIR})
761 # Restore the original find library ordering
762 if( Protobuf_USE_STATIC_LIBS )
763 set(CMAKE_FIND_LIBRARY_SUFFIXES ${_protobuf_ORIG_CMAKE_FIND_LIBRARY_SUFFIXES})
766 # Backwards compatibility
767 # Define upper case versions of output variables
769 Protobuf_SRC_ROOT_FOLDER
772 Protobuf_INCLUDE_DIRS
774 Protobuf_PROTOC_LIBRARIES
775 Protobuf_LITE_LIBRARIES
777 Protobuf_PROTOC_LIBRARY
779 Protobuf_PROTOC_EXECUTABLE
780 Protobuf_LIBRARY_DEBUG
781 Protobuf_PROTOC_LIBRARY_DEBUG
782 Protobuf_LITE_LIBRARY
783 Protobuf_LITE_LIBRARY_DEBUG
785 string(TOUPPER ${Camel} UPPER)
786 set(${UPPER} ${${Camel}})