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

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