FindProtobuf.cmake 6.9 KB

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