FindProtobuf.cmake 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789
  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. .. versionadded:: 3.6
  8. Support for :command:`find_package` version checks.
  9. .. versionchanged:: 3.6
  10. All input and output variables use the ``Protobuf_`` prefix.
  11. Variables with ``PROTOBUF_`` prefix are still supported for compatibility.
  12. The following variables can be set and are optional:
  13. ``Protobuf_SRC_ROOT_FOLDER``
  14. When compiling with MSVC, if this cache variable is set
  15. the protobuf-default VS project build locations
  16. (vsprojects/Debug and vsprojects/Release
  17. or vsprojects/x64/Debug and vsprojects/x64/Release)
  18. will be searched for libraries and binaries.
  19. ``Protobuf_IMPORT_DIRS``
  20. List of additional directories to be searched for
  21. imported .proto files.
  22. ``Protobuf_DEBUG``
  23. .. versionadded:: 3.6
  24. Show debug messages.
  25. ``Protobuf_USE_STATIC_LIBS``
  26. .. versionadded:: 3.9
  27. Set to ON to force the use of the static libraries.
  28. Default is OFF.
  29. Defines the following variables:
  30. ``Protobuf_FOUND``
  31. Found the Google Protocol Buffers library
  32. (libprotobuf & header files)
  33. ``Protobuf_VERSION``
  34. .. versionadded:: 3.6
  35. Version of package found.
  36. ``Protobuf_INCLUDE_DIRS``
  37. Include directories for Google Protocol Buffers
  38. ``Protobuf_LIBRARIES``
  39. The protobuf libraries
  40. ``Protobuf_PROTOC_LIBRARIES``
  41. The protoc libraries
  42. ``Protobuf_LITE_LIBRARIES``
  43. The protobuf-lite libraries
  44. .. versionadded:: 3.9
  45. The following :prop_tgt:`IMPORTED` targets are also defined:
  46. ``protobuf::libprotobuf``
  47. The protobuf library.
  48. ``protobuf::libprotobuf-lite``
  49. The protobuf lite library.
  50. ``protobuf::libprotoc``
  51. The protoc library.
  52. ``protobuf::protoc``
  53. .. versionadded:: 3.10
  54. The protoc compiler.
  55. The following cache variables are also available to set or use:
  56. ``Protobuf_LIBRARY``
  57. The protobuf library
  58. ``Protobuf_PROTOC_LIBRARY``
  59. The protoc library
  60. ``Protobuf_INCLUDE_DIR``
  61. The include directory for protocol buffers
  62. ``Protobuf_PROTOC_EXECUTABLE``
  63. The protoc compiler
  64. ``Protobuf_LIBRARY_DEBUG``
  65. The protobuf library (debug)
  66. ``Protobuf_PROTOC_LIBRARY_DEBUG``
  67. The protoc library (debug)
  68. ``Protobuf_LITE_LIBRARY``
  69. The protobuf lite library
  70. ``Protobuf_LITE_LIBRARY_DEBUG``
  71. The protobuf lite library (debug)
  72. Example:
  73. .. code-block:: cmake
  74. find_package(Protobuf REQUIRED)
  75. include_directories(${Protobuf_INCLUDE_DIRS})
  76. include_directories(${CMAKE_CURRENT_BINARY_DIR})
  77. protobuf_generate_cpp(PROTO_SRCS PROTO_HDRS foo.proto)
  78. protobuf_generate_cpp(PROTO_SRCS PROTO_HDRS EXPORT_MACRO DLL_EXPORT foo.proto)
  79. protobuf_generate_cpp(PROTO_SRCS PROTO_HDRS DESCRIPTORS PROTO_DESCS foo.proto)
  80. protobuf_generate_python(PROTO_PY foo.proto)
  81. add_executable(bar bar.cc ${PROTO_SRCS} ${PROTO_HDRS})
  82. target_link_libraries(bar ${Protobuf_LIBRARIES})
  83. .. note::
  84. The ``protobuf_generate_cpp`` and ``protobuf_generate_python``
  85. functions and :command:`add_executable` or :command:`add_library`
  86. calls only work properly within the same directory.
  87. .. command:: protobuf_generate_cpp
  88. Add custom commands to process ``.proto`` files to C++::
  89. protobuf_generate_cpp (<SRCS> <HDRS>
  90. [DESCRIPTORS <DESC>] [EXPORT_MACRO <MACRO>] [<ARGN>...])
  91. ``SRCS``
  92. Variable to define with autogenerated source files
  93. ``HDRS``
  94. Variable to define with autogenerated header files
  95. ``DESCRIPTORS``
  96. .. versionadded:: 3.10
  97. Variable to define with autogenerated descriptor files, if requested.
  98. ``EXPORT_MACRO``
  99. is a macro which should expand to ``__declspec(dllexport)`` or
  100. ``__declspec(dllimport)`` depending on what is being compiled.
  101. ``ARGN``
  102. ``.proto`` files
  103. .. command:: protobuf_generate_python
  104. .. versionadded:: 3.4
  105. Add custom commands to process ``.proto`` files to Python::
  106. protobuf_generate_python (<PY> [<ARGN>...])
  107. ``PY``
  108. Variable to define with autogenerated Python files
  109. ``ARGN``
  110. ``.proto`` files
  111. .. command:: protobuf_generate
  112. .. versionadded:: 3.13
  113. Automatically generate source files from ``.proto`` schema files at build time::
  114. protobuf_generate (
  115. TARGET <target>
  116. [LANGUAGE <lang>]
  117. [OUT_VAR <out_var>]
  118. [EXPORT_MACRO <macro>]
  119. [PROTOC_OUT_DIR <dir>]
  120. [PLUGIN <plugin>]
  121. [PLUGIN_OPTIONS <plugin_options>]
  122. [DEPENDENCIES <depends]
  123. [PROTOS <protobuf_files>]
  124. [IMPORT_DIRS <dirs>]
  125. [GENERATE_EXTENSIONS <extensions>]
  126. [PROTOC_OPTIONS <protoc_options>]
  127. [APPEND_PATH])
  128. ``APPEND_PATH``
  129. A flag that causes the base path of all proto schema files to be added to
  130. ``IMPORT_DIRS``.
  131. ``LANGUAGE``
  132. A single value: cpp or python. Determines what kind of source files are
  133. being generated. Defaults to cpp.
  134. ``OUT_VAR``
  135. Name of a CMake variable that will be filled with the paths to the generated
  136. source files.
  137. ``EXPORT_MACRO``
  138. Name of a macro that is applied to all generated Protobuf message classes
  139. and extern variables. It can, for example, be used to declare DLL exports.
  140. ``PROTOC_OUT_DIR``
  141. Output directory of generated source files. Defaults to ``CMAKE_CURRENT_BINARY_DIR``.
  142. ``PLUGIN``
  143. .. versionadded:: 3.21
  144. An optional plugin executable. This could, for example, be the path to
  145. ``grpc_cpp_plugin``.
  146. ``PLUGIN_OPTIONS``
  147. .. versionadded:: 3.28
  148. Additional options provided to the plugin, such as ``generate_mock_code=true``
  149. for the gRPC cpp plugin.
  150. ``DEPENDENCIES``
  151. .. versionadded:: 3.28
  152. Arguments forwarded to the ``DEPENDS`` of the underlying ``add_custom_command``
  153. invocation.
  154. ``TARGET``
  155. CMake target that will have the generated files added as sources.
  156. ``PROTOS``
  157. List of proto schema files. If omitted, then every source file ending in *proto* of ``TARGET`` will be used.
  158. ``IMPORT_DIRS``
  159. A common parent directory for the schema files. For example, if the schema file is
  160. ``proto/helloworld/helloworld.proto`` and the import directory ``proto/`` then the
  161. generated files are ``${PROTOC_OUT_DIR}/helloworld/helloworld.pb.h`` and
  162. ``${PROTOC_OUT_DIR}/helloworld/helloworld.pb.cc``.
  163. ``GENERATE_EXTENSIONS``
  164. If LANGUAGE is omitted then this must be set to the extensions that protoc generates.
  165. ``PROTOC_OPTIONS``
  166. .. versionadded:: 3.28
  167. Additional arguments that are forwarded to protoc.
  168. Example::
  169. find_package(gRPC CONFIG REQUIRED)
  170. find_package(Protobuf REQUIRED)
  171. add_library(ProtoTest Test.proto)
  172. target_link_libraries(ProtoTest PUBLIC gRPC::grpc++)
  173. protobuf_generate(TARGET ProtoTest)
  174. protobuf_generate(
  175. TARGET ProtoTest
  176. LANGUAGE grpc
  177. PLUGIN protoc-gen-grpc=$<TARGET_FILE:gRPC::grpc_cpp_plugin>
  178. PLUGIN_OPTIONS generate_mock_code=true
  179. GENERATE_EXTENSIONS .grpc.pb.h .grpc.pb.cc)
  180. #]=======================================================================]
  181. cmake_policy(PUSH)
  182. cmake_policy(SET CMP0159 NEW) # file(STRINGS) with REGEX updates CMAKE_MATCH_<n>
  183. function(protobuf_generate)
  184. set(_options APPEND_PATH DESCRIPTORS)
  185. set(_singleargs LANGUAGE OUT_VAR EXPORT_MACRO PROTOC_OUT_DIR PLUGIN PLUGIN_OPTIONS DEPENDENCIES)
  186. if(COMMAND target_sources)
  187. list(APPEND _singleargs TARGET)
  188. endif()
  189. set(_multiargs PROTOS IMPORT_DIRS GENERATE_EXTENSIONS PROTOC_OPTIONS)
  190. cmake_parse_arguments(protobuf_generate "${_options}" "${_singleargs}" "${_multiargs}" "${ARGN}")
  191. if(NOT protobuf_generate_PROTOS AND NOT protobuf_generate_TARGET)
  192. message(SEND_ERROR "Error: protobuf_generate called without any targets or source files")
  193. return()
  194. endif()
  195. if(NOT protobuf_generate_OUT_VAR AND NOT protobuf_generate_TARGET)
  196. message(SEND_ERROR "Error: protobuf_generate called without a target or output variable")
  197. return()
  198. endif()
  199. if(NOT protobuf_generate_LANGUAGE)
  200. set(protobuf_generate_LANGUAGE cpp)
  201. endif()
  202. string(TOLOWER ${protobuf_generate_LANGUAGE} protobuf_generate_LANGUAGE)
  203. if(NOT protobuf_generate_PROTOC_OUT_DIR)
  204. set(protobuf_generate_PROTOC_OUT_DIR ${CMAKE_CURRENT_BINARY_DIR})
  205. endif()
  206. if(protobuf_generate_EXPORT_MACRO AND protobuf_generate_LANGUAGE STREQUAL cpp)
  207. set(_dll_export_decl "dllexport_decl=${protobuf_generate_EXPORT_MACRO}")
  208. endif()
  209. foreach(_option ${_dll_export_decl} ${protobuf_generate_PLUGIN_OPTIONS})
  210. # append comma - not using CMake lists and string replacement as users
  211. # might have semicolons in options
  212. if(_plugin_options)
  213. set( _plugin_options "${_plugin_options},")
  214. endif()
  215. set(_plugin_options "${_plugin_options}${_option}")
  216. endforeach()
  217. if(protobuf_generate_PLUGIN)
  218. set(_plugin "--plugin=${protobuf_generate_PLUGIN}")
  219. endif()
  220. if(NOT protobuf_generate_GENERATE_EXTENSIONS)
  221. if(protobuf_generate_LANGUAGE STREQUAL cpp)
  222. set(protobuf_generate_GENERATE_EXTENSIONS .pb.h .pb.cc)
  223. elseif(protobuf_generate_LANGUAGE STREQUAL python)
  224. set(protobuf_generate_GENERATE_EXTENSIONS _pb2.py)
  225. else()
  226. message(SEND_ERROR "Error: protobuf_generate given unknown Language ${LANGUAGE}, please provide a value for GENERATE_EXTENSIONS")
  227. return()
  228. endif()
  229. endif()
  230. if(protobuf_generate_TARGET)
  231. get_target_property(_source_list ${protobuf_generate_TARGET} SOURCES)
  232. foreach(_file ${_source_list})
  233. if(_file MATCHES "proto$")
  234. list(APPEND protobuf_generate_PROTOS ${_file})
  235. endif()
  236. endforeach()
  237. endif()
  238. if(NOT protobuf_generate_PROTOS)
  239. message(SEND_ERROR "Error: protobuf_generate could not find any .proto files")
  240. return()
  241. endif()
  242. if(protobuf_generate_APPEND_PATH)
  243. # Create an include path for each file specified
  244. foreach(_file ${protobuf_generate_PROTOS})
  245. get_filename_component(_abs_file ${_file} ABSOLUTE)
  246. get_filename_component(_abs_dir ${_abs_file} DIRECTORY)
  247. list(FIND _protobuf_include_path ${_abs_dir} _contains_already)
  248. if(${_contains_already} EQUAL -1)
  249. list(APPEND _protobuf_include_path -I ${_abs_dir})
  250. endif()
  251. endforeach()
  252. else()
  253. set(_protobuf_include_path -I ${CMAKE_CURRENT_SOURCE_DIR})
  254. endif()
  255. foreach(DIR ${protobuf_generate_IMPORT_DIRS})
  256. get_filename_component(ABS_PATH ${DIR} ABSOLUTE)
  257. list(FIND _protobuf_include_path ${ABS_PATH} _contains_already)
  258. if(${_contains_already} EQUAL -1)
  259. list(APPEND _protobuf_include_path -I ${ABS_PATH})
  260. endif()
  261. endforeach()
  262. set(_generated_srcs_all)
  263. foreach(_proto ${protobuf_generate_PROTOS})
  264. get_filename_component(_abs_file ${_proto} ABSOLUTE)
  265. get_filename_component(_abs_dir ${_abs_file} DIRECTORY)
  266. get_filename_component(_basename ${_proto} NAME_WLE)
  267. file(RELATIVE_PATH _rel_dir ${CMAKE_CURRENT_SOURCE_DIR} ${_abs_dir})
  268. set(_possible_rel_dir)
  269. if (NOT protobuf_generate_APPEND_PATH)
  270. set(_possible_rel_dir ${_rel_dir}/)
  271. endif()
  272. set(_generated_srcs)
  273. foreach(_ext ${protobuf_generate_GENERATE_EXTENSIONS})
  274. list(APPEND _generated_srcs "${protobuf_generate_PROTOC_OUT_DIR}/${_possible_rel_dir}${_basename}${_ext}")
  275. endforeach()
  276. if(protobuf_generate_DESCRIPTORS AND protobuf_generate_LANGUAGE STREQUAL cpp)
  277. set(_descriptor_file "${CMAKE_CURRENT_BINARY_DIR}/${_basename}.desc")
  278. set(_dll_desc_out "--descriptor_set_out=${_descriptor_file}")
  279. list(APPEND _generated_srcs ${_descriptor_file})
  280. endif()
  281. list(APPEND _generated_srcs_all ${_generated_srcs})
  282. set(_comment "Running ${protobuf_generate_LANGUAGE} protocol buffer compiler on ${_proto}")
  283. if(protobuf_generate_PROTOC_OPTIONS)
  284. set(_comment "${_comment}, protoc-options: ${protobuf_generate_PROTOC_OPTIONS}")
  285. endif()
  286. if(_plugin_options)
  287. set(_comment "${_comment}, plugin-options: ${_plugin_options}")
  288. endif()
  289. add_custom_command(
  290. OUTPUT ${_generated_srcs}
  291. COMMAND protobuf::protoc
  292. ARGS ${protobuf_generate_PROTOC_OPTIONS} --${protobuf_generate_LANGUAGE}_out ${_plugin_options}:${protobuf_generate_PROTOC_OUT_DIR} ${_plugin} ${_dll_desc_out} ${_protobuf_include_path} ${_abs_file}
  293. DEPENDS ${_abs_file} protobuf::protoc ${protobuf_generate_DEPENDENCIES}
  294. COMMENT ${_comment}
  295. VERBATIM )
  296. endforeach()
  297. set_source_files_properties(${_generated_srcs_all} PROPERTIES GENERATED TRUE)
  298. if(protobuf_generate_OUT_VAR)
  299. set(${protobuf_generate_OUT_VAR} ${_generated_srcs_all} PARENT_SCOPE)
  300. endif()
  301. if(protobuf_generate_TARGET)
  302. target_sources(${protobuf_generate_TARGET} PRIVATE ${_generated_srcs_all})
  303. endif()
  304. endfunction()
  305. function(PROTOBUF_GENERATE_CPP SRCS HDRS)
  306. cmake_parse_arguments(protobuf_generate_cpp "" "EXPORT_MACRO;DESCRIPTORS" "" ${ARGN})
  307. set(_proto_files "${protobuf_generate_cpp_UNPARSED_ARGUMENTS}")
  308. if(NOT _proto_files)
  309. message(SEND_ERROR "Error: PROTOBUF_GENERATE_CPP() called without any proto files")
  310. return()
  311. endif()
  312. if(PROTOBUF_GENERATE_CPP_APPEND_PATH)
  313. set(_append_arg APPEND_PATH)
  314. endif()
  315. if(protobuf_generate_cpp_DESCRIPTORS)
  316. set(_descriptors DESCRIPTORS)
  317. endif()
  318. if(DEFINED PROTOBUF_IMPORT_DIRS AND NOT DEFINED Protobuf_IMPORT_DIRS)
  319. set(Protobuf_IMPORT_DIRS "${PROTOBUF_IMPORT_DIRS}")
  320. endif()
  321. if(DEFINED Protobuf_IMPORT_DIRS)
  322. set(_import_arg IMPORT_DIRS ${Protobuf_IMPORT_DIRS})
  323. endif()
  324. set(_outvar)
  325. protobuf_generate(${_append_arg} ${_descriptors} LANGUAGE cpp EXPORT_MACRO ${protobuf_generate_cpp_EXPORT_MACRO} OUT_VAR _outvar ${_import_arg} PROTOS ${_proto_files})
  326. set(${SRCS})
  327. set(${HDRS})
  328. if(protobuf_generate_cpp_DESCRIPTORS)
  329. set(${protobuf_generate_cpp_DESCRIPTORS})
  330. endif()
  331. foreach(_file ${_outvar})
  332. if(_file MATCHES "cc$")
  333. list(APPEND ${SRCS} ${_file})
  334. elseif(_file MATCHES "desc$")
  335. list(APPEND ${protobuf_generate_cpp_DESCRIPTORS} ${_file})
  336. else()
  337. list(APPEND ${HDRS} ${_file})
  338. endif()
  339. endforeach()
  340. set(${SRCS} ${${SRCS}} PARENT_SCOPE)
  341. set(${HDRS} ${${HDRS}} PARENT_SCOPE)
  342. if(protobuf_generate_cpp_DESCRIPTORS)
  343. set(${protobuf_generate_cpp_DESCRIPTORS} "${${protobuf_generate_cpp_DESCRIPTORS}}" PARENT_SCOPE)
  344. endif()
  345. endfunction()
  346. function(PROTOBUF_GENERATE_PYTHON SRCS)
  347. if(NOT ARGN)
  348. message(SEND_ERROR "Error: PROTOBUF_GENERATE_PYTHON() called without any proto files")
  349. return()
  350. endif()
  351. if(PROTOBUF_GENERATE_CPP_APPEND_PATH)
  352. set(_append_arg APPEND_PATH)
  353. endif()
  354. if(DEFINED PROTOBUF_IMPORT_DIRS AND NOT DEFINED Protobuf_IMPORT_DIRS)
  355. set(Protobuf_IMPORT_DIRS "${PROTOBUF_IMPORT_DIRS}")
  356. endif()
  357. if(DEFINED Protobuf_IMPORT_DIRS)
  358. set(_import_arg IMPORT_DIRS ${Protobuf_IMPORT_DIRS})
  359. endif()
  360. set(_outvar)
  361. protobuf_generate(${_append_arg} LANGUAGE python OUT_VAR _outvar ${_import_arg} PROTOS ${ARGN})
  362. set(${SRCS} ${_outvar} PARENT_SCOPE)
  363. endfunction()
  364. if(Protobuf_DEBUG)
  365. # Output some of their choices
  366. message(STATUS "[ ${CMAKE_CURRENT_LIST_FILE}:${CMAKE_CURRENT_LIST_LINE} ] "
  367. "Protobuf_USE_STATIC_LIBS = ${Protobuf_USE_STATIC_LIBS}")
  368. endif()
  369. # Backwards compatibility
  370. # Define camel case versions of input variables
  371. foreach(UPPER
  372. PROTOBUF_SRC_ROOT_FOLDER
  373. PROTOBUF_IMPORT_DIRS
  374. PROTOBUF_DEBUG
  375. PROTOBUF_LIBRARY
  376. PROTOBUF_PROTOC_LIBRARY
  377. PROTOBUF_INCLUDE_DIR
  378. PROTOBUF_PROTOC_EXECUTABLE
  379. PROTOBUF_LIBRARY_DEBUG
  380. PROTOBUF_PROTOC_LIBRARY_DEBUG
  381. PROTOBUF_LITE_LIBRARY
  382. PROTOBUF_LITE_LIBRARY_DEBUG
  383. )
  384. if (DEFINED ${UPPER})
  385. string(REPLACE "PROTOBUF_" "Protobuf_" Camel ${UPPER})
  386. if (NOT DEFINED ${Camel})
  387. set(${Camel} ${${UPPER}})
  388. endif()
  389. endif()
  390. endforeach()
  391. if(CMAKE_SIZEOF_VOID_P EQUAL 8)
  392. set(_PROTOBUF_ARCH_DIR x64/)
  393. endif()
  394. # Support preference of static libs by adjusting CMAKE_FIND_LIBRARY_SUFFIXES
  395. if( Protobuf_USE_STATIC_LIBS )
  396. set( _protobuf_ORIG_CMAKE_FIND_LIBRARY_SUFFIXES ${CMAKE_FIND_LIBRARY_SUFFIXES})
  397. if(WIN32)
  398. set(CMAKE_FIND_LIBRARY_SUFFIXES .lib .a ${CMAKE_FIND_LIBRARY_SUFFIXES})
  399. else()
  400. set(CMAKE_FIND_LIBRARY_SUFFIXES .a )
  401. endif()
  402. endif()
  403. include(${CMAKE_CURRENT_LIST_DIR}/SelectLibraryConfigurations.cmake)
  404. # Internal function: search for normal library as well as a debug one
  405. # if the debug one is specified also include debug/optimized keywords
  406. # in *_LIBRARIES variable
  407. function(_protobuf_find_libraries name filename)
  408. if(${name}_LIBRARIES)
  409. # Use result recorded by a previous call.
  410. return()
  411. elseif(${name}_LIBRARY)
  412. # Honor cache entry used by CMake 3.5 and lower.
  413. set(${name}_LIBRARIES "${${name}_LIBRARY}" PARENT_SCOPE)
  414. else()
  415. find_library(${name}_LIBRARY_RELEASE
  416. NAMES ${filename}
  417. NAMES_PER_DIR
  418. PATHS ${Protobuf_SRC_ROOT_FOLDER}/vsprojects/${_PROTOBUF_ARCH_DIR}Release)
  419. mark_as_advanced(${name}_LIBRARY_RELEASE)
  420. find_library(${name}_LIBRARY_DEBUG
  421. NAMES ${filename}d ${filename}
  422. NAMES_PER_DIR
  423. PATHS ${Protobuf_SRC_ROOT_FOLDER}/vsprojects/${_PROTOBUF_ARCH_DIR}Debug)
  424. mark_as_advanced(${name}_LIBRARY_DEBUG)
  425. select_library_configurations(${name})
  426. if(UNIX AND Threads_FOUND AND ${name}_LIBRARY)
  427. list(APPEND ${name}_LIBRARIES ${CMAKE_THREAD_LIBS_INIT})
  428. endif()
  429. set(${name}_LIBRARY "${${name}_LIBRARY}" PARENT_SCOPE)
  430. set(${name}_LIBRARIES "${${name}_LIBRARIES}" PARENT_SCOPE)
  431. endif()
  432. endfunction()
  433. #
  434. # Main.
  435. #
  436. # By default have PROTOBUF_GENERATE_CPP macro pass -I to protoc
  437. # for each directory where a proto file is referenced.
  438. if(NOT DEFINED PROTOBUF_GENERATE_CPP_APPEND_PATH)
  439. set(PROTOBUF_GENERATE_CPP_APPEND_PATH TRUE)
  440. endif()
  441. # Google's provided vcproj files generate libraries with a "lib"
  442. # prefix on Windows
  443. if(MSVC)
  444. set(Protobuf_ORIG_FIND_LIBRARY_PREFIXES "${CMAKE_FIND_LIBRARY_PREFIXES}")
  445. set(CMAKE_FIND_LIBRARY_PREFIXES "lib" "")
  446. find_path(Protobuf_SRC_ROOT_FOLDER protobuf.pc.in)
  447. endif()
  448. if(UNIX)
  449. # Protobuf headers may depend on threading.
  450. find_package(Threads QUIET)
  451. endif()
  452. # The Protobuf library
  453. _protobuf_find_libraries(Protobuf protobuf)
  454. #DOC "The Google Protocol Buffers RELEASE Library"
  455. _protobuf_find_libraries(Protobuf_LITE protobuf-lite)
  456. # The Protobuf Protoc Library
  457. _protobuf_find_libraries(Protobuf_PROTOC protoc)
  458. # Restore original find library prefixes
  459. if(MSVC)
  460. set(CMAKE_FIND_LIBRARY_PREFIXES "${Protobuf_ORIG_FIND_LIBRARY_PREFIXES}")
  461. endif()
  462. # Find the include directory
  463. find_path(Protobuf_INCLUDE_DIR
  464. google/protobuf/service.h
  465. PATHS ${Protobuf_SRC_ROOT_FOLDER}/src
  466. )
  467. mark_as_advanced(Protobuf_INCLUDE_DIR)
  468. # Find the protoc Executable
  469. find_program(Protobuf_PROTOC_EXECUTABLE
  470. NAMES protoc
  471. DOC "The Google Protocol Buffers Compiler"
  472. PATHS
  473. ${Protobuf_SRC_ROOT_FOLDER}/vsprojects/${_PROTOBUF_ARCH_DIR}Release
  474. ${Protobuf_SRC_ROOT_FOLDER}/vsprojects/${_PROTOBUF_ARCH_DIR}Debug
  475. )
  476. mark_as_advanced(Protobuf_PROTOC_EXECUTABLE)
  477. if(Protobuf_DEBUG)
  478. message(STATUS "[ ${CMAKE_CURRENT_LIST_FILE}:${CMAKE_CURRENT_LIST_LINE} ] "
  479. "requested version of Google Protobuf is ${Protobuf_FIND_VERSION}")
  480. endif()
  481. if(Protobuf_INCLUDE_DIR)
  482. set(_PROTOBUF_COMMON_HEADER ${Protobuf_INCLUDE_DIR}/google/protobuf/stubs/common.h)
  483. if(Protobuf_DEBUG)
  484. message(STATUS "[ ${CMAKE_CURRENT_LIST_FILE}:${CMAKE_CURRENT_LIST_LINE} ] "
  485. "location of common.h: ${_PROTOBUF_COMMON_HEADER}")
  486. endif()
  487. set(Protobuf_VERSION "")
  488. set(Protobuf_LIB_VERSION "")
  489. file(STRINGS ${_PROTOBUF_COMMON_HEADER} _PROTOBUF_COMMON_H_CONTENTS REGEX "#define[ \t]+GOOGLE_PROTOBUF_VERSION[ \t]+")
  490. if(_PROTOBUF_COMMON_H_CONTENTS MATCHES "#define[ \t]+GOOGLE_PROTOBUF_VERSION[ \t]+([0-9]+)")
  491. set(Protobuf_LIB_VERSION "${CMAKE_MATCH_1}")
  492. endif()
  493. unset(_PROTOBUF_COMMON_H_CONTENTS)
  494. math(EXPR _PROTOBUF_MAJOR_VERSION "${Protobuf_LIB_VERSION} / 1000000")
  495. math(EXPR _PROTOBUF_MINOR_VERSION "${Protobuf_LIB_VERSION} / 1000 % 1000")
  496. math(EXPR _PROTOBUF_SUBMINOR_VERSION "${Protobuf_LIB_VERSION} % 1000")
  497. set(Protobuf_VERSION "${_PROTOBUF_MAJOR_VERSION}.${_PROTOBUF_MINOR_VERSION}.${_PROTOBUF_SUBMINOR_VERSION}")
  498. if(Protobuf_DEBUG)
  499. message(STATUS "[ ${CMAKE_CURRENT_LIST_FILE}:${CMAKE_CURRENT_LIST_LINE} ] "
  500. "${_PROTOBUF_COMMON_HEADER} reveals protobuf ${Protobuf_VERSION}")
  501. endif()
  502. if(Protobuf_PROTOC_EXECUTABLE)
  503. # Check Protobuf compiler version to be aligned with libraries version
  504. execute_process(COMMAND ${Protobuf_PROTOC_EXECUTABLE} --version
  505. OUTPUT_VARIABLE _PROTOBUF_PROTOC_EXECUTABLE_VERSION)
  506. if("${_PROTOBUF_PROTOC_EXECUTABLE_VERSION}" MATCHES "libprotoc ([0-9.]+)")
  507. set(_PROTOBUF_PROTOC_EXECUTABLE_VERSION "${CMAKE_MATCH_1}")
  508. endif()
  509. if(Protobuf_DEBUG)
  510. message(STATUS "[ ${CMAKE_CURRENT_LIST_FILE}:${CMAKE_CURRENT_LIST_LINE} ] "
  511. "${Protobuf_PROTOC_EXECUTABLE} reveals version ${_PROTOBUF_PROTOC_EXECUTABLE_VERSION}")
  512. endif()
  513. # protoc version 22 and up don't print the major version any more
  514. if(NOT "${_PROTOBUF_PROTOC_EXECUTABLE_VERSION}" VERSION_EQUAL "${Protobuf_VERSION}" AND
  515. NOT "${_PROTOBUF_PROTOC_EXECUTABLE_VERSION}" VERSION_EQUAL "${_PROTOBUF_MINOR_VERSION}.${_PROTOBUF_SUBMINOR_VERSION}")
  516. message(WARNING "Protobuf compiler version ${_PROTOBUF_PROTOC_EXECUTABLE_VERSION}"
  517. " doesn't match library version ${Protobuf_VERSION}")
  518. endif()
  519. endif()
  520. if(Protobuf_LIBRARY)
  521. if(NOT TARGET protobuf::libprotobuf)
  522. add_library(protobuf::libprotobuf UNKNOWN IMPORTED)
  523. set_target_properties(protobuf::libprotobuf PROPERTIES
  524. INTERFACE_INCLUDE_DIRECTORIES "${Protobuf_INCLUDE_DIR}")
  525. if(EXISTS "${Protobuf_LIBRARY}")
  526. set_target_properties(protobuf::libprotobuf PROPERTIES
  527. IMPORTED_LOCATION "${Protobuf_LIBRARY}")
  528. endif()
  529. if(EXISTS "${Protobuf_LIBRARY_RELEASE}")
  530. set_property(TARGET protobuf::libprotobuf APPEND PROPERTY
  531. IMPORTED_CONFIGURATIONS RELEASE)
  532. set_target_properties(protobuf::libprotobuf PROPERTIES
  533. IMPORTED_LOCATION_RELEASE "${Protobuf_LIBRARY_RELEASE}")
  534. endif()
  535. if(EXISTS "${Protobuf_LIBRARY_DEBUG}")
  536. set_property(TARGET protobuf::libprotobuf APPEND PROPERTY
  537. IMPORTED_CONFIGURATIONS DEBUG)
  538. set_target_properties(protobuf::libprotobuf PROPERTIES
  539. IMPORTED_LOCATION_DEBUG "${Protobuf_LIBRARY_DEBUG}")
  540. endif()
  541. if (Protobuf_VERSION VERSION_GREATER_EQUAL "3.6")
  542. set_property(TARGET protobuf::libprotobuf APPEND PROPERTY
  543. INTERFACE_COMPILE_FEATURES cxx_std_11
  544. )
  545. endif()
  546. if (WIN32 AND NOT Protobuf_USE_STATIC_LIBS)
  547. set_property(TARGET protobuf::libprotobuf APPEND PROPERTY
  548. INTERFACE_COMPILE_DEFINITIONS "PROTOBUF_USE_DLLS"
  549. )
  550. endif()
  551. if(UNIX AND TARGET Threads::Threads)
  552. set_property(TARGET protobuf::libprotobuf APPEND PROPERTY
  553. INTERFACE_LINK_LIBRARIES Threads::Threads)
  554. endif()
  555. endif()
  556. endif()
  557. if(Protobuf_LITE_LIBRARY)
  558. if(NOT TARGET protobuf::libprotobuf-lite)
  559. add_library(protobuf::libprotobuf-lite UNKNOWN IMPORTED)
  560. set_target_properties(protobuf::libprotobuf-lite PROPERTIES
  561. INTERFACE_INCLUDE_DIRECTORIES "${Protobuf_INCLUDE_DIR}")
  562. if(EXISTS "${Protobuf_LITE_LIBRARY}")
  563. set_target_properties(protobuf::libprotobuf-lite PROPERTIES
  564. IMPORTED_LOCATION "${Protobuf_LITE_LIBRARY}")
  565. endif()
  566. if(EXISTS "${Protobuf_LITE_LIBRARY_RELEASE}")
  567. set_property(TARGET protobuf::libprotobuf-lite APPEND PROPERTY
  568. IMPORTED_CONFIGURATIONS RELEASE)
  569. set_target_properties(protobuf::libprotobuf-lite PROPERTIES
  570. IMPORTED_LOCATION_RELEASE "${Protobuf_LITE_LIBRARY_RELEASE}")
  571. endif()
  572. if(EXISTS "${Protobuf_LITE_LIBRARY_DEBUG}")
  573. set_property(TARGET protobuf::libprotobuf-lite APPEND PROPERTY
  574. IMPORTED_CONFIGURATIONS DEBUG)
  575. set_target_properties(protobuf::libprotobuf-lite PROPERTIES
  576. IMPORTED_LOCATION_DEBUG "${Protobuf_LITE_LIBRARY_DEBUG}")
  577. endif()
  578. if (WIN32 AND NOT Protobuf_USE_STATIC_LIBS)
  579. set_property(TARGET protobuf::libprotobuf-lite APPEND PROPERTY
  580. INTERFACE_COMPILE_DEFINITIONS "PROTOBUF_USE_DLLS"
  581. )
  582. endif()
  583. if(UNIX AND TARGET Threads::Threads)
  584. set_property(TARGET protobuf::libprotobuf-lite APPEND PROPERTY
  585. INTERFACE_LINK_LIBRARIES Threads::Threads)
  586. endif()
  587. endif()
  588. endif()
  589. if(Protobuf_PROTOC_LIBRARY)
  590. if(NOT TARGET protobuf::libprotoc)
  591. add_library(protobuf::libprotoc UNKNOWN IMPORTED)
  592. set_target_properties(protobuf::libprotoc PROPERTIES
  593. INTERFACE_INCLUDE_DIRECTORIES "${Protobuf_INCLUDE_DIR}")
  594. if(EXISTS "${Protobuf_PROTOC_LIBRARY}")
  595. set_target_properties(protobuf::libprotoc PROPERTIES
  596. IMPORTED_LOCATION "${Protobuf_PROTOC_LIBRARY}")
  597. endif()
  598. if(EXISTS "${Protobuf_PROTOC_LIBRARY_RELEASE}")
  599. set_property(TARGET protobuf::libprotoc APPEND PROPERTY
  600. IMPORTED_CONFIGURATIONS RELEASE)
  601. set_target_properties(protobuf::libprotoc PROPERTIES
  602. IMPORTED_LOCATION_RELEASE "${Protobuf_PROTOC_LIBRARY_RELEASE}")
  603. endif()
  604. if(EXISTS "${Protobuf_PROTOC_LIBRARY_DEBUG}")
  605. set_property(TARGET protobuf::libprotoc APPEND PROPERTY
  606. IMPORTED_CONFIGURATIONS DEBUG)
  607. set_target_properties(protobuf::libprotoc PROPERTIES
  608. IMPORTED_LOCATION_DEBUG "${Protobuf_PROTOC_LIBRARY_DEBUG}")
  609. endif()
  610. if (Protobuf_VERSION VERSION_GREATER_EQUAL "3.6")
  611. set_property(TARGET protobuf::libprotoc APPEND PROPERTY
  612. INTERFACE_COMPILE_FEATURES cxx_std_11
  613. )
  614. endif()
  615. if (WIN32 AND NOT Protobuf_USE_STATIC_LIBS)
  616. set_property(TARGET protobuf::libprotoc APPEND PROPERTY
  617. INTERFACE_COMPILE_DEFINITIONS "PROTOBUF_USE_DLLS"
  618. )
  619. endif()
  620. if(UNIX AND TARGET Threads::Threads)
  621. set_property(TARGET protobuf::libprotoc APPEND PROPERTY
  622. INTERFACE_LINK_LIBRARIES Threads::Threads)
  623. endif()
  624. endif()
  625. endif()
  626. if(Protobuf_PROTOC_EXECUTABLE)
  627. if(NOT TARGET protobuf::protoc)
  628. add_executable(protobuf::protoc IMPORTED)
  629. if(EXISTS "${Protobuf_PROTOC_EXECUTABLE}")
  630. set_target_properties(protobuf::protoc PROPERTIES
  631. IMPORTED_LOCATION "${Protobuf_PROTOC_EXECUTABLE}")
  632. endif()
  633. endif()
  634. endif()
  635. endif()
  636. include(${CMAKE_CURRENT_LIST_DIR}/FindPackageHandleStandardArgs.cmake)
  637. FIND_PACKAGE_HANDLE_STANDARD_ARGS(Protobuf
  638. REQUIRED_VARS Protobuf_LIBRARIES Protobuf_INCLUDE_DIR
  639. VERSION_VAR Protobuf_VERSION
  640. )
  641. if(Protobuf_FOUND)
  642. set(Protobuf_INCLUDE_DIRS ${Protobuf_INCLUDE_DIR})
  643. endif()
  644. # Restore the original find library ordering
  645. if( Protobuf_USE_STATIC_LIBS )
  646. set(CMAKE_FIND_LIBRARY_SUFFIXES ${_protobuf_ORIG_CMAKE_FIND_LIBRARY_SUFFIXES})
  647. endif()
  648. # Backwards compatibility
  649. # Define upper case versions of output variables
  650. foreach(Camel
  651. Protobuf_SRC_ROOT_FOLDER
  652. Protobuf_IMPORT_DIRS
  653. Protobuf_DEBUG
  654. Protobuf_INCLUDE_DIRS
  655. Protobuf_LIBRARIES
  656. Protobuf_PROTOC_LIBRARIES
  657. Protobuf_LITE_LIBRARIES
  658. Protobuf_LIBRARY
  659. Protobuf_PROTOC_LIBRARY
  660. Protobuf_INCLUDE_DIR
  661. Protobuf_PROTOC_EXECUTABLE
  662. Protobuf_LIBRARY_DEBUG
  663. Protobuf_PROTOC_LIBRARY_DEBUG
  664. Protobuf_LITE_LIBRARY
  665. Protobuf_LITE_LIBRARY_DEBUG
  666. )
  667. string(TOUPPER ${Camel} UPPER)
  668. set(${UPPER} ${${Camel}})
  669. endforeach()
  670. cmake_policy(POP)