QtProtobufCommon.cmake 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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_INCLUDES})
  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 MULTI QML)
  37. set(multiValueArgs SOURCES GENERATED_HEADERS EXCLUDE_HEADERS PROTO_FILES PROTO_INCLUDES)
  38. cmake_parse_arguments(add_test_target "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
  39. find_package(Threads REQUIRED)
  40. find_package(Qt5 COMPONENTS Test REQUIRED)
  41. ## test sources build
  42. # policy enables automoc for generated files
  43. if(${CMAKE_VERSION} VERSION_GREATER "3.10.0")
  44. cmake_policy(SET CMP0071 NEW)
  45. endif()
  46. set(GENERATED_SOURCES_DIR ${CMAKE_CURRENT_BINARY_DIR}/generated)
  47. if(DEFINED add_test_target_PROTO_FILES)
  48. file(GLOB PROTO_FILES ABSOLUTE ${add_test_target_PROTO_FILES})
  49. else()
  50. file(GLOB PROTO_FILES ABSOLUTE ${CMAKE_CURRENT_SOURCE_DIR}/proto/*.proto)
  51. endif()
  52. generate_qtprotobuf(TARGET ${add_test_target_TARGET}
  53. OUT_DIR ${GENERATED_SOURCES_DIR}
  54. PROTO_FILES ${PROTO_FILES}
  55. GENERATED_HEADERS ${add_test_target_GENERATED_HEADERS}
  56. EXCLUDE_HEADERS ${add_test_target_EXCLUDE_HEADERS}
  57. MULTI ${add_test_target_MULTI}
  58. QML ${add_test_target_QML}
  59. PROTO_INCLUDES ${add_test_target_PROTO_INCLUDES})
  60. add_executable(${add_test_target_TARGET} ${add_test_target_SOURCES})
  61. if(Qt5_POSITION_INDEPENDENT_CODE)
  62. set_target_properties(${add_test_target_TARGET} PROPERTIES POSITION_INDEPENDENT_CODE FALSE)
  63. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fPIC")
  64. endif()
  65. add_dependencies(${add_test_target_TARGET} ${QtProtobuf_GENERATED})
  66. target_link_libraries(${add_test_target_TARGET} gtest_main gtest ${QtProtobuf_GENERATED} ${QTPROTOBUF_COMMON_NAMESPACE}::QtProtobuf ${QTPROTOBUF_COMMON_NAMESPACE}::QtGrpc Qt5::Core Qt5::Test Qt5::Network ${CMAKE_THREAD_LIBS_INIT})
  67. if (${add_test_target_QML})
  68. target_link_libraries(${add_test_target_TARGET} Qt5::Qml)
  69. endif()
  70. endfunction(add_test_target)
  71. function(add_target_qml)
  72. set(options)
  73. set(oneValueArgs TARGET QMLDIR_FILE)
  74. set(multiValueArgs QML_FILES)
  75. cmake_parse_arguments(add_target_qml "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
  76. add_custom_target(${add_target_qml_TARGET}_qml DEPENDS ${add_qml_QML_FILES} ${add_target_qml_QMLDIR_FILE})
  77. foreach(QML_FILE IN LISTS add_target_qml_QML_FILES)
  78. add_custom_command(TARGET ${add_target_qml_TARGET}_qml COMMAND ${CMAKE_COMMAND} -E copy ${QML_FILE}
  79. ${CMAKE_CURRENT_BINARY_DIR})
  80. endforeach()
  81. if(DEFINED ${add_target_qml_QMLDIR_FILE})
  82. add_custom_command(TARGET ${add_target_qml_TARGET}_qml COMMAND ${CMAKE_COMMAND} -E copy ${add_qml_QMLDIR_FILE}
  83. ${CMAKE_CURRENT_BINARY_DIR})
  84. endif()
  85. add_dependencies(${add_target_qml_TARGET} ${add_target_qml_TARGET}_qml)
  86. endfunction()
  87. function(add_target_windeployqt)
  88. if(WIN32)
  89. set(options)
  90. set(oneValueArgs QML_DIR TARGET)
  91. set(multiValueArgs)
  92. cmake_parse_arguments(add_target_windeployqt "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
  93. find_program(WINDEPLOYQT_EXECUTABLE "windeployqt" PATHS ${PATH})
  94. if(WINDEPLOYQT_EXECUTABLE STREQUAL WINDEPLOYQT_EXECUTABLE-NOTFOUND)
  95. message(WARNING "windeployqt is not found in specified PATH! Please, copy dependencies manually")
  96. else()
  97. if(DEFINED add_target_windeployqt_QML_DIR)
  98. set(QML_DIR --qmldir ${add_target_windeployqt_QML_DIR})
  99. endif()
  100. add_custom_command(TARGET ${add_target_windeployqt_TARGET} POST_BUILD
  101. COMMAND ${CMAKE_COMMAND} -E copy $<TARGET_FILE:QtProtobuf> $<TARGET_FILE_DIR:${add_target_windeployqt_TARGET}>
  102. COMMAND ${CMAKE_COMMAND} -E copy $<TARGET_FILE:QtGrpc> $<TARGET_FILE_DIR:${add_target_windeployqt_TARGET}>
  103. COMMAND ${WINDEPLOYQT_EXECUTABLE} ${QML_DIR} $<TARGET_FILE_DIR:${add_target_windeployqt_TARGET}>)
  104. endif()
  105. endif()
  106. endfunction()