You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

125 lines
4.5 KiB

  1. #
  2. # Locate and configure the Google Protocol Buffers library
  3. #
  4. # Adds the following targets:
  5. #
  6. # protobuf::libprotobuf - Protobuf library
  7. # protobuf::libprotobuf-lite - Protobuf lite library
  8. # protobuf::libprotoc - Protobuf Protoc Library
  9. # protobuf::protoc - protoc executable
  10. #
  11. #
  12. # Generates C++ sources from the .proto files
  13. #
  14. # protobuf_generate_cpp (<SRCS> <HDRS> <DEST> [<ARGN>...])
  15. #
  16. # SRCS - variable to define with autogenerated source files
  17. # HDRS - variable to define with autogenerated header files
  18. # DEST - directory where the source files will be created
  19. # ARGN - .proto files
  20. #
  21. function(PROTOBUF_GENERATE_CPP SRCS HDRS DEST)
  22. if(NOT ARGN)
  23. message(SEND_ERROR "Error: PROTOBUF_GENERATE_CPP() called without any proto files")
  24. return()
  25. endif()
  26. if(PROTOBUF_GENERATE_CPP_APPEND_PATH)
  27. # Create an include path for each file specified
  28. foreach(FIL ${ARGN})
  29. get_filename_component(ABS_FIL ${FIL} ABSOLUTE)
  30. get_filename_component(ABS_PATH ${ABS_FIL} PATH)
  31. list(FIND _protobuf_include_path ${ABS_PATH} _contains_already)
  32. if(${_contains_already} EQUAL -1)
  33. list(APPEND _protobuf_include_path -I ${ABS_PATH})
  34. endif()
  35. endforeach()
  36. else()
  37. set(_protobuf_include_path -I ${CMAKE_CURRENT_SOURCE_DIR})
  38. endif()
  39. if(DEFINED PROTOBUF_IMPORT_DIRS)
  40. foreach(DIR ${PROTOBUF_IMPORT_DIRS})
  41. get_filename_component(ABS_PATH ${DIR} ABSOLUTE)
  42. list(FIND _protobuf_include_path ${ABS_PATH} _contains_already)
  43. if(${_contains_already} EQUAL -1)
  44. list(APPEND _protobuf_include_path -I ${ABS_PATH})
  45. endif()
  46. endforeach()
  47. endif()
  48. set(${SRCS})
  49. set(${HDRS})
  50. foreach(FIL ${ARGN})
  51. get_filename_component(ABS_FIL ${FIL} ABSOLUTE)
  52. get_filename_component(FIL_WE ${FIL} NAME_WE)
  53. list(APPEND ${SRCS} "${DEST}/${FIL_WE}.pb.cc")
  54. list(APPEND ${HDRS} "${DEST}/${FIL_WE}.pb.h")
  55. add_custom_command(
  56. OUTPUT "${DEST}/${FIL_WE}.pb.cc"
  57. "${DEST}/${FIL_WE}.pb.h"
  58. COMMAND protobuf::protoc
  59. ARGS --cpp_out ${DEST} ${_protobuf_include_path} ${ABS_FIL}
  60. DEPENDS ${ABS_FIL} protobuf::protoc
  61. COMMENT "Running C++ protocol buffer compiler on ${FIL}"
  62. VERBATIM )
  63. endforeach()
  64. set_source_files_properties(${${SRCS}} ${${HDRS}} PROPERTIES GENERATED TRUE)
  65. set(${SRCS} ${${SRCS}} PARENT_SCOPE)
  66. set(${HDRS} ${${HDRS}} PARENT_SCOPE)
  67. endfunction()
  68. # By default have PROTOBUF_GENERATE_CPP macro pass -I to protoc
  69. # for each directory where a proto file is referenced.
  70. if(NOT DEFINED PROTOBUF_GENERATE_CPP_APPEND_PATH)
  71. set(PROTOBUF_GENERATE_CPP_APPEND_PATH TRUE)
  72. endif()
  73. # Find the include directory
  74. find_path(PROTOBUF_INCLUDE_DIR google/protobuf/service.h)
  75. mark_as_advanced(PROTOBUF_INCLUDE_DIR)
  76. # The Protobuf library
  77. find_library(PROTOBUF_LIBRARY NAMES protobuf)
  78. mark_as_advanced(PROTOBUF_LIBRARY)
  79. add_library(protobuf::libprotobuf UNKNOWN IMPORTED)
  80. set_target_properties(protobuf::libprotobuf PROPERTIES
  81. INTERFACE_INCLUDE_DIRECTORIES ${PROTOBUF_INCLUDE_DIR}
  82. INTERFACE_LINK_LIBRARIES pthread
  83. IMPORTED_LOCATION ${PROTOBUF_LIBRARY}
  84. )
  85. # The Protobuf lite library
  86. find_library(PROTOBUF_LITE_LIBRARY NAMES protobuf-lite)
  87. mark_as_advanced(PROTOBUF_LITE_LIBRARY)
  88. add_library(protobuf::libprotobuf-lite UNKNOWN IMPORTED)
  89. set_target_properties(protobuf::libprotobuf-lite PROPERTIES
  90. INTERFACE_INCLUDE_DIRECTORIES ${PROTOBUF_INCLUDE_DIR}
  91. INTERFACE_LINK_LIBRARIES pthread
  92. IMPORTED_LOCATION ${PROTOBUF_LITE_LIBRARY}
  93. )
  94. # The Protobuf Protoc Library
  95. find_library(PROTOBUF_PROTOC_LIBRARY NAMES protoc)
  96. mark_as_advanced(PROTOBUF_PROTOC_LIBRARY)
  97. add_library(protobuf::libprotoc UNKNOWN IMPORTED)
  98. set_target_properties(protobuf::libprotoc PROPERTIES
  99. INTERFACE_INCLUDE_DIRECTORIES ${PROTOBUF_INCLUDE_DIR}
  100. INTERFACE_LINK_LIBRARIES protobuf::libprotobuf
  101. IMPORTED_LOCATION ${PROTOBUF_PROTOC_LIBRARY}
  102. )
  103. # Find the protoc Executable
  104. find_program(PROTOBUF_PROTOC_EXECUTABLE NAMES protoc)
  105. mark_as_advanced(PROTOBUF_PROTOC_EXECUTABLE)
  106. add_executable(protobuf::protoc IMPORTED)
  107. set_target_properties(protobuf::protoc PROPERTIES
  108. IMPORTED_LOCATION ${PROTOBUF_PROTOC_EXECUTABLE}
  109. )
  110. include(${CMAKE_ROOT}/Modules/FindPackageHandleStandardArgs.cmake)
  111. FIND_PACKAGE_HANDLE_STANDARD_ARGS(Protobuf DEFAULT_MSG
  112. PROTOBUF_LIBRARY PROTOBUF_INCLUDE_DIR PROTOBUF_PROTOC_EXECUTABLE)