From b8b8dd9fe2fb4257dc5a2062dfb8f4f5ef6f5891 Mon Sep 17 00:00:00 2001 From: Alexey Edelev Date: Tue, 18 Mar 2025 18:45:22 +0100 Subject: [PATCH] Avoid storing absolute paths to protobuf module includes Sanitize proto includes to ensure the generated ProtobufProperties.cmake is fully relocatable. Drive-by, ensure that includes do not contain duplicates. Task-number: QTBUG-130113 Change-Id: I64f56d497d412705f174a027f711b90ad7614abf Reviewed-by: Alexandru Croitor --- src/protobuf/Qt6ProtobufBuildInternals.cmake | 62 ++++++++++++++++---- src/protobuf/QtProtobufProperties.cmake.in | 15 ++--- 2 files changed, 57 insertions(+), 20 deletions(-) diff --git a/src/protobuf/Qt6ProtobufBuildInternals.cmake b/src/protobuf/Qt6ProtobufBuildInternals.cmake index f76bbe5d..619c7a00 100644 --- a/src/protobuf/Qt6ProtobufBuildInternals.cmake +++ b/src/protobuf/Qt6ProtobufBuildInternals.cmake @@ -53,8 +53,8 @@ function(qt_internal_add_protobuf_module target) endif() if(arg_PROTO_FILES) - set(module_extra_properties_filename - "${INSTALL_CMAKE_NAMESPACE}${target}ProtobufProperties.cmake") + qt_internal_protobuf_get_module_properties_file_name(module_extra_properties_filename + ${target}) endif() qt_internal_add_module(${target} @@ -104,16 +104,12 @@ function(qt_internal_add_protobuf_module target) set_target_properties(${target} PROPERTIES QT_PROTO_INCLUDES "${proto_files_base_dir}") - set(proto_include_dirs "${module_install_interface_include_dir}") + qt_internal_protobuf_generate_properties(${target} + PROTO_INCLUDES "${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) + set(export_name "${INSTALL_CMAKE_NAMESPACE}${target}") + list(REMOVE_DUPLICATES generated_targets) qt_install(TARGETS ${generated_targets} EXPORT "${export_name}Targets" @@ -169,3 +165,49 @@ QT_END_NAMESPACE\n") endif() endif() endfunction() + +# Returns name of the protobuf module extra properties file +function(qt_internal_protobuf_get_module_properties_file_name out_var target) + set(${out_var} "${INSTALL_CMAKE_NAMESPACE}${target}ProtobufProperties.cmake" PARENT_SCOPE) +endfunction() + +# Generates the protobuf module extra properties file +# +# Synopsys +# +# qt_internal_protobuf_generate_properties(target +# [PROTO_INCLUDES path...] +# ) +# +# Arguments +# +# `target` the protobuf module target +# +# `PROTO_INCLUDES` list of the protobuf include paths to be stored in properties +# file. All paths must be relative to the module install prefix. +function(qt_internal_protobuf_generate_properties target) + cmake_parse_arguments(PARSE_ARGV 1 arg "" "" "PROTO_INCLUDES") + + # Sanitize includes and ensure they only contain relative paths + set(PROTO_INCLUDE_DIRS "") + foreach(inc IN LISTS arg_PROTO_INCLUDES) + if(IS_ABSOLUTE "${inc}") + message(FATAL_ERROR "The ${target} protobuf include path is absolute, but should be" + " relative to the target install prefix.") + endif() + if(NOT "${inc}" IN_LIST PROTO_INCLUDE_DIRS) + list(APPEND PROTO_INCLUDE_DIRS "${inc}") + endif() + endforeach() + + qt_internal_protobuf_get_module_properties_file_name(module_extra_properties_filename ${target}) + + 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}") +endfunction() diff --git a/src/protobuf/QtProtobufProperties.cmake.in b/src/protobuf/QtProtobufProperties.cmake.in index 8b49dfab..e58c11d2 100644 --- a/src/protobuf/QtProtobufProperties.cmake.in +++ b/src/protobuf/QtProtobufProperties.cmake.in @@ -1,16 +1,11 @@ if(NOT QT_NO_CREATE_TARGETS) - set(_@target@_proto_include_dirs "@proto_include_dirs@") + set(_@target@_proto_include_dirs "@PROTO_INCLUDE_DIRS@") foreach(proto_include_dir IN LISTS _@target@_proto_include_dirs) - if(IS_ABSOLUTE "${proto_include_dir}") + set_property(TARGET ${QT_CMAKE_EXPORT_NAMESPACE}::@target@ APPEND PROPERTY + QT_PROTO_INCLUDES "${QT6_INSTALL_PREFIX}/${proto_include_dir}") + if(CMAKE_STAGING_PREFIX) set_property(TARGET ${QT_CMAKE_EXPORT_NAMESPACE}::@target@ APPEND PROPERTY - QT_PROTO_INCLUDES "${proto_include_dir}") - else() - set_property(TARGET ${QT_CMAKE_EXPORT_NAMESPACE}::@target@ APPEND PROPERTY - QT_PROTO_INCLUDES "${QT6_INSTALL_PREFIX}/${proto_include_dir}") - if(CMAKE_STAGING_PREFIX) - set_property(TARGET ${QT_CMAKE_EXPORT_NAMESPACE}::@target@ APPEND PROPERTY - QT_PROTO_INCLUDES "${CMAKE_STAGING_PREFIX}/${proto_include_dir}") - endif() + QT_PROTO_INCLUDES "${CMAKE_STAGING_PREFIX}/${proto_include_dir}") endif() endforeach() unset(_@target@_proto_include_dirs)