diff --git a/cmake/QtBuildRepoExamplesHelpers.cmake b/cmake/QtBuildRepoExamplesHelpers.cmake index 13e6822da2d..4c60bdd79e3 100644 --- a/cmake/QtBuildRepoExamplesHelpers.cmake +++ b/cmake/QtBuildRepoExamplesHelpers.cmake @@ -135,6 +135,13 @@ endmacro() # Allows building an example either as an ExternalProject or in-tree with the Qt build. # Also allows installing the example sources. function(qt_internal_add_example subdir) + # Don't show warnings for examples that were added via qt_internal_add_example. + # Those that are added via add_subdirectory will see the warning, due to the parent scope + # having the variable set to TRUE. + if(QT_FEATURE_developer_build AND NOT QT_NO_WARN_ABOUT_EXAMPLE_ADD_SUBDIRECTORY_WARNING) + set(QT_WARN_ABOUT_EXAMPLE_ADD_SUBDIRECTORY FALSE) + endif() + # Pre-compute unique example name based on the subdir, in case of target name clashes. qt_internal_get_example_unique_name(unique_example_name "${subdir}") diff --git a/cmake/QtBuildRepoHelpers.cmake b/cmake/QtBuildRepoHelpers.cmake index bcee2b5a871..f2d25e03130 100644 --- a/cmake/QtBuildRepoHelpers.cmake +++ b/cmake/QtBuildRepoHelpers.cmake @@ -447,7 +447,15 @@ macro(qt_build_repo_impl_examples) message(STATUS "Configuring examples.") option(QT_BUILD_EXAMPLES_PROJECT_${PROJECT_NAME} "Configure examples for project ${PROJECT_NAME}" TRUE) - if (QT_BUILD_EXAMPLES_PROJECT_${PROJECT_NAME}) + if(QT_BUILD_EXAMPLES_PROJECT_${PROJECT_NAME}) + + # Set this before any examples subdirectories are added, to warn about examples that are + # added via add_subdirectory() calls instead of qt_internal_add_example(). + if(QT_FEATURE_developer_build + AND NOT QT_NO_WARN_ABOUT_EXAMPLE_ADD_SUBDIRECTORY_WARNING) + set(QT_WARN_ABOUT_EXAMPLE_ADD_SUBDIRECTORY TRUE) + endif() + add_subdirectory(examples) endif() endif() diff --git a/cmake/QtPublicTargetHelpers.cmake b/cmake/QtPublicTargetHelpers.cmake index df9e87db117..02d55465608 100644 --- a/cmake/QtPublicTargetHelpers.cmake +++ b/cmake/QtPublicTargetHelpers.cmake @@ -321,3 +321,17 @@ function(_qt_internal_set_up_static_runtime_library target) endif() endif() endfunction() + +function(_qt_internal_warn_about_example_add_subdirectory) + # This is set by qt_build_repo_impl_examples() in QtBuildRepoHelpers.cmake, only for developer + # builds, to catch examples that are added via add_subdirectory instead of via + # qt_internal_add_example. + if(QT_WARN_ABOUT_EXAMPLE_ADD_SUBDIRECTORY) + get_filename_component(dir_name "${PROJECT_SOURCE_DIR}" NAME) + message(AUTHOR_WARNING + "It looks like this example project was added via add_subdirectory instead of via " + "qt_internal_add_example. This causes issues in certain build configurations. Please " + "change the code to use\n qt_internal_add_example(${dir_name})\ninstead." + ) + endif() +endfunction() diff --git a/src/corelib/Qt6CoreMacros.cmake b/src/corelib/Qt6CoreMacros.cmake index f8fda0ab833..07289156a3a 100644 --- a/src/corelib/Qt6CoreMacros.cmake +++ b/src/corelib/Qt6CoreMacros.cmake @@ -617,6 +617,8 @@ set(_Qt6_COMPONENT_PATH "${CMAKE_CURRENT_LIST_DIR}/..") function(qt6_add_executable target) cmake_parse_arguments(PARSE_ARGV 1 arg "MANUAL_FINALIZATION" "" "") + _qt_internal_warn_about_example_add_subdirectory() + _qt_internal_create_executable("${target}" ${arg_UNPARSED_ARGUMENTS}) target_link_libraries("${target}" PRIVATE Qt6::Core) set_property(TARGET ${target} PROPERTY _qt_expects_finalization TRUE)