QtProtobufCommon.cmake 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. find_package(QtProtobufProject COMPONENTS QtProtobuf QtGrpc REQUIRED)
  2. function(protobuf_generate_all)
  3. set(options)
  4. set(oneValueArgs OUT_DIR TARGET)
  5. set(multiValueArgs GENERATED_SOURCES PROTO_FILES)
  6. cmake_parse_arguments(protobuf_generate_all "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
  7. set(GEN_TARGET "${protobuf_generate_all_TARGET}_generate")
  8. foreach(PROTO_FILE IN LISTS protobuf_generate_all_PROTO_FILES)
  9. get_filename_component(BASE_DIR ${PROTO_FILE} DIRECTORY)
  10. set(PROTO_INCLUDES -I"${BASE_DIR}" ${PROTO_INCUDES})
  11. endforeach()
  12. if(NOT DEFINED protobuf_generate_all_OUT_DIR)
  13. set(OUT_DIR ${CMAKE_CURRENT_BINARY_DIR})
  14. else()
  15. set(OUT_DIR ${protobuf_generate_all_OUT_DIR})
  16. endif()
  17. file(MAKE_DIRECTORY ${OUT_DIR})
  18. if(NOT TARGET gRPC::grpc_cpp_plugin)
  19. message(FATAL_ERROR "gRPC plugin is not found")
  20. endif()
  21. add_custom_command(
  22. OUTPUT ${GENERATED_SOURCES}
  23. COMMAND $<TARGET_FILE:protobuf::protoc>
  24. ARGS --grpc_out="${OUT_DIR}"
  25. --cpp_out="${OUT_DIR}"
  26. ${PROTO_INCLUDES}
  27. --plugin=protoc-gen-grpc=$<TARGET_FILE:gRPC::grpc_cpp_plugin>
  28. ${protobuf_generate_all_PROTO_FILES}
  29. DEPENDS ${protobuf_generate_all_PROTO_FILES}
  30. )
  31. add_custom_target(${GEN_TARGET} DEPENDS ${protobuf_generate_all_PROTO_FILES} ${GENERATED_SOURCES})
  32. add_dependencies(${protobuf_generate_all_TARGET} ${GEN_TARGET})
  33. endfunction(protobuf_generate_all)
  34. function(add_test_target)
  35. set(options)
  36. set(oneValueArgs QML_DIR TARGET)
  37. set(multiValueArgs SOURCES GENERATED_HEADERS EXCLUDE_HEADERS PROTO_FILES)
  38. cmake_parse_arguments(add_test_target "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
  39. ## test sources build
  40. # policy enables automoc for generated files
  41. if(${CMAKE_VERSION} VERSION_GREATER "3.10.0")
  42. cmake_policy(SET CMP0071 NEW)
  43. endif()
  44. set(GENERATED_SOURCES_DIR ${CMAKE_CURRENT_BINARY_DIR}/generated)
  45. if(DEFINED add_test_target_PROTO_FILES)
  46. file(GLOB PROTO_FILES ABSOLUTE ${add_test_target_PROTO_FILES})
  47. else()
  48. file(GLOB PROTO_FILES ABSOLUTE ${CMAKE_CURRENT_SOURCE_DIR}/proto/*.proto)
  49. endif()
  50. generate_qtprotobuf(TARGET ${add_test_target_TARGET}
  51. OUT_DIR ${GENERATED_SOURCES_DIR}
  52. PROTO_FILES ${PROTO_FILES}
  53. GENERATED_HEADERS ${add_test_target_GENERATED_HEADERS}
  54. EXCLUDE_HEADERS ${add_test_target_EXCLUDE_HEADERS})
  55. add_executable(${add_test_target_TARGET} ${add_test_target_SOURCES})
  56. if(Qt5_POSITION_INDEPENDENT_CODE)
  57. set_target_properties(${add_test_target_TARGET} PROPERTIES POSITION_INDEPENDENT_CODE FALSE)
  58. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fPIC")
  59. endif()
  60. add_dependencies(${add_test_target_TARGET} ${QtProtobuf_GENERATED})
  61. target_link_libraries(${add_test_target_TARGET} gtest_main gtest ${QtProtobuf_GENERATED} ${QTPROTOBUF_COMMON_NAMESPACE}::QtProtobuf ${QTPROTOBUF_COMMON_NAMESPACE}::QtGrpc Qt5::Core Qt5::Qml Qt5::Network)
  62. endfunction(add_test_target)
  63. function(add_target_qml)
  64. set(options)
  65. set(oneValueArgs TARGET QMLDIR_FILE)
  66. set(multiValueArgs QML_FILES)
  67. cmake_parse_arguments(add_target_qml "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
  68. add_custom_target(${add_target_qml_TARGET}_qml DEPENDS ${add_qml_QML_FILES} ${add_target_qml_QMLDIR_FILE})
  69. foreach(QML_FILE IN LISTS add_target_qml_QML_FILES)
  70. add_custom_command(TARGET ${add_target_qml_TARGET}_qml COMMAND ${CMAKE_COMMAND} -E copy ${QML_FILE}
  71. ${CMAKE_CURRENT_BINARY_DIR})
  72. endforeach()
  73. if(DEFINED ${add_target_qml_QMLDIR_FILE})
  74. add_custom_command(TARGET ${add_target_qml_TARGET}_qml COMMAND ${CMAKE_COMMAND} -E copy ${add_qml_QMLDIR_FILE}
  75. ${CMAKE_CURRENT_BINARY_DIR})
  76. endif()
  77. add_dependencies(${add_target_qml_TARGET} ${add_target_qml_TARGET}_qml)
  78. endfunction()
  79. function(add_target_windeployqt)
  80. if(WIN32)
  81. set(options)
  82. set(oneValueArgs QML_DIR TARGET)
  83. set(multiValueArgs)
  84. cmake_parse_arguments(add_target_windeployqt "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
  85. find_program(WINDEPLOYQT_EXECUTABLE "windeployqt" PATHS ${PATH})
  86. if(WINDEPLOYQT_EXECUTABLE STREQUAL WINDEPLOYQT_EXECUTABLE-NOTFOUND)
  87. message(WARNING "windeployqt is not found in specified PATH! Please, copy dependencies manually")
  88. else()
  89. if(DEFINED add_target_windeployqt_QML_DIR)
  90. set(QML_DIR --qmldir ${add_target_windeployqt_QML_DIR})
  91. endif()
  92. add_custom_command(TARGET ${add_target_windeployqt_TARGET} POST_BUILD
  93. COMMAND ${CMAKE_COMMAND} -E copy $<TARGET_FILE:QtProtobuf> $<TARGET_FILE_DIR:${add_target_windeployqt_TARGET}>
  94. COMMAND ${CMAKE_COMMAND} -E copy $<TARGET_FILE:QtGrpc> $<TARGET_FILE_DIR:${add_target_windeployqt_TARGET}>
  95. COMMAND ${WINDEPLOYQT_EXECUTABLE} ${QML_DIR} $<TARGET_FILE_DIR:${add_target_windeployqt_TARGET}>)
  96. endif()
  97. endif()
  98. endfunction()