FindProtobuf.cmake 20 KB

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