Extend qt_internal_add_protobuf_module function

The function now accepts the list of .proto files as an argument and
calls 'qt6_add_protobuf' implictly. .proto files are treated as an
interface of the module and installed to the module include path.
The respective module include path is added to the target's
QT_PROTO_INCLUDES property for the further use in user projects as
PROTO_INCLUDES argument of 'qt_add_protobuf' and 'qt_add_grpc'
functions.

QtProtobufBuildInternals.cmake is renamed and moved to the correct
directory for the implicit processing by qt_internal_add_module
function.

Pick-to: 6.5
Change-Id: I3322a18a6c487bb8dd0165ce9bca5f84c89cbbd5
Reviewed-by: Tatiana Borisova <tatiana.borisova@qt.io>
Reviewed-by: Jörg Bornemann <joerg.bornemann@qt.io>
Reviewed-by: Konrad Kujawa <konrad.kujawa@qt.io>
This commit is contained in:
Alexey Edelev 2023-01-03 16:40:26 +01:00 committed by Tatiana Borisova
parent 907b8077d0
commit 494cf44775
5 changed files with 172 additions and 74 deletions

View File

@ -14,8 +14,8 @@ project(QtGrpc
find_package(Qt6 ${PROJECT_VERSION} CONFIG REQUIRED COMPONENTS BuildInternals Core)
find_package(Qt6 ${PROJECT_VERSION} CONFIG OPTIONAL_COMPONENTS Network)
include("${CMAKE_CURRENT_LIST_DIR}/cmake/QtProtobufBuildInternals.cmake")
include("${CMAKE_CURRENT_LIST_DIR}/src/tools/qtprotobufgen/Qt6ProtobufToolsMacros.cmake")
include("${CMAKE_CURRENT_LIST_DIR}/src/tools/qtgrpcgen/Qt6GrpcToolsMacros.cmake")
include("${CMAKE_CURRENT_LIST_DIR}/src/protobuf/Qt6ProtobufBuildInternals.cmake")
qt_build_repo()

View File

@ -1,73 +0,0 @@
# Copyright (C) 2022 The Qt Company Ltd.
# SPDX-License-Identifier: BSD-3-Clause
function(qt_internal_add_protobuf_module target)
qt_internal_get_internal_add_module_keywords(module_option_args module_single_args
module_multi_args)
set(protobuf_module_single_args TYPE_REGISTRATION_FUNCTION)
set(option_args ${module_option_args})
set(single_args ${module_single_args} ${protobuf_module_single_args})
set(multi_args ${module_multi_args})
qt_parse_all_arguments(arg "qt_internal_add_protobuf_module"
"${option_args}"
"${single_args}"
"${multi_args}"
${ARGN}
)
qt_remove_args(module_args
ARGS_TO_REMOVE
${protobuf_module_single_args}
ALL_ARGS
${option_args}
${single_args}
${multi_args}
ARGS
${ARGN}
)
qt_internal_add_module(${target} ${module_args})
get_target_property(target_type ${target} TYPE)
if(arg_TYPE_REGISTRATION_FUNCTION)
set(registration_file
"${CMAKE_CURRENT_BINARY_DIR}/${target}_protobuf_module_registration.cpp")
set(content "#include <QtProtobuf/qtprotobufglobal.h>\n\
QT_BEGIN_NAMESPACE\n\
extern Q_DECL_IMPORT void ${arg_TYPE_REGISTRATION_FUNCTION}();\n\
Q_CONSTRUCTOR_FUNCTION(${arg_TYPE_REGISTRATION_FUNCTION})\n\
QT_END_NAMESPACE\n")
qt_internal_get_main_cmake_configuration(main_config)
file(GENERATE
OUTPUT "${registration_file}"
CONTENT "${content}"
CONDITION "$<CONFIG:${main_config}>")
if(target_type STREQUAL "STATIC_LIBRARY"
OR (WIN32 AND NOT target_type STREQUAL "EXECUTABLE"))
set(object_library ${target}_protobuf_module_registration)
add_library(${object_library} OBJECT ${registration_file})
qt_internal_link_internal_platform_for_object_library(${object_library})
_qt_internal_copy_dependency_properties(${object_library}
${target} PRIVATE_ONLY)
target_link_libraries(${target}
INTERFACE "$<TARGET_OBJECTS:$<TARGET_NAME:${object_library}>>")
add_dependencies(${target} ${object_library})
qt_install(TARGETS ${object_library}
EXPORT "${INSTALL_CMAKE_NAMESPACE}${target}Targets"
DESTINATION "${INSTALL_LIBDIR}"
)
qt_internal_add_targets_to_additional_targets_export_file(
TARGETS ${object_library}
EXPORT_NAME_PREFIX "${INSTALL_CMAKE_NAMESPACE}${target}"
)
else()
target_sources(${target} PRIVATE "${registration_file}")
endif()
endif()
endfunction()

View File

@ -13,6 +13,8 @@ qt_internal_add_module(Protobuf
qtprotobuflogging.cpp qtprotobuflogging_p.h
qtprotobuftypes.cpp qtprotobuftypes.h
GENERATE_CPP_EXPORTS
EXTRA_CMAKE_FILES
"${CMAKE_CURRENT_SOURCE_DIR}/QtProtobufProperties.cmake.in"
PUBLIC_LIBRARIES
Qt::Core
LIBRARIES

View File

@ -0,0 +1,163 @@
# Copyright (C) 2022 The Qt Company Ltd.
# SPDX-License-Identifier: BSD-3-Clause
set(__qt_protobuf_build_internals_base_dir "${CMAKE_CURRENT_LIST_DIR}" CACHE INTERNAL "")
macro(qt_internal_get_internal_add_protobuf_module_keywords option_args single_args multi_args)
set(${option_args} "")
set(${single_args}
TYPE_REGISTRATION_FUNCTION
)
set(${multi_args}
PROTO_FILES
)
endmacro()
function(qt_internal_add_protobuf_module target)
qt_internal_get_internal_add_module_keywords(module_option_args module_single_args
module_multi_args)
qt_internal_get_internal_add_protobuf_module_keywords(
protobuf_module_option_args
protobuf_module_single_args
protobuf_module_multi_args
)
set(protobuf_module_single_args TYPE_REGISTRATION_FUNCTION)
set(option_args ${module_option_args} ${protobuf_module_option_args})
set(single_args ${module_single_args} ${protobuf_module_single_args})
set(multi_args ${module_multi_args} ${protobuf_module_multi_args})
qt_parse_all_arguments(arg "qt_internal_add_protobuf_module"
"${option_args}"
"${single_args}"
"${multi_args}"
${ARGN}
)
qt_remove_args(module_args
ARGS_TO_REMOVE
${protobuf_module_option_args}
${protobuf_module_single_args}
${protobuf_module_multi_args}
EXTRA_CMAKE_INCLUDES
ALL_ARGS
${option_args}
${single_args}
${multi_args}
ARGS
${ARGN}
)
if(NOT arg_EXTRA_CMAKE_INCLUDES)
set(arg_EXTRA_CMAKE_INCLUDES "")
endif()
set(module_extra_properties_filename
"${INSTALL_CMAKE_NAMESPACE}${target}ProtobufProperties.cmake")
qt_internal_add_module(${target}
EXTRA_CMAKE_INCLUDES
"${module_extra_properties_filename}"
${arg_EXTRA_CMAKE_INCLUDES}
${module_args}
)
if(arg_PROTO_FILES)
unset(generated_targets)
qt6_add_protobuf(${target}
PROTO_FILES
${arg_PROTO_FILES}
OUTPUT_TARGETS generated_targets
GENERATE_PACKAGE_SUBFOLDERS
EXTRA_NAMESPACE ${QT_NAMESPACE}
)
qt_internal_module_info(module ${target})
if(arg_PROTO_FILES_BASE_DIR)
set(proto_files_base_dir "${arg_PROTO_FILES_BASE_DIR}")
else()
set(proto_files_base_dir "${CMAKE_CURRENT_SOURCE_DIR}")
endif()
foreach(f IN LISTS arg_PROTO_FILES)
if(IS_ABSOLUTE "${f}")
file(RELATIVE_PATH f_rel "${proto_files_base_dir}" "${f}")
else()
set(f_rel "${f}")
endif()
get_filename_component(relative_directory "${f_rel}" DIRECTORY)
qt_copy_or_install(
FILES
${f}
DESTINATION
"${module_install_interface_include_dir}/${relative_directory}"
)
endforeach()
set_target_properties(${target} PROPERTIES QT_PROTO_INCLUDES "${proto_files_base_dir}")
qt_path_join(proto_include_dir "\${QT6_INSTALL_PREFIX}"
${module_install_interface_include_dir})
set(export_name "${INSTALL_CMAKE_NAMESPACE}${target}")
qt_path_join(config_build_dir ${QT_CONFIG_BUILD_DIR} ${export_name})
qt_path_join(config_install_dir ${QT_CONFIG_INSTALL_DIR} ${export_name})
set(module_extra_properties_file "${config_build_dir}/${module_extra_properties_filename}")
configure_file("${__qt_protobuf_build_internals_base_dir}/QtProtobufProperties.cmake.in"
"${module_extra_properties_file}" @ONLY)
qt_install(FILES "${module_extra_properties_file}" DESTINATION "${config_install_dir}")
if(generated_targets)
list(REMOVE_DUPLICATES generated_targets)
qt_install(TARGETS ${generated_targets}
EXPORT "${export_name}Targets"
DESTINATION "${INSTALL_LIBDIR}"
)
qt_internal_add_targets_to_additional_targets_export_file(
TARGETS ${generated_targets}
EXPORT_NAME_PREFIX "${export_name}"
)
endif()
endif()
get_target_property(target_type ${target} TYPE)
if(arg_TYPE_REGISTRATION_FUNCTION)
set(registration_file
"${CMAKE_CURRENT_BINARY_DIR}/${target}_protobuf_module_registration.cpp")
set(content "#include <QtProtobuf/qtprotobufglobal.h>\n\
QT_BEGIN_NAMESPACE\n\
extern Q_DECL_IMPORT void ${arg_TYPE_REGISTRATION_FUNCTION}();\n\
Q_CONSTRUCTOR_FUNCTION(${arg_TYPE_REGISTRATION_FUNCTION})\n\
QT_END_NAMESPACE\n")
qt_internal_get_main_cmake_configuration(main_config)
file(GENERATE
OUTPUT "${registration_file}"
CONTENT "${content}"
CONDITION "$<CONFIG:${main_config}>")
if(target_type STREQUAL "STATIC_LIBRARY"
OR (WIN32 AND NOT target_type STREQUAL "EXECUTABLE"))
set(object_library ${target}_protobuf_module_registration)
add_library(${object_library} OBJECT ${registration_file})
qt_internal_link_internal_platform_for_object_library(${object_library})
_qt_internal_copy_dependency_properties(${object_library}
${target} PRIVATE_ONLY)
target_link_libraries(${target}
INTERFACE "$<TARGET_OBJECTS:$<TARGET_NAME:${object_library}>>")
add_dependencies(${target} ${object_library})
qt_install(TARGETS ${object_library}
EXPORT "${INSTALL_CMAKE_NAMESPACE}${target}Targets"
DESTINATION "${INSTALL_LIBDIR}"
)
qt_internal_add_targets_to_additional_targets_export_file(
TARGETS ${object_library}
EXPORT_NAME_PREFIX "${INSTALL_CMAKE_NAMESPACE}${target}"
)
else()
target_sources(${target} PRIVATE "${registration_file}")
endif()
endif()
endfunction()

View File

@ -0,0 +1,6 @@
set_property(TARGET ${QT_CMAKE_EXPORT_NAMESPACE}::@target@ APPEND PROPERTY
QT_PROTO_INCLUDES "@proto_include_dir@")
if(NOT QT_NO_CREATE_VERSIONLESS_TARGETS)
set_property(TARGET Qt::@target@ PROPERTY QT_PROTO_INCLUDES
$<TARGET_PROPERTY:${QT_CMAKE_EXPORT_NAMESPACE}::@target@,QT_PROTO_INCLUDES>)
endif()