FindProtobuf.cmake 7.5 KB

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