QtProtobufCommon.cmake 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. function(protobuf_generate_all)
  2. set(options)
  3. set(oneValueArgs OUT_DIR TARGET)
  4. set(multiValueArgs GENERATED_SOURCES PROTO_FILES)
  5. cmake_parse_arguments(protobuf_generate_all "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
  6. set(GEN_TARGET "${protobuf_generate_all_TARGET}_generate")
  7. foreach(PROTO_FILE IN LISTS protobuf_generate_all_PROTO_FILES)
  8. get_filename_component(BASE_DIR ${PROTO_FILE} DIRECTORY)
  9. set(PROTO_INCLUDES -I"${BASE_DIR}" ${PROTO_INCLUDES})
  10. endforeach()
  11. if(NOT DEFINED protobuf_generate_all_OUT_DIR)
  12. set(OUT_DIR ${CMAKE_CURRENT_BINARY_DIR})
  13. else()
  14. set(OUT_DIR ${protobuf_generate_all_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 $<TARGET_FILE: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. ${protobuf_generate_all_PROTO_FILES}
  28. DEPENDS ${protobuf_generate_all_PROTO_FILES}
  29. )
  30. add_custom_target(${GEN_TARGET} DEPENDS ${protobuf_generate_all_PROTO_FILES} ${GENERATED_SOURCES})
  31. add_dependencies(${protobuf_generate_all_TARGET} ${GEN_TARGET})
  32. endfunction(protobuf_generate_all)
  33. function(add_target_qml)
  34. set(options)
  35. set(oneValueArgs TARGET QMLDIR_FILE)
  36. set(multiValueArgs QML_FILES)
  37. cmake_parse_arguments(add_target_qml "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
  38. add_custom_target(${add_target_qml_TARGET}_qml DEPENDS ${add_qml_QML_FILES} ${add_target_qml_QMLDIR_FILE})
  39. foreach(QML_FILE IN LISTS add_target_qml_QML_FILES)
  40. add_custom_command(TARGET ${add_target_qml_TARGET}_qml COMMAND ${CMAKE_COMMAND} -E copy ${QML_FILE}
  41. ${CMAKE_CURRENT_BINARY_DIR})
  42. endforeach()
  43. if(DEFINED ${add_target_qml_QMLDIR_FILE})
  44. add_custom_command(TARGET ${add_target_qml_TARGET}_qml COMMAND ${CMAKE_COMMAND} -E copy ${add_qml_QMLDIR_FILE}
  45. ${CMAKE_CURRENT_BINARY_DIR})
  46. endif()
  47. add_dependencies(${add_target_qml_TARGET} ${add_target_qml_TARGET}_qml)
  48. endfunction()
  49. function(add_target_windeployqt)
  50. if(WIN32)
  51. set(options)
  52. set(oneValueArgs QML_DIR TARGET)
  53. set(multiValueArgs)
  54. cmake_parse_arguments(add_target_windeployqt "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
  55. find_program(WINDEPLOYQT_EXECUTABLE "windeployqt" PATHS ${PATH})
  56. if(WINDEPLOYQT_EXECUTABLE STREQUAL WINDEPLOYQT_EXECUTABLE-NOTFOUND)
  57. message(WARNING "windeployqt is not found in specified PATH! Please, copy dependencies manually")
  58. else()
  59. if(DEFINED add_target_windeployqt_QML_DIR)
  60. set(QML_DIR --qmldir ${add_target_windeployqt_QML_DIR})
  61. endif()
  62. add_custom_command(TARGET ${add_target_windeployqt_TARGET} POST_BUILD
  63. COMMAND ${CMAKE_COMMAND} -E copy $<TARGET_FILE:QtProtobuf> $<TARGET_FILE_DIR:${add_target_windeployqt_TARGET}>
  64. COMMAND ${CMAKE_COMMAND} -E copy $<TARGET_FILE:QtGrpc> $<TARGET_FILE_DIR:${add_target_windeployqt_TARGET}>
  65. COMMAND ${WINDEPLOYQT_EXECUTABLE} ${QML_DIR} $<TARGET_FILE_DIR:${add_target_windeployqt_TARGET}>)
  66. endif()
  67. endif()
  68. endfunction()
  69. function(extract_qt_variable VARIABLE)
  70. if(NOT DEFINED QT_QMAKE_EXECUTABLE)
  71. find_program(QT_QMAKE_EXECUTABLE "qmake")
  72. if(QT_QMAKE_EXECUTABLE STREQUAL QT_QMAKE_EXECUTABLE-NOTFOUND)
  73. message(FATAL_ERROR "Could not find qmake executable")
  74. endif()
  75. endif()
  76. execute_process(
  77. COMMAND ${QT_QMAKE_EXECUTABLE} -query ${VARIABLE}
  78. OUTPUT_VARIABLE ${VARIABLE}
  79. OUTPUT_STRIP_TRAILING_WHITESPACE
  80. )
  81. set(${VARIABLE} ${${VARIABLE}} PARENT_SCOPE)
  82. endfunction()