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

protobuf.cmake 3.5 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. function(PROTOBUF_GENERATE_CPP_WITH_ROOT SRCS HDRS ROOT_DIR)
  2. if(NOT ARGN)
  3. message(SEND_ERROR "Error: PROTOBUF_GENERATE_CPP_WITH_ROOT() called without any proto files")
  4. return()
  5. endif()
  6. set(${SRCS})
  7. set(${HDRS})
  8. foreach(FIL ${ARGN})
  9. set(ABS_FIL ${ROOT_DIR}/${FIL})
  10. get_filename_component(FIL_WE ${FIL} NAME_WE)
  11. get_filename_component(FIL_DIR ${ABS_FIL} PATH)
  12. file(RELATIVE_PATH REL_DIR ${ROOT_DIR} ${FIL_DIR})
  13. list(APPEND ${SRCS} "${CMAKE_CURRENT_BINARY_DIR}/${FIL_WE}.pb.cc")
  14. list(APPEND ${HDRS} "${CMAKE_CURRENT_BINARY_DIR}/${FIL_WE}.pb.h")
  15. add_custom_command(
  16. OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/${FIL_WE}.pb.cc"
  17. "${CMAKE_CURRENT_BINARY_DIR}/${FIL_WE}.pb.h"
  18. COMMAND ${PROTOBUF_PROTOC_EXECUTABLE}
  19. ARGS --cpp_out ${CMAKE_CURRENT_BINARY_DIR} -I ${FIL_DIR} ${ABS_FIL} -I ${PROTOBUF_INCLUDE_DIRS}
  20. DEPENDS ${ABS_FIL} libprotobuf
  21. COMMENT "Running C++ protocol buffer compiler on ${FIL}"
  22. VERBATIM)
  23. endforeach()
  24. set_source_files_properties(${${SRCS}} ${${HDRS}} PROPERTIES GENERATED TRUE)
  25. set(${SRCS} ${${SRCS}} PARENT_SCOPE)
  26. set(${HDRS} ${${HDRS}} PARENT_SCOPE)
  27. endfunction()
  28. if(MGE_USE_SYSTEM_LIB)
  29. find_package(Protobuf)
  30. if(Protobuf_FOUND)
  31. add_library(libprotobuf INTERFACE)
  32. target_link_libraries(libprotobuf INTERFACE ${Protobuf_LIBRARIES})
  33. target_include_directories(libprotobuf INTERFACE ${Protobuf_INCLUDE_DIRS})
  34. get_filename_component(Protobuf_ROOT ${Protobuf_INCLUDE_DIR} DIRECTORY)
  35. set(PROTOBUF_ROOT ${Protobuf_ROOT})
  36. set(PROTOBUF_PROTOC_EXECUTABLE ${Protobuf_PROTOC_EXECUTABLE})
  37. set(PROTOBUF_INCLUDE_DIRS ${Protobuf_INCLUDE_DIRS})
  38. return()
  39. endif()
  40. endif()
  41. include(ExternalProject)
  42. include(GNUInstallDirs)
  43. set(PROTOBUF_DIR "${PROJECT_SOURCE_DIR}/third_party/protobuf" CACHE STRING "protobuf directory")
  44. set(PROTOBUF_BUILD_DIR ${PROJECT_BINARY_DIR}/third_party/protobuf)
  45. if(${CMAKE_BUILD_TYPE} STREQUAL "Debug")
  46. set(PROTOBUF_LIB ${PROTOBUF_BUILD_DIR}/${CMAKE_INSTALL_LIBDIR}/libprotobufd.a)
  47. else()
  48. set(PROTOBUF_LIB ${PROTOBUF_BUILD_DIR}/${CMAKE_INSTALL_LIBDIR}/libprotobuf.a)
  49. endif()
  50. set(PROTOBUF_PROTOC_EXECUTABLE ${PROTOBUF_BUILD_DIR}/bin/protoc)
  51. ExternalProject_add(
  52. protobuf
  53. SOURCE_DIR ${PROTOBUF_DIR}/cmake
  54. PREFIX ${PROTOBUF_BUILD_DIR}
  55. CMAKE_ARGS -DCMAKE_C_COMPILER_LAUNCHER=${CMAKE_C_COMPILER_LAUNCHER} -DCMAKE_CXX_COMPILER_LAUNCHER=${CMAKE_CXX_COMPILER_LAUNCHER} -DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE} -DCMAKE_INSTALL_PREFIX=${PROTOBUF_BUILD_DIR} -Dprotobuf_BUILD_EXAMPLES=OFF -Dprotobuf_BUILD_TESTS=OFF -DBUILD_SHARED_LIBS=OFF -DCMAKE_POSITION_INDEPENDENT_CODE=ON
  56. BUILD_BYPRODUCTS ${PROTOBUF_LIB} ${PROTOBUF_PROTOC_EXECUTABLE}
  57. )
  58. set(PROTOBUF_INC ${PROTOBUF_BUILD_DIR}/include)
  59. file(MAKE_DIRECTORY ${PROTOBUF_INC})
  60. add_library(libprotobuf STATIC IMPORTED GLOBAL)
  61. add_dependencies(libprotobuf protobuf)
  62. set_target_properties(
  63. libprotobuf PROPERTIES
  64. IMPORTED_LOCATION ${PROTOBUF_LIB}
  65. INTERFACE_INCLUDE_DIRECTORIES ${PROTOBUF_BUILD_DIR}/include
  66. )
  67. add_executable(protoc IMPORTED GLOBAL)
  68. add_dependencies(protoc protobuf)
  69. set_target_properties(
  70. protoc PROPERTIES
  71. IMPORTED_LOCATION ${PROTOBUF_BUILD_DIR}/bin/protoc
  72. )
  73. set(PROTOBUF_ROOT ${PROTOBUF_BUILD_DIR})
  74. set(PROTOBUF_PROTOC_EXECUTABLE protoc)
  75. set(PROTOBUF_INCLUDE_DIRS ${PROTOBUF_BUILD_DIR}/include)