FindProtobuf.cmake 7.0 KB

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