QtProtobufTest.cmake 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. find_package(${QT_PROTOBUF_PROJECT} COMPONENTS QtProtobuf QtGrpc REQUIRED)
  2. include(${QT_PROTOBUF_CMAKE_DIR}/QtProtobufCommon.cmake)
  3. function(add_test_target)
  4. set(options MULTI QML)
  5. set(oneValueArgs QML_DIR TARGET)
  6. set(multiValueArgs SOURCES GENERATED_HEADERS EXCLUDE_HEADERS PROTO_FILES PROTO_INCLUDES)
  7. cmake_parse_arguments(add_test_target "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
  8. find_package(Threads REQUIRED)
  9. find_package(Qt5 COMPONENTS Test REQUIRED)
  10. ## test sources build
  11. # policy enables automoc for generated files
  12. if(${CMAKE_VERSION} VERSION_GREATER "3.10.0")
  13. cmake_policy(SET CMP0071 NEW)
  14. endif()
  15. set(GENERATED_SOURCES_DIR ${CMAKE_CURRENT_BINARY_DIR}/${add_test_target_TARGET}_generated)
  16. if(DEFINED add_test_target_PROTO_FILES)
  17. file(GLOB PROTO_FILES ABSOLUTE ${add_test_target_PROTO_FILES})
  18. else()
  19. file(GLOB PROTO_FILES ABSOLUTE ${CMAKE_CURRENT_SOURCE_DIR}/proto/*.proto)
  20. endif()
  21. add_executable(${add_test_target_TARGET} ${add_test_target_SOURCES})
  22. set(EXTRA_OPTIONS COMMENTS)
  23. if(add_test_target_MULTI)
  24. set(EXTRA_OPTIONS ${EXTRA_OPTIONS} MULTI)
  25. endif()
  26. if(add_test_target_QML)
  27. set(EXTRA_OPTIONS ${EXTRA_OPTIONS} QML)
  28. endif()
  29. qtprotobuf_generate(TARGET ${add_test_target_TARGET}
  30. OUT_DIR ${GENERATED_SOURCES_DIR}
  31. PROTO_FILES ${PROTO_FILES}
  32. GENERATED_HEADERS ${add_test_target_GENERATED_HEADERS}
  33. EXCLUDE_HEADERS ${add_test_target_EXCLUDE_HEADERS}
  34. ${EXTRA_OPTIONS}
  35. PROTO_INCLUDES ${add_test_target_PROTO_INCLUDES})
  36. if(Qt5_POSITION_INDEPENDENT_CODE)
  37. set_target_properties(${add_test_target_TARGET} PROPERTIES POSITION_INDEPENDENT_CODE FALSE)
  38. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fPIC")
  39. endif()
  40. target_link_libraries(${add_test_target_TARGET} PUBLIC gtest_main gtest ${QT_PROTOBUF_PROJECT}::QtProtobuf ${QT_PROTOBUF_PROJECT}::QtGrpc Qt5::Core Qt5::Test Qt5::Network ${CMAKE_THREAD_LIBS_INIT})
  41. if (${add_test_target_QML})
  42. target_link_libraries(${add_test_target_TARGET} PUBLIC Qt5::Qml)
  43. endif()
  44. endfunction(add_test_target)