Skip to content
Snippets Groups Projects
Commit 3ddd7f35 authored by Brad King's avatar Brad King
Browse files

enable_language: Fix test for working compiler with CMP0126 NEW behavior

Update the logic that converts a `try_compile` result from a cache
entry to a normal variable to tolerate an existing normal variable
under CMP0126 NEW behavior.  Otherwise the `try_compile` result
is ignored because CMake uses the false value of the normal variable,
and CMake incorrectly reports that the compiler does not work.

This went unnoticed for some languages (e.g. C and CXX) because the
check for a working compiler is skipped if ABI detection works.
It does affect other languages (e.g. CSharp).

Fixes: #22423
parent 7c6234dd
No related branches found
No related tags found
No related merge requests found
...@@ -50,6 +50,9 @@ if(NOT CMAKE_C_COMPILER_WORKS) ...@@ -50,6 +50,9 @@ if(NOT CMAKE_C_COMPILER_WORKS)
"int main(int argc, char* argv[])\n" "int main(int argc, char* argv[])\n"
"#endif\n" "#endif\n"
"{ (void)argv; return argc-1;}\n") "{ (void)argv; return argc-1;}\n")
# Clear result from normal variable.
unset(CMAKE_C_COMPILER_WORKS)
# Puts test result in cache variable.
try_compile(CMAKE_C_COMPILER_WORKS ${CMAKE_BINARY_DIR} try_compile(CMAKE_C_COMPILER_WORKS ${CMAKE_BINARY_DIR}
${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/testCCompiler.c ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/testCCompiler.c
OUTPUT_VARIABLE __CMAKE_C_COMPILER_OUTPUT) OUTPUT_VARIABLE __CMAKE_C_COMPILER_OUTPUT)
......
...@@ -30,6 +30,9 @@ if(NOT CMAKE_CSharp_COMPILER_WORKS) ...@@ -30,6 +30,9 @@ if(NOT CMAKE_CSharp_COMPILER_WORKS)
" }" " }"
"}" "}"
) )
# Clear result from normal variable.
unset(CMAKE_CSharp_COMPILER_WORKS)
# Puts test result in cache variable.
try_compile(CMAKE_CSharp_COMPILER_WORKS ${CMAKE_BINARY_DIR} "${test_compile_file}" try_compile(CMAKE_CSharp_COMPILER_WORKS ${CMAKE_BINARY_DIR} "${test_compile_file}"
OUTPUT_VARIABLE __CMAKE_CSharp_COMPILER_OUTPUT OUTPUT_VARIABLE __CMAKE_CSharp_COMPILER_OUTPUT
) )
......
...@@ -36,6 +36,10 @@ if(NOT CMAKE_CUDA_COMPILER_WORKS) ...@@ -36,6 +36,10 @@ if(NOT CMAKE_CUDA_COMPILER_WORKS)
"#endif\n" "#endif\n"
"int main(){return 0;}\n") "int main(){return 0;}\n")
# Clear result from normal variable.
unset(CMAKE_CUDA_COMPILER_WORKS)
# Puts test result in cache variable.
try_compile(CMAKE_CUDA_COMPILER_WORKS ${CMAKE_BINARY_DIR} try_compile(CMAKE_CUDA_COMPILER_WORKS ${CMAKE_BINARY_DIR}
${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/main.cu ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/main.cu
OUTPUT_VARIABLE __CMAKE_CUDA_COMPILER_OUTPUT) OUTPUT_VARIABLE __CMAKE_CUDA_COMPILER_OUTPUT)
......
...@@ -43,6 +43,9 @@ if(NOT CMAKE_CXX_COMPILER_WORKS) ...@@ -43,6 +43,9 @@ if(NOT CMAKE_CXX_COMPILER_WORKS)
"# error \"The CMAKE_CXX_COMPILER is set to a C compiler\"\n" "# error \"The CMAKE_CXX_COMPILER is set to a C compiler\"\n"
"#endif\n" "#endif\n"
"int main(){return 0;}\n") "int main(){return 0;}\n")
# Clear result from normal variable.
unset(CMAKE_CXX_COMPILER_WORKS)
# Puts test result in cache variable.
try_compile(CMAKE_CXX_COMPILER_WORKS ${CMAKE_BINARY_DIR} try_compile(CMAKE_CXX_COMPILER_WORKS ${CMAKE_BINARY_DIR}
${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/testCXXCompiler.cxx ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/testCXXCompiler.cxx
OUTPUT_VARIABLE __CMAKE_CXX_COMPILER_OUTPUT) OUTPUT_VARIABLE __CMAKE_CXX_COMPILER_OUTPUT)
......
...@@ -36,6 +36,9 @@ if(NOT CMAKE_Fortran_COMPILER_WORKS) ...@@ -36,6 +36,9 @@ if(NOT CMAKE_Fortran_COMPILER_WORKS)
PRINT *, 'Hello' PRINT *, 'Hello'
END END
") ")
# Clear result from normal variable.
unset(CMAKE_Fortran_COMPILER_WORKS)
# Puts test result in cache variable.
try_compile(CMAKE_Fortran_COMPILER_WORKS ${CMAKE_BINARY_DIR} try_compile(CMAKE_Fortran_COMPILER_WORKS ${CMAKE_BINARY_DIR}
${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/testFortranCompiler.f ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/testFortranCompiler.f
OUTPUT_VARIABLE OUTPUT) OUTPUT_VARIABLE OUTPUT)
......
...@@ -46,6 +46,9 @@ if(NOT CMAKE_HIP_COMPILER_WORKS) ...@@ -46,6 +46,9 @@ if(NOT CMAKE_HIP_COMPILER_WORKS)
"# error \"The CMAKE_HIP_COMPILER is set to a C/CXX compiler\"\n" "# error \"The CMAKE_HIP_COMPILER is set to a C/CXX compiler\"\n"
"#endif\n" "#endif\n"
"int main(){return 0;}\n") "int main(){return 0;}\n")
# Clear result from normal variable.
unset(CMAKE_HIP_COMPILER_WORKS)
# Puts test result in cache variable.
try_compile(CMAKE_HIP_COMPILER_WORKS ${CMAKE_BINARY_DIR} try_compile(CMAKE_HIP_COMPILER_WORKS ${CMAKE_BINARY_DIR}
${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/testHIPCompiler.hip ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/testHIPCompiler.hip
OUTPUT_VARIABLE __CMAKE_HIP_COMPILER_OUTPUT) OUTPUT_VARIABLE __CMAKE_HIP_COMPILER_OUTPUT)
......
...@@ -47,6 +47,9 @@ if(NOT CMAKE_OBJC_COMPILER_WORKS) ...@@ -47,6 +47,9 @@ if(NOT CMAKE_OBJC_COMPILER_WORKS)
"#endif\n" "#endif\n"
"int main(int argc, char* argv[])\n" "int main(int argc, char* argv[])\n"
"{ (void)argv; return argc-1;}\n") "{ (void)argv; return argc-1;}\n")
# Clear result from normal variable.
unset(CMAKE_OBJC_COMPILER_WORKS)
# Puts test result in cache variable.
try_compile(CMAKE_OBJC_COMPILER_WORKS ${CMAKE_BINARY_DIR} try_compile(CMAKE_OBJC_COMPILER_WORKS ${CMAKE_BINARY_DIR}
${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/testOBJCCompiler.m ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/testOBJCCompiler.m
OUTPUT_VARIABLE __CMAKE_OBJC_COMPILER_OUTPUT) OUTPUT_VARIABLE __CMAKE_OBJC_COMPILER_OUTPUT)
......
...@@ -46,6 +46,9 @@ if(NOT CMAKE_OBJCXX_COMPILER_WORKS) ...@@ -46,6 +46,9 @@ if(NOT CMAKE_OBJCXX_COMPILER_WORKS)
"# error \"The CMAKE_OBJCXX_COMPILER is not an Objective-C++ compiler\"\n" "# error \"The CMAKE_OBJCXX_COMPILER is not an Objective-C++ compiler\"\n"
"#endif\n" "#endif\n"
"int main(){return 0;}\n") "int main(){return 0;}\n")
# Clear result from normal variable.
unset(CMAKE_OBJCXX_COMPILER_WORKS)
# Puts test result in cache variable.
try_compile(CMAKE_OBJCXX_COMPILER_WORKS ${CMAKE_BINARY_DIR} try_compile(CMAKE_OBJCXX_COMPILER_WORKS ${CMAKE_BINARY_DIR}
${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/testOBJCXXCompiler.mm ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/testOBJCXXCompiler.mm
OUTPUT_VARIABLE __CMAKE_OBJCXX_COMPILER_OUTPUT) OUTPUT_VARIABLE __CMAKE_OBJCXX_COMPILER_OUTPUT)
......
...@@ -23,6 +23,9 @@ if(NOT CMAKE_Swift_COMPILER_WORKS) ...@@ -23,6 +23,9 @@ if(NOT CMAKE_Swift_COMPILER_WORKS)
PrintTestCompilerStatus("Swift") PrintTestCompilerStatus("Swift")
file(WRITE ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/main.swift file(WRITE ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/main.swift
"print(\"CMake\")\n") "print(\"CMake\")\n")
# Clear result from normal variable.
unset(CMAKE_Swift_COMPILER_WORKS)
# Puts test result in cache variable.
try_compile(CMAKE_Swift_COMPILER_WORKS ${CMAKE_BINARY_DIR} try_compile(CMAKE_Swift_COMPILER_WORKS ${CMAKE_BINARY_DIR}
${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/main.swift ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/main.swift
OUTPUT_VARIABLE __CMAKE_Swift_COMPILER_OUTPUT) OUTPUT_VARIABLE __CMAKE_Swift_COMPILER_OUTPUT)
......
cmake_minimum_required(VERSION 3.3) cmake_minimum_required(VERSION 3.3)
if(POLICY CMP0126)
cmake_policy(SET CMP0126 NEW)
endif()
# a simple CSharp only test case # a simple CSharp only test case
project (CSharpOnly CSharp) project (CSharpOnly CSharp)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment