QtProtobufCommon.cmake 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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_INCUDES})
  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 TARGET)
  37. set(multiValueArgs SOURCES GENERATED_HEADERS)
  38. cmake_parse_arguments(add_test_target "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
  39. ## test sources build
  40. # policy enables automoc for generated files
  41. if(${CMAKE_VERSION} VERSION_GREATER "3.10.0")
  42. cmake_policy(SET CMP0071 NEW)
  43. endif()
  44. set(GENERATED_SOURCES_DIR ${CMAKE_CURRENT_BINARY_DIR}/generated)
  45. file(GLOB PROTO_FILES ABSOLUTE ${CMAKE_CURRENT_SOURCE_DIR}/proto/*.proto)
  46. generate_qtprotobuf(TARGET ${add_test_target_TARGET}
  47. OUT_DIR ${GENERATED_SOURCES_DIR}
  48. PROTO_FILES ${PROTO_FILES}
  49. GENERATED_HEADERS ${add_test_target_GENERATED_HEADERS})
  50. add_executable(${add_test_target_TARGET} ${add_test_target_SOURCES})
  51. if(Qt5_POSITION_INDEPENDENT_CODE)
  52. set_target_properties(${add_test_target_TARGET} PROPERTIES POSITION_INDEPENDENT_CODE FALSE)
  53. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fPIC")
  54. endif()
  55. add_dependencies(${add_test_target_TARGET} ${QtProtobuf_GENERATED})
  56. target_link_libraries(${add_test_target_TARGET} gtest_main gtest ${QtProtobuf_GENERATED} ${QTPROTOBUF_COMMON_NAMESPACE}::QtProtobuf ${QTPROTOBUF_COMMON_NAMESPACE}::QtGrpc Qt5::Core Qt5::Qml Qt5::Network)
  57. target_include_directories(${add_test_target_TARGET} PRIVATE ${GENERATED_SOURCES_DIR})
  58. endfunction(add_test_target)