QtProtobufTest.cmake 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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 FIELDENUM)
  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. if(add_test_target_FIELDENUM)
  30. set(EXTRA_OPTIONS ${EXTRA_OPTIONS} FIELDENUM)
  31. endif()
  32. qtprotobuf_generate(TARGET ${add_test_target_TARGET}
  33. OUT_DIR ${GENERATED_SOURCES_DIR}
  34. PROTO_FILES ${PROTO_FILES}
  35. GENERATED_HEADERS ${add_test_target_GENERATED_HEADERS}
  36. EXCLUDE_HEADERS ${add_test_target_EXCLUDE_HEADERS}
  37. ${EXTRA_OPTIONS}
  38. PROTO_INCLUDES ${add_test_target_PROTO_INCLUDES})
  39. if(Qt5_POSITION_INDEPENDENT_CODE)
  40. set_target_properties(${add_test_target_TARGET} PROPERTIES POSITION_INDEPENDENT_CODE FALSE)
  41. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fPIC")
  42. endif()
  43. 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})
  44. if (${add_test_target_QML})
  45. target_link_libraries(${add_test_target_TARGET} PUBLIC Qt5::Qml)
  46. endif()
  47. endfunction(add_test_target)