FindProtobuf.cmake 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172
  1. # Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. # file LICENSE.rst or https://cmake.org/licensing for details.
  3. #[=======================================================================[.rst:
  4. FindProtobuf
  5. ------------
  6. .. note::
  7. If the Protobuf library is built and installed using its CMake-based
  8. build system, it provides a :ref:`package configuration file
  9. <Config File Packages>` for use with the :command:`find_package` command
  10. in *config mode*:
  11. .. code-block:: cmake
  12. find_package(Protobuf CONFIG)
  13. In this case, imported targets and CMake commands such as
  14. :command:`protobuf_generate` are provided by the upstream package rather
  15. than this module. Additionally, some variables documented here are not
  16. available in *config mode*, as imported targets are preferred. For usage
  17. details, refer to the upstream documentation, which is the recommended
  18. way to use Protobuf with CMake.
  19. This module works only in *module mode*.
  20. This module finds the Protocol Buffers library (Protobuf) in *module mode*:
  21. .. code-block:: cmake
  22. find_package(Protobuf [<version>] [...])
  23. Protobuf is an open-source, language-neutral, and platform-neutral mechanism
  24. for serializing structured data, developed by Google. It is commonly used
  25. for data exchange between programs or across networks.
  26. .. versionadded:: 3.6
  27. Support for the ``<version>`` argument in
  28. :command:`find_package(Protobuf \<version\>)`.
  29. .. versionchanged:: 3.6
  30. All input and output variables use the ``Protobuf_`` prefix. Variables
  31. with ``PROTOBUF_`` prefix are supported for backward compatibility.
  32. Imported Targets
  33. ^^^^^^^^^^^^^^^^
  34. This module provides the following :ref:`Imported Targets`:
  35. ``protobuf::libprotobuf``
  36. .. versionadded:: 3.9
  37. Target encapsulating the Protobuf library usage requirements, available if
  38. Protobuf library is found.
  39. ``protobuf::libprotobuf-lite``
  40. .. versionadded:: 3.9
  41. Target encapsulating the ``protobuf-lite`` library usage requirements,
  42. available if Protobuf and its lite library are found.
  43. ``protobuf::libprotoc``
  44. .. versionadded:: 3.9
  45. Target encapsulating the ``protoc`` library usage requirements, available
  46. if Protobuf and its ``protoc`` library are found.
  47. ``protobuf::protoc``
  48. .. versionadded:: 3.10
  49. Imported executable target encapsulating the ``protoc`` compiler usage
  50. requirements, available if Protobuf and ``protoc`` are found.
  51. Result Variables
  52. ^^^^^^^^^^^^^^^^
  53. This module defines the following variables:
  54. ``Protobuf_FOUND``
  55. .. versionadded:: 3.3
  56. Boolean indicating whether the (requested version of) Protobuf library
  57. was found.
  58. ``Protobuf_VERSION``
  59. .. versionadded:: 3.6
  60. The version of Protobuf found.
  61. ``Protobuf_INCLUDE_DIRS``
  62. Include directories needed to use Protobuf.
  63. ``Protobuf_LIBRARIES``
  64. Libraries needed to link against to use Protobuf.
  65. ``Protobuf_PROTOC_LIBRARIES``
  66. Libraries needed to link against to use the ``protoc`` library.
  67. ``Protobuf_LITE_LIBRARIES``
  68. Libraries needed to link against to use the ``protobuf-lite`` library.
  69. Cache Variables
  70. ^^^^^^^^^^^^^^^
  71. The following cache variables may also be set:
  72. ``Protobuf_INCLUDE_DIR``
  73. The include directory containing Protobuf headers.
  74. ``Protobuf_LIBRARY``
  75. The path to the ``protobuf`` library.
  76. ``Protobuf_PROTOC_LIBRARY``
  77. The path to the ``protoc`` library.
  78. ``Protobuf_PROTOC_EXECUTABLE``
  79. The path to the ``protoc`` compiler.
  80. ``Protobuf_LIBRARY_DEBUG``
  81. The path to the ``protobuf`` debug library.
  82. ``Protobuf_PROTOC_LIBRARY_DEBUG``
  83. The path to the ``protoc`` debug library.
  84. ``Protobuf_LITE_LIBRARY``
  85. The path to the ``protobuf-lite`` library.
  86. ``Protobuf_LITE_LIBRARY_DEBUG``
  87. The path to the ``protobuf-lite`` debug library.
  88. ``Protobuf_SRC_ROOT_FOLDER``
  89. When compiling with MSVC, if this cache variable is set, the
  90. protobuf-default Visual Studio project build locations will be searched for
  91. libraries and binaries:
  92. * ``<Protobuf_SRC_ROOT_FOLDER>/vsprojects/{Debug,Release}``, or
  93. * ``<Protobuf_SRC_ROOT_FOLDER>/vsprojects/x64/{Debug,Release}``
  94. Hints
  95. ^^^^^
  96. This module accepts the following optional variables before calling the
  97. ``find_package(Protobuf)``:
  98. ``Protobuf_DEBUG``
  99. .. versionadded:: 3.6
  100. Boolean variable that enables debug messages of this module to be printed
  101. for debugging purposes.
  102. ``Protobuf_USE_STATIC_LIBS``
  103. .. versionadded:: 3.9
  104. Set to ON to force the use of the static libraries. Default is OFF.
  105. Commands
  106. ^^^^^^^^
  107. This module provides the following commands if Protobuf is found:
  108. Generating Source Files
  109. """""""""""""""""""""""
  110. .. command:: protobuf_generate
  111. .. versionadded:: 3.13
  112. Automatically generates source files from ``.proto`` schema files at build
  113. time:
  114. .. code-block:: cmake
  115. protobuf_generate(
  116. [TARGET <target>]
  117. [LANGUAGE <lang>]
  118. [OUT_VAR <variable>]
  119. [EXPORT_MACRO <macro>]
  120. [PROTOC_OUT_DIR <out-dir>]
  121. [PLUGIN <plugin>]
  122. [PLUGIN_OPTIONS <plugin-options>]
  123. [DEPENDENCIES <dependencies>...]
  124. [PROTOS <proto-files>...]
  125. [IMPORT_DIRS <dirs>...]
  126. [APPEND_PATH]
  127. [GENERATE_EXTENSIONS <extensions>...]
  128. [PROTOC_OPTIONS <options>...]
  129. [PROTOC_EXE <executable>]
  130. [DESCRIPTORS]
  131. )
  132. ``TARGET <target>``
  133. The CMake target to which the generated files are added as sources. This
  134. option is required when ``OUT_VAR <variable>`` is not used.
  135. ``LANGUAGE <lang>``
  136. A single value: ``cpp`` or ``python``. Determines the kind of source
  137. files to generate. Defaults to ``cpp``. For other languages, use the
  138. ``GENERATE_EXTENSIONS`` option.
  139. ``OUT_VAR <variable>``
  140. The name of a CMake variable that will be populated with the paths to
  141. the generated source files.
  142. ``EXPORT_MACRO <macro>``
  143. The name of a preprocessor macro applied to all generated Protobuf message
  144. classes and extern variables. This can be used, for example, to declare
  145. DLL exports. The macro should expand to ``__declspec(dllexport)`` or
  146. ``__declspec(dllimport)``, depending on what is being compiled.
  147. This option is only used when ``LANGUAGE`` is ``cpp``.
  148. ``PROTOC_OUT_DIR <out-dir>``
  149. The output directory for generated source files. Defaults to:
  150. :variable:`CMAKE_CURRENT_BINARY_DIR`.
  151. ``PLUGIN <plugin>``
  152. .. versionadded:: 3.21
  153. An optional plugin executable. This could be, for example, the path to
  154. ``grpc_cpp_plugin``.
  155. ``PLUGIN_OPTIONS <plugin-options>``
  156. .. versionadded:: 3.28
  157. Additional options passed to the plugin, such as ``generate_mock_code=true``
  158. for the gRPC C++ plugin.
  159. ``DEPENDENCIES <dependencies>...``
  160. .. versionadded:: 3.28
  161. Dependencies on which the generation of files depends on. These are
  162. forwarded to the underlying :command:`add_custom_command(DEPENDS)`
  163. invocation.
  164. .. versionchanged:: 4.1
  165. This argument now accepts multiple values (``DEPENDENCIES a b c...``).
  166. Previously, only a single value could be specified
  167. (``DEPENDENCIES "a;b;c;..."``).
  168. ``PROTOS <proto-files>...``
  169. A list of ``.proto`` schema files to process. If ``<target>`` is also
  170. specified, these will be combined with all ``.proto`` source files from
  171. that target.
  172. ``IMPORT_DIRS <dirs>...``
  173. A list of one or more common parent directories for the schema files.
  174. For example, if the schema file is ``proto/helloworld/helloworld.proto``
  175. and the import directory is ``proto/``, then the generated files will be
  176. ``<out-dir>/helloworld/helloworld.pb.h`` and
  177. ``<out-dir>/helloworld/helloworld.pb.cc``.
  178. ``APPEND_PATH``
  179. If specified, the base paths of all proto schema files are appended to
  180. ``IMPORT_DIRS`` (it causes ``protoc`` to be invoked with ``-I`` argument
  181. for each directory containing a ``.proto`` file).
  182. ``GENERATE_EXTENSIONS <extensions>...``
  183. If ``LANGUAGE`` is omitted, this must be set to specify the extensions
  184. generated by ``protoc``.
  185. ``PROTOC_OPTIONS <options>...``
  186. .. versionadded:: 3.28
  187. A list of additional command-line options passed directly to the
  188. ``protoc`` compiler.
  189. ``PROTOC_EXE <executable>``
  190. .. versionadded:: 4.0
  191. The command-line program, path, or CMake executable used to generate
  192. Protobuf bindings. If omitted, ``protobuf::protoc`` imported target is
  193. used by default.
  194. ``DESCRIPTORS``
  195. If specified, a command-line option ``--descriptor_set_out=<proto-file>``
  196. is appended to ``protoc`` compiler for each ``.proto`` source file,
  197. enabling the creation of self-describing messages. This option can only
  198. be used when ``<lang>`` is ``cpp`` and Protobuf is found in *module mode*.
  199. .. note::
  200. This option is not available when Protobuf is found in *config mode*.
  201. Deprecated Commands
  202. """""""""""""""""""
  203. The following commands are provided for backward compatibility.
  204. .. note::
  205. The ``protobuf_generate_cpp()`` and ``protobuf_generate_python()``
  206. commands work correctly only within the same directory scope, where
  207. ``find_package(Protobuf ...)`` is called.
  208. .. note::
  209. If Protobuf is found in *config mode*, the ``protobuf_generate_cpp()`` and
  210. ``protobuf_generate_python()`` commands are **not available** as of
  211. Protobuf version 3.0.0, unless the upstream package configuration hint
  212. variable ``protobuf_MODULE_COMPATIBLE`` is set to boolean true before
  213. calling ``find_package(Protobuf ...)``.
  214. .. command:: protobuf_generate_cpp
  215. .. deprecated:: 4.1
  216. Use :command:`protobuf_generate`.
  217. Automatically generates C++ source files from ``.proto`` schema files at
  218. build time:
  219. .. code-block:: cmake
  220. protobuf_generate_cpp(
  221. <sources-variable>
  222. <headers-variable>
  223. [DESCRIPTORS <variable>]
  224. [EXPORT_MACRO <macro>]
  225. <proto-files>...
  226. )
  227. ``<sources-variable>``
  228. Name of the variable to define, which will contain a list of generated
  229. C++ source files.
  230. ``<headers-variable>``
  231. Name of the variable to define, which will contain a list of generated
  232. header files.
  233. ``DESCRIPTORS <variable>``
  234. .. versionadded:: 3.10
  235. Name of the variable to define, which will contain a list of generated
  236. descriptor files if requested.
  237. .. note::
  238. This option is not available when Protobuf is found in *config mode*.
  239. ``EXPORT_MACRO <macro>``
  240. Name of a macro that should expand to ``__declspec(dllexport)`` or
  241. ``__declspec(dllimport)``, depending on what is being compiled.
  242. ``<proto-files>...``
  243. One of more ``.proto`` files to be processed.
  244. .. command:: protobuf_generate_python
  245. .. deprecated:: 4.1
  246. Use :command:`protobuf_generate`.
  247. .. versionadded:: 3.4
  248. Automatically generates Python source files from ``.proto`` schema files at
  249. build time:
  250. .. code-block:: cmake
  251. protobuf_generate_python(<python-sources-variable> <proto-files>...)
  252. ``<python-sources-variable>``
  253. Name of the variable to define, which will contain a list of generated
  254. Python source files.
  255. ``<proto-files>...``
  256. One or more ``.proto`` files to be processed.
  257. ---------------------------------------------------------------------
  258. The ``protobuf_generate_cpp()`` and ``protobuf_generate_python()`` commands
  259. accept the following optional variables before being invoked:
  260. ``Protobuf_IMPORT_DIRS``
  261. .. deprecated:: 4.1
  262. A list of additional directories to search for imported ``.proto`` files.
  263. ``PROTOBUF_GENERATE_CPP_APPEND_PATH``
  264. .. deprecated:: 4.1
  265. Use :command:`protobuf_generate(APPEND_PATH)` command option.
  266. A boolean variable that, if set to boolean true, causes ``protoc`` to be
  267. invoked with ``-I`` argument for each directory containing a ``.proto``
  268. file. By default, it is set to boolean true.
  269. Examples
  270. ^^^^^^^^
  271. Examples: Finding Protobuf
  272. """"""""""""""""""""""""""
  273. Finding Protobuf library:
  274. .. code-block:: cmake
  275. find_package(Protobuf)
  276. Or, finding Protobuf and specifying a minimum required version:
  277. .. code-block:: cmake
  278. find_package(Protobuf 30)
  279. Or, finding Protobuf and making it required (if not found, processing stops
  280. with an error message):
  281. .. code-block:: cmake
  282. find_package(Protobuf REQUIRED)
  283. Example: Finding Protobuf in Config Mode
  284. """"""""""""""""""""""""""""""""""""""""
  285. When Protobuf library is built and installed using its CMake-based build
  286. system, it can be found in *config mode*:
  287. .. code-block:: cmake
  288. find_package(Protobuf CONFIG)
  289. However, some Protobuf installations might still not provide package
  290. configuration file. The following example shows, how to use the
  291. :variable:`CMAKE_FIND_PACKAGE_PREFER_CONFIG` variable to find Protobuf in
  292. *config mode* and falling back to *module mode* if config file is not found:
  293. .. code-block:: cmake
  294. set(CMAKE_FIND_PACKAGE_PREFER_CONFIG TRUE)
  295. find_package(Protobuf)
  296. unset(CMAKE_FIND_PACKAGE_PREFER_CONFIG)
  297. Example: Using Protobuf
  298. """""""""""""""""""""""
  299. Finding Protobuf and linking its imported library target to a project target:
  300. .. code-block:: cmake
  301. find_package(Protobuf)
  302. target_link_libraries(example PRIVATE protobuf::libprotobuf)
  303. Example: Processing Proto Schema Files
  304. """"""""""""""""""""""""""""""""""""""
  305. The following example demonstrates how to process all ``*.proto`` schema
  306. source files added to a target into C++ source files:
  307. .. code-block:: cmake
  308. :caption: ``CMakeLists.txt``
  309. cmake_minimum_required(VERSION 3.24)
  310. project(ProtobufExample)
  311. add_executable(example main.cxx person.proto)
  312. find_package(Protobuf)
  313. if(Protobuf_FOUND)
  314. protobuf_generate(TARGET example)
  315. endif()
  316. target_link_libraries(example PRIVATE protobuf::libprotobuf)
  317. target_include_directories(example PRIVATE ${CMAKE_CURRENT_BINARY_DIR})
  318. .. code-block:: proto
  319. :caption: ``person.proto``
  320. syntax = "proto3";
  321. message Person {
  322. string name = 1;
  323. int32 id = 2;
  324. }
  325. .. code-block:: c++
  326. :caption: ``main.cxx``
  327. #include <iostream>
  328. #include "person.pb.h"
  329. int main()
  330. {
  331. Person person;
  332. person.set_name("Alice");
  333. person.set_id(123);
  334. std::cout << "Name: " << person.name() << "\n";
  335. std::cout << "ID: " << person.id() << "\n";
  336. return 0;
  337. }
  338. Example: Using Protobuf and gRPC
  339. """"""""""""""""""""""""""""""""
  340. The following example shows how to use Protobuf and gRPC:
  341. .. code-block:: cmake
  342. :caption: ``CMakeLists.txt``
  343. find_package(Protobuf REQUIRED)
  344. find_package(gRPC CONFIG REQUIRED)
  345. add_library(ProtoExample Example.proto)
  346. target_link_libraries(ProtoExample PUBLIC gRPC::grpc++)
  347. protobuf_generate(TARGET ProtoExample)
  348. protobuf_generate(
  349. TARGET ProtoExample
  350. LANGUAGE grpc
  351. PLUGIN protoc-gen-grpc=$<TARGET_FILE:gRPC::grpc_cpp_plugin>
  352. PLUGIN_OPTIONS generate_mock_code=true
  353. GENERATE_EXTENSIONS .grpc.pb.h .grpc.pb.cc
  354. )
  355. Examples: Upgrading Deprecated Commands
  356. """""""""""""""""""""""""""""""""""""""
  357. The following example shows how to process ``.proto`` files to C++ code,
  358. using a deprecated command and its modern replacement:
  359. .. code-block:: cmake
  360. :caption: ``CMakeLists.txt`` with deprecated command
  361. find_package(Protobuf)
  362. if(Protobuf_FOUND)
  363. protobuf_generate_cpp(
  364. proto_sources
  365. proto_headers
  366. EXPORT_MACRO DLL_EXPORT
  367. DESCRIPTORS proto_descriptors
  368. src/protocol/Proto1.proto
  369. src/protocol/Proto2.proto
  370. )
  371. endif()
  372. target_sources(
  373. example
  374. PRIVATE ${proto_sources} ${proto_headers} ${proto_descriptors}
  375. )
  376. target_link_libraries(example PRIVATE protobuf::libprotobuf)
  377. .. code-block:: cmake
  378. :caption: ``CMakeLists.txt`` with upgraded code
  379. find_package(Protobuf)
  380. if(Protobuf_FOUND)
  381. protobuf_generate(
  382. TARGET example
  383. EXPORT_MACRO DLL_EXPORT
  384. IMPORT_DIRS src/protocol
  385. DESCRIPTORS
  386. PROTOS
  387. src/protocol/Proto1.proto
  388. src/protocol/Proto2.proto
  389. )
  390. endif()
  391. target_link_libraries(example PRIVATE protobuf::libprotobuf)
  392. The following example shows how to process ``.proto`` files to Python code,
  393. using a deprecated command and its modern replacement:
  394. .. code-block:: cmake
  395. :caption: ``CMakeLists.txt`` with deprecated command
  396. find_package(Protobuf)
  397. if(Protobuf_FOUND)
  398. protobuf_generate_python(python_sources foo.proto)
  399. endif()
  400. add_custom_target(proto_files DEPENDS ${python_sources})
  401. .. code-block:: cmake
  402. :caption: ``CMakeLists.txt`` with upgraded code
  403. find_package(Protobuf)
  404. if(Protobuf_FOUND)
  405. protobuf_generate(
  406. LANGUAGE python
  407. PROTOS foo.proto
  408. OUT_VAR python_sources
  409. )
  410. endif()
  411. add_custom_target(proto_files DEPENDS ${python_sources})
  412. #]=======================================================================]
  413. cmake_policy(PUSH)
  414. cmake_policy(SET CMP0159 NEW) # file(STRINGS) with REGEX updates CMAKE_MATCH_<n>
  415. function(protobuf_generate)
  416. set(_options APPEND_PATH DESCRIPTORS)
  417. set(_singleargs LANGUAGE OUT_VAR EXPORT_MACRO PROTOC_OUT_DIR PLUGIN PLUGIN_OPTIONS PROTOC_EXE)
  418. if(COMMAND target_sources)
  419. list(APPEND _singleargs TARGET)
  420. endif()
  421. set(_multiargs PROTOS IMPORT_DIRS GENERATE_EXTENSIONS PROTOC_OPTIONS DEPENDENCIES)
  422. cmake_parse_arguments(protobuf_generate "${_options}" "${_singleargs}" "${_multiargs}" "${ARGN}")
  423. if(NOT protobuf_generate_PROTOS AND NOT protobuf_generate_TARGET)
  424. message(SEND_ERROR "Error: protobuf_generate called without any targets or source files")
  425. return()
  426. endif()
  427. if(NOT protobuf_generate_OUT_VAR AND NOT protobuf_generate_TARGET)
  428. message(SEND_ERROR "Error: protobuf_generate called without a target or output variable")
  429. return()
  430. endif()
  431. if(NOT protobuf_generate_LANGUAGE)
  432. set(protobuf_generate_LANGUAGE cpp)
  433. endif()
  434. string(TOLOWER ${protobuf_generate_LANGUAGE} protobuf_generate_LANGUAGE)
  435. if(NOT protobuf_generate_PROTOC_OUT_DIR)
  436. set(protobuf_generate_PROTOC_OUT_DIR ${CMAKE_CURRENT_BINARY_DIR})
  437. endif()
  438. if(protobuf_generate_EXPORT_MACRO AND protobuf_generate_LANGUAGE STREQUAL cpp)
  439. set(_dll_export_decl "dllexport_decl=${protobuf_generate_EXPORT_MACRO}")
  440. endif()
  441. foreach(_option ${_dll_export_decl} ${protobuf_generate_PLUGIN_OPTIONS})
  442. # append comma - not using CMake lists and string replacement as users
  443. # might have semicolons in options
  444. if(_plugin_options)
  445. set( _plugin_options "${_plugin_options},")
  446. endif()
  447. set(_plugin_options "${_plugin_options}${_option}")
  448. endforeach()
  449. if(protobuf_generate_PLUGIN)
  450. set(_plugin "--plugin=${protobuf_generate_PLUGIN}")
  451. endif()
  452. if(NOT protobuf_generate_GENERATE_EXTENSIONS)
  453. if(protobuf_generate_LANGUAGE STREQUAL cpp)
  454. set(protobuf_generate_GENERATE_EXTENSIONS .pb.h .pb.cc)
  455. elseif(protobuf_generate_LANGUAGE STREQUAL python)
  456. set(protobuf_generate_GENERATE_EXTENSIONS _pb2.py)
  457. else()
  458. message(SEND_ERROR "Error: protobuf_generate given unknown Language ${LANGUAGE}, please provide a value for GENERATE_EXTENSIONS")
  459. return()
  460. endif()
  461. endif()
  462. if(protobuf_generate_TARGET)
  463. get_target_property(_source_list ${protobuf_generate_TARGET} SOURCES)
  464. foreach(_file ${_source_list})
  465. if(_file MATCHES "proto$")
  466. list(APPEND protobuf_generate_PROTOS ${_file})
  467. endif()
  468. endforeach()
  469. endif()
  470. if(NOT protobuf_generate_PROTOS)
  471. message(SEND_ERROR "Error: protobuf_generate could not find any .proto files")
  472. return()
  473. endif()
  474. if(NOT protobuf_generate_PROTOC_EXE)
  475. if(NOT TARGET protobuf::protoc)
  476. message(SEND_ERROR "protoc executable not found. "
  477. "Please define the Protobuf_PROTOC_EXECUTABLE variable, or pass PROTOC_EXE to protobuf_generate, or ensure that protoc is in CMake's search path.")
  478. return()
  479. endif()
  480. # Default to using the CMake executable
  481. set(protobuf_generate_PROTOC_EXE protobuf::protoc)
  482. endif()
  483. if(protobuf_generate_APPEND_PATH)
  484. # Create an include path for each file specified
  485. foreach(_file ${protobuf_generate_PROTOS})
  486. get_filename_component(_abs_file ${_file} ABSOLUTE)
  487. get_filename_component(_abs_dir ${_abs_file} DIRECTORY)
  488. list(FIND _protobuf_include_path ${_abs_dir} _contains_already)
  489. if(${_contains_already} EQUAL -1)
  490. list(APPEND _protobuf_include_path -I ${_abs_dir})
  491. endif()
  492. endforeach()
  493. endif()
  494. foreach(DIR ${protobuf_generate_IMPORT_DIRS})
  495. get_filename_component(ABS_PATH ${DIR} ABSOLUTE)
  496. list(FIND _protobuf_include_path ${ABS_PATH} _contains_already)
  497. if(${_contains_already} EQUAL -1)
  498. list(APPEND _protobuf_include_path -I ${ABS_PATH})
  499. endif()
  500. endforeach()
  501. if(NOT protobuf_generate_APPEND_PATH)
  502. list(APPEND _protobuf_include_path -I ${CMAKE_CURRENT_SOURCE_DIR})
  503. endif()
  504. set(_generated_srcs_all)
  505. foreach(_proto ${protobuf_generate_PROTOS})
  506. get_filename_component(_abs_file ${_proto} ABSOLUTE)
  507. get_filename_component(_abs_dir ${_abs_file} DIRECTORY)
  508. get_filename_component(_basename ${_proto} NAME_WLE)
  509. file(RELATIVE_PATH _rel_dir ${CMAKE_CURRENT_SOURCE_DIR} ${_abs_dir})
  510. set(_possible_rel_dir)
  511. if (NOT protobuf_generate_APPEND_PATH)
  512. foreach(DIR ${_protobuf_include_path})
  513. if(NOT DIR STREQUAL "-I")
  514. file(RELATIVE_PATH _rel_dir ${DIR} ${_abs_dir})
  515. if(_rel_dir STREQUAL _abs_dir)
  516. continue()
  517. endif()
  518. string(FIND "${_rel_dir}" "../" _is_in_parent_folder)
  519. if (NOT ${_is_in_parent_folder} EQUAL 0)
  520. break()
  521. endif()
  522. endif()
  523. endforeach()
  524. set(_possible_rel_dir ${_rel_dir}/)
  525. endif()
  526. set(_generated_srcs)
  527. foreach(_ext ${protobuf_generate_GENERATE_EXTENSIONS})
  528. list(APPEND _generated_srcs "${protobuf_generate_PROTOC_OUT_DIR}/${_possible_rel_dir}${_basename}${_ext}")
  529. endforeach()
  530. if(protobuf_generate_DESCRIPTORS AND protobuf_generate_LANGUAGE STREQUAL cpp)
  531. set(_descriptor_file "${CMAKE_CURRENT_BINARY_DIR}/${_basename}.desc")
  532. set(_dll_desc_out "--descriptor_set_out=${_descriptor_file}")
  533. list(APPEND _generated_srcs ${_descriptor_file})
  534. endif()
  535. list(APPEND _generated_srcs_all ${_generated_srcs})
  536. set(_comment "Running ${protobuf_generate_LANGUAGE} protocol buffer compiler on ${_proto}")
  537. if(protobuf_generate_PROTOC_OPTIONS)
  538. set(_comment "${_comment}, protoc-options: ${protobuf_generate_PROTOC_OPTIONS}")
  539. endif()
  540. if(_plugin_options)
  541. set(_comment "${_comment}, plugin-options: ${_plugin_options}")
  542. endif()
  543. add_custom_command(
  544. OUTPUT ${_generated_srcs}
  545. COMMAND ${protobuf_generate_PROTOC_EXE}
  546. 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}
  547. DEPENDS ${_abs_file} protobuf::protoc ${protobuf_generate_DEPENDENCIES}
  548. COMMENT ${_comment}
  549. VERBATIM )
  550. endforeach()
  551. set_source_files_properties(${_generated_srcs_all} PROPERTIES GENERATED TRUE)
  552. if(protobuf_generate_OUT_VAR)
  553. set(${protobuf_generate_OUT_VAR} ${_generated_srcs_all} PARENT_SCOPE)
  554. endif()
  555. if(protobuf_generate_TARGET)
  556. target_sources(${protobuf_generate_TARGET} PRIVATE ${_generated_srcs_all})
  557. endif()
  558. endfunction()
  559. function(PROTOBUF_GENERATE_CPP SRCS HDRS)
  560. cmake_parse_arguments(protobuf_generate_cpp "" "EXPORT_MACRO;DESCRIPTORS" "" ${ARGN})
  561. set(_proto_files "${protobuf_generate_cpp_UNPARSED_ARGUMENTS}")
  562. if(NOT _proto_files)
  563. message(SEND_ERROR "Error: PROTOBUF_GENERATE_CPP() called without any proto files")
  564. return()
  565. endif()
  566. if(PROTOBUF_GENERATE_CPP_APPEND_PATH)
  567. set(_append_arg APPEND_PATH)
  568. endif()
  569. if(protobuf_generate_cpp_DESCRIPTORS)
  570. set(_descriptors DESCRIPTORS)
  571. endif()
  572. if(DEFINED PROTOBUF_IMPORT_DIRS AND NOT DEFINED Protobuf_IMPORT_DIRS)
  573. set(Protobuf_IMPORT_DIRS "${PROTOBUF_IMPORT_DIRS}")
  574. endif()
  575. if(DEFINED Protobuf_IMPORT_DIRS)
  576. set(_import_arg IMPORT_DIRS ${Protobuf_IMPORT_DIRS})
  577. endif()
  578. set(_outvar)
  579. protobuf_generate(${_append_arg} ${_descriptors} LANGUAGE cpp EXPORT_MACRO ${protobuf_generate_cpp_EXPORT_MACRO} OUT_VAR _outvar ${_import_arg} PROTOS ${_proto_files})
  580. set(${SRCS})
  581. set(${HDRS})
  582. if(protobuf_generate_cpp_DESCRIPTORS)
  583. set(${protobuf_generate_cpp_DESCRIPTORS})
  584. endif()
  585. foreach(_file ${_outvar})
  586. if(_file MATCHES "cc$")
  587. list(APPEND ${SRCS} ${_file})
  588. elseif(_file MATCHES "desc$")
  589. list(APPEND ${protobuf_generate_cpp_DESCRIPTORS} ${_file})
  590. else()
  591. list(APPEND ${HDRS} ${_file})
  592. endif()
  593. endforeach()
  594. set(${SRCS} ${${SRCS}} PARENT_SCOPE)
  595. set(${HDRS} ${${HDRS}} PARENT_SCOPE)
  596. if(protobuf_generate_cpp_DESCRIPTORS)
  597. set(${protobuf_generate_cpp_DESCRIPTORS} "${${protobuf_generate_cpp_DESCRIPTORS}}" PARENT_SCOPE)
  598. endif()
  599. endfunction()
  600. function(PROTOBUF_GENERATE_PYTHON SRCS)
  601. if(NOT ARGN)
  602. message(SEND_ERROR "Error: PROTOBUF_GENERATE_PYTHON() called without any proto files")
  603. return()
  604. endif()
  605. if(PROTOBUF_GENERATE_CPP_APPEND_PATH)
  606. set(_append_arg APPEND_PATH)
  607. endif()
  608. if(DEFINED PROTOBUF_IMPORT_DIRS AND NOT DEFINED Protobuf_IMPORT_DIRS)
  609. set(Protobuf_IMPORT_DIRS "${PROTOBUF_IMPORT_DIRS}")
  610. endif()
  611. if(DEFINED Protobuf_IMPORT_DIRS)
  612. set(_import_arg IMPORT_DIRS ${Protobuf_IMPORT_DIRS})
  613. endif()
  614. set(_outvar)
  615. protobuf_generate(${_append_arg} LANGUAGE python OUT_VAR _outvar ${_import_arg} PROTOS ${ARGN})
  616. set(${SRCS} ${_outvar} PARENT_SCOPE)
  617. endfunction()
  618. if(Protobuf_DEBUG)
  619. # Output some of their choices
  620. message(STATUS "[ ${CMAKE_CURRENT_LIST_FILE}:${CMAKE_CURRENT_LIST_LINE} ] "
  621. "Protobuf_USE_STATIC_LIBS = ${Protobuf_USE_STATIC_LIBS}")
  622. endif()
  623. # Backwards compatibility
  624. # Define camel case versions of input variables
  625. foreach(UPPER
  626. PROTOBUF_SRC_ROOT_FOLDER
  627. PROTOBUF_IMPORT_DIRS
  628. PROTOBUF_DEBUG
  629. PROTOBUF_LIBRARY
  630. PROTOBUF_PROTOC_LIBRARY
  631. PROTOBUF_INCLUDE_DIR
  632. PROTOBUF_PROTOC_EXECUTABLE
  633. PROTOBUF_LIBRARY_DEBUG
  634. PROTOBUF_PROTOC_LIBRARY_DEBUG
  635. PROTOBUF_LITE_LIBRARY
  636. PROTOBUF_LITE_LIBRARY_DEBUG
  637. )
  638. if (DEFINED ${UPPER})
  639. string(REPLACE "PROTOBUF_" "Protobuf_" Camel ${UPPER})
  640. if (NOT DEFINED ${Camel})
  641. set(${Camel} ${${UPPER}})
  642. endif()
  643. endif()
  644. endforeach()
  645. if(CMAKE_SIZEOF_VOID_P EQUAL 8)
  646. set(_PROTOBUF_ARCH_DIR x64/)
  647. endif()
  648. # Support preference of static libs by adjusting CMAKE_FIND_LIBRARY_SUFFIXES
  649. if( Protobuf_USE_STATIC_LIBS )
  650. set( _protobuf_ORIG_CMAKE_FIND_LIBRARY_SUFFIXES ${CMAKE_FIND_LIBRARY_SUFFIXES})
  651. if(WIN32)
  652. set(CMAKE_FIND_LIBRARY_SUFFIXES .lib .a ${CMAKE_FIND_LIBRARY_SUFFIXES})
  653. else()
  654. set(CMAKE_FIND_LIBRARY_SUFFIXES .a )
  655. endif()
  656. endif()
  657. include(${CMAKE_CURRENT_LIST_DIR}/SelectLibraryConfigurations.cmake)
  658. # Internal function: search for normal library as well as a debug one
  659. # if the debug one is specified also include debug/optimized keywords
  660. # in *_LIBRARIES variable
  661. function(_protobuf_find_libraries name filename)
  662. if(${name}_LIBRARIES)
  663. # Use result recorded by a previous call.
  664. return()
  665. elseif(${name}_LIBRARY)
  666. # Honor cache entry used by CMake 3.5 and lower.
  667. set(${name}_LIBRARIES "${${name}_LIBRARY}" PARENT_SCOPE)
  668. else()
  669. find_library(${name}_LIBRARY_RELEASE
  670. NAMES ${filename}
  671. NAMES_PER_DIR
  672. PATHS ${Protobuf_SRC_ROOT_FOLDER}/vsprojects/${_PROTOBUF_ARCH_DIR}Release)
  673. mark_as_advanced(${name}_LIBRARY_RELEASE)
  674. find_library(${name}_LIBRARY_DEBUG
  675. NAMES ${filename}d ${filename}
  676. NAMES_PER_DIR
  677. PATHS ${Protobuf_SRC_ROOT_FOLDER}/vsprojects/${_PROTOBUF_ARCH_DIR}Debug)
  678. mark_as_advanced(${name}_LIBRARY_DEBUG)
  679. select_library_configurations(${name})
  680. if(UNIX AND Threads_FOUND AND ${name}_LIBRARY)
  681. list(APPEND ${name}_LIBRARIES ${CMAKE_THREAD_LIBS_INIT})
  682. endif()
  683. set(${name}_LIBRARY "${${name}_LIBRARY}" PARENT_SCOPE)
  684. set(${name}_LIBRARIES "${${name}_LIBRARIES}" PARENT_SCOPE)
  685. endif()
  686. endfunction()
  687. #
  688. # Main.
  689. #
  690. # By default have PROTOBUF_GENERATE_CPP macro pass -I to protoc
  691. # for each directory where a proto file is referenced.
  692. if(NOT DEFINED PROTOBUF_GENERATE_CPP_APPEND_PATH)
  693. set(PROTOBUF_GENERATE_CPP_APPEND_PATH TRUE)
  694. endif()
  695. # Google's provided vcproj files generate libraries with a "lib"
  696. # prefix on Windows
  697. if(MSVC)
  698. set(Protobuf_ORIG_FIND_LIBRARY_PREFIXES "${CMAKE_FIND_LIBRARY_PREFIXES}")
  699. set(CMAKE_FIND_LIBRARY_PREFIXES "lib" "")
  700. find_path(Protobuf_SRC_ROOT_FOLDER protobuf.pc.in)
  701. endif()
  702. if(UNIX)
  703. # Protobuf headers may depend on threading.
  704. find_package(Threads QUIET)
  705. endif()
  706. # The Protobuf library
  707. _protobuf_find_libraries(Protobuf protobuf)
  708. #DOC "The Google Protocol Buffers RELEASE Library"
  709. _protobuf_find_libraries(Protobuf_LITE protobuf-lite)
  710. # The Protobuf Protoc Library
  711. _protobuf_find_libraries(Protobuf_PROTOC protoc)
  712. # Restore original find library prefixes
  713. if(MSVC)
  714. set(CMAKE_FIND_LIBRARY_PREFIXES "${Protobuf_ORIG_FIND_LIBRARY_PREFIXES}")
  715. endif()
  716. # Find the include directory
  717. find_path(Protobuf_INCLUDE_DIR
  718. google/protobuf/service.h
  719. PATHS ${Protobuf_SRC_ROOT_FOLDER}/src
  720. )
  721. mark_as_advanced(Protobuf_INCLUDE_DIR)
  722. # Find the protoc Executable
  723. find_program(Protobuf_PROTOC_EXECUTABLE
  724. NAMES protoc
  725. DOC "The Google Protocol Buffers Compiler"
  726. PATHS
  727. ${Protobuf_SRC_ROOT_FOLDER}/vsprojects/${_PROTOBUF_ARCH_DIR}Release
  728. ${Protobuf_SRC_ROOT_FOLDER}/vsprojects/${_PROTOBUF_ARCH_DIR}Debug
  729. )
  730. mark_as_advanced(Protobuf_PROTOC_EXECUTABLE)
  731. if(Protobuf_DEBUG)
  732. message(STATUS "[ ${CMAKE_CURRENT_LIST_FILE}:${CMAKE_CURRENT_LIST_LINE} ] "
  733. "requested version of Google Protobuf is ${Protobuf_FIND_VERSION}")
  734. endif()
  735. if(Protobuf_INCLUDE_DIR)
  736. set(_PROTOBUF_COMMON_HEADER ${Protobuf_INCLUDE_DIR}/google/protobuf/stubs/common.h)
  737. if(Protobuf_DEBUG)
  738. message(STATUS "[ ${CMAKE_CURRENT_LIST_FILE}:${CMAKE_CURRENT_LIST_LINE} ] "
  739. "location of common.h: ${_PROTOBUF_COMMON_HEADER}")
  740. endif()
  741. set(Protobuf_VERSION "")
  742. set(Protobuf_LIB_VERSION "")
  743. file(STRINGS ${_PROTOBUF_COMMON_HEADER} _PROTOBUF_COMMON_H_CONTENTS REGEX "#define[ \t]+GOOGLE_PROTOBUF_VERSION[ \t]+")
  744. if(_PROTOBUF_COMMON_H_CONTENTS MATCHES "#define[ \t]+GOOGLE_PROTOBUF_VERSION[ \t]+([0-9]+)")
  745. set(Protobuf_LIB_VERSION "${CMAKE_MATCH_1}")
  746. endif()
  747. unset(_PROTOBUF_COMMON_H_CONTENTS)
  748. math(EXPR _PROTOBUF_MAJOR_VERSION "${Protobuf_LIB_VERSION} / 1000000")
  749. math(EXPR _PROTOBUF_MINOR_VERSION "${Protobuf_LIB_VERSION} / 1000 % 1000")
  750. math(EXPR _PROTOBUF_SUBMINOR_VERSION "${Protobuf_LIB_VERSION} % 1000")
  751. set(Protobuf_VERSION "${_PROTOBUF_MAJOR_VERSION}.${_PROTOBUF_MINOR_VERSION}.${_PROTOBUF_SUBMINOR_VERSION}")
  752. if(Protobuf_DEBUG)
  753. message(STATUS "[ ${CMAKE_CURRENT_LIST_FILE}:${CMAKE_CURRENT_LIST_LINE} ] "
  754. "${_PROTOBUF_COMMON_HEADER} reveals protobuf ${Protobuf_VERSION}")
  755. endif()
  756. if(Protobuf_PROTOC_EXECUTABLE)
  757. # Check Protobuf compiler version to be aligned with libraries version
  758. execute_process(COMMAND ${Protobuf_PROTOC_EXECUTABLE} --version
  759. OUTPUT_VARIABLE _PROTOBUF_PROTOC_EXECUTABLE_VERSION)
  760. if("${_PROTOBUF_PROTOC_EXECUTABLE_VERSION}" MATCHES "libprotoc ([0-9.]+)")
  761. set(_PROTOBUF_PROTOC_EXECUTABLE_VERSION "${CMAKE_MATCH_1}")
  762. endif()
  763. if(Protobuf_DEBUG)
  764. message(STATUS "[ ${CMAKE_CURRENT_LIST_FILE}:${CMAKE_CURRENT_LIST_LINE} ] "
  765. "${Protobuf_PROTOC_EXECUTABLE} reveals version ${_PROTOBUF_PROTOC_EXECUTABLE_VERSION}")
  766. endif()
  767. # protoc version 22 and up don't print the major version any more
  768. if(NOT "${_PROTOBUF_PROTOC_EXECUTABLE_VERSION}" VERSION_EQUAL "${Protobuf_VERSION}" AND
  769. NOT "${_PROTOBUF_PROTOC_EXECUTABLE_VERSION}" VERSION_EQUAL "${_PROTOBUF_MINOR_VERSION}.${_PROTOBUF_SUBMINOR_VERSION}")
  770. message(WARNING "Protobuf compiler version ${_PROTOBUF_PROTOC_EXECUTABLE_VERSION}"
  771. " doesn't match library version ${Protobuf_VERSION}")
  772. endif()
  773. endif()
  774. if(Protobuf_LIBRARY)
  775. if(NOT TARGET protobuf::libprotobuf)
  776. add_library(protobuf::libprotobuf UNKNOWN IMPORTED)
  777. set_target_properties(protobuf::libprotobuf PROPERTIES
  778. INTERFACE_INCLUDE_DIRECTORIES "${Protobuf_INCLUDE_DIR}")
  779. if(EXISTS "${Protobuf_LIBRARY}")
  780. set_target_properties(protobuf::libprotobuf PROPERTIES
  781. IMPORTED_LOCATION "${Protobuf_LIBRARY}")
  782. endif()
  783. if(EXISTS "${Protobuf_LIBRARY_RELEASE}")
  784. set_property(TARGET protobuf::libprotobuf APPEND PROPERTY
  785. IMPORTED_CONFIGURATIONS RELEASE)
  786. set_target_properties(protobuf::libprotobuf PROPERTIES
  787. IMPORTED_LOCATION_RELEASE "${Protobuf_LIBRARY_RELEASE}")
  788. endif()
  789. if(EXISTS "${Protobuf_LIBRARY_DEBUG}")
  790. set_property(TARGET protobuf::libprotobuf APPEND PROPERTY
  791. IMPORTED_CONFIGURATIONS DEBUG)
  792. set_target_properties(protobuf::libprotobuf PROPERTIES
  793. IMPORTED_LOCATION_DEBUG "${Protobuf_LIBRARY_DEBUG}")
  794. endif()
  795. if (Protobuf_VERSION VERSION_GREATER_EQUAL "3.6")
  796. set_property(TARGET protobuf::libprotobuf APPEND PROPERTY
  797. INTERFACE_COMPILE_FEATURES cxx_std_11
  798. )
  799. endif()
  800. if (WIN32 AND NOT Protobuf_USE_STATIC_LIBS)
  801. set_property(TARGET protobuf::libprotobuf APPEND PROPERTY
  802. INTERFACE_COMPILE_DEFINITIONS "PROTOBUF_USE_DLLS"
  803. )
  804. endif()
  805. if(UNIX AND TARGET Threads::Threads)
  806. set_property(TARGET protobuf::libprotobuf APPEND PROPERTY
  807. INTERFACE_LINK_LIBRARIES Threads::Threads)
  808. endif()
  809. endif()
  810. endif()
  811. if(Protobuf_LITE_LIBRARY)
  812. if(NOT TARGET protobuf::libprotobuf-lite)
  813. add_library(protobuf::libprotobuf-lite UNKNOWN IMPORTED)
  814. set_target_properties(protobuf::libprotobuf-lite PROPERTIES
  815. INTERFACE_INCLUDE_DIRECTORIES "${Protobuf_INCLUDE_DIR}")
  816. if(EXISTS "${Protobuf_LITE_LIBRARY}")
  817. set_target_properties(protobuf::libprotobuf-lite PROPERTIES
  818. IMPORTED_LOCATION "${Protobuf_LITE_LIBRARY}")
  819. endif()
  820. if(EXISTS "${Protobuf_LITE_LIBRARY_RELEASE}")
  821. set_property(TARGET protobuf::libprotobuf-lite APPEND PROPERTY
  822. IMPORTED_CONFIGURATIONS RELEASE)
  823. set_target_properties(protobuf::libprotobuf-lite PROPERTIES
  824. IMPORTED_LOCATION_RELEASE "${Protobuf_LITE_LIBRARY_RELEASE}")
  825. endif()
  826. if(EXISTS "${Protobuf_LITE_LIBRARY_DEBUG}")
  827. set_property(TARGET protobuf::libprotobuf-lite APPEND PROPERTY
  828. IMPORTED_CONFIGURATIONS DEBUG)
  829. set_target_properties(protobuf::libprotobuf-lite PROPERTIES
  830. IMPORTED_LOCATION_DEBUG "${Protobuf_LITE_LIBRARY_DEBUG}")
  831. endif()
  832. if (WIN32 AND NOT Protobuf_USE_STATIC_LIBS)
  833. set_property(TARGET protobuf::libprotobuf-lite APPEND PROPERTY
  834. INTERFACE_COMPILE_DEFINITIONS "PROTOBUF_USE_DLLS"
  835. )
  836. endif()
  837. if(UNIX AND TARGET Threads::Threads)
  838. set_property(TARGET protobuf::libprotobuf-lite APPEND PROPERTY
  839. INTERFACE_LINK_LIBRARIES Threads::Threads)
  840. endif()
  841. endif()
  842. endif()
  843. if(Protobuf_PROTOC_LIBRARY)
  844. if(NOT TARGET protobuf::libprotoc)
  845. add_library(protobuf::libprotoc UNKNOWN IMPORTED)
  846. set_target_properties(protobuf::libprotoc PROPERTIES
  847. INTERFACE_INCLUDE_DIRECTORIES "${Protobuf_INCLUDE_DIR}")
  848. if(EXISTS "${Protobuf_PROTOC_LIBRARY}")
  849. set_target_properties(protobuf::libprotoc PROPERTIES
  850. IMPORTED_LOCATION "${Protobuf_PROTOC_LIBRARY}")
  851. endif()
  852. if(EXISTS "${Protobuf_PROTOC_LIBRARY_RELEASE}")
  853. set_property(TARGET protobuf::libprotoc APPEND PROPERTY
  854. IMPORTED_CONFIGURATIONS RELEASE)
  855. set_target_properties(protobuf::libprotoc PROPERTIES
  856. IMPORTED_LOCATION_RELEASE "${Protobuf_PROTOC_LIBRARY_RELEASE}")
  857. endif()
  858. if(EXISTS "${Protobuf_PROTOC_LIBRARY_DEBUG}")
  859. set_property(TARGET protobuf::libprotoc APPEND PROPERTY
  860. IMPORTED_CONFIGURATIONS DEBUG)
  861. set_target_properties(protobuf::libprotoc PROPERTIES
  862. IMPORTED_LOCATION_DEBUG "${Protobuf_PROTOC_LIBRARY_DEBUG}")
  863. endif()
  864. if (Protobuf_VERSION VERSION_GREATER_EQUAL "3.6")
  865. set_property(TARGET protobuf::libprotoc APPEND PROPERTY
  866. INTERFACE_COMPILE_FEATURES cxx_std_11
  867. )
  868. endif()
  869. if (WIN32 AND NOT Protobuf_USE_STATIC_LIBS)
  870. set_property(TARGET protobuf::libprotoc APPEND PROPERTY
  871. INTERFACE_COMPILE_DEFINITIONS "PROTOBUF_USE_DLLS"
  872. )
  873. endif()
  874. if(UNIX AND TARGET Threads::Threads)
  875. set_property(TARGET protobuf::libprotoc APPEND PROPERTY
  876. INTERFACE_LINK_LIBRARIES Threads::Threads)
  877. endif()
  878. endif()
  879. endif()
  880. if(Protobuf_PROTOC_EXECUTABLE)
  881. if(NOT TARGET protobuf::protoc)
  882. add_executable(protobuf::protoc IMPORTED)
  883. if(EXISTS "${Protobuf_PROTOC_EXECUTABLE}")
  884. set_target_properties(protobuf::protoc PROPERTIES
  885. IMPORTED_LOCATION "${Protobuf_PROTOC_EXECUTABLE}")
  886. endif()
  887. endif()
  888. endif()
  889. endif()
  890. include(FindPackageHandleStandardArgs)
  891. find_package_handle_standard_args(Protobuf
  892. REQUIRED_VARS Protobuf_LIBRARIES Protobuf_INCLUDE_DIR
  893. VERSION_VAR Protobuf_VERSION
  894. )
  895. if(Protobuf_FOUND)
  896. set(Protobuf_INCLUDE_DIRS ${Protobuf_INCLUDE_DIR})
  897. endif()
  898. # Restore the original find library ordering
  899. if( Protobuf_USE_STATIC_LIBS )
  900. set(CMAKE_FIND_LIBRARY_SUFFIXES ${_protobuf_ORIG_CMAKE_FIND_LIBRARY_SUFFIXES})
  901. endif()
  902. # Backwards compatibility
  903. # Define upper case versions of output variables
  904. foreach(Camel
  905. Protobuf_SRC_ROOT_FOLDER
  906. Protobuf_IMPORT_DIRS
  907. Protobuf_DEBUG
  908. Protobuf_INCLUDE_DIRS
  909. Protobuf_LIBRARIES
  910. Protobuf_PROTOC_LIBRARIES
  911. Protobuf_LITE_LIBRARIES
  912. Protobuf_LIBRARY
  913. Protobuf_PROTOC_LIBRARY
  914. Protobuf_INCLUDE_DIR
  915. Protobuf_PROTOC_EXECUTABLE
  916. Protobuf_LIBRARY_DEBUG
  917. Protobuf_PROTOC_LIBRARY_DEBUG
  918. Protobuf_LITE_LIBRARY
  919. Protobuf_LITE_LIBRARY_DEBUG
  920. )
  921. string(TOUPPER ${Camel} UPPER)
  922. set(${UPPER} ${${Camel}})
  923. endforeach()
  924. cmake_policy(POP)