Files
cmake/Tests/RunCMake/EnableLanguageSubdir/RunCMakeTest.cmake
T
Arha Gatram 86759dce81 enable_language: add support for usage in subdirectories
Propagates variables set in enable_language() logic up variable scopes
so that the language can be used by targets in the ancestor directories.
Works through block() and function() scopes as well. The
add_subdirectory() call can be thought of as the effective call site for
enabling the language. The same holds for endblock() and the actual
function call.

Adds a policy CMP0220 to control this behavior. As the language is
"enabled" at each ancestor scope, the policy is checked. If OLD, the
language is not enabled at this scope and the ones above.

Closes: #27881 #27508 #27389
Related: #26751 #27564
2026-08-07 11:05:57 -04:00

49 lines
1.6 KiB
CMake

include(RunCMake)
function(configure_and_build case)
set(RunCMake_TEST_BINARY_DIR ${RunCMake_BINARY_DIR}/${case}-build)
run_cmake(${case})
set(RunCMake_TEST_NO_CLEAN 1)
set(RunCMake_TEST_OUTPUT_MERGE 1)
run_cmake_command(${case}-build ${CMAKE_COMMAND} --build . --target main)
unset(RunCMake_TEST_NO_CLEAN)
unset(RunCMake_TEST_OUTPUT_MERGE)
unset(RunCMake_TEST_BINARY_DIR)
endfunction()
# CMP0220 policy behavior. These cases are language-agnostic (they use CXX)
# and configure-only, so they run on every platform regardless of CMake_TEST_CUDA.
# NEW: language configuration propagates up out of the enabling scope.
run_cmake(CMP0220-NEW)
run_cmake(CMP0220-NEW-function)
run_cmake(CMP0220-NEW-multilevel)
# OLD: language configuration stays in the scope that enabled it.
run_cmake(CMP0220-OLD)
# MIXED: NEW and OLD set at different scope levels around the enabling scope.
run_cmake(CMP0220-MIXED-block)
run_cmake(CMP0220-MIXED-nested-block)
# WARN: whether the optional CMP0220 warning is emitted.
run_cmake(CMP0220-WARN)
run_cmake(CMP0220-WARN-default)
run_cmake(CMP0220-WARN-block)
run_cmake(CMP0220-WARN-top-level)
# These cases enable CXX in a subdirectory and verify that its implicit RC
# language state is usable from an ancestor scope.
if(CMake_TEST_RESOURCES)
configure_and_build(CXXImplicitRC)
run_cmake(CMP0220-MIXED-implicit-RC)
endif()
# The following cases enable CUDA in a subdirectory and build a target in an
# ancestor scope, so they require a working CUDA toolchain.
if(CMake_TEST_CUDA)
foreach(case IN ITEMS CUDAParent CUDASibling)
configure_and_build(${case})
endforeach()
endif()