FindProtobuf.cmake 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. #.rst:
  2. # FindProtobuf
  3. # ------------
  4. #
  5. #
  6. #
  7. # Locate and configure the Google Protocol Buffers library.
  8. #
  9. # The following variables can be set and are optional:
  10. #
  11. # ::
  12. #
  13. # PROTOBUF_SRC_ROOT_FOLDER - When compiling with MSVC, if this cache variable is set
  14. # the protobuf-default VS project build locations
  15. # (vsprojects/Debug & vsprojects/Release) will be searched
  16. # for libraries and binaries.
  17. #
  18. #
  19. #
  20. # ::
  21. #
  22. # PROTOBUF_IMPORT_DIRS - List of additional directories to be searched for
  23. # imported .proto files. (New in CMake 2.8.8)
  24. #
  25. #
  26. #
  27. # Defines the following variables:
  28. #
  29. # ::
  30. #
  31. # PROTOBUF_FOUND - Found the Google Protocol Buffers library (libprotobuf & header files)
  32. # PROTOBUF_INCLUDE_DIRS - Include directories for Google Protocol Buffers
  33. # PROTOBUF_LIBRARIES - The protobuf libraries
  34. #
  35. # [New in CMake 2.8.5]
  36. #
  37. # ::
  38. #
  39. # PROTOBUF_PROTOC_LIBRARIES - The protoc libraries
  40. # PROTOBUF_LITE_LIBRARIES - The protobuf-lite libraries
  41. #
  42. #
  43. #
  44. # The following cache variables are also available to set or use:
  45. #
  46. # ::
  47. #
  48. # PROTOBUF_LIBRARY - The protobuf library
  49. # PROTOBUF_PROTOC_LIBRARY - The protoc library
  50. # PROTOBUF_INCLUDE_DIR - The include directory for protocol buffers
  51. # PROTOBUF_PROTOC_EXECUTABLE - The protoc compiler
  52. #
  53. # [New in CMake 2.8.5]
  54. #
  55. # ::
  56. #
  57. # PROTOBUF_LIBRARY_DEBUG - The protobuf library (debug)
  58. # PROTOBUF_PROTOC_LIBRARY_DEBUG - The protoc library (debug)
  59. # PROTOBUF_LITE_LIBRARY - The protobuf lite library
  60. # PROTOBUF_LITE_LIBRARY_DEBUG - The protobuf lite library (debug)
  61. #
  62. #
  63. #
  64. # ::
  65. #
  66. # ====================================================================
  67. # Example:
  68. #
  69. #
  70. #
  71. # ::
  72. #
  73. # find_package(Protobuf REQUIRED)
  74. # include_directories(${PROTOBUF_INCLUDE_DIRS})
  75. #
  76. #
  77. #
  78. # ::
  79. #
  80. # include_directories(${CMAKE_CURRENT_BINARY_DIR})
  81. # PROTOBUF_GENERATE_CPP(PROTO_SRCS PROTO_HDRS foo.proto)
  82. # add_executable(bar bar.cc ${PROTO_SRCS} ${PROTO_HDRS})
  83. # target_link_libraries(bar ${PROTOBUF_LIBRARIES})
  84. #
  85. #
  86. #
  87. # NOTE: You may need to link against pthreads, depending
  88. #
  89. # ::
  90. #
  91. # on the platform.
  92. #
  93. #
  94. #
  95. # NOTE: The PROTOBUF_GENERATE_CPP macro & add_executable() or
  96. # add_library()
  97. #
  98. # ::
  99. #
  100. # calls only work properly within the same directory.
  101. #
  102. #
  103. #
  104. # ::
  105. #
  106. # ====================================================================
  107. #
  108. #
  109. #
  110. # PROTOBUF_GENERATE_CPP (public function)
  111. #
  112. # ::
  113. #
  114. # SRCS = Variable to define with autogenerated
  115. # source files
  116. # HDRS = Variable to define with autogenerated
  117. # header files
  118. # ARGN = proto files
  119. #
  120. #
  121. #
  122. # ::
  123. #
  124. # ====================================================================
  125. #=============================================================================
  126. # Copyright 2009 Kitware, Inc.
  127. # Copyright 2009-2011 Philip Lowman <[email protected]>
  128. # Copyright 2008 Esben Mose Hansen, Ange Optimization ApS
  129. #
  130. # Distributed under the OSI-approved BSD License (the "License");
  131. # see accompanying file Copyright.txt for details.
  132. #
  133. # This software is distributed WITHOUT ANY WARRANTY; without even the
  134. # implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  135. # See the License for more information.
  136. #=============================================================================
  137. # (To distribute this file outside of CMake, substitute the full
  138. # License text for the above reference.)
  139. function(PROTOBUF_GENERATE_CPP SRCS HDRS)
  140. if(NOT ARGN)
  141. message(SEND_ERROR "Error: PROTOBUF_GENERATE_CPP() called without any proto files")
  142. return()
  143. endif()
  144. if(PROTOBUF_GENERATE_CPP_APPEND_PATH)
  145. # Create an include path for each file specified
  146. foreach(FIL ${ARGN})
  147. get_filename_component(ABS_FIL ${FIL} ABSOLUTE)
  148. get_filename_component(ABS_PATH ${ABS_FIL} PATH)
  149. list(FIND _protobuf_include_path ${ABS_PATH} _contains_already)
  150. if(${_contains_already} EQUAL -1)
  151. list(APPEND _protobuf_include_path -I ${ABS_PATH})
  152. endif()
  153. endforeach()
  154. else()
  155. set(_protobuf_include_path -I ${CMAKE_CURRENT_SOURCE_DIR})
  156. endif()
  157. if(DEFINED PROTOBUF_IMPORT_DIRS)
  158. foreach(DIR ${PROTOBUF_IMPORT_DIRS})
  159. get_filename_component(ABS_PATH ${DIR} ABSOLUTE)
  160. list(FIND _protobuf_include_path ${ABS_PATH} _contains_already)
  161. if(${_contains_already} EQUAL -1)
  162. list(APPEND _protobuf_include_path -I ${ABS_PATH})
  163. endif()
  164. endforeach()
  165. endif()
  166. set(${SRCS})
  167. set(${HDRS})
  168. foreach(FIL ${ARGN})
  169. get_filename_component(ABS_FIL ${FIL} ABSOLUTE)
  170. get_filename_component(FIL_WE ${FIL} NAME_WE)
  171. list(APPEND ${SRCS} "${CMAKE_CURRENT_BINARY_DIR}/${FIL_WE}.pb.cc")
  172. list(APPEND ${HDRS} "${CMAKE_CURRENT_BINARY_DIR}/${FIL_WE}.pb.h")
  173. add_custom_command(
  174. OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/${FIL_WE}.pb.cc"
  175. "${CMAKE_CURRENT_BINARY_DIR}/${FIL_WE}.pb.h"
  176. COMMAND ${PROTOBUF_PROTOC_EXECUTABLE}
  177. ARGS --cpp_out ${CMAKE_CURRENT_BINARY_DIR} ${_protobuf_include_path} ${ABS_FIL}
  178. DEPENDS ${ABS_FIL} ${PROTOBUF_PROTOC_EXECUTABLE}
  179. COMMENT "Running C++ protocol buffer compiler on ${FIL}"
  180. VERBATIM )
  181. endforeach()
  182. set_source_files_properties(${${SRCS}} ${${HDRS}} PROPERTIES GENERATED TRUE)
  183. set(${SRCS} ${${SRCS}} PARENT_SCOPE)
  184. set(${HDRS} ${${HDRS}} PARENT_SCOPE)
  185. endfunction()
  186. # Internal function: search for normal library as well as a debug one
  187. # if the debug one is specified also include debug/optimized keywords
  188. # in *_LIBRARIES variable
  189. function(_protobuf_find_libraries name filename)
  190. find_library(${name}_LIBRARY
  191. NAMES ${filename}
  192. PATHS ${PROTOBUF_SRC_ROOT_FOLDER}/vsprojects/Release)
  193. mark_as_advanced(${name}_LIBRARY)
  194. find_library(${name}_LIBRARY_DEBUG
  195. NAMES ${filename}
  196. PATHS ${PROTOBUF_SRC_ROOT_FOLDER}/vsprojects/Debug)
  197. mark_as_advanced(${name}_LIBRARY_DEBUG)
  198. if(NOT ${name}_LIBRARY_DEBUG)
  199. # There is no debug library
  200. set(${name}_LIBRARY_DEBUG ${${name}_LIBRARY} PARENT_SCOPE)
  201. set(${name}_LIBRARIES ${${name}_LIBRARY} PARENT_SCOPE)
  202. else()
  203. # There IS a debug library
  204. set(${name}_LIBRARIES
  205. optimized ${${name}_LIBRARY}
  206. debug ${${name}_LIBRARY_DEBUG}
  207. PARENT_SCOPE
  208. )
  209. endif()
  210. endfunction()
  211. # Internal function: find threads library
  212. function(_protobuf_find_threads)
  213. set(CMAKE_THREAD_PREFER_PTHREAD TRUE)
  214. find_package(Threads)
  215. if(Threads_FOUND)
  216. list(APPEND PROTOBUF_LIBRARIES ${CMAKE_THREAD_LIBS_INIT})
  217. set(PROTOBUF_LIBRARIES "${PROTOBUF_LIBRARIES}" PARENT_SCOPE)
  218. endif()
  219. endfunction()
  220. #
  221. # Main.
  222. #
  223. # By default have PROTOBUF_GENERATE_CPP macro pass -I to protoc
  224. # for each directory where a proto file is referenced.
  225. if(NOT DEFINED PROTOBUF_GENERATE_CPP_APPEND_PATH)
  226. set(PROTOBUF_GENERATE_CPP_APPEND_PATH TRUE)
  227. endif()
  228. # Google's provided vcproj files generate libraries with a "lib"
  229. # prefix on Windows
  230. if(MSVC)
  231. set(PROTOBUF_ORIG_FIND_LIBRARY_PREFIXES "${CMAKE_FIND_LIBRARY_PREFIXES}")
  232. set(CMAKE_FIND_LIBRARY_PREFIXES "lib" "")
  233. find_path(PROTOBUF_SRC_ROOT_FOLDER protobuf.pc.in)
  234. endif()
  235. # The Protobuf library
  236. _protobuf_find_libraries(PROTOBUF protobuf)
  237. #DOC "The Google Protocol Buffers RELEASE Library"
  238. _protobuf_find_libraries(PROTOBUF_LITE protobuf-lite)
  239. # The Protobuf Protoc Library
  240. _protobuf_find_libraries(PROTOBUF_PROTOC protoc)
  241. # Restore original find library prefixes
  242. if(MSVC)
  243. set(CMAKE_FIND_LIBRARY_PREFIXES "${PROTOBUF_ORIG_FIND_LIBRARY_PREFIXES}")
  244. endif()
  245. if(UNIX)
  246. _protobuf_find_threads()
  247. endif()
  248. # Find the include directory
  249. find_path(PROTOBUF_INCLUDE_DIR
  250. google/protobuf/service.h
  251. PATHS ${PROTOBUF_SRC_ROOT_FOLDER}/src
  252. )
  253. mark_as_advanced(PROTOBUF_INCLUDE_DIR)
  254. # Find the protoc Executable
  255. find_program(PROTOBUF_PROTOC_EXECUTABLE
  256. NAMES protoc
  257. DOC "The Google Protocol Buffers Compiler"
  258. PATHS
  259. ${PROTOBUF_SRC_ROOT_FOLDER}/vsprojects/Release
  260. ${PROTOBUF_SRC_ROOT_FOLDER}/vsprojects/Debug
  261. )
  262. mark_as_advanced(PROTOBUF_PROTOC_EXECUTABLE)
  263. include(${CMAKE_CURRENT_LIST_DIR}/FindPackageHandleStandardArgs.cmake)
  264. FIND_PACKAGE_HANDLE_STANDARD_ARGS(PROTOBUF DEFAULT_MSG
  265. PROTOBUF_LIBRARY PROTOBUF_INCLUDE_DIR)
  266. if(PROTOBUF_FOUND)
  267. set(PROTOBUF_INCLUDE_DIRS ${PROTOBUF_INCLUDE_DIR})
  268. endif()