FindProtobuf.cmake 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. # Locate and configure the Google Protocol Buffers library.
  2. # Defines the following variables:
  3. #
  4. # PROTOBUF_FOUND - Found the Google Protocol Buffers library
  5. # PROTOBUF_INCLUDE_DIRS - Include directories for Google Protocol Buffers
  6. # PROTOBUF_LIBRARIES - The protobuf library
  7. #
  8. # The following cache variables are also defined:
  9. # PROTOBUF_LIBRARY - The protobuf library
  10. # PROTOBUF_PROTOC_LIBRARY - The protoc library
  11. # PROTOBUF_INCLUDE_DIR - The include directory for protocol buffers
  12. # PROTOBUF_PROTOC_EXECUTABLE - The protoc compiler
  13. #
  14. #====================================================================
  15. # Example:
  16. #
  17. # find_package(Protobuf REQUIRED)
  18. # include_directories(${PROTOBUF_INCLUDE_DIRS})
  19. #
  20. # include_directories(${CMAKE_CURRENT_BINARY_DIR})
  21. # PROTOBUF_GENERATE_CPP(PROTO_SRCS PROTO_HDRS foo.proto)
  22. # add_executable(bar bar.cc ${PROTO_SRCS} ${PROTO_HDRS})
  23. # target_link_libraries(bar ${PROTOBUF_LIBRARY})
  24. #
  25. # NOTE: You may need to link against pthreads, depending
  26. # on the platform.
  27. #====================================================================
  28. #
  29. # PROTOBUF_GENERATE_CPP (public function)
  30. # SRCS = Variable to define with autogenerated
  31. # source files
  32. # HDRS = Variable to define with autogenerated
  33. # header files
  34. # ARGN = proto files
  35. #
  36. #====================================================================
  37. #=============================================================================
  38. # Copyright 2009 Kitware, Inc.
  39. # Copyright 2009 Philip Lowman <[email protected]>
  40. # Copyright 2008 Esben Mose Hansen, Ange Optimization ApS
  41. #
  42. # Distributed under the OSI-approved BSD License (the "License");
  43. # see accompanying file Copyright.txt for details.
  44. #
  45. # This software is distributed WITHOUT ANY WARRANTY; without even the
  46. # implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  47. # See the License for more information.
  48. #=============================================================================
  49. # (To distributed this file outside of CMake, substitute the full
  50. # License text for the above reference.)
  51. function(PROTOBUF_GENERATE_CPP SRCS HDRS)
  52. if(NOT ARGN)
  53. message(SEND_ERROR "Error: PROTOBUF_GENERATE_CPP() called without any proto files")
  54. return()
  55. endif(NOT ARGN)
  56. set(${SRCS})
  57. set(${HDRS})
  58. foreach(FIL ${ARGN})
  59. get_filename_component(ABS_FIL ${FIL} ABSOLUTE)
  60. get_filename_component(FIL_WE ${FIL} NAME_WE)
  61. list(APPEND ${SRCS} "${CMAKE_CURRENT_BINARY_DIR}/${FIL_WE}.pb.cc")
  62. list(APPEND ${HDRS} "${CMAKE_CURRENT_BINARY_DIR}/${FIL_WE}.pb.h")
  63. add_custom_command(
  64. OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/${FIL_WE}.pb.cc"
  65. "${CMAKE_CURRENT_BINARY_DIR}/${FIL_WE}.pb.h"
  66. COMMAND ${PROTOBUF_PROTOC_EXECUTABLE}
  67. ARGS --cpp_out ${CMAKE_CURRENT_BINARY_DIR} --proto_path ${CMAKE_CURRENT_SOURCE_DIR} ${ABS_FIL}
  68. DEPENDS ${ABS_FIL}
  69. COMMENT "Running C++ protocol buffer compiler on ${FIL}"
  70. VERBATIM )
  71. endforeach()
  72. set_source_files_properties(${${SRCS}} ${${HDRS}} PROPERTIES GENERATED TRUE)
  73. set(${SRCS} ${${SRCS}} PARENT_SCOPE)
  74. set(${HDRS} ${${HDRS}} PARENT_SCOPE)
  75. endfunction()
  76. find_path(PROTOBUF_INCLUDE_DIR google/protobuf/service.h)
  77. # Google's provided vcproj files generate libraries with a "lib"
  78. # prefix on Windows
  79. if(WIN32)
  80. set(PROTOBUF_ORIG_FIND_LIBRARY_PREFIXES "${CMAKE_FIND_LIBRARY_PREFIXES}")
  81. set(CMAKE_FIND_LIBRARY_PREFIXES "lib" "")
  82. endif()
  83. find_library(PROTOBUF_LIBRARY NAMES protobuf
  84. DOC "The Google Protocol Buffers Library"
  85. )
  86. find_library(PROTOBUF_PROTOC_LIBRARY NAMES protoc
  87. DOC "The Google Protocol Buffers Compiler Library"
  88. )
  89. find_program(PROTOBUF_PROTOC_EXECUTABLE NAMES protoc
  90. DOC "The Google Protocol Buffers Compiler"
  91. )
  92. mark_as_advanced(PROTOBUF_INCLUDE_DIR
  93. PROTOBUF_LIBRARY
  94. PROTOBUF_PROTOC_LIBRARY
  95. PROTOBUF_PROTOC_EXECUTABLE)
  96. # Restore original find library prefixes
  97. if(WIN32)
  98. set(CMAKE_FIND_LIBRARY_PREFIXES "${PROTOBUF_ORIG_FIND_LIBRARY_PREFIXES}")
  99. endif()
  100. include(FindPackageHandleStandardArgs)
  101. FIND_PACKAGE_HANDLE_STANDARD_ARGS(PROTOBUF DEFAULT_MSG
  102. PROTOBUF_LIBRARY PROTOBUF_INCLUDE_DIR)
  103. if(PROTOBUF_FOUND)
  104. set(PROTOBUF_INCLUDE_DIRS ${PROTOBUF_INCLUDE_DIR})
  105. set(PROTOBUF_LIBRARIES ${PROTOBUF_LIBRARY})
  106. endif()