FindProtobuf.cmake 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  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. #
  23. # Defines the following variables:
  24. #
  25. # ``Protobuf_FOUND``
  26. # Found the Google Protocol Buffers library
  27. # (libprotobuf & header files)
  28. # ``Protobuf_VERSION``
  29. # Version of package found.
  30. # ``Protobuf_INCLUDE_DIRS``
  31. # Include directories for Google Protocol Buffers
  32. # ``Protobuf_LIBRARIES``
  33. # The protobuf libraries
  34. # ``Protobuf_PROTOC_LIBRARIES``
  35. # The protoc libraries
  36. # ``Protobuf_LITE_LIBRARIES``
  37. # The protobuf-lite libraries
  38. #
  39. # The following cache variables are also available to set or use:
  40. #
  41. # ``Protobuf_LIBRARY``
  42. # The protobuf library
  43. # ``Protobuf_PROTOC_LIBRARY``
  44. # The protoc library
  45. # ``Protobuf_INCLUDE_DIR``
  46. # The include directory for protocol buffers
  47. # ``Protobuf_PROTOC_EXECUTABLE``
  48. # The protoc compiler
  49. # ``Protobuf_LIBRARY_DEBUG``
  50. # The protobuf library (debug)
  51. # ``Protobuf_PROTOC_LIBRARY_DEBUG``
  52. # The protoc library (debug)
  53. # ``Protobuf_LITE_LIBRARY``
  54. # The protobuf lite library
  55. # ``Protobuf_LITE_LIBRARY_DEBUG``
  56. # The protobuf lite library (debug)
  57. #
  58. # Example:
  59. #
  60. # .. code-block:: cmake
  61. #
  62. # find_package(Protobuf REQUIRED)
  63. # include_directories(${Protobuf_INCLUDE_DIRS})
  64. # include_directories(${CMAKE_CURRENT_BINARY_DIR})
  65. # protobuf_generate_cpp(PROTO_SRCS PROTO_HDRS foo.proto)
  66. # protobuf_generate_python(PROTO_PY foo.proto)
  67. # add_executable(bar bar.cc ${PROTO_SRCS} ${PROTO_HDRS})
  68. # target_link_libraries(bar ${Protobuf_LIBRARIES})
  69. #
  70. # .. note::
  71. # The ``protobuf_generate_cpp`` and ``protobuf_generate_python``
  72. # functions and :command:`add_executable` or :command:`add_library`
  73. # calls only work properly within the same directory.
  74. #
  75. # .. command:: protobuf_generate_cpp
  76. #
  77. # Add custom commands to process ``.proto`` files to C++::
  78. #
  79. # protobuf_generate_cpp (<SRCS> <HDRS> [<ARGN>...])
  80. #
  81. # ``SRCS``
  82. # Variable to define with autogenerated source files
  83. # ``HDRS``
  84. # Variable to define with autogenerated header files
  85. # ``ARGN``
  86. # ``.proto`` files
  87. #
  88. # .. command:: protobuf_generate_python
  89. #
  90. # Add custom commands to process ``.proto`` files to Python::
  91. #
  92. # protobuf_generate_python (<PY> [<ARGN>...])
  93. #
  94. # ``PY``
  95. # Variable to define with autogenerated Python files
  96. # ``ARGN``
  97. # ``.proto`` filess
  98. function(PROTOBUF_GENERATE_CPP SRCS HDRS)
  99. if(NOT ARGN)
  100. message(SEND_ERROR "Error: PROTOBUF_GENERATE_CPP() called without any proto files")
  101. return()
  102. endif()
  103. if(PROTOBUF_GENERATE_CPP_APPEND_PATH)
  104. # Create an include path for each file specified
  105. foreach(FIL ${ARGN})
  106. get_filename_component(ABS_FIL ${FIL} ABSOLUTE)
  107. get_filename_component(ABS_PATH ${ABS_FIL} PATH)
  108. list(FIND _protobuf_include_path ${ABS_PATH} _contains_already)
  109. if(${_contains_already} EQUAL -1)
  110. list(APPEND _protobuf_include_path -I ${ABS_PATH})
  111. endif()
  112. endforeach()
  113. else()
  114. set(_protobuf_include_path -I ${CMAKE_CURRENT_SOURCE_DIR})
  115. endif()
  116. if(DEFINED PROTOBUF_IMPORT_DIRS AND NOT DEFINED Protobuf_IMPORT_DIRS)
  117. set(Protobuf_IMPORT_DIRS "${PROTOBUF_IMPORT_DIRS}")
  118. endif()
  119. if(DEFINED Protobuf_IMPORT_DIRS)
  120. foreach(DIR ${Protobuf_IMPORT_DIRS})
  121. get_filename_component(ABS_PATH ${DIR} ABSOLUTE)
  122. list(FIND _protobuf_include_path ${ABS_PATH} _contains_already)
  123. if(${_contains_already} EQUAL -1)
  124. list(APPEND _protobuf_include_path -I ${ABS_PATH})
  125. endif()
  126. endforeach()
  127. endif()
  128. set(${SRCS})
  129. set(${HDRS})
  130. foreach(FIL ${ARGN})
  131. get_filename_component(ABS_FIL ${FIL} ABSOLUTE)
  132. get_filename_component(FIL_WE ${FIL} NAME_WE)
  133. if(NOT PROTOBUF_GENERATE_CPP_APPEND_PATH)
  134. get_filename_component(FIL_DIR ${FIL} DIRECTORY)
  135. if(FIL_DIR)
  136. set(FIL_WE "${FIL_DIR}/${FIL_WE}")
  137. endif()
  138. endif()
  139. list(APPEND ${SRCS} "${CMAKE_CURRENT_BINARY_DIR}/${FIL_WE}.pb.cc")
  140. list(APPEND ${HDRS} "${CMAKE_CURRENT_BINARY_DIR}/${FIL_WE}.pb.h")
  141. add_custom_command(
  142. OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/${FIL_WE}.pb.cc"
  143. "${CMAKE_CURRENT_BINARY_DIR}/${FIL_WE}.pb.h"
  144. COMMAND ${Protobuf_PROTOC_EXECUTABLE}
  145. ARGS --cpp_out ${CMAKE_CURRENT_BINARY_DIR} ${_protobuf_include_path} ${ABS_FIL}
  146. DEPENDS ${ABS_FIL} ${Protobuf_PROTOC_EXECUTABLE}
  147. COMMENT "Running C++ protocol buffer compiler on ${FIL}"
  148. VERBATIM )
  149. endforeach()
  150. set_source_files_properties(${${SRCS}} ${${HDRS}} PROPERTIES GENERATED TRUE)
  151. set(${SRCS} ${${SRCS}} PARENT_SCOPE)
  152. set(${HDRS} ${${HDRS}} PARENT_SCOPE)
  153. endfunction()
  154. function(PROTOBUF_GENERATE_PYTHON SRCS)
  155. if(NOT ARGN)
  156. message(SEND_ERROR "Error: PROTOBUF_GENERATE_PYTHON() called without any proto files")
  157. return()
  158. endif()
  159. if(PROTOBUF_GENERATE_CPP_APPEND_PATH)
  160. # Create an include path for each file specified
  161. foreach(FIL ${ARGN})
  162. get_filename_component(ABS_FIL ${FIL} ABSOLUTE)
  163. get_filename_component(ABS_PATH ${ABS_FIL} PATH)
  164. list(FIND _protobuf_include_path ${ABS_PATH} _contains_already)
  165. if(${_contains_already} EQUAL -1)
  166. list(APPEND _protobuf_include_path -I ${ABS_PATH})
  167. endif()
  168. endforeach()
  169. else()
  170. set(_protobuf_include_path -I ${CMAKE_CURRENT_SOURCE_DIR})
  171. endif()
  172. if(DEFINED PROTOBUF_IMPORT_DIRS AND NOT DEFINED Protobuf_IMPORT_DIRS)
  173. set(Protobuf_IMPORT_DIRS "${PROTOBUF_IMPORT_DIRS}")
  174. endif()
  175. if(DEFINED Protobuf_IMPORT_DIRS)
  176. foreach(DIR ${Protobuf_IMPORT_DIRS})
  177. get_filename_component(ABS_PATH ${DIR} ABSOLUTE)
  178. list(FIND _protobuf_include_path ${ABS_PATH} _contains_already)
  179. if(${_contains_already} EQUAL -1)
  180. list(APPEND _protobuf_include_path -I ${ABS_PATH})
  181. endif()
  182. endforeach()
  183. endif()
  184. set(${SRCS})
  185. foreach(FIL ${ARGN})
  186. get_filename_component(ABS_FIL ${FIL} ABSOLUTE)
  187. get_filename_component(FIL_WE ${FIL} NAME_WE)
  188. if(NOT PROTOBUF_GENERATE_CPP_APPEND_PATH)
  189. get_filename_component(FIL_DIR ${FIL} DIRECTORY)
  190. if(FIL_DIR)
  191. set(FIL_WE "${FIL_DIR}/${FIL_WE}")
  192. endif()
  193. endif()
  194. list(APPEND ${SRCS} "${CMAKE_CURRENT_BINARY_DIR}/${FIL_WE}_pb2.py")
  195. add_custom_command(
  196. OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/${FIL_WE}_pb2.py"
  197. COMMAND ${Protobuf_PROTOC_EXECUTABLE} --python_out ${CMAKE_CURRENT_BINARY_DIR} ${_protobuf_include_path} ${ABS_FIL}
  198. DEPENDS ${ABS_FIL} ${Protobuf_PROTOC_EXECUTABLE}
  199. COMMENT "Running Python protocol buffer compiler on ${FIL}"
  200. VERBATIM )
  201. endforeach()
  202. set(${SRCS} ${${SRCS}} PARENT_SCOPE)
  203. endfunction()
  204. # Backwards compatibility
  205. # Define camel case versions of input variables
  206. foreach(UPPER
  207. PROTOBUF_SRC_ROOT_FOLDER
  208. PROTOBUF_IMPORT_DIRS
  209. PROTOBUF_DEBUG
  210. PROTOBUF_LIBRARY
  211. PROTOBUF_PROTOC_LIBRARY
  212. PROTOBUF_INCLUDE_DIR
  213. PROTOBUF_PROTOC_EXECUTABLE
  214. PROTOBUF_LIBRARY_DEBUG
  215. PROTOBUF_PROTOC_LIBRARY_DEBUG
  216. PROTOBUF_LITE_LIBRARY
  217. PROTOBUF_LITE_LIBRARY_DEBUG
  218. )
  219. if (DEFINED ${UPPER})
  220. string(REPLACE "PROTOBUF_" "Protobuf_" Camel ${UPPER})
  221. if (NOT DEFINED ${Camel})
  222. set(${Camel} ${${UPPER}})
  223. endif()
  224. endif()
  225. endforeach()
  226. if(CMAKE_SIZEOF_VOID_P EQUAL 8)
  227. set(_PROTOBUF_ARCH_DIR x64/)
  228. endif()
  229. include(${CMAKE_CURRENT_LIST_DIR}/SelectLibraryConfigurations.cmake)
  230. # Internal function: search for normal library as well as a debug one
  231. # if the debug one is specified also include debug/optimized keywords
  232. # in *_LIBRARIES variable
  233. function(_protobuf_find_libraries name filename)
  234. if(${name}_LIBRARIES)
  235. # Use result recorded by a previous call.
  236. return()
  237. elseif(${name}_LIBRARY)
  238. # Honor cache entry used by CMake 3.5 and lower.
  239. set(${name}_LIBRARIES "${${name}_LIBRARY}" PARENT_SCOPE)
  240. else()
  241. find_library(${name}_LIBRARY_RELEASE
  242. NAMES ${filename}
  243. PATHS ${Protobuf_SRC_ROOT_FOLDER}/vsprojects/${_PROTOBUF_ARCH_DIR}Release)
  244. mark_as_advanced(${name}_LIBRARY_RELEASE)
  245. find_library(${name}_LIBRARY_DEBUG
  246. NAMES ${filename}d ${filename}
  247. PATHS ${Protobuf_SRC_ROOT_FOLDER}/vsprojects/${_PROTOBUF_ARCH_DIR}Debug)
  248. mark_as_advanced(${name}_LIBRARY_DEBUG)
  249. select_library_configurations(${name})
  250. set(${name}_LIBRARY "${${name}_LIBRARY}" PARENT_SCOPE)
  251. set(${name}_LIBRARIES "${${name}_LIBRARIES}" PARENT_SCOPE)
  252. endif()
  253. endfunction()
  254. # Internal function: find threads library
  255. function(_protobuf_find_threads)
  256. set(CMAKE_THREAD_PREFER_PTHREAD TRUE)
  257. find_package(Threads)
  258. if(Threads_FOUND)
  259. list(APPEND Protobuf_LIBRARIES ${CMAKE_THREAD_LIBS_INIT})
  260. set(Protobuf_LIBRARIES "${Protobuf_LIBRARIES}" PARENT_SCOPE)
  261. endif()
  262. endfunction()
  263. #
  264. # Main.
  265. #
  266. # By default have PROTOBUF_GENERATE_CPP macro pass -I to protoc
  267. # for each directory where a proto file is referenced.
  268. if(NOT DEFINED PROTOBUF_GENERATE_CPP_APPEND_PATH)
  269. set(PROTOBUF_GENERATE_CPP_APPEND_PATH TRUE)
  270. endif()
  271. # Google's provided vcproj files generate libraries with a "lib"
  272. # prefix on Windows
  273. if(MSVC)
  274. set(Protobuf_ORIG_FIND_LIBRARY_PREFIXES "${CMAKE_FIND_LIBRARY_PREFIXES}")
  275. set(CMAKE_FIND_LIBRARY_PREFIXES "lib" "")
  276. find_path(Protobuf_SRC_ROOT_FOLDER protobuf.pc.in)
  277. endif()
  278. # The Protobuf library
  279. _protobuf_find_libraries(Protobuf protobuf)
  280. #DOC "The Google Protocol Buffers RELEASE Library"
  281. _protobuf_find_libraries(Protobuf_LITE protobuf-lite)
  282. # The Protobuf Protoc Library
  283. _protobuf_find_libraries(Protobuf_PROTOC protoc)
  284. # Restore original find library prefixes
  285. if(MSVC)
  286. set(CMAKE_FIND_LIBRARY_PREFIXES "${Protobuf_ORIG_FIND_LIBRARY_PREFIXES}")
  287. endif()
  288. if(UNIX)
  289. _protobuf_find_threads()
  290. endif()
  291. # Find the include directory
  292. find_path(Protobuf_INCLUDE_DIR
  293. google/protobuf/service.h
  294. PATHS ${Protobuf_SRC_ROOT_FOLDER}/src
  295. )
  296. mark_as_advanced(Protobuf_INCLUDE_DIR)
  297. # Find the protoc Executable
  298. find_program(Protobuf_PROTOC_EXECUTABLE
  299. NAMES protoc
  300. DOC "The Google Protocol Buffers Compiler"
  301. PATHS
  302. ${Protobuf_SRC_ROOT_FOLDER}/vsprojects/${_PROTOBUF_ARCH_DIR}Release
  303. ${Protobuf_SRC_ROOT_FOLDER}/vsprojects/${_PROTOBUF_ARCH_DIR}Debug
  304. )
  305. mark_as_advanced(Protobuf_PROTOC_EXECUTABLE)
  306. if(Protobuf_DEBUG)
  307. message(STATUS "[ ${CMAKE_CURRENT_LIST_FILE}:${CMAKE_CURRENT_LIST_LINE} ] "
  308. "requested version of Google Protobuf is ${Protobuf_FIND_VERSION}")
  309. endif()
  310. if(Protobuf_INCLUDE_DIR)
  311. set(_PROTOBUF_COMMON_HEADER ${Protobuf_INCLUDE_DIR}/google/protobuf/stubs/common.h)
  312. if(Protobuf_DEBUG)
  313. message(STATUS "[ ${CMAKE_CURRENT_LIST_FILE}:${CMAKE_CURRENT_LIST_LINE} ] "
  314. "location of common.h: ${_PROTOBUF_COMMON_HEADER}")
  315. endif()
  316. set(Protobuf_VERSION "")
  317. set(Protobuf_LIB_VERSION "")
  318. file(STRINGS ${_PROTOBUF_COMMON_HEADER} _PROTOBUF_COMMON_H_CONTENTS REGEX "#define[ \t]+GOOGLE_PROTOBUF_VERSION[ \t]+")
  319. if(_PROTOBUF_COMMON_H_CONTENTS MATCHES "#define[ \t]+GOOGLE_PROTOBUF_VERSION[ \t]+([0-9]+)")
  320. set(Protobuf_LIB_VERSION "${CMAKE_MATCH_1}")
  321. endif()
  322. unset(_PROTOBUF_COMMON_H_CONTENTS)
  323. math(EXPR _PROTOBUF_MAJOR_VERSION "${Protobuf_LIB_VERSION} / 1000000")
  324. math(EXPR _PROTOBUF_MINOR_VERSION "${Protobuf_LIB_VERSION} / 1000 % 1000")
  325. math(EXPR _PROTOBUF_SUBMINOR_VERSION "${Protobuf_LIB_VERSION} % 1000")
  326. set(Protobuf_VERSION "${_PROTOBUF_MAJOR_VERSION}.${_PROTOBUF_MINOR_VERSION}.${_PROTOBUF_SUBMINOR_VERSION}")
  327. if(Protobuf_DEBUG)
  328. message(STATUS "[ ${CMAKE_CURRENT_LIST_FILE}:${CMAKE_CURRENT_LIST_LINE} ] "
  329. "${_PROTOBUF_COMMON_HEADER} reveals protobuf ${Protobuf_VERSION}")
  330. endif()
  331. # Check Protobuf compiler version to be aligned with libraries version
  332. execute_process(COMMAND ${Protobuf_PROTOC_EXECUTABLE} --version
  333. OUTPUT_VARIABLE _PROTOBUF_PROTOC_EXECUTABLE_VERSION)
  334. if("${_PROTOBUF_PROTOC_EXECUTABLE_VERSION}" MATCHES "libprotoc ([0-9.]+)")
  335. set(_PROTOBUF_PROTOC_EXECUTABLE_VERSION "${CMAKE_MATCH_1}")
  336. endif()
  337. if(Protobuf_DEBUG)
  338. message(STATUS "[ ${CMAKE_CURRENT_LIST_FILE}:${CMAKE_CURRENT_LIST_LINE} ] "
  339. "${Protobuf_PROTOC_EXECUTABLE} reveals version ${_PROTOBUF_PROTOC_EXECUTABLE_VERSION}")
  340. endif()
  341. if(NOT "${_PROTOBUF_PROTOC_EXECUTABLE_VERSION}" VERSION_EQUAL "${Protobuf_VERSION}")
  342. message(WARNING "Protobuf compiler version ${_PROTOBUF_PROTOC_EXECUTABLE_VERSION}"
  343. " doesn't match library version ${Protobuf_VERSION}")
  344. endif()
  345. endif()
  346. include(${CMAKE_CURRENT_LIST_DIR}/FindPackageHandleStandardArgs.cmake)
  347. FIND_PACKAGE_HANDLE_STANDARD_ARGS(Protobuf
  348. REQUIRED_VARS Protobuf_LIBRARIES Protobuf_INCLUDE_DIR
  349. VERSION_VAR Protobuf_VERSION
  350. )
  351. if(Protobuf_FOUND)
  352. set(Protobuf_INCLUDE_DIRS ${Protobuf_INCLUDE_DIR})
  353. endif()
  354. # Backwards compatibility
  355. # Define upper case versions of output variables
  356. foreach(Camel
  357. Protobuf_SRC_ROOT_FOLDER
  358. Protobuf_IMPORT_DIRS
  359. Protobuf_DEBUG
  360. Protobuf_INCLUDE_DIRS
  361. Protobuf_LIBRARIES
  362. Protobuf_PROTOC_LIBRARIES
  363. Protobuf_LITE_LIBRARIES
  364. Protobuf_LIBRARY
  365. Protobuf_PROTOC_LIBRARY
  366. Protobuf_INCLUDE_DIR
  367. Protobuf_PROTOC_EXECUTABLE
  368. Protobuf_LIBRARY_DEBUG
  369. Protobuf_PROTOC_LIBRARY_DEBUG
  370. Protobuf_LITE_LIBRARY
  371. Protobuf_LITE_LIBRARY_DEBUG
  372. )
  373. string(TOUPPER ${Camel} UPPER)
  374. set(${UPPER} ${${Camel}})
  375. endforeach()