From dac30f073b25ad70f51e66299dcb69fd0233bfce Mon Sep 17 00:00:00 2001 From: Alexandru Croitor Date: Thu, 19 Jun 2025 11:07:59 +0200 Subject: [PATCH] CMake: Pass qt-configured linker flag when building examples If Qt is configured to build with lld, our examples should use it as well. But we want to do it without forcing the same linker on user projects. Normally if we wanted to do it for all user projects, we would propagate the linker flag via the Platform target, but we can't. In case of in-tree examples, we propagate the flags using add_link_options which is directory-scoped, and will apply to all targets created under the examples directory. For out-of-tree examples, we pass the flags via the equivalent CMAKE__LINKER_FLAGS_INIT variable to the external project. Fixes: QTBUG-137882 Change-Id: Iade8ee2f37032b4a7801e5a628c9cc177d478129 Reviewed-by: Alexey Edelev Reviewed-by: Moss Heim (cherry picked from commit 2293fe9de819ebc6b31eef334dd94112d71fe663) Reviewed-by: Qt Cherry-pick Bot (cherry picked from commit ecefe63b8d5608ba648c6e355bd15a0f1eb368c5) (cherry picked from commit 74a05a5027249f86b5c094127d027237e7532530) --- cmake/QtBuildRepoExamplesHelpers.cmake | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/cmake/QtBuildRepoExamplesHelpers.cmake b/cmake/QtBuildRepoExamplesHelpers.cmake index 9daf2e09a2..d9e219ae80 100644 --- a/cmake/QtBuildRepoExamplesHelpers.cmake +++ b/cmake/QtBuildRepoExamplesHelpers.cmake @@ -83,6 +83,12 @@ macro(qt_examples_build_begin) if(NOT QT_BUILD_EXAMPLES_BY_DEFAULT) set_directory_properties(PROPERTIES EXCLUDE_FROM_ALL TRUE) endif() + + # Add active linker flags to all targets created below this subdirectory. + qt_internal_get_active_linker_flags(active_linker_flags) + if(active_linker_flags) + add_link_options(${active_linker_flags}) + endif() endif() # TODO: Change this to TRUE when all examples in all repos are ported to use @@ -500,6 +506,15 @@ function(qt_internal_add_example_external_project subdir) list(APPEND var_defs -DCMAKE_AUTOGEN_VERBOSE:BOOL=TRUE) endif() + # Pass active linker flags. + qt_internal_get_active_linker_flags(active_linker_flags) + if(active_linker_flags) + foreach(item_type EXE MODULE SHARED) + list(APPEND var_defs + "-DCMAKE_${item_type}_LINKER_FLAGS_INIT:STRING=${active_linker_flags}") + endforeach() + endif() + set(deps "") list(REMOVE_DUPLICATES QT_EXAMPLE_DEPENDENCIES) foreach(dep IN LISTS QT_EXAMPLE_DEPENDENCIES)