QtProtobufFunctions.cmake.in 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. find_package(Protobuf)
  2. # TODO: replace with target_link_directories once released
  3. link_directories(@TARGET_LIB_DIR@)
  4. set(@TARGET@_INCLUDE_DIRS @TARGET_INCLUDE_DIR@)
  5. set(@TARGET@_LIBRARIES @PROTOBUF_LIBRARY_TARGET@)
  6. set(CMAKE_CXX_STANDARD 14)
  7. set(CMAKE_CXX_STANDARD_REQUIRED ON)
  8. function(generate_qtprotobuf)
  9. set(options)
  10. set(oneValueArgs OUT_DIR TARGET)
  11. set(multiValueArgs GENERATED_HEADERS PROTO_FILES)
  12. cmake_parse_arguments(generate_qtprotobuf "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
  13. set(QtProtobuf_GENERATED ${generate_qtprotobuf_TARGET}_qtprotobuf_gen PARENT_SCOPE)
  14. set(QtProtobuf_GENERATED ${generate_qtprotobuf_TARGET}_qtprotobuf_gen)
  15. set(GEN_TARGET ${generate_qtprotobuf_TARGET}_qtprotobuf_generate)
  16. add_custom_target(${GEN_TARGET})
  17. if(NOT DEFINED QTPROTOBUF_EXECUTABLE)
  18. set(QTPROTOBUF_EXECUTABLE @TARGET_BIN_DIR@/@GENERATOR_TARGET@)
  19. endif()
  20. foreach(PROTO_FILE IN LISTS generate_qtprotobuf_PROTO_FILES)
  21. get_filename_component(BASE_DIR ${PROTO_FILE} DIRECTORY)
  22. set(PROTO_INCLUDES -I"${BASE_DIR}" ${PROTO_INCUDES})
  23. endforeach()
  24. if(NOT DEFINED generate_qtprotobuf_OUT_DIR)
  25. set(OUT_DIR ${CMAKE_CURRENT_BINARY_DIR})
  26. else()
  27. set(OUT_DIR ${generate_qtprotobuf_OUT_DIR})
  28. endif()
  29. file(MAKE_DIRECTORY ${OUT_DIR})
  30. unset(QTPROTOBUF_GENERATED_SOURCES)
  31. unset(QTPROTOBUF_GENERATED_HEADERS)
  32. foreach(GENERATED_HEADER IN LISTS generate_qtprotobuf_GENERATED_HEADERS)
  33. get_filename_component(GENERATED_BASENAME ${GENERATED_HEADER} NAME_WE)
  34. list(APPEND QTPROTOBUF_GENERATED_SOURCES ${OUT_DIR}/${GENERATED_BASENAME}.cpp)
  35. list(APPEND QTPROTOBUF_GENERATED_HEADERS ${OUT_DIR}/${GENERATED_BASENAME}.h)
  36. set_property(SOURCE ${OUT_DIR}/${GENERATED_BASENAME}.cpp PROPERTY SKIP_AUTOMOC ON)
  37. endforeach()
  38. add_custom_command(TARGET ${GEN_TARGET}
  39. COMMAND ${Protobuf_PROTOC_EXECUTABLE}
  40. --@GENERATOR_TARGET@_opt=out=${OUT_DIR}
  41. --plugin=protoc-gen-@GENERATOR_TARGET@=${QTPROTOBUF_EXECUTABLE}
  42. --@GENERATOR_TARGET@_out=${OUT_DIR}
  43. ${PROTO_INCLUDES}
  44. ${PROTO_FILES}
  45. WORKING_DIRECTORY ${OUT_DIR}
  46. DEPENDS ${PROTO_FILES}
  47. COMMENT "Generating test headers"
  48. )
  49. qt5_wrap_cpp(MOC_SOURCES ${QTPROTOBUF_GENERATED_HEADERS})
  50. list(APPEND GENERATED_SOURCES ${MOC_SOURCES})
  51. set_source_files_properties(${QTPROTOBUF_GENERATED_SOURCES} PROPERTIES GENERATED TRUE)
  52. add_library(${QtProtobuf_GENERATED} ${QTPROTOBUF_GENERATED_SOURCES} ${MOC_SOURCES})
  53. add_dependencies(${QtProtobuf_GENERATED} ${GEN_TARGET})
  54. target_include_directories(${QtProtobuf_GENERATED} PRIVATE ${Qt5Core_INCLUDE_DIRS} ${Qt5Qml_INCLUDE_DIRS} ${QtProtobuf_INCLUDE_DIRS} ${QtGrpc_INCLUDE_DIRS} ${OUT_DIR})
  55. endfunction()