From b641a80d55ccde59d8e5c25c63b44df5ee0d999c Mon Sep 17 00:00:00 2001 From: Brad King Date: Mon, 31 Oct 2022 12:43:48 -0400 Subject: Tests: Factor out a CMake_TEST_OBJC variable for OBJC/OBJCXX test conditions --- Tests/CMakeLists.txt | 14 +++++++++++--- Tests/RunCMake/CMakeLists.txt | 12 ++++++------ 2 files changed, 17 insertions(+), 9 deletions(-) diff --git a/Tests/CMakeLists.txt b/Tests/CMakeLists.txt index 850e743..112455b 100644 --- a/Tests/CMakeLists.txt +++ b/Tests/CMakeLists.txt @@ -349,6 +349,12 @@ if(BUILD_TESTING) endif() endif() + if(NOT DEFINED CMake_TEST_OBJC) + if(APPLE AND CMAKE_C_COMPILER_ID MATCHES "Clang|GNU") + set(CMake_TEST_OBJC 1) + endif() + endif() + # Use 1500 or CTEST_TEST_TIMEOUT for long test timeout value, # whichever is greater. set(CMAKE_LONG_TEST_TIMEOUT 1500) @@ -438,6 +444,11 @@ if(BUILD_TESTING) endif() endif() + if(CMake_TEST_OBJC) + add_subdirectory(ObjC) + add_subdirectory(ObjCXX) + endif() + if(${CMAKE_GENERATOR} MATCHES "Visual Studio ([^9]|[9][0-9])") ADD_TEST_MACRO(CSharpOnly CSharpOnly) if(NOT CMAKE_VS_PLATFORM_NAME STREQUAL "ARM64") @@ -2506,9 +2517,6 @@ if(BUILD_TESTING) -Dgen=${CMAKE_GENERATOR} -P ${CMake_SOURCE_DIR}/Tests/CFBundleTest/VerifyResult.cmake) list(APPEND TEST_BUILD_DIRS "${CMake_BINARY_DIR}/Tests/CFBundleTest") - - add_subdirectory(ObjC) - add_subdirectory(ObjCXX) endif() endif() diff --git a/Tests/RunCMake/CMakeLists.txt b/Tests/RunCMake/CMakeLists.txt index 280a21a..88dbe25 100644 --- a/Tests/RunCMake/CMakeLists.txt +++ b/Tests/RunCMake/CMakeLists.txt @@ -299,8 +299,8 @@ if(UNIX AND "${CMAKE_GENERATOR}" MATCHES "Unix Makefiles|Ninja") add_RunCMake_test(CompilerChange) endif() add_RunCMake_test(CompilerNotFound) -if (APPLE AND CMAKE_C_COMPILER_ID MATCHES "Clang|GNU") - list(APPEND CompilerTest_ARGS -DCMake_TEST_OBJC=1) +if(DEFINED CMake_TEST_OBJC) + list(APPEND CompilerTest_ARGS -DCMake_TEST_OBJC=${CMake_TEST_OBJC}) endif() if(CMAKE_Fortran_COMPILER) list(APPEND CompilerTest_ARGS -DCMake_TEST_Fortran=1) @@ -897,13 +897,13 @@ if("${CMAKE_GENERATOR}" MATCHES "Make|Ninja") if(DEFINED CMake_TEST_ISPC) list(APPEND CompilerLauncher_ARGS -DCMake_TEST_ISPC=${CMake_TEST_ISPC}) endif() + if(DEFINED CMake_TEST_OBJC) + list(APPEND CompilerLauncher_ARGS -DCMake_TEST_OBJC=${CMake_TEST_OBJC}) + list(APPEND LinkerLauncher_ARGS -DCMake_TEST_OBJC=${CMake_TEST_OBJC}) + endif() if(CMAKE_Fortran_COMPILER) list(APPEND CompilerLauncher_ARGS -DCMake_TEST_Fortran=1) endif() - if (APPLE AND CMAKE_C_COMPILER_ID MATCHES "Clang|GNU") - list(APPEND CompilerLauncher_ARGS -DCMake_TEST_OBJC=1) - list(APPEND LinkerLauncher_ARGS -DCMake_TEST_OBJC=1) - endif() add_RunCMake_test(CompilerLauncher) set_property(TEST RunCMake.CompilerLauncher APPEND PROPERTY LABELS "CUDA;HIP;ISPC") -- cgit v0.12 From 1abcb094a7172bfadd5fde90b4c6c19f5527a818 Mon Sep 17 00:00:00 2001 From: Brad King Date: Mon, 31 Oct 2022 12:48:40 -0400 Subject: Tests: Fix logic to enable RunCMake.try_compile OBJC/OBJCXX cases CMake itself does not enable the OBJC/OBJCXX languages, so the `CMAKE_OBJ{C,CXX}_STANDARD_DEFAULT` variables are never defined. Detect them inside the test instead. --- Tests/RunCMake/CMakeLists.txt | 3 +-- Tests/RunCMake/try_compile/Inspect.cmake | 20 +++++++++++++++++--- Tests/RunCMake/try_compile/RunCMakeTest.cmake | 15 +++++++++------ 3 files changed, 27 insertions(+), 11 deletions(-) diff --git a/Tests/RunCMake/CMakeLists.txt b/Tests/RunCMake/CMakeLists.txt index 88dbe25..58c1bc8 100644 --- a/Tests/RunCMake/CMakeLists.txt +++ b/Tests/RunCMake/CMakeLists.txt @@ -557,9 +557,8 @@ function(add_RunCMake_test_try_compile) CMake_TEST_CUDA CMake_TEST_ISPC CMake_TEST_HIP + CMake_TEST_OBJC CMake_TEST_FILESYSTEM_1S - CMAKE_OBJC_STANDARD_DEFAULT - CMAKE_OBJCXX_STANDARD_DEFAULT ) if(DEFINED ${var}) list(APPEND try_compile_ARGS -D${var}=${${var}}) diff --git a/Tests/RunCMake/try_compile/Inspect.cmake b/Tests/RunCMake/try_compile/Inspect.cmake index added41..bfca882 100644 --- a/Tests/RunCMake/try_compile/Inspect.cmake +++ b/Tests/RunCMake/try_compile/Inspect.cmake @@ -1,4 +1,18 @@ enable_language(CXX) -file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/info.cmake" " -set(CMAKE_CXX_EXTENSIONS_DEFAULT \"${CMAKE_CXX_EXTENSIONS_DEFAULT}\") -") +if(CMake_TEST_OBJC) + enable_language(OBJC) + enable_language(OBJCXX) +endif() + +set(info "") +foreach(var + CMAKE_CXX_EXTENSIONS_DEFAULT + CMAKE_OBJC_STANDARD_DEFAULT + CMAKE_OBJCXX_STANDARD_DEFAULT + ) + if(DEFINED ${var}) + string(APPEND info "set(${var} \"${${var}}\")\n") + endif() +endforeach() + +file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/info.cmake" "${info}") diff --git a/Tests/RunCMake/try_compile/RunCMakeTest.cmake b/Tests/RunCMake/try_compile/RunCMakeTest.cmake index 7245471..d02a391 100644 --- a/Tests/RunCMake/try_compile/RunCMakeTest.cmake +++ b/Tests/RunCMake/try_compile/RunCMakeTest.cmake @@ -1,5 +1,14 @@ include(RunCMake) +# Detect information from the toolchain: +# - CMAKE_CXX_EXTENSIONS_DEFAULT +# - CMAKE_OBJC_STANDARD_DEFAULT +# - CMAKE_OBJCXX_STANDARD_DEFAULT +run_cmake_with_options(Inspect + -DCMake_TEST_OBJC=${CMake_TEST_OBJC} + ) +include("${RunCMake_BINARY_DIR}/Inspect-build/info.cmake") + run_cmake(NoArgs) run_cmake(OneArg) run_cmake(TwoArgs) @@ -88,12 +97,6 @@ if(RunCMake_GENERATOR MATCHES "Make|Ninja") unset(RunCMake_TEST_NO_CLEAN) endif() -# Lookup CMAKE_CXX_EXTENSIONS_DEFAULT. -# FIXME: Someday we could move this to the top of the file and use it in -# place of some of the values passed by 'Tests/RunCMake/CMakeLists.txt'. -run_cmake(Inspect) -include("${RunCMake_BINARY_DIR}/Inspect-build/info.cmake") - # FIXME: Support more compilers and default standard levels. if (DEFINED CMAKE_CXX_STANDARD_DEFAULT AND DEFINED CMAKE_CXX_EXTENSIONS_DEFAULT AND ( -- cgit v0.12 From bb2370842f5d3919db1b755f94288dfffe28d39f Mon Sep 17 00:00:00 2001 From: Brad King Date: Mon, 31 Oct 2022 13:02:55 -0400 Subject: Tests: Simplify RunCMake.try_compile compiler inspection Detect the compiler id, version, and default standard level in the test's inspection step rather than passing it all in from the host CMake build's results. This avoids repeating details not known to older versions of CMake, and shortens the code. --- Tests/RunCMake/CMakeLists.txt | 48 --------------------------- Tests/RunCMake/try_compile/Inspect.cmake | 7 ++++ Tests/RunCMake/try_compile/RunCMakeTest.cmake | 6 ++++ 3 files changed, 13 insertions(+), 48 deletions(-) diff --git a/Tests/RunCMake/CMakeLists.txt b/Tests/RunCMake/CMakeLists.txt index 58c1bc8..6bf8f3f 100644 --- a/Tests/RunCMake/CMakeLists.txt +++ b/Tests/RunCMake/CMakeLists.txt @@ -502,58 +502,10 @@ if(APPLE) endif() function(add_RunCMake_test_try_compile) - if(CMAKE_VERSION VERSION_LESS 3.9.20170907 AND "x${CMAKE_CXX_COMPILER_ID}" STREQUAL "xMSVC") - # Older CMake versions do not know about MSVC language standards. - # Approximate our logic from MSVC-CXX.cmake. - if ((NOT CMAKE_CXX_COMPILER_VERSION VERSION_LESS 19.0.24215.1 AND - CMAKE_CXX_COMPILER_VERSION VERSION_LESS 19.10) OR - NOT CMAKE_CXX_COMPILER_VERSION VERSION_LESS 19.10.25017) - set(CMAKE_CXX_STANDARD_DEFAULT 14) - elseif (NOT CMAKE_CXX_COMPILER_VERSION VERSION_LESS 16.0) - set(CMAKE_CXX_STANDARD_DEFAULT "") - else() - unset(CMAKE_CXX_STANDARD_DEFAULT) - endif() - endif() - if(CMAKE_VERSION VERSION_LESS 3.18.20200813 AND "x${CMAKE_C_COMPILER_ID}" STREQUAL "xMSVC") - # Older CMake versions do not know about MSVC language standards. - # Approximate our logic from MSVC-C.cmake. - if(NOT CMAKE_C_COMPILER_VERSION VERSION_LESS 19.27) - set(CMAKE_C_STANDARD_DEFAULT 99) - else() - set(CMAKE_C_STANDARD_DEFAULT "") - endif() - endif() - if(CMAKE_VERSION VERSION_LESS 3.20.20210225 AND "x${CMAKE_C_COMPILER_ID}" STREQUAL "xClang") - # Older CMake versions do not know about Clang MSVC compatibility mode - # standards. Approximate the logic from Clang-C.cmake. - if(CMAKE_C_COMPILER_VERSION VERSION_GREATER_EQUAL 11.0) - set(CMAKE_C_STANDARD_DEFAULT 17) - elseif(CMAKE_C_COMPILER_VERSION VERSION_GREATER_EQUAL 3.5.2) - set(CMAKE_C_STANDARD_DEFAULT 11) - endif() - endif() - if(CMAKE_VERSION VERSION_LESS 3.20.6 AND "x${CMAKE_C_COMPILER_ID}" STREQUAL "xIntelLLVM" AND "x${CMAKE_C_SIMULATE_ID}" STREQUAL "xMSVC") - # Older CMake versions accidentally set the default standards to empty when - # IntelLLVM targets the MSVC ABI, thus not activating standard selection. - # Approximate the logic from IntelLLVM-{C,CXX}.cmake. - if(DEFINED CMAKE_C_STANDARD_DEFAULT AND "${CMAKE_C_STANDARD_DEFAULT}" STREQUAL "") - set(CMAKE_C_STANDARD_DEFAULT 17) - endif() - if(DEFINED CMAKE_CXX_STANDARD_DEFAULT AND "${CMAKE_CXX_STANDARD_DEFAULT}" STREQUAL "") - set(CMAKE_CXX_STANDARD_DEFAULT 14) - endif() - endif() foreach( var IN ITEMS CMAKE_SYSTEM_NAME - CMAKE_C_COMPILER_ID - CMAKE_C_COMPILER_VERSION - CMAKE_C_STANDARD_DEFAULT - CMAKE_CXX_COMPILER_ID - CMAKE_CXX_COMPILER_VERSION - CMAKE_CXX_STANDARD_DEFAULT CMake_TEST_CUDA CMake_TEST_ISPC CMake_TEST_HIP diff --git a/Tests/RunCMake/try_compile/Inspect.cmake b/Tests/RunCMake/try_compile/Inspect.cmake index bfca882..2977d02 100644 --- a/Tests/RunCMake/try_compile/Inspect.cmake +++ b/Tests/RunCMake/try_compile/Inspect.cmake @@ -1,3 +1,4 @@ +enable_language(C) enable_language(CXX) if(CMake_TEST_OBJC) enable_language(OBJC) @@ -6,6 +7,12 @@ endif() set(info "") foreach(var + CMAKE_C_COMPILER_ID + CMAKE_C_COMPILER_VERSION + CMAKE_C_STANDARD_DEFAULT + CMAKE_CXX_COMPILER_ID + CMAKE_CXX_COMPILER_VERSION + CMAKE_CXX_STANDARD_DEFAULT CMAKE_CXX_EXTENSIONS_DEFAULT CMAKE_OBJC_STANDARD_DEFAULT CMAKE_OBJCXX_STANDARD_DEFAULT diff --git a/Tests/RunCMake/try_compile/RunCMakeTest.cmake b/Tests/RunCMake/try_compile/RunCMakeTest.cmake index d02a391..ad1cc29 100644 --- a/Tests/RunCMake/try_compile/RunCMakeTest.cmake +++ b/Tests/RunCMake/try_compile/RunCMakeTest.cmake @@ -1,6 +1,12 @@ include(RunCMake) # Detect information from the toolchain: +# - CMAKE_C_COMPILER_ID +# - CMAKE_C_COMPILER_VERSION +# - CMAKE_C_STANDARD_DEFAULT +# - CMAKE_CXX_COMPILER_ID +# - CMAKE_CXX_COMPILER_VERSION +# - CMAKE_CXX_STANDARD_DEFAULT # - CMAKE_CXX_EXTENSIONS_DEFAULT # - CMAKE_OBJC_STANDARD_DEFAULT # - CMAKE_OBJCXX_STANDARD_DEFAULT -- cgit v0.12