CMake: Silence the private module warning for dependencies

If a private module package is pulled in by another Qt module we showed
the private module warning message. The user couldn't act on the warning
other than disabling it completely. This doesn't seem useful.

Now, we only show the warning if the user explicitly does
find_package(Qt6FooPrivate).

There's the situation where a private module is pulled in as dependency
and later pulled in by the user. For example, in a static Linux build
    find_package(Qt6 REQUIRED COMPONENTS Gui CorePrivate)
will pull in CorePrivate as transitive dependency of Gui's plugins.

To still see a warning for CorePrivate in this case we need to trigger
the warning in Qt6Config.cmake when handling the COMPONENTS.
To achieve that, the warning code has been moved to a function which is
called in Qt6Config.cmake and Qt6FooPrivateConfig.cmake

Task-number: QTBUG-87776
Change-Id: Ie9cc2d287d1fcb94db77045165f703cebcb6983d
Reviewed-by: Alexey Edelev <alexey.edelev@qt.io>
Reviewed-by: Alexandru Croitor <alexandru.croitor@qt.io>
(cherry picked from commit a9c18e5df240ce4a8bdc4dd3e7b8d5289227d2d9)
This commit is contained in:
Joerg Bornemann 2025-01-29 08:50:17 +01:00
parent 06a591842e
commit 1d4c440053
3 changed files with 41 additions and 12 deletions

View File

@ -165,7 +165,15 @@ foreach(module ${__qt_umbrella_find_components})
_qt_internal_save_find_package_context_for_debugging(@INSTALL_CMAKE_NAMESPACE@${module})
if(NOT @INSTALL_CMAKE_NAMESPACE@${module}_FOUND)
if(@INSTALL_CMAKE_NAMESPACE@${module}_FOUND)
get_target_property(__qt_${module}_is_private @INSTALL_CMAKE_NAMESPACE@::${module}
_qt_is_private_module
)
if(__qt_${module}_is_private)
_qt_internal_show_private_module_warning(${module})
endif()
unset(__qt_${module}_is_private)
else()
find_package(@INSTALL_CMAKE_NAMESPACE@${module}
${@INSTALL_CMAKE_NAMESPACE@_FIND_VERSION}
${_@INSTALL_CMAKE_NAMESPACE@_FIND_PARTS_QUIET}

View File

@ -20,17 +20,8 @@ if(NOT DEFINED "@INSTALL_CMAKE_NAMESPACE@@target_private@_FOUND")
set("@INSTALL_CMAKE_NAMESPACE@@target_private@_FOUND" TRUE)
endif()
if(NOT DEFINED QT_REPO_MODULE_VERSION
AND NOT QT_NO_PRIVATE_MODULE_WARNING
AND NOT __qt_private_module_@target_private@_warning_shown)
message(WARNING
"This project is using headers of the @target_private@ module and will therefore be tied "
"to this specific Qt module build version. "
"Running this project against other versions of the Qt modules may crash at any arbitrary "
"point. This is not a bug, but a result of using Qt internals. You have been warned! "
"\nYou can disable this warning by setting QT_NO_PRIVATE_MODULE_WARNING to ON."
)
set(__qt_private_module_@target_private@_warning_shown TRUE)
if(NOT __qt_@target@_always_load_private_module)
_qt_internal_show_private_module_warning(@target_private@)
endif()
if(NOT QT_NO_CREATE_TARGETS AND @INSTALL_CMAKE_NAMESPACE@@target_private@_FOUND)

View File

@ -127,6 +127,11 @@ endmacro()
# contain preformed dependencies. See foreach block for reference.
# The same applies for find_dependency_path_list.
macro(_qt_internal_find_qt_dependencies target target_dep_list find_dependency_path_list)
set(__qt_${target}_find_qt_dependencies_save_QT_NO_PRIVATE_MODULE_WARNING
${QT_NO_PRIVATE_MODULE_WARNING}
)
set(QT_NO_PRIVATE_MODULE_WARNING ON)
foreach(__qt_${target}_target_dep IN LISTS ${target_dep_list})
list(GET __qt_${target}_target_dep 0 __qt_${target}_pkg)
list(GET __qt_${target}_target_dep 1 __qt_${target}_version)
@ -146,6 +151,11 @@ macro(_qt_internal_find_qt_dependencies target target_dep_list find_dependency_p
endif()
endif()
endforeach()
set(QT_NO_PRIVATE_MODULE_WARNING
${__qt_${target}_find_qt_dependencies_save_QT_NO_PRIVATE_MODULE_WARNING}
)
unset(__qt_${target}_find_qt_dependencies_save_QT_NO_PRIVATE_MODULE_WARNING)
endmacro()
# If a dependency package was not found, provide some hints in the error message on how to debug
@ -312,3 +322,23 @@ macro(_qt_internal_setup_qt_host_path
endif()
endif()
endmacro()
function(_qt_internal_show_private_module_warning module)
if(DEFINED QT_REPO_MODULE_VERSION OR QT_NO_PRIVATE_MODULE_WARNING)
return()
endif()
get_cmake_property(warning_shown __qt_private_module_${module}_warning_shown)
if(warning_shown)
return()
endif()
message(WARNING
"This project is using headers of the ${module} module and will therefore be tied "
"to this specific Qt module build version. "
"Running this project against other versions of the Qt modules may crash at any arbitrary "
"point. This is not a bug, but a result of using Qt internals. You have been warned! "
"\nYou can disable this warning by setting QT_NO_PRIVATE_MODULE_WARNING to ON."
)
set_property(GLOBAL PROPERTY __qt_private_module_${module}_warning_shown TRUE)
endfunction()