FindProtobuf.cmake 19 KB

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