FindProtobuf.cmake 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578
  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_CPP SRCS HDRS)
  121. cmake_parse_arguments(protobuf "" "EXPORT_MACRO;DESCRIPTORS" "" ${ARGN})
  122. set(PROTO_FILES "${protobuf_UNPARSED_ARGUMENTS}")
  123. if(NOT PROTO_FILES)
  124. message(SEND_ERROR "Error: PROTOBUF_GENERATE_CPP() called without any proto files")
  125. return()
  126. endif()
  127. if(protobuf_EXPORT_MACRO)
  128. set(DLL_EXPORT_DECL "dllexport_decl=${protobuf_EXPORT_MACRO}:")
  129. endif()
  130. if(PROTOBUF_GENERATE_CPP_APPEND_PATH)
  131. # Create an include path for each file specified
  132. foreach(FIL ${PROTO_FILES})
  133. get_filename_component(ABS_FIL ${FIL} ABSOLUTE)
  134. get_filename_component(ABS_PATH ${ABS_FIL} PATH)
  135. list(FIND _protobuf_include_path ${ABS_PATH} _contains_already)
  136. if(${_contains_already} EQUAL -1)
  137. list(APPEND _protobuf_include_path -I ${ABS_PATH})
  138. endif()
  139. endforeach()
  140. else()
  141. set(_protobuf_include_path -I ${CMAKE_CURRENT_SOURCE_DIR})
  142. endif()
  143. if(DEFINED PROTOBUF_IMPORT_DIRS AND NOT DEFINED Protobuf_IMPORT_DIRS)
  144. set(Protobuf_IMPORT_DIRS "${PROTOBUF_IMPORT_DIRS}")
  145. endif()
  146. if(DEFINED Protobuf_IMPORT_DIRS)
  147. foreach(DIR ${Protobuf_IMPORT_DIRS})
  148. get_filename_component(ABS_PATH ${DIR} ABSOLUTE)
  149. list(FIND _protobuf_include_path ${ABS_PATH} _contains_already)
  150. if(${_contains_already} EQUAL -1)
  151. list(APPEND _protobuf_include_path -I ${ABS_PATH})
  152. endif()
  153. endforeach()
  154. endif()
  155. set(${SRCS})
  156. set(${HDRS})
  157. if (protobuf_DESCRIPTORS)
  158. set(${protobuf_DESCRIPTORS})
  159. endif()
  160. foreach(FIL ${PROTO_FILES})
  161. get_filename_component(ABS_FIL ${FIL} ABSOLUTE)
  162. get_filename_component(FIL_WE ${FIL} NAME_WE)
  163. if(NOT PROTOBUF_GENERATE_CPP_APPEND_PATH)
  164. get_filename_component(FIL_DIR ${FIL} DIRECTORY)
  165. if(FIL_DIR)
  166. set(FIL_WE "${FIL_DIR}/${FIL_WE}")
  167. endif()
  168. endif()
  169. set(_protobuf_protoc_src "${CMAKE_CURRENT_BINARY_DIR}/${FIL_WE}.pb.cc")
  170. set(_protobuf_protoc_hdr "${CMAKE_CURRENT_BINARY_DIR}/${FIL_WE}.pb.h")
  171. list(APPEND ${SRCS} "${_protobuf_protoc_src}")
  172. list(APPEND ${HDRS} "${_protobuf_protoc_hdr}")
  173. if(protobuf_DESCRIPTORS)
  174. set(_protobuf_protoc_desc "${CMAKE_CURRENT_BINARY_DIR}/${FIL_WE}.desc")
  175. set(_protobuf_protoc_flags "--descriptor_set_out=${_protobuf_protoc_desc}")
  176. list(APPEND ${protobuf_DESCRIPTORS} "${_protobuf_protoc_desc}")
  177. else()
  178. set(_protobuf_protoc_desc "")
  179. set(_protobuf_protoc_flags "")
  180. endif()
  181. add_custom_command(
  182. OUTPUT "${_protobuf_protoc_src}"
  183. "${_protobuf_protoc_hdr}"
  184. ${_protobuf_protoc_desc}
  185. COMMAND protobuf::protoc
  186. "--cpp_out=${DLL_EXPORT_DECL}${CMAKE_CURRENT_BINARY_DIR}"
  187. ${_protobuf_protoc_flags}
  188. ${_protobuf_include_path} ${ABS_FIL}
  189. DEPENDS ${ABS_FIL} protobuf::protoc
  190. COMMENT "Running C++ protocol buffer compiler on ${FIL}"
  191. VERBATIM )
  192. endforeach()
  193. set(${SRCS} "${${SRCS}}" PARENT_SCOPE)
  194. set(${HDRS} "${${HDRS}}" PARENT_SCOPE)
  195. if(protobuf_DESCRIPTORS)
  196. set(${protobuf_DESCRIPTORS} "${${protobuf_DESCRIPTORS}}" PARENT_SCOPE)
  197. endif()
  198. endfunction()
  199. function(PROTOBUF_GENERATE_PYTHON SRCS)
  200. if(NOT ARGN)
  201. message(SEND_ERROR "Error: PROTOBUF_GENERATE_PYTHON() called without any proto files")
  202. return()
  203. endif()
  204. if(PROTOBUF_GENERATE_CPP_APPEND_PATH)
  205. # Create an include path for each file specified
  206. foreach(FIL ${ARGN})
  207. get_filename_component(ABS_FIL ${FIL} ABSOLUTE)
  208. get_filename_component(ABS_PATH ${ABS_FIL} PATH)
  209. list(FIND _protobuf_include_path ${ABS_PATH} _contains_already)
  210. if(${_contains_already} EQUAL -1)
  211. list(APPEND _protobuf_include_path -I ${ABS_PATH})
  212. endif()
  213. endforeach()
  214. else()
  215. set(_protobuf_include_path -I ${CMAKE_CURRENT_SOURCE_DIR})
  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. foreach(DIR ${Protobuf_IMPORT_DIRS})
  222. get_filename_component(ABS_PATH ${DIR} ABSOLUTE)
  223. list(FIND _protobuf_include_path ${ABS_PATH} _contains_already)
  224. if(${_contains_already} EQUAL -1)
  225. list(APPEND _protobuf_include_path -I ${ABS_PATH})
  226. endif()
  227. endforeach()
  228. endif()
  229. set(${SRCS})
  230. foreach(FIL ${ARGN})
  231. get_filename_component(ABS_FIL ${FIL} ABSOLUTE)
  232. get_filename_component(FIL_WE ${FIL} NAME_WE)
  233. if(NOT PROTOBUF_GENERATE_CPP_APPEND_PATH)
  234. get_filename_component(FIL_DIR ${FIL} DIRECTORY)
  235. if(FIL_DIR)
  236. set(FIL_WE "${FIL_DIR}/${FIL_WE}")
  237. endif()
  238. endif()
  239. list(APPEND ${SRCS} "${CMAKE_CURRENT_BINARY_DIR}/${FIL_WE}_pb2.py")
  240. add_custom_command(
  241. OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/${FIL_WE}_pb2.py"
  242. COMMAND protobuf::protoc --python_out ${CMAKE_CURRENT_BINARY_DIR} ${_protobuf_include_path} ${ABS_FIL}
  243. DEPENDS ${ABS_FIL} protobuf::protoc
  244. COMMENT "Running Python protocol buffer compiler on ${FIL}"
  245. VERBATIM )
  246. endforeach()
  247. set(${SRCS} ${${SRCS}} PARENT_SCOPE)
  248. endfunction()
  249. if(Protobuf_DEBUG)
  250. # Output some of their choices
  251. message(STATUS "[ ${CMAKE_CURRENT_LIST_FILE}:${CMAKE_CURRENT_LIST_LINE} ] "
  252. "Protobuf_USE_STATIC_LIBS = ${Protobuf_USE_STATIC_LIBS}")
  253. endif()
  254. # Backwards compatibility
  255. # Define camel case versions of input variables
  256. foreach(UPPER
  257. PROTOBUF_SRC_ROOT_FOLDER
  258. PROTOBUF_IMPORT_DIRS
  259. PROTOBUF_DEBUG
  260. PROTOBUF_LIBRARY
  261. PROTOBUF_PROTOC_LIBRARY
  262. PROTOBUF_INCLUDE_DIR
  263. PROTOBUF_PROTOC_EXECUTABLE
  264. PROTOBUF_LIBRARY_DEBUG
  265. PROTOBUF_PROTOC_LIBRARY_DEBUG
  266. PROTOBUF_LITE_LIBRARY
  267. PROTOBUF_LITE_LIBRARY_DEBUG
  268. )
  269. if (DEFINED ${UPPER})
  270. string(REPLACE "PROTOBUF_" "Protobuf_" Camel ${UPPER})
  271. if (NOT DEFINED ${Camel})
  272. set(${Camel} ${${UPPER}})
  273. endif()
  274. endif()
  275. endforeach()
  276. if(CMAKE_SIZEOF_VOID_P EQUAL 8)
  277. set(_PROTOBUF_ARCH_DIR x64/)
  278. endif()
  279. # Support preference of static libs by adjusting CMAKE_FIND_LIBRARY_SUFFIXES
  280. if( Protobuf_USE_STATIC_LIBS )
  281. set( _protobuf_ORIG_CMAKE_FIND_LIBRARY_SUFFIXES ${CMAKE_FIND_LIBRARY_SUFFIXES})
  282. if(WIN32)
  283. set(CMAKE_FIND_LIBRARY_SUFFIXES .lib .a ${CMAKE_FIND_LIBRARY_SUFFIXES})
  284. else()
  285. set(CMAKE_FIND_LIBRARY_SUFFIXES .a )
  286. endif()
  287. endif()
  288. include(${CMAKE_CURRENT_LIST_DIR}/SelectLibraryConfigurations.cmake)
  289. # Internal function: search for normal library as well as a debug one
  290. # if the debug one is specified also include debug/optimized keywords
  291. # in *_LIBRARIES variable
  292. function(_protobuf_find_libraries name filename)
  293. if(${name}_LIBRARIES)
  294. # Use result recorded by a previous call.
  295. return()
  296. elseif(${name}_LIBRARY)
  297. # Honor cache entry used by CMake 3.5 and lower.
  298. set(${name}_LIBRARIES "${${name}_LIBRARY}" PARENT_SCOPE)
  299. else()
  300. find_library(${name}_LIBRARY_RELEASE
  301. NAMES ${filename}
  302. PATHS ${Protobuf_SRC_ROOT_FOLDER}/vsprojects/${_PROTOBUF_ARCH_DIR}Release)
  303. mark_as_advanced(${name}_LIBRARY_RELEASE)
  304. find_library(${name}_LIBRARY_DEBUG
  305. NAMES ${filename}d ${filename}
  306. PATHS ${Protobuf_SRC_ROOT_FOLDER}/vsprojects/${_PROTOBUF_ARCH_DIR}Debug)
  307. mark_as_advanced(${name}_LIBRARY_DEBUG)
  308. select_library_configurations(${name})
  309. set(${name}_LIBRARY "${${name}_LIBRARY}" PARENT_SCOPE)
  310. set(${name}_LIBRARIES "${${name}_LIBRARIES}" PARENT_SCOPE)
  311. endif()
  312. endfunction()
  313. # Internal function: find threads library
  314. function(_protobuf_find_threads)
  315. set(CMAKE_THREAD_PREFER_PTHREAD TRUE)
  316. find_package(Threads)
  317. if(Threads_FOUND)
  318. list(APPEND Protobuf_LIBRARIES ${CMAKE_THREAD_LIBS_INIT})
  319. set(Protobuf_LIBRARIES "${Protobuf_LIBRARIES}" PARENT_SCOPE)
  320. endif()
  321. endfunction()
  322. #
  323. # Main.
  324. #
  325. # By default have PROTOBUF_GENERATE_CPP macro pass -I to protoc
  326. # for each directory where a proto file is referenced.
  327. if(NOT DEFINED PROTOBUF_GENERATE_CPP_APPEND_PATH)
  328. set(PROTOBUF_GENERATE_CPP_APPEND_PATH TRUE)
  329. endif()
  330. # Google's provided vcproj files generate libraries with a "lib"
  331. # prefix on Windows
  332. if(MSVC)
  333. set(Protobuf_ORIG_FIND_LIBRARY_PREFIXES "${CMAKE_FIND_LIBRARY_PREFIXES}")
  334. set(CMAKE_FIND_LIBRARY_PREFIXES "lib" "")
  335. find_path(Protobuf_SRC_ROOT_FOLDER protobuf.pc.in)
  336. endif()
  337. # The Protobuf library
  338. _protobuf_find_libraries(Protobuf protobuf)
  339. #DOC "The Google Protocol Buffers RELEASE Library"
  340. _protobuf_find_libraries(Protobuf_LITE protobuf-lite)
  341. # The Protobuf Protoc Library
  342. _protobuf_find_libraries(Protobuf_PROTOC protoc)
  343. # Restore original find library prefixes
  344. if(MSVC)
  345. set(CMAKE_FIND_LIBRARY_PREFIXES "${Protobuf_ORIG_FIND_LIBRARY_PREFIXES}")
  346. endif()
  347. if(UNIX)
  348. _protobuf_find_threads()
  349. endif()
  350. # Find the include directory
  351. find_path(Protobuf_INCLUDE_DIR
  352. google/protobuf/service.h
  353. PATHS ${Protobuf_SRC_ROOT_FOLDER}/src
  354. )
  355. mark_as_advanced(Protobuf_INCLUDE_DIR)
  356. # Find the protoc Executable
  357. find_program(Protobuf_PROTOC_EXECUTABLE
  358. NAMES protoc
  359. DOC "The Google Protocol Buffers Compiler"
  360. PATHS
  361. ${Protobuf_SRC_ROOT_FOLDER}/vsprojects/${_PROTOBUF_ARCH_DIR}Release
  362. ${Protobuf_SRC_ROOT_FOLDER}/vsprojects/${_PROTOBUF_ARCH_DIR}Debug
  363. )
  364. mark_as_advanced(Protobuf_PROTOC_EXECUTABLE)
  365. if(Protobuf_DEBUG)
  366. message(STATUS "[ ${CMAKE_CURRENT_LIST_FILE}:${CMAKE_CURRENT_LIST_LINE} ] "
  367. "requested version of Google Protobuf is ${Protobuf_FIND_VERSION}")
  368. endif()
  369. if(Protobuf_INCLUDE_DIR)
  370. set(_PROTOBUF_COMMON_HEADER ${Protobuf_INCLUDE_DIR}/google/protobuf/stubs/common.h)
  371. if(Protobuf_DEBUG)
  372. message(STATUS "[ ${CMAKE_CURRENT_LIST_FILE}:${CMAKE_CURRENT_LIST_LINE} ] "
  373. "location of common.h: ${_PROTOBUF_COMMON_HEADER}")
  374. endif()
  375. set(Protobuf_VERSION "")
  376. set(Protobuf_LIB_VERSION "")
  377. file(STRINGS ${_PROTOBUF_COMMON_HEADER} _PROTOBUF_COMMON_H_CONTENTS REGEX "#define[ \t]+GOOGLE_PROTOBUF_VERSION[ \t]+")
  378. if(_PROTOBUF_COMMON_H_CONTENTS MATCHES "#define[ \t]+GOOGLE_PROTOBUF_VERSION[ \t]+([0-9]+)")
  379. set(Protobuf_LIB_VERSION "${CMAKE_MATCH_1}")
  380. endif()
  381. unset(_PROTOBUF_COMMON_H_CONTENTS)
  382. math(EXPR _PROTOBUF_MAJOR_VERSION "${Protobuf_LIB_VERSION} / 1000000")
  383. math(EXPR _PROTOBUF_MINOR_VERSION "${Protobuf_LIB_VERSION} / 1000 % 1000")
  384. math(EXPR _PROTOBUF_SUBMINOR_VERSION "${Protobuf_LIB_VERSION} % 1000")
  385. set(Protobuf_VERSION "${_PROTOBUF_MAJOR_VERSION}.${_PROTOBUF_MINOR_VERSION}.${_PROTOBUF_SUBMINOR_VERSION}")
  386. if(Protobuf_DEBUG)
  387. message(STATUS "[ ${CMAKE_CURRENT_LIST_FILE}:${CMAKE_CURRENT_LIST_LINE} ] "
  388. "${_PROTOBUF_COMMON_HEADER} reveals protobuf ${Protobuf_VERSION}")
  389. endif()
  390. # Check Protobuf compiler version to be aligned with libraries version
  391. execute_process(COMMAND ${Protobuf_PROTOC_EXECUTABLE} --version
  392. OUTPUT_VARIABLE _PROTOBUF_PROTOC_EXECUTABLE_VERSION)
  393. if("${_PROTOBUF_PROTOC_EXECUTABLE_VERSION}" MATCHES "libprotoc ([0-9.]+)")
  394. set(_PROTOBUF_PROTOC_EXECUTABLE_VERSION "${CMAKE_MATCH_1}")
  395. endif()
  396. if(Protobuf_DEBUG)
  397. message(STATUS "[ ${CMAKE_CURRENT_LIST_FILE}:${CMAKE_CURRENT_LIST_LINE} ] "
  398. "${Protobuf_PROTOC_EXECUTABLE} reveals version ${_PROTOBUF_PROTOC_EXECUTABLE_VERSION}")
  399. endif()
  400. if(NOT "${_PROTOBUF_PROTOC_EXECUTABLE_VERSION}" VERSION_EQUAL "${Protobuf_VERSION}")
  401. message(WARNING "Protobuf compiler version ${_PROTOBUF_PROTOC_EXECUTABLE_VERSION}"
  402. " doesn't match library version ${Protobuf_VERSION}")
  403. endif()
  404. if(Protobuf_LIBRARY)
  405. if(NOT TARGET protobuf::libprotobuf)
  406. add_library(protobuf::libprotobuf UNKNOWN IMPORTED)
  407. set_target_properties(protobuf::libprotobuf PROPERTIES
  408. INTERFACE_INCLUDE_DIRECTORIES "${Protobuf_INCLUDE_DIR}")
  409. if(EXISTS "${Protobuf_LIBRARY}")
  410. set_target_properties(protobuf::libprotobuf PROPERTIES
  411. IMPORTED_LOCATION "${Protobuf_LIBRARY}")
  412. endif()
  413. if(EXISTS "${Protobuf_LIBRARY_RELEASE}")
  414. set_property(TARGET protobuf::libprotobuf APPEND PROPERTY
  415. IMPORTED_CONFIGURATIONS RELEASE)
  416. set_target_properties(protobuf::libprotobuf PROPERTIES
  417. IMPORTED_LOCATION_RELEASE "${Protobuf_LIBRARY_RELEASE}")
  418. endif()
  419. if(EXISTS "${Protobuf_LIBRARY_DEBUG}")
  420. set_property(TARGET protobuf::libprotobuf APPEND PROPERTY
  421. IMPORTED_CONFIGURATIONS DEBUG)
  422. set_target_properties(protobuf::libprotobuf PROPERTIES
  423. IMPORTED_LOCATION_DEBUG "${Protobuf_LIBRARY_DEBUG}")
  424. endif()
  425. endif()
  426. endif()
  427. if(Protobuf_LITE_LIBRARY)
  428. if(NOT TARGET protobuf::libprotobuf-lite)
  429. add_library(protobuf::libprotobuf-lite UNKNOWN IMPORTED)
  430. set_target_properties(protobuf::libprotobuf-lite PROPERTIES
  431. INTERFACE_INCLUDE_DIRECTORIES "${Protobuf_INCLUDE_DIR}")
  432. if(EXISTS "${Protobuf_LITE_LIBRARY}")
  433. set_target_properties(protobuf::libprotobuf-lite PROPERTIES
  434. IMPORTED_LOCATION "${Protobuf_LITE_LIBRARY}")
  435. endif()
  436. if(EXISTS "${Protobuf_LITE_LIBRARY_RELEASE}")
  437. set_property(TARGET protobuf::libprotobuf-lite APPEND PROPERTY
  438. IMPORTED_CONFIGURATIONS RELEASE)
  439. set_target_properties(protobuf::libprotobuf-lite PROPERTIES
  440. IMPORTED_LOCATION_RELEASE "${Protobuf_LITE_LIBRARY_RELEASE}")
  441. endif()
  442. if(EXISTS "${Protobuf_LITE_LIBRARY_DEBUG}")
  443. set_property(TARGET protobuf::libprotobuf-lite APPEND PROPERTY
  444. IMPORTED_CONFIGURATIONS DEBUG)
  445. set_target_properties(protobuf::libprotobuf-lite PROPERTIES
  446. IMPORTED_LOCATION_DEBUG "${Protobuf_LITE_LIBRARY_DEBUG}")
  447. endif()
  448. endif()
  449. endif()
  450. if(Protobuf_PROTOC_LIBRARY)
  451. if(NOT TARGET protobuf::libprotoc)
  452. add_library(protobuf::libprotoc UNKNOWN IMPORTED)
  453. set_target_properties(protobuf::libprotoc PROPERTIES
  454. INTERFACE_INCLUDE_DIRECTORIES "${Protobuf_INCLUDE_DIR}")
  455. if(EXISTS "${Protobuf_PROTOC_LIBRARY}")
  456. set_target_properties(protobuf::libprotoc PROPERTIES
  457. IMPORTED_LOCATION "${Protobuf_PROTOC_LIBRARY}")
  458. endif()
  459. if(EXISTS "${Protobuf_PROTOC_LIBRARY_RELEASE}")
  460. set_property(TARGET protobuf::libprotoc APPEND PROPERTY
  461. IMPORTED_CONFIGURATIONS RELEASE)
  462. set_target_properties(protobuf::libprotoc PROPERTIES
  463. IMPORTED_LOCATION_RELEASE "${Protobuf_PROTOC_LIBRARY_RELEASE}")
  464. endif()
  465. if(EXISTS "${Protobuf_PROTOC_LIBRARY_DEBUG}")
  466. set_property(TARGET protobuf::libprotoc APPEND PROPERTY
  467. IMPORTED_CONFIGURATIONS DEBUG)
  468. set_target_properties(protobuf::libprotoc PROPERTIES
  469. IMPORTED_LOCATION_DEBUG "${Protobuf_PROTOC_LIBRARY_DEBUG}")
  470. endif()
  471. endif()
  472. endif()
  473. if(Protobuf_PROTOC_EXECUTABLE)
  474. if(NOT TARGET protobuf::protoc)
  475. add_executable(protobuf::protoc IMPORTED)
  476. if(EXISTS "${Protobuf_PROTOC_EXECUTABLE}")
  477. set_target_properties(protobuf::protoc PROPERTIES
  478. IMPORTED_LOCATION "${Protobuf_PROTOC_EXECUTABLE}")
  479. endif()
  480. endif()
  481. endif()
  482. endif()
  483. include(${CMAKE_CURRENT_LIST_DIR}/FindPackageHandleStandardArgs.cmake)
  484. FIND_PACKAGE_HANDLE_STANDARD_ARGS(Protobuf
  485. REQUIRED_VARS Protobuf_LIBRARIES Protobuf_INCLUDE_DIR
  486. VERSION_VAR Protobuf_VERSION
  487. )
  488. if(Protobuf_FOUND)
  489. set(Protobuf_INCLUDE_DIRS ${Protobuf_INCLUDE_DIR})
  490. endif()
  491. # Restore the original find library ordering
  492. if( Protobuf_USE_STATIC_LIBS )
  493. set(CMAKE_FIND_LIBRARY_SUFFIXES ${_protobuf_ORIG_CMAKE_FIND_LIBRARY_SUFFIXES})
  494. endif()
  495. # Backwards compatibility
  496. # Define upper case versions of output variables
  497. foreach(Camel
  498. Protobuf_SRC_ROOT_FOLDER
  499. Protobuf_IMPORT_DIRS
  500. Protobuf_DEBUG
  501. Protobuf_INCLUDE_DIRS
  502. Protobuf_LIBRARIES
  503. Protobuf_PROTOC_LIBRARIES
  504. Protobuf_LITE_LIBRARIES
  505. Protobuf_LIBRARY
  506. Protobuf_PROTOC_LIBRARY
  507. Protobuf_INCLUDE_DIR
  508. Protobuf_PROTOC_EXECUTABLE
  509. Protobuf_LIBRARY_DEBUG
  510. Protobuf_PROTOC_LIBRARY_DEBUG
  511. Protobuf_LITE_LIBRARY
  512. Protobuf_LITE_LIBRARY_DEBUG
  513. )
  514. string(TOUPPER ${Camel} UPPER)
  515. set(${UPPER} ${${Camel}})
  516. endforeach()