QtProtobufInternalHelpers.cmake 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. include(${QT_PROTOBUF_CMAKE_DIR}/QtProtobufCommon.cmake)
  2. macro(qt_protobuf_internal_find_dependencies)
  3. find_package(Qt5 COMPONENTS Core Qml Network REQUIRED)
  4. find_package(Threads REQUIRED)
  5. if(NOT TARGET ${QT_PROTOBUF_PROJECT}::QtProtobuf)
  6. find_package(${QT_PROTOBUF_PROJECT} COMPONENTS QtProtobuf REQUIRED)
  7. endif()
  8. if(NOT TARGET ${QT_PROTOBUF_PROJECT}::QtGrpc)
  9. find_package(${QT_PROTOBUF_PROJECT} COMPONENTS QtGrpc REQUIRED)
  10. endif()
  11. if(QT_PROTOBUF_STATIC)
  12. add_definitions(-DQT_PROTOBUF_STATIC)# add_definitions is used because old cmake versions
  13. # compatibility
  14. endif()
  15. set(CMAKE_POSITION_INDEPENDENT_CODE ${Qt5_POSITION_INDEPENDENT_CODE})
  16. set(CMAKE_AUTOMOC ON)
  17. set(CMAKE_AUTORCC ON)
  18. endmacro()
  19. function(qt_protobuf_internal_add_test_target)
  20. set(options MULTI QML FIELDENUM)
  21. set(oneValueArgs QML_DIR TARGET)
  22. set(multiValueArgs SOURCES GENERATED_HEADERS EXCLUDE_HEADERS PROTO_FILES PROTO_INCLUDES)
  23. cmake_parse_arguments(add_test_target "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
  24. find_package(Qt5 COMPONENTS Test REQUIRED)
  25. ## test sources build
  26. # policy enables automoc for generated files
  27. if(${CMAKE_VERSION} VERSION_GREATER "3.10.0")
  28. cmake_policy(SET CMP0071 NEW)
  29. endif()
  30. set(CMAKE_POSITION_INDEPENDENT_CODE ${Qt5_POSITION_INDEPENDENT_CODE})
  31. set(GENERATED_SOURCES_DIR ${CMAKE_CURRENT_BINARY_DIR}/${add_test_target_TARGET}_generated)
  32. if(DEFINED add_test_target_PROTO_FILES)
  33. file(GLOB PROTO_FILES ABSOLUTE ${add_test_target_PROTO_FILES})
  34. else()
  35. file(GLOB PROTO_FILES ABSOLUTE ${CMAKE_CURRENT_SOURCE_DIR}/proto/*.proto)
  36. endif()
  37. add_executable(${add_test_target_TARGET} ${add_test_target_SOURCES})
  38. set(EXTRA_OPTIONS COMMENTS)
  39. if(add_test_target_MULTI)
  40. set(EXTRA_OPTIONS ${EXTRA_OPTIONS} MULTI)
  41. endif()
  42. if(add_test_target_QML)
  43. set(EXTRA_OPTIONS ${EXTRA_OPTIONS} QML)
  44. endif()
  45. if(add_test_target_FIELDENUM)
  46. set(EXTRA_OPTIONS ${EXTRA_OPTIONS} FIELDENUM)
  47. endif()
  48. qtprotobuf_generate(TARGET ${add_test_target_TARGET}
  49. OUT_DIR ${GENERATED_SOURCES_DIR}
  50. PROTO_FILES ${PROTO_FILES}
  51. GENERATED_HEADERS ${add_test_target_GENERATED_HEADERS}
  52. EXCLUDE_HEADERS ${add_test_target_EXCLUDE_HEADERS}
  53. ${EXTRA_OPTIONS}
  54. PROTO_INCLUDES ${add_test_target_PROTO_INCLUDES})
  55. target_link_libraries(${add_test_target_TARGET} PUBLIC gtest_main
  56. gtest
  57. ${QT_PROTOBUF_PROJECT}::QtProtobuf
  58. ${QT_PROTOBUF_PROJECT}::QtGrpc
  59. Qt5::Core
  60. Qt5::Test
  61. Qt5::Network
  62. ${CMAKE_THREAD_LIBS_INIT})
  63. if (${add_test_target_QML})
  64. target_link_libraries(${add_test_target_TARGET} PUBLIC Qt5::Qml)
  65. endif()
  66. endfunction()
  67. function(qt_protobuf_internal_add_example_target)
  68. set(options)
  69. set(oneValueArgs TARGET QML_DIR)
  70. set(multiValueArgs SOURCES PROTO_FILES RESOURCES)
  71. cmake_parse_arguments(arg "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
  72. find_package(Qt5 COMPONENTS Quick REQUIRED)
  73. set(CMAKE_INCLUDE_CURRENT_DIR ON)
  74. set(CMAKE_AUTOMOC ON)
  75. set(CMAKE_AUTORCC ON)
  76. file(GLOB PROTO_FILES ABSOLUTE ${arg_PROTO_FILES})
  77. file(GLOB SOURCES ${arg_SOURCES})
  78. add_executable(${TARGET} ${SOURCES} ${arg_RESOURCES})
  79. qtprotobuf_generate(TARGET ${TARGET}
  80. OUT_DIR ${CMAKE_CURRENT_BINARY_DIR}/generated
  81. PROTO_FILES ${PROTO_FILES}
  82. QML)
  83. add_target_windeployqt(TARGET ${TARGET} QML_DIR ${arg_QML_DIR})
  84. target_link_libraries(${TARGET} PRIVATE examples_common
  85. QtProtobufProject::QtProtobuf
  86. QtProtobufProject::QtGrpc
  87. Qt5::Quick
  88. Qt5::Qml)
  89. endfunction()