FindProtobuf.cmake 23 KB

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