CMake: Teach qt_config_compile_test to set the build output

The qt_config_compile_test command now assigns the build output of a
config test to the TEST_${name}_OUTPUT variable in the callers scope.

We can use this to show error messages, and it can also be seen in
trace files for better troubleshooting.

It works for all project based calls with CMake 3.16, but for source
code based tests, due to the usage of check_cxx_source_compiles instead
of try_compile, it will only work for CMake 3.23+.

Task-number: QTBUG-122596
Change-Id: Ib9664c158ba9a391bd17bf30a28f9a34eba991d5
Reviewed-by:  Alexey Edelev <alexey.edelev@qt.io>
(cherry picked from commit 3334a77ecfb792fba0144e99887f11cd0fa2506d)
Reviewed-by:  Alexey Edelev <alexey.edelev@qt.io>
(cherry picked from commit 6674c0f8f971d6a54cd3fda1d67103644cc71ff9)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
(cherry picked from commit b37f9b42903d5300064bd4fcc73e7bdeae64c425)
This commit is contained in:
Alexandru Croitor 2024-02-21 11:29:51 +01:00 committed by Qt Cherry-pick Bot
parent 8e2e4cc0e0
commit dd200f61fb

View File

@ -922,6 +922,11 @@ macro(qt_internal_compute_features_from_possible_inputs)
qt_internal_compute_feature_value_from_possible_input(no_prefix)
endmacro()
# Builds either a string of source code or a whole project to determine whether the build is
# successful.
#
# Sets a TEST_${name}_OUTPUT variable with the build output, to the scope of the calling function.
# Sets a TEST_${name} cache variable to either TRUE or FALSE if the build is successful or not.
function(qt_config_compile_test name)
if(DEFINED "TEST_${name}")
return()
@ -1044,8 +1049,11 @@ function(qt_config_compile_test name)
get_filename_component(arg_PROJECT_PATH "${arg_PROJECT_PATH}" REALPATH)
endif()
try_compile(HAVE_${name} "${CMAKE_BINARY_DIR}/config.tests/${name}" "${arg_PROJECT_PATH}"
"${name}" CMAKE_FLAGS ${flags} ${arg_CMAKE_FLAGS})
try_compile(
HAVE_${name} "${CMAKE_BINARY_DIR}/config.tests/${name}" "${arg_PROJECT_PATH}" "${name}"
CMAKE_FLAGS ${flags} ${arg_CMAKE_FLAGS}
OUTPUT_VARIABLE try_compile_output
)
if(${HAVE_${name}})
set(status_label "Success")
@ -1110,7 +1118,19 @@ function(qt_config_compile_test name)
set(_save_CMAKE_REQUIRED_LIBRARIES "${CMAKE_REQUIRED_LIBRARIES}")
set(CMAKE_REQUIRED_LIBRARIES "${arg_LIBRARIES}")
check_cxx_source_compiles("${arg_UNPARSED_ARGUMENTS} ${arg_CODE}" HAVE_${name})
# OUTPUT_VARIABLE is an internal undocumented variable of check_cxx_source_compiles
# since 3.23. Allow an opt out in case this breaks in the future.
set(try_compile_output "")
set(output_var "")
if(CMAKE_VERSION VERSION_GREATER_EQUAL "3.23"
AND NOT QT_INTERNAL_NO_TRY_COMPILE_OUTPUT_VARIABLE)
set(output_var OUTPUT_VARIABLE try_compile_output)
endif()
check_cxx_source_compiles(
"${arg_UNPARSED_ARGUMENTS} ${arg_CODE}" HAVE_${name} ${output_var}
)
set(CMAKE_REQUIRED_LIBRARIES "${_save_CMAKE_REQUIRED_LIBRARIES}")
set(CMAKE_C_STANDARD "${_save_CMAKE_C_STANDARD}")
@ -1122,6 +1142,7 @@ function(qt_config_compile_test name)
endif()
endif()
set(TEST_${name}_OUTPUT "${try_compile_output}" PARENT_SCOPE)
set(TEST_${name} "${HAVE_${name}}" CACHE INTERNAL "${arg_LABEL}")
endfunction()