FindProtobuf.cmake 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. # Esben Mose Hansen <[EMAIL PROTECTED]>, (c) Ange Optimization ApS 2008
  15. # Adapted by Philip Lowman <[email protected]> (c) 2009
  16. #
  17. # Redistribution and use is allowed according to the terms of the BSD license.
  18. # For details see the accompanying COPYING-CMAKE-SCRIPTS file.
  19. #
  20. #====================================================================
  21. # Example:
  22. #
  23. # find_package(Protobuf REQUIRED)
  24. # include_directories(${PROTOBUF_INCLUDE_DIRS})
  25. #
  26. # include_directories(${CMAKE_CURRENT_BINARY_DIR})
  27. # PROTOBUF_GENERATE_CPP(PROTO_SRCS PROTO_HDRS foo.proto)
  28. # add_executable(bar bar.cc ${PROTO_SRCS} ${PROTO_HDRS})
  29. # target_link_libraries(bar ${PROTOBUF_LIBRARY})
  30. #
  31. # NOTE: You may need to link against pthreads as well depending
  32. # on the platform.
  33. #====================================================================
  34. #==================================================
  35. # PROTOBUF_GENERATE_CPP (public function)
  36. # SRCS = Variable to define with autogenerated
  37. # source files
  38. # HDRS = Variable to define with autogenerated
  39. # header files
  40. # ARGN = proto files
  41. #==================================================
  42. function(PROTOBUF_GENERATE_CPP SRCS HDRS)
  43. if(NOT ARGN)
  44. message(SEND_ERROR "Error: PROTOBUF_GENERATE_CPP() called without any proto files")
  45. return()
  46. endif(NOT ARGN)
  47. set(${SRCS})
  48. set(${HDRS})
  49. foreach(FIL ${ARGN})
  50. get_filename_component(ABS_FIL ${FIL} ABSOLUTE)
  51. get_filename_component(FIL_WE ${FIL} NAME_WE)
  52. list(APPEND ${SRCS} "${CMAKE_CURRENT_BINARY_DIR}/${FIL_WE}.pb.cc")
  53. list(APPEND ${HDRS} "${CMAKE_CURRENT_BINARY_DIR}/${FIL_WE}.pb.h")
  54. add_custom_command(
  55. OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/${FIL_WE}.pb.cc"
  56. "${CMAKE_CURRENT_BINARY_DIR}/${FIL_WE}.pb.h"
  57. COMMAND ${PROTOBUF_PROTOC_EXECUTABLE}
  58. ARGS --cpp_out ${CMAKE_CURRENT_BINARY_DIR} --proto_path ${CMAKE_CURRENT_SOURCE_DIR} ${ABS_FIL}
  59. DEPENDS ${ABS_FIL}
  60. COMMENT "Running C++ protocol buffer compiler on ${FIL}"
  61. VERBATIM )
  62. endforeach()
  63. set_source_files_properties(${${SRCS}} ${${HDRS}} PROPERTIES GENERATED TRUE)
  64. set(${SRCS} ${${SRCS}} PARENT_SCOPE)
  65. set(${HDRS} ${${HDRS}} PARENT_SCOPE)
  66. endfunction()
  67. find_path(PROTOBUF_INCLUDE_DIR google/protobuf/service.h)
  68. # Google's provided vcproj files generate libraries with a "lib"
  69. # prefix on Windows
  70. if(WIN32)
  71. set(PROTOBUF_ORIG_FIND_LIBRARY_PREFIXES "${CMAKE_FIND_LIBRARY_PREFIXES}")
  72. set(CMAKE_FIND_LIBRARY_PREFIXES "lib" "")
  73. endif()
  74. find_library(PROTOBUF_LIBRARY NAMES protobuf
  75. DOC "The Google Protocol Buffers Library"
  76. )
  77. find_library(PROTOBUF_PROTOC_LIBRARY NAMES protoc
  78. DOC "The Google Protocol Buffers Compiler Library"
  79. )
  80. find_program(PROTOBUF_PROTOC_EXECUTABLE NAMES protoc
  81. DOC "The Google Protocol Buffers Compiler"
  82. )
  83. # Restore original find library prefixes
  84. if(WIN32)
  85. set(CMAKE_FIND_LIBRARY_PREFIXES ${PROTOBUF_ORIG_FIND_LIBRARY_PREFIXES})
  86. endif()
  87. include(FindPackageHandleStandardArgs)
  88. FIND_PACKAGE_HANDLE_STANDARD_ARGS(PROTOBUF DEFAULT_MSG
  89. PROTOBUF_LIBRARY PROTOBUF_INCLUDE_DIR)
  90. if(PROTOBUF_FOUND)
  91. set(PROTOBUF_INCLUDE_DIRS ${PROTOBUF_INCLUDE_DIR})
  92. set(PROTOBUF_LIBRARIES ${PROTOBUF_LIBRARY})
  93. endif()