QtProtobufTestHelpers.cmake 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. function(qt_protobuf_internal_protobuf_generate_all)
  2. set(options)
  3. set(oneValueArgs OUT_DIR TARGET)
  4. set(multiValueArgs GENERATED_SOURCES PROTO_FILES)
  5. cmake_parse_arguments(arg "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
  6. set(GEN_TARGET "${arg_TARGET}_generate")
  7. foreach(PROTO_FILE IN LISTS arg_PROTO_FILES)
  8. get_filename_component(BASE_DIR "${PROTO_FILE}" DIRECTORY)
  9. list(APPEND PROTO_INCLUDES "-I${BASE_DIR}")
  10. endforeach()
  11. if(NOT DEFINED arg_OUT_DIR)
  12. set(OUT_DIR "${CMAKE_CURRENT_BINARY_DIR}")
  13. else()
  14. set(OUT_DIR "${arg_OUT_DIR}")
  15. endif()
  16. file(MAKE_DIRECTORY "${OUT_DIR}")
  17. if(NOT TARGET gRPC::grpc_cpp_plugin)
  18. message(FATAL_ERROR "gRPC plugin is not found")
  19. endif()
  20. add_custom_command(
  21. OUTPUT ${GENERATED_SOURCES}
  22. COMMAND protobuf::protoc
  23. ARGS --grpc_out="${OUT_DIR}"
  24. --cpp_out="${OUT_DIR}"
  25. ${PROTO_INCLUDES}
  26. "--plugin=protoc-gen-grpc=$<TARGET_FILE:gRPC::grpc_cpp_plugin>"
  27. ${arg_PROTO_FILES}
  28. DEPENDS ${arg_PROTO_FILES}
  29. )
  30. add_custom_target(${GEN_TARGET} DEPENDS ${arg_PROTO_FILES} ${GENERATED_SOURCES})
  31. add_dependencies(${arg_TARGET} ${GEN_TARGET})
  32. endfunction()
  33. function(qt_protobuf_internal_add_target_qml)
  34. set(options)
  35. set(oneValueArgs TARGET QMLDIR_FILE)
  36. set(multiValueArgs QML_FILES)
  37. cmake_parse_arguments(arg "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
  38. add_custom_target(${arg_TARGET}_qml DEPENDS ${arg_QML_FILES} ${arg_QMLDIR_FILE})
  39. foreach(QML_FILE IN LISTS arg_QML_FILES)
  40. add_custom_command(TARGET ${arg_TARGET}_qml
  41. COMMAND ${CMAKE_COMMAND} -E copy "${QML_FILE}" "${CMAKE_CURRENT_BINARY_DIR}"
  42. )
  43. endforeach()
  44. if(DEFINED ${arg_QMLDIR_FILE})
  45. add_custom_command(TARGET ${arg_TARGET}_qml
  46. COMMAND ${CMAKE_COMMAND} -E copy "${arg_QMLDIR_FILE}" "${CMAKE_CURRENT_BINARY_DIR}"
  47. )
  48. endif()
  49. add_dependencies(${arg_TARGET} ${arg_TARGET}_qml)
  50. endfunction()
  51. function(qt_protobuf_internal_add_target_windeployqt)
  52. if(WIN32)
  53. set(options)
  54. set(oneValueArgs "QML_DIR" "TARGET")
  55. set(multiValueArgs)
  56. cmake_parse_arguments(arg "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
  57. find_program(WINDEPLOYQT_EXECUTABLE "windeployqt" PATHS ${PATH})
  58. if(WINDEPLOYQT_EXECUTABLE)
  59. if(DEFINED arg_QML_DIR)
  60. set(QML_DIR --qmldir ${arg_QML_DIR})
  61. endif()
  62. add_custom_command(TARGET ${arg_TARGET} POST_BUILD
  63. COMMAND ${CMAKE_COMMAND}
  64. -E copy $<TARGET_FILE:${QT_PROTOBUF_NAMESPACE}::Protobuf>
  65. $<TARGET_FILE_DIR:${arg_TARGET}>
  66. COMMAND ${CMAKE_COMMAND}
  67. -E copy $<TARGET_FILE:${QT_PROTOBUF_NAMESPACE}::Grpc>
  68. $<TARGET_FILE_DIR:${arg_TARGET}>
  69. COMMAND ${WINDEPLOYQT_EXECUTABLE}
  70. "${QML_DIR}" $<TARGET_FILE_DIR:${arg_TARGET}>)
  71. else()
  72. message(WARNING "windeployqt is not found in specified PATH! \
  73. Please, copy dependencies manually")
  74. endif()
  75. endif()
  76. endfunction()
  77. macro(qt_protobuf_internal_find_dependencies)
  78. find_package(${QT_VERSIONED_PREFIX} COMPONENTS Core Qml Network REQUIRED)
  79. find_package(Threads REQUIRED)
  80. if(NOT TARGET ${QT_PROTOBUF_NAMESPACE}::Protobuf)
  81. find_package(${QT_PROTOBUF_NAMESPACE} COMPONENTS Protobuf REQUIRED)
  82. endif()
  83. if(NOT TARGET ${QT_PROTOBUF_NAMESPACE}::Grpc)
  84. find_package(${QT_PROTOBUF_NAMESPACE} COMPONENTS Grpc REQUIRED)
  85. endif()
  86. if(QT_PROTOBUF_STATIC)
  87. add_definitions(-DQT_PROTOBUF_STATIC)# add_definitions is used because old cmake versions
  88. # compatibility
  89. endif()
  90. set(CMAKE_AUTOMOC ON)
  91. set(CMAKE_AUTORCC ON)
  92. endmacro()