FindProtobuf.cmake 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603
  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 AND NOT DEFINED Protobuf_IMPORT_DIRS)
  227. set(Protobuf_IMPORT_DIRS "${PROTOBUF_IMPORT_DIRS}")
  228. endif()
  229. if(DEFINED Protobuf_IMPORT_DIRS)
  230. set(_import_arg IMPORT_DIRS ${Protobuf_IMPORT_DIRS})
  231. endif()
  232. set(_outvar)
  233. protobuf_generate(${_append_arg} LANGUAGE cpp EXPORT_MACRO ${protobuf_generate_cpp_EXPORT_MACRO} OUT_VAR _outvar ${_import_arg} PROTOS ${_proto_files})
  234. set(${SRCS})
  235. set(${HDRS})
  236. foreach(_file ${_outvar})
  237. if(_file MATCHES "cc$")
  238. list(APPEND ${SRCS} ${_file})
  239. else()
  240. list(APPEND ${HDRS} ${_file})
  241. endif()
  242. endforeach()
  243. set(${SRCS} ${${SRCS}} PARENT_SCOPE)
  244. set(${HDRS} ${${HDRS}} PARENT_SCOPE)
  245. endfunction()
  246. function(PROTOBUF_GENERATE_PYTHON SRCS)
  247. if(NOT ARGN)
  248. message(SEND_ERROR "Error: PROTOBUF_GENERATE_PYTHON() called without any proto files")
  249. return()
  250. endif()
  251. if(PROTOBUF_GENERATE_CPP_APPEND_PATH)
  252. set(_append_arg APPEND_PATH)
  253. endif()
  254. if(DEFINED PROTOBUF_IMPORT_DIRS AND NOT DEFINED Protobuf_IMPORT_DIRS)
  255. set(Protobuf_IMPORT_DIRS "${PROTOBUF_IMPORT_DIRS}")
  256. endif()
  257. if(DEFINED Protobuf_IMPORT_DIRS)
  258. set(_import_arg IMPORT_DIRS ${Protobuf_IMPORT_DIRS})
  259. endif()
  260. set(_outvar)
  261. protobuf_generate(${_append_arg} LANGUAGE python OUT_VAR _outvar ${_import_arg} PROTOS ${ARGN})
  262. set(${SRCS} ${_outvar} PARENT_SCOPE)
  263. endfunction()
  264. if(Protobuf_DEBUG)
  265. # Output some of their choices
  266. message(STATUS "[ ${CMAKE_CURRENT_LIST_FILE}:${CMAKE_CURRENT_LIST_LINE} ] "
  267. "Protobuf_USE_STATIC_LIBS = ${Protobuf_USE_STATIC_LIBS}")
  268. endif()
  269. # Backwards compatibility
  270. # Define camel case versions of input variables
  271. foreach(UPPER
  272. PROTOBUF_SRC_ROOT_FOLDER
  273. PROTOBUF_IMPORT_DIRS
  274. PROTOBUF_DEBUG
  275. PROTOBUF_LIBRARY
  276. PROTOBUF_PROTOC_LIBRARY
  277. PROTOBUF_INCLUDE_DIR
  278. PROTOBUF_PROTOC_EXECUTABLE
  279. PROTOBUF_LIBRARY_DEBUG
  280. PROTOBUF_PROTOC_LIBRARY_DEBUG
  281. PROTOBUF_LITE_LIBRARY
  282. PROTOBUF_LITE_LIBRARY_DEBUG
  283. )
  284. if (DEFINED ${UPPER})
  285. string(REPLACE "PROTOBUF_" "Protobuf_" Camel ${UPPER})
  286. if (NOT DEFINED ${Camel})
  287. set(${Camel} ${${UPPER}})
  288. endif()
  289. endif()
  290. endforeach()
  291. if(CMAKE_SIZEOF_VOID_P EQUAL 8)
  292. set(_PROTOBUF_ARCH_DIR x64/)
  293. endif()
  294. # Support preference of static libs by adjusting CMAKE_FIND_LIBRARY_SUFFIXES
  295. if( Protobuf_USE_STATIC_LIBS )
  296. set( _protobuf_ORIG_CMAKE_FIND_LIBRARY_SUFFIXES ${CMAKE_FIND_LIBRARY_SUFFIXES})
  297. if(WIN32)
  298. set(CMAKE_FIND_LIBRARY_SUFFIXES .lib .a ${CMAKE_FIND_LIBRARY_SUFFIXES})
  299. else()
  300. set(CMAKE_FIND_LIBRARY_SUFFIXES .a )
  301. endif()
  302. endif()
  303. include(${CMAKE_CURRENT_LIST_DIR}/SelectLibraryConfigurations.cmake)
  304. # Internal function: search for normal library as well as a debug one
  305. # if the debug one is specified also include debug/optimized keywords
  306. # in *_LIBRARIES variable
  307. function(_protobuf_find_libraries name filename)
  308. if(${name}_LIBRARIES)
  309. # Use result recorded by a previous call.
  310. return()
  311. elseif(${name}_LIBRARY)
  312. # Honor cache entry used by CMake 3.5 and lower.
  313. set(${name}_LIBRARIES "${${name}_LIBRARY}" PARENT_SCOPE)
  314. else()
  315. find_library(${name}_LIBRARY_RELEASE
  316. NAMES ${filename}
  317. PATHS ${Protobuf_SRC_ROOT_FOLDER}/vsprojects/${_PROTOBUF_ARCH_DIR}Release)
  318. mark_as_advanced(${name}_LIBRARY_RELEASE)
  319. find_library(${name}_LIBRARY_DEBUG
  320. NAMES ${filename}d ${filename}
  321. PATHS ${Protobuf_SRC_ROOT_FOLDER}/vsprojects/${_PROTOBUF_ARCH_DIR}Debug)
  322. mark_as_advanced(${name}_LIBRARY_DEBUG)
  323. select_library_configurations(${name})
  324. set(${name}_LIBRARY "${${name}_LIBRARY}" PARENT_SCOPE)
  325. set(${name}_LIBRARIES "${${name}_LIBRARIES}" PARENT_SCOPE)
  326. endif()
  327. endfunction()
  328. # Internal function: find threads library
  329. function(_protobuf_find_threads)
  330. set(CMAKE_THREAD_PREFER_PTHREAD TRUE)
  331. find_package(Threads)
  332. if(Threads_FOUND)
  333. list(APPEND Protobuf_LIBRARIES ${CMAKE_THREAD_LIBS_INIT})
  334. set(Protobuf_LIBRARIES "${Protobuf_LIBRARIES}" PARENT_SCOPE)
  335. endif()
  336. endfunction()
  337. #
  338. # Main.
  339. #
  340. # By default have PROTOBUF_GENERATE_CPP macro pass -I to protoc
  341. # for each directory where a proto file is referenced.
  342. if(NOT DEFINED PROTOBUF_GENERATE_CPP_APPEND_PATH)
  343. set(PROTOBUF_GENERATE_CPP_APPEND_PATH TRUE)
  344. endif()
  345. # Google's provided vcproj files generate libraries with a "lib"
  346. # prefix on Windows
  347. if(MSVC)
  348. set(Protobuf_ORIG_FIND_LIBRARY_PREFIXES "${CMAKE_FIND_LIBRARY_PREFIXES}")
  349. set(CMAKE_FIND_LIBRARY_PREFIXES "lib" "")
  350. find_path(Protobuf_SRC_ROOT_FOLDER protobuf.pc.in)
  351. endif()
  352. # The Protobuf library
  353. _protobuf_find_libraries(Protobuf protobuf)
  354. #DOC "The Google Protocol Buffers RELEASE Library"
  355. _protobuf_find_libraries(Protobuf_LITE protobuf-lite)
  356. # The Protobuf Protoc Library
  357. _protobuf_find_libraries(Protobuf_PROTOC protoc)
  358. # Restore original find library prefixes
  359. if(MSVC)
  360. set(CMAKE_FIND_LIBRARY_PREFIXES "${Protobuf_ORIG_FIND_LIBRARY_PREFIXES}")
  361. endif()
  362. if(UNIX)
  363. _protobuf_find_threads()
  364. endif()
  365. # Find the include directory
  366. find_path(Protobuf_INCLUDE_DIR
  367. google/protobuf/service.h
  368. PATHS ${Protobuf_SRC_ROOT_FOLDER}/src
  369. )
  370. mark_as_advanced(Protobuf_INCLUDE_DIR)
  371. # Find the protoc Executable
  372. find_program(Protobuf_PROTOC_EXECUTABLE
  373. NAMES protoc
  374. DOC "The Google Protocol Buffers Compiler"
  375. PATHS
  376. ${Protobuf_SRC_ROOT_FOLDER}/vsprojects/${_PROTOBUF_ARCH_DIR}Release
  377. ${Protobuf_SRC_ROOT_FOLDER}/vsprojects/${_PROTOBUF_ARCH_DIR}Debug
  378. )
  379. mark_as_advanced(Protobuf_PROTOC_EXECUTABLE)
  380. if(Protobuf_DEBUG)
  381. message(STATUS "[ ${CMAKE_CURRENT_LIST_FILE}:${CMAKE_CURRENT_LIST_LINE} ] "
  382. "requested version of Google Protobuf is ${Protobuf_FIND_VERSION}")
  383. endif()
  384. if(Protobuf_INCLUDE_DIR)
  385. set(_PROTOBUF_COMMON_HEADER ${Protobuf_INCLUDE_DIR}/google/protobuf/stubs/common.h)
  386. if(Protobuf_DEBUG)
  387. message(STATUS "[ ${CMAKE_CURRENT_LIST_FILE}:${CMAKE_CURRENT_LIST_LINE} ] "
  388. "location of common.h: ${_PROTOBUF_COMMON_HEADER}")
  389. endif()
  390. set(Protobuf_VERSION "")
  391. set(Protobuf_LIB_VERSION "")
  392. file(STRINGS ${_PROTOBUF_COMMON_HEADER} _PROTOBUF_COMMON_H_CONTENTS REGEX "#define[ \t]+GOOGLE_PROTOBUF_VERSION[ \t]+")
  393. if(_PROTOBUF_COMMON_H_CONTENTS MATCHES "#define[ \t]+GOOGLE_PROTOBUF_VERSION[ \t]+([0-9]+)")
  394. set(Protobuf_LIB_VERSION "${CMAKE_MATCH_1}")
  395. endif()
  396. unset(_PROTOBUF_COMMON_H_CONTENTS)
  397. math(EXPR _PROTOBUF_MAJOR_VERSION "${Protobuf_LIB_VERSION} / 1000000")
  398. math(EXPR _PROTOBUF_MINOR_VERSION "${Protobuf_LIB_VERSION} / 1000 % 1000")
  399. math(EXPR _PROTOBUF_SUBMINOR_VERSION "${Protobuf_LIB_VERSION} % 1000")
  400. set(Protobuf_VERSION "${_PROTOBUF_MAJOR_VERSION}.${_PROTOBUF_MINOR_VERSION}.${_PROTOBUF_SUBMINOR_VERSION}")
  401. if(Protobuf_DEBUG)
  402. message(STATUS "[ ${CMAKE_CURRENT_LIST_FILE}:${CMAKE_CURRENT_LIST_LINE} ] "
  403. "${_PROTOBUF_COMMON_HEADER} reveals protobuf ${Protobuf_VERSION}")
  404. endif()
  405. # Check Protobuf compiler version to be aligned with libraries version
  406. execute_process(COMMAND ${Protobuf_PROTOC_EXECUTABLE} --version
  407. OUTPUT_VARIABLE _PROTOBUF_PROTOC_EXECUTABLE_VERSION)
  408. if("${_PROTOBUF_PROTOC_EXECUTABLE_VERSION}" MATCHES "libprotoc ([0-9.]+)")
  409. set(_PROTOBUF_PROTOC_EXECUTABLE_VERSION "${CMAKE_MATCH_1}")
  410. endif()
  411. if(Protobuf_DEBUG)
  412. message(STATUS "[ ${CMAKE_CURRENT_LIST_FILE}:${CMAKE_CURRENT_LIST_LINE} ] "
  413. "${Protobuf_PROTOC_EXECUTABLE} reveals version ${_PROTOBUF_PROTOC_EXECUTABLE_VERSION}")
  414. endif()
  415. if(NOT "${_PROTOBUF_PROTOC_EXECUTABLE_VERSION}" VERSION_EQUAL "${Protobuf_VERSION}")
  416. message(WARNING "Protobuf compiler version ${_PROTOBUF_PROTOC_EXECUTABLE_VERSION}"
  417. " doesn't match library version ${Protobuf_VERSION}")
  418. endif()
  419. if(Protobuf_LIBRARY)
  420. if(NOT TARGET protobuf::libprotobuf)
  421. add_library(protobuf::libprotobuf UNKNOWN IMPORTED)
  422. set_target_properties(protobuf::libprotobuf PROPERTIES
  423. INTERFACE_INCLUDE_DIRECTORIES "${Protobuf_INCLUDE_DIR}")
  424. if(EXISTS "${Protobuf_LIBRARY}")
  425. set_target_properties(protobuf::libprotobuf PROPERTIES
  426. IMPORTED_LOCATION "${Protobuf_LIBRARY}")
  427. endif()
  428. if(EXISTS "${Protobuf_LIBRARY_RELEASE}")
  429. set_property(TARGET protobuf::libprotobuf APPEND PROPERTY
  430. IMPORTED_CONFIGURATIONS RELEASE)
  431. set_target_properties(protobuf::libprotobuf PROPERTIES
  432. IMPORTED_LOCATION_RELEASE "${Protobuf_LIBRARY_RELEASE}")
  433. endif()
  434. if(EXISTS "${Protobuf_LIBRARY_DEBUG}")
  435. set_property(TARGET protobuf::libprotobuf APPEND PROPERTY
  436. IMPORTED_CONFIGURATIONS DEBUG)
  437. set_target_properties(protobuf::libprotobuf PROPERTIES
  438. IMPORTED_LOCATION_DEBUG "${Protobuf_LIBRARY_DEBUG}")
  439. endif()
  440. endif()
  441. endif()
  442. if(Protobuf_LITE_LIBRARY)
  443. if(NOT TARGET protobuf::libprotobuf-lite)
  444. add_library(protobuf::libprotobuf-lite UNKNOWN IMPORTED)
  445. set_target_properties(protobuf::libprotobuf-lite PROPERTIES
  446. INTERFACE_INCLUDE_DIRECTORIES "${Protobuf_INCLUDE_DIR}")
  447. if(EXISTS "${Protobuf_LITE_LIBRARY}")
  448. set_target_properties(protobuf::libprotobuf-lite PROPERTIES
  449. IMPORTED_LOCATION "${Protobuf_LITE_LIBRARY}")
  450. endif()
  451. if(EXISTS "${Protobuf_LITE_LIBRARY_RELEASE}")
  452. set_property(TARGET protobuf::libprotobuf-lite APPEND PROPERTY
  453. IMPORTED_CONFIGURATIONS RELEASE)
  454. set_target_properties(protobuf::libprotobuf-lite PROPERTIES
  455. IMPORTED_LOCATION_RELEASE "${Protobuf_LITE_LIBRARY_RELEASE}")
  456. endif()
  457. if(EXISTS "${Protobuf_LITE_LIBRARY_DEBUG}")
  458. set_property(TARGET protobuf::libprotobuf-lite APPEND PROPERTY
  459. IMPORTED_CONFIGURATIONS DEBUG)
  460. set_target_properties(protobuf::libprotobuf-lite PROPERTIES
  461. IMPORTED_LOCATION_DEBUG "${Protobuf_LITE_LIBRARY_DEBUG}")
  462. endif()
  463. endif()
  464. endif()
  465. if(Protobuf_PROTOC_LIBRARY)
  466. if(NOT TARGET protobuf::libprotoc)
  467. add_library(protobuf::libprotoc UNKNOWN IMPORTED)
  468. set_target_properties(protobuf::libprotoc PROPERTIES
  469. INTERFACE_INCLUDE_DIRECTORIES "${Protobuf_INCLUDE_DIR}")
  470. if(EXISTS "${Protobuf_PROTOC_LIBRARY}")
  471. set_target_properties(protobuf::libprotoc PROPERTIES
  472. IMPORTED_LOCATION "${Protobuf_PROTOC_LIBRARY}")
  473. endif()
  474. if(EXISTS "${Protobuf_PROTOC_LIBRARY_RELEASE}")
  475. set_property(TARGET protobuf::libprotoc APPEND PROPERTY
  476. IMPORTED_CONFIGURATIONS RELEASE)
  477. set_target_properties(protobuf::libprotoc PROPERTIES
  478. IMPORTED_LOCATION_RELEASE "${Protobuf_PROTOC_LIBRARY_RELEASE}")
  479. endif()
  480. if(EXISTS "${Protobuf_PROTOC_LIBRARY_DEBUG}")
  481. set_property(TARGET protobuf::libprotoc APPEND PROPERTY
  482. IMPORTED_CONFIGURATIONS DEBUG)
  483. set_target_properties(protobuf::libprotoc PROPERTIES
  484. IMPORTED_LOCATION_DEBUG "${Protobuf_PROTOC_LIBRARY_DEBUG}")
  485. endif()
  486. endif()
  487. endif()
  488. if(Protobuf_PROTOC_EXECUTABLE)
  489. if(NOT TARGET protobuf::protoc)
  490. add_executable(protobuf::protoc IMPORTED)
  491. if(EXISTS "${Protobuf_PROTOC_EXECUTABLE}")
  492. set_target_properties(protobuf::protoc PROPERTIES
  493. IMPORTED_LOCATION "${Protobuf_PROTOC_EXECUTABLE}")
  494. endif()
  495. endif()
  496. endif()
  497. endif()
  498. include(${CMAKE_CURRENT_LIST_DIR}/FindPackageHandleStandardArgs.cmake)
  499. FIND_PACKAGE_HANDLE_STANDARD_ARGS(Protobuf
  500. REQUIRED_VARS Protobuf_LIBRARIES Protobuf_INCLUDE_DIR
  501. VERSION_VAR Protobuf_VERSION
  502. )
  503. if(Protobuf_FOUND)
  504. set(Protobuf_INCLUDE_DIRS ${Protobuf_INCLUDE_DIR})
  505. endif()
  506. # Restore the original find library ordering
  507. if( Protobuf_USE_STATIC_LIBS )
  508. set(CMAKE_FIND_LIBRARY_SUFFIXES ${_protobuf_ORIG_CMAKE_FIND_LIBRARY_SUFFIXES})
  509. endif()
  510. # Backwards compatibility
  511. # Define upper case versions of output variables
  512. foreach(Camel
  513. Protobuf_SRC_ROOT_FOLDER
  514. Protobuf_IMPORT_DIRS
  515. Protobuf_DEBUG
  516. Protobuf_INCLUDE_DIRS
  517. Protobuf_LIBRARIES
  518. Protobuf_PROTOC_LIBRARIES
  519. Protobuf_LITE_LIBRARIES
  520. Protobuf_LIBRARY
  521. Protobuf_PROTOC_LIBRARY
  522. Protobuf_INCLUDE_DIR
  523. Protobuf_PROTOC_EXECUTABLE
  524. Protobuf_LIBRARY_DEBUG
  525. Protobuf_PROTOC_LIBRARY_DEBUG
  526. Protobuf_LITE_LIBRARY
  527. Protobuf_LITE_LIBRARY_DEBUG
  528. )
  529. string(TOUPPER ${Camel} UPPER)
  530. set(${UPPER} ${${Camel}})
  531. endforeach()