QtProtobufGen.cmake 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. function(qtprotobuf_link_archive TARGET GENERATED_TARGET)
  2. set_target_properties(${qtprotobuf_generate_TARGET} PROPERTIES LINKER_LANGUAGE CXX)
  3. set_target_properties(${TARGET} PROPERTIES LINK_DIRECTORIES "$<TARGET_FILE_DIR:${GENERATED_TARGET}>")
  4. get_target_property(EXISTING_FLAGS ${TARGET} LINK_FLAGS)
  5. if(WIN32)
  6. if("${EXISTING_FLAGS}" STREQUAL "EXISTING_FLAGS-NOTFOUND")
  7. set_target_properties(${TARGET} PROPERTIES LINK_FLAGS "/WHOLEARCHIVE:${GENERATED_TARGET}")
  8. else()
  9. set_target_properties(${TARGET} PROPERTIES LINK_FLAGS "${EXISTING_FLAGS} /WHOLEARCHIVE:${GENERATED_TARGET}")
  10. endif()
  11. else()
  12. target_link_libraries(${TARGET} PRIVATE
  13. -Wl,--whole-archive $<TARGET_FILE:${GENERATED_TARGET}> -Wl,--no-whole-archive)
  14. endif()
  15. target_include_directories(${TARGET} PRIVATE $<TARGET_PROPERTY:${GENERATED_TARGET},INCLUDE_DIRECTORIES>)
  16. add_dependencies(${TARGET} ${GENERATED_TARGET})
  17. endfunction()
  18. function(qtprotobuf_generate)
  19. set(options MULTI QML COMMENTS FOLDER)
  20. set(oneValueArgs OUT_DIR TARGET GENERATED_TARGET)
  21. set(multiValueArgs GENERATED_HEADERS EXCLUDE_HEADERS PROTO_FILES PROTO_INCLUDES)
  22. cmake_parse_arguments(qtprotobuf_generate "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
  23. if(DEFINED qtprotobuf_generate_GENERATED_TARGET)
  24. set(GENERATED_TARGET_NAME ${qtprotobuf_generate_GENERATED_TARGET})
  25. elseif(DEFINED qtprotobuf_generate_TARGET)
  26. set(GENERATED_TARGET_NAME ${qtprotobuf_generate_TARGET}_qtprotobuf_gen)
  27. else()
  28. message(FATAL_ERROR "Either TARGET or GENERATED_TARGET must be specified")
  29. endif()
  30. set(GEN_TARGET ${GENERATED_TARGET_NAME}_generate)
  31. if(NOT DEFINED QT_PROTOBUF_EXECUTABLE)
  32. set(QT_PROTOBUF_EXECUTABLE "${QT_PROTOBUF_EXECUTABLE_INSTALL}")
  33. endif()
  34. #Options handling
  35. set(GENERATION_TYPE "SIGNLE")
  36. if(qtprotobuf_generate_MULTI)
  37. set(GENERATION_TYPE "MULTI")
  38. set(qtprotobuf_generate_FOLDER TRUE)
  39. message(STATUS "Multi-file generation is set. Folder-based generation enabled automatically for ${GENERATED_TARGET_NAME}")
  40. endif()
  41. set(GENERATION_OPTIONS ${GENERATION_TYPE})
  42. if(qtprotobuf_generate_QML)
  43. message(STATUS "Enabled QML generation for ${GENERATED_TARGET_NAME}")
  44. set(GENERATION_OPTIONS "${GENERATION_OPTIONS}:QML")
  45. endif()
  46. if(qtprotobuf_generate_COMMENTS)
  47. message(STATUS "Enabled COMMENTS generation for ${GENERATED_TARGET_NAME}")
  48. set(GENERATION_OPTIONS "${GENERATION_OPTIONS}:COMMENTS")
  49. endif()
  50. if(qtprotobuf_generate_FOLDER)
  51. message(STATUS "Enabled FOLDER generation for ${GENERATED_TARGET_NAME}")
  52. set(GENERATION_OPTIONS "${GENERATION_OPTIONS}:FOLDER")
  53. set(FOLDER_ENABLED "FOLDER")
  54. endif()
  55. if(WIN32)
  56. set(PROTOC_COMMAND set QT_PROTOBUF_OPTIONS=${GENERATION_OPTIONS}&& $<TARGET_FILE:protobuf::protoc>)
  57. else()
  58. set(PROTOC_COMMAND "QT_PROTOBUF_OPTIONS=${GENERATION_OPTIONS}" $<TARGET_FILE:protobuf::protoc>)
  59. endif()
  60. set(PROTO_INCLUDES ${qtprotobuf_generate_PROTO_INCLUDES})
  61. if(DEFINED qtprotobuf_generate_GENERATED_HEADERS)
  62. set(GENERATED_HEADERS ${qtprotobuf_generate_GENERATED_HEADERS})
  63. else()
  64. find_program(GO_EXECUTABLE "go")
  65. if (GO_EXECUTABLE STREQUAL GO_EXECUTABLE-NOTFOUND)
  66. 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")
  67. endif()
  68. foreach(PROTO_FILE IN LISTS qtprotobuf_generate_PROTO_FILES)
  69. get_filename_component(BASE_DIR ${PROTO_FILE} DIRECTORY)
  70. set(PROTO_INCLUDES -I"${BASE_DIR}" ${PROTO_INCLUDES})
  71. execute_process(COMMAND ${GO_EXECUTABLE} run ${PROTO_PARSER} ${PROTO_FILE} ${GENERATION_TYPE} ${FOLDER_ENABLED} OUTPUT_VARIABLE GENERATED_HEADERS_PART ERROR_VARIABLE PARSER_ERROR)
  72. set(GENERATED_HEADERS ${GENERATED_HEADERS} ${GENERATED_HEADERS_PART})
  73. endforeach()
  74. endif()
  75. if(qtprotobuf_generate_MULTI)
  76. #TODO: add globalenums by default. But it's better to verify if proto file contains any global enum
  77. set(GENERATED_HEADERS ${GENERATED_HEADERS} globalenums.h)
  78. endif()
  79. if(NOT "${PARSER_ERROR}" STREQUAL "")
  80. message(FATAL_ERROR "Go parser error ${PARSER_ERROR}")
  81. endif()
  82. foreach(EXCLUDE_HEADER IN LISTS qtprotobuf_generate_EXCLUDE_HEADERS)
  83. list(REMOVE_ITEM GENERATED_HEADERS ${EXCLUDE_HEADER})
  84. endforeach()
  85. list(REMOVE_DUPLICATES GENERATED_HEADERS)
  86. if(NOT DEFINED qtprotobuf_generate_OUT_DIR)
  87. set(OUT_DIR ${CMAKE_CURRENT_BINARY_DIR})
  88. else()
  89. set(OUT_DIR ${qtprotobuf_generate_OUT_DIR})
  90. endif()
  91. file(MAKE_DIRECTORY ${OUT_DIR})
  92. unset(GENERATED_SOURCES_FULL)
  93. unset(GENERATED_HEADERS_FULL)
  94. foreach(GENERATED_HEADER IN LISTS GENERATED_HEADERS)
  95. get_filename_component(GENERATED_DIRECTORY ${GENERATED_HEADER} DIRECTORY)
  96. get_filename_component(GENERATED_BASENAME ${GENERATED_HEADER} NAME)
  97. string(REGEX REPLACE "\\.[^.]*$" "" GENERATED_BASENAME ${GENERATED_BASENAME})
  98. if(qtprotobuf_generate_FOLDER)
  99. list(APPEND GENERATED_SOURCES_FULL ${OUT_DIR}/${GENERATED_DIRECTORY}/${GENERATED_BASENAME}.cpp)
  100. list(APPEND GENERATED_HEADERS_FULL ${OUT_DIR}/${GENERATED_DIRECTORY}/${GENERATED_BASENAME}.h)
  101. else()
  102. list(APPEND GENERATED_SOURCES_FULL ${OUT_DIR}/${GENERATED_BASENAME}.cpp)
  103. list(APPEND GENERATED_HEADERS_FULL ${OUT_DIR}/${GENERATED_BASENAME}.h)
  104. endif()
  105. set_property(SOURCE ${OUT_DIR}/${GENERATED_DIRECTORY}/${GENERATED_BASENAME}.cpp PROPERTY SKIP_AUTOMOC ON)
  106. endforeach()
  107. set_source_files_properties(${GENERATED_SOURCES_FULL} PROPERTIES GENERATED TRUE)
  108. qt5_wrap_cpp(MOC_SOURCES ${GENERATED_HEADERS_FULL})
  109. add_custom_command(
  110. OUTPUT ${GENERATED_SOURCES_FULL} ${GENERATED_HEADERS_FULL}
  111. COMMAND ${PROTOC_COMMAND}
  112. --plugin=protoc-gen-${GENERATOR_TARGET}=${QT_PROTOBUF_EXECUTABLE}
  113. --${GENERATOR_TARGET}_out=${OUT_DIR}
  114. ${PROTO_INCLUDES}
  115. ${qtprotobuf_generate_PROTO_FILES}
  116. WORKING_DIRECTORY ${OUT_DIR}
  117. DEPENDS ${qtprotobuf_generate_PROTO_FILES} ${QT_PROTOBUF_EXECUTABLE}
  118. COMMENT "Generating QtProtobuf ${GENERATED_TARGET_NAME} sources..."
  119. )
  120. add_custom_target(${GEN_TARGET} DEPENDS ${GENERATED_SOURCES_FULL} ${GENERATED_HEADERS_FULL} ${qtprotobuf_generate_PROTO_FILES})
  121. add_library(${GENERATED_TARGET_NAME} ${GENERATED_SOURCES_FULL} ${MOC_SOURCES})
  122. add_dependencies(${GENERATED_TARGET_NAME} ${GEN_TARGET})
  123. set_target_properties(${GENERATED_TARGET_NAME} PROPERTIES PUBLIC_HEADER "${GENERATED_HEADERS_FULL}")
  124. #Add include directories in case if projects are enabled by find_project
  125. target_include_directories(${GENERATED_TARGET_NAME} PUBLIC ${OUT_DIR} PRIVATE ${Qt5Core_INCLUDE_DIRS}
  126. $<TARGET_PROPERTY:${QT_PROTOBUF_PROJECT}::QtProtobuf,INTERFACE_INCLUDE_DIRECTORIES>)
  127. if(TARGET ${QT_PROTOBUF_PROJECT}::QtGrpc)
  128. target_include_directories(${GENERATED_TARGET_NAME} PRIVATE
  129. $<TARGET_PROPERTY:${QT_PROTOBUF_PROJECT}::QtGrpc,INTERFACE_INCLUDE_DIRECTORIES>)
  130. endif()
  131. if(NOT WIN32)
  132. if(TARGET ${QT_PROTOBUF_PROJECT}::QtProtobufWellKnownTypes)
  133. target_include_directories(${GENERATED_TARGET_NAME} PRIVATE
  134. $<TARGET_PROPERTY:${QT_PROTOBUF_PROJECT}::QtProtobufWellKnownTypes,INTERFACE_INCLUDE_DIRECTORIES>)
  135. endif()
  136. endif()
  137. #Automatically link whole static library to specified in parameters target
  138. if(DEFINED qtprotobuf_generate_TARGET)
  139. qtprotobuf_link_archive(${qtprotobuf_generate_TARGET} ${GENERATED_TARGET_NAME})
  140. endif()
  141. endfunction()