QtProtobufGen.cmake 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. function(qtprotobuf_link_target TARGET GENERATED_TARGET)
  2. target_sources(${TARGET} PRIVATE $<TARGET_OBJECTS:${GENERATED_TARGET}>)
  3. target_include_directories(${TARGET} PRIVATE $<TARGET_PROPERTY:${GENERATED_TARGET},INTERFACE_INCLUDE_DIRECTORIES>)
  4. add_dependencies(${TARGET} ${GENERATED_TARGET})
  5. endfunction()
  6. function(qtprotobuf_generate)
  7. set(options MULTI QML COMMENTS FOLDER FIELDENUM)
  8. set(oneValueArgs OUT_DIR TARGET GENERATED_TARGET EXTRA_NAMESPACE)
  9. set(multiValueArgs GENERATED_HEADERS EXCLUDE_HEADERS PROTO_FILES PROTO_INCLUDES)
  10. cmake_parse_arguments(qtprotobuf_generate "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
  11. if(DEFINED qtprotobuf_generate_GENERATED_TARGET)
  12. set(GENERATED_TARGET_NAME ${qtprotobuf_generate_GENERATED_TARGET})
  13. elseif(DEFINED qtprotobuf_generate_TARGET)
  14. set(GENERATED_TARGET_NAME ${qtprotobuf_generate_TARGET}_qtprotobuf_gen)
  15. else()
  16. message(FATAL_ERROR "Either TARGET or GENERATED_TARGET must be specified")
  17. endif()
  18. set(deps_target ${GENERATED_TARGET_NAME}_deps)
  19. set(QT_PROTOBUF_EXECUTABLE $<TARGET_FILE:${QT_PROTOBUF_NAMESPACE}::qtprotobufgen>)
  20. #Options handling
  21. set(GENERATION_TYPE "SIGNLE")
  22. if(qtprotobuf_generate_MULTI)
  23. set(GENERATION_TYPE "MULTI")
  24. set(qtprotobuf_generate_FOLDER TRUE)
  25. message(STATUS "Multi-file generation is set. Folder-based generation enabled automatically for ${GENERATED_TARGET_NAME}")
  26. endif()
  27. set(GENERATION_OPTIONS ${GENERATION_TYPE})
  28. if(qtprotobuf_generate_QML)
  29. if(NOT TARGET Qt5::Qml)
  30. message(FATAL_ERROR "Trying to enable QML support for ${GENERATED_TARGET_NAME}, but Qt5::Qml is not a target."
  31. " find_package(Qt5 COMPONENTS Qml) is missing?")
  32. endif()
  33. message(STATUS "Enabling QML generation for ${GENERATED_TARGET_NAME}")
  34. set(GENERATION_OPTIONS "${GENERATION_OPTIONS}:QML")
  35. endif()
  36. if(qtprotobuf_generate_COMMENTS)
  37. message(STATUS "Enabled COMMENTS generation for ${GENERATED_TARGET_NAME}")
  38. set(GENERATION_OPTIONS "${GENERATION_OPTIONS}:COMMENTS")
  39. endif()
  40. if(qtprotobuf_generate_FOLDER)
  41. message(STATUS "Enabled FOLDER generation for ${GENERATED_TARGET_NAME}")
  42. set(GENERATION_OPTIONS "${GENERATION_OPTIONS}:FOLDER")
  43. set(FOLDER_ENABLED "FOLDER")
  44. endif()
  45. if(qtprotobuf_generate_FIELDENUM)
  46. message(STATUS "Enabled FIELDENUM generation for ${GENERATED_TARGET_NAME}")
  47. set(GENERATION_OPTIONS "${GENERATION_OPTIONS}:FIELDENUM")
  48. endif()
  49. if(qtprotobuf_generate_EXTRA_NAMESPACE)
  50. set(GENERATION_OPTIONS
  51. "${GENERATION_OPTIONS}:EXTRA_NAMESPACE=\"${qtprotobuf_generate_EXTRA_NAMESPACE}\""
  52. )
  53. endif()
  54. if(WIN32)
  55. set(PROTOC_COMMAND set QT_PROTOBUF_OPTIONS=${GENERATION_OPTIONS}&& $<TARGET_FILE:protobuf::protoc>)
  56. else()
  57. set(PROTOC_COMMAND "QT_PROTOBUF_OPTIONS=${GENERATION_OPTIONS}" $<TARGET_FILE:protobuf::protoc>)
  58. endif()
  59. set(PROTO_INCLUDES ${qtprotobuf_generate_PROTO_INCLUDES})
  60. if(DEFINED qtprotobuf_generate_GENERATED_HEADERS)
  61. set(GENERATED_HEADERS ${qtprotobuf_generate_GENERATED_HEADERS})
  62. else()
  63. find_program(GO_EXECUTABLE "go")
  64. if (GO_EXECUTABLE STREQUAL GO_EXECUTABLE-NOTFOUND)
  65. message(FATAL_ERROR "Golang is mandatory dependency for QtProtobuf if GENERATED_HEADERS is not specified. Please install it and ensure that it's accessible by PATH environment variable")
  66. endif()
  67. get_target_property(PROTO_PARSER ${QT_PROTOBUF_NAMESPACE}::qtprotobufgen PROTO_PARSER)
  68. if(NOT PROTO_PARSER)
  69. message(FATAL_ERROR "Unable to locate parsemessages.go script")
  70. endif()
  71. foreach(PROTO_FILE IN LISTS qtprotobuf_generate_PROTO_FILES)
  72. get_filename_component(BASE_DIR ${PROTO_FILE} DIRECTORY)
  73. set(PROTO_INCLUDES "-I\"${BASE_DIR}\"" ${PROTO_INCLUDES})
  74. execute_process(COMMAND ${GO_EXECUTABLE} run ${PROTO_PARSER} ${PROTO_FILE} ${GENERATION_TYPE} ${FOLDER_ENABLED} OUTPUT_VARIABLE GENERATED_HEADERS_PART ERROR_VARIABLE PARSER_ERROR)
  75. list(APPEND GENERATED_HEADERS ${GENERATED_HEADERS_PART})
  76. endforeach()
  77. endif()
  78. if(qtprotobuf_generate_MULTI)
  79. #TODO: add globalenums by default. But it's better to verify if proto file contains any global enum
  80. set(GENERATED_HEADERS ${GENERATED_HEADERS} globalenums.h)
  81. endif()
  82. if(NOT "${PARSER_ERROR}" STREQUAL "")
  83. message(FATAL_ERROR "Go parser error ${PARSER_ERROR}")
  84. endif()
  85. foreach(EXCLUDE_HEADER IN LISTS qtprotobuf_generate_EXCLUDE_HEADERS)
  86. list(REMOVE_ITEM GENERATED_HEADERS ${EXCLUDE_HEADER})
  87. endforeach()
  88. list(REMOVE_DUPLICATES GENERATED_HEADERS)
  89. if(NOT DEFINED qtprotobuf_generate_OUT_DIR)
  90. set(OUT_DIR ${CMAKE_CURRENT_BINARY_DIR})
  91. else()
  92. set(OUT_DIR ${qtprotobuf_generate_OUT_DIR})
  93. endif()
  94. file(MAKE_DIRECTORY ${OUT_DIR})
  95. unset(GENERATED_SOURCES_FULL)
  96. unset(GENERATED_HEADERS_FULL)
  97. foreach(GENERATED_HEADER IN LISTS GENERATED_HEADERS)
  98. get_filename_component(GENERATED_DIRECTORY ${GENERATED_HEADER} DIRECTORY)
  99. get_filename_component(GENERATED_BASENAME ${GENERATED_HEADER} NAME)
  100. string(REGEX REPLACE "\\.[^.]*$" "" GENERATED_BASENAME ${GENERATED_BASENAME})
  101. if(qtprotobuf_generate_FOLDER)
  102. list(APPEND GENERATED_SOURCES_FULL "${OUT_DIR}/${GENERATED_DIRECTORY}/${GENERATED_BASENAME}.cpp")
  103. list(APPEND GENERATED_HEADERS_FULL "${OUT_DIR}/${GENERATED_DIRECTORY}/${GENERATED_BASENAME}.h")
  104. else()
  105. list(APPEND GENERATED_SOURCES_FULL "${OUT_DIR}/${GENERATED_BASENAME}.cpp")
  106. list(APPEND GENERATED_HEADERS_FULL "${OUT_DIR}/${GENERATED_BASENAME}.h")
  107. endif()
  108. endforeach()
  109. set_source_files_properties(${GENERATED_SOURCES_FULL};${GENERATED_HEADERS_FULL} PROPERTIES
  110. GENERATED TRUE
  111. SKIP_AUTOMOC ON
  112. SKIP_AUTOUIC ON
  113. SKIP_AUTOGEN ON
  114. )
  115. qt5_wrap_cpp(MOC_SOURCES ${GENERATED_HEADERS_FULL})
  116. add_custom_command(
  117. OUTPUT ${GENERATED_SOURCES_FULL} ${GENERATED_HEADERS_FULL}
  118. COMMAND ${PROTOC_COMMAND}
  119. --plugin=protoc-gen-qtprotobufgen=${QT_PROTOBUF_EXECUTABLE}
  120. --qtprotobufgen_out=${OUT_DIR}
  121. ${PROTO_INCLUDES}
  122. ${qtprotobuf_generate_PROTO_FILES}
  123. WORKING_DIRECTORY ${OUT_DIR}
  124. DEPENDS ${qtprotobuf_generate_PROTO_FILES} ${QT_PROTOBUF_EXECUTABLE}
  125. COMMENT "Generating QtProtobuf ${GENERATED_TARGET_NAME} sources..."
  126. COMMAND_EXPAND_LISTS
  127. )
  128. add_custom_target(${deps_target} DEPENDS ${GENERATED_SOURCES_FULL} ${GENERATED_HEADERS_FULL}
  129. ${qtprotobuf_generate_PROTO_FILES}
  130. )
  131. add_library(${GENERATED_TARGET_NAME} OBJECT ${GENERATED_SOURCES_FULL} ${MOC_SOURCES})
  132. add_dependencies(${GENERATED_TARGET_NAME} ${deps_target})
  133. if(qtprotobuf_generate_TARGET)
  134. set_property(TARGET ${qtprotobuf_generate_TARGET} APPEND PROPERTY PUBLIC_HEADER ${GENERATED_HEADERS_FULL})
  135. endif()
  136. set_target_properties(${GENERATED_TARGET_NAME} PROPERTIES PUBLIC_HEADER "${GENERATED_HEADERS_FULL}")
  137. #Add include directories in case if targets are found by find_project or in source tree
  138. target_include_directories(${GENERATED_TARGET_NAME} PUBLIC ${OUT_DIR} PRIVATE
  139. $<TARGET_PROPERTY:${QT_PROTOBUF_NAMESPACE}::Protobuf,INTERFACE_INCLUDE_DIRECTORIES>)
  140. #TODO: Do not link targets if they are not used in .proto files.
  141. if(TARGET ${QT_PROTOBUF_NAMESPACE}::Grpc)
  142. target_include_directories(${GENERATED_TARGET_NAME} PRIVATE
  143. $<TARGET_PROPERTY:${QT_PROTOBUF_NAMESPACE}::Grpc,INTERFACE_INCLUDE_DIRECTORIES>)
  144. endif()
  145. if(TARGET ${QT_PROTOBUF_NAMESPACE}::ProtobufWellKnownTypes)
  146. target_include_directories(${GENERATED_TARGET_NAME} PRIVATE
  147. $<TARGET_PROPERTY:${QT_PROTOBUF_NAMESPACE}::ProtobufWellKnownTypes,INTERFACE_INCLUDE_DIRECTORIES>)
  148. endif()
  149. if(TARGET ${QT_PROTOBUF_NAMESPACE}::ProtobufQtTypes)
  150. target_include_directories(${GENERATED_TARGET_NAME} PRIVATE
  151. $<TARGET_PROPERTY:${QT_PROTOBUF_NAMESPACE}::ProtobufQtTypes,INTERFACE_INCLUDE_DIRECTORIES>)
  152. endif()
  153. #Automatically link whole static library to specified in parameters target
  154. if(DEFINED qtprotobuf_generate_TARGET)
  155. qtprotobuf_link_target(${qtprotobuf_generate_TARGET} ${GENERATED_TARGET_NAME})
  156. endif()
  157. endfunction()