From f78ad6223ae1b9495c63f5a466caa89dc50983b5 Mon Sep 17 00:00:00 2001 From: Brad King Date: Fri, 21 Oct 2022 10:31:04 -0400 Subject: Tests: Provide Apple inspection results to CMakeOnly and RunCMake tests Re-order logic to make `CMake_TEST_XCODE_VERSION` and friends available to the `Tests/CMakeOnly` and `Tests/RunCMake` directories. --- Tests/CMakeLists.txt | 35 ++++++++++++++++++----------------- 1 file changed, 18 insertions(+), 17 deletions(-) diff --git a/Tests/CMakeLists.txt b/Tests/CMakeLists.txt index 10242fd..04db70b 100644 --- a/Tests/CMakeLists.txt +++ b/Tests/CMakeLists.txt @@ -271,14 +271,6 @@ if(BUILD_TESTING) find_package(Qt5Widgets QUIET NO_MODULE) endif() - if(NOT CMake_TEST_EXTERNAL_CMAKE) - add_subdirectory(CMakeLib) - endif() - add_subdirectory(CMakeOnly) - add_subdirectory(RunCMake) - - add_subdirectory(FindPackageModeMakefileTest) - # Collect a list of all test build directories. set(TEST_BUILD_DIRS) @@ -342,6 +334,16 @@ if(BUILD_TESTING) endif() endif() + if(CMake_TEST_XCODE_VERSION AND CMAKE_OSX_SDKVERSION AND CMAKE_OSX_SDKPRODUCT) + if((NOT CMake_TEST_XCODE_VERSION VERSION_LESS 6.1) AND + ((NOT CMAKE_OSX_SDKPRODUCT STREQUAL "Mac OS X") OR + (NOT CMAKE_OSX_SDKVERSION VERSION_LESS 10.10))) + if(CMAKE_GENERATOR STREQUAL "Xcode") + set(CMake_TEST_XCODE_SWIFT 1) + endif() + endif() + endif() + # Use 1500 or CTEST_TEST_TIMEOUT for long test timeout value, # whichever is greater. set(CMAKE_LONG_TEST_TIMEOUT 1500) @@ -352,6 +354,14 @@ if(BUILD_TESTING) set(CMAKE_LONG_TEST_TIMEOUT 1500) endif() + if(NOT CMake_TEST_EXTERNAL_CMAKE) + add_subdirectory(CMakeLib) + endif() + add_subdirectory(CMakeOnly) + add_subdirectory(RunCMake) + + add_subdirectory(FindPackageModeMakefileTest) + add_test(NAME CMake.Copyright COMMAND ${CMAKE_CMAKE_COMMAND} -P ${CMAKE_CURRENT_SOURCE_DIR}/CMakeCopyright.cmake) @@ -380,15 +390,6 @@ if(BUILD_TESTING) ADD_TEST_MACRO(MissingSourceFile MissingSourceFile) set_tests_properties(MissingSourceFile PROPERTIES PASS_REGULAR_EXPRESSION "CMake Error at CMakeLists.txt:3 \\(add_executable\\):[ \r\n]*Cannot find source file:[ \r\n]*DoesNotExist/MissingSourceFile.c") - if(CMake_TEST_XCODE_VERSION AND CMAKE_OSX_SDKVERSION AND CMAKE_OSX_SDKPRODUCT) - if((NOT CMake_TEST_XCODE_VERSION VERSION_LESS 6.1) AND - ((NOT CMAKE_OSX_SDKPRODUCT STREQUAL "Mac OS X") OR - (NOT CMAKE_OSX_SDKVERSION VERSION_LESS 10.10))) - if(CMAKE_GENERATOR STREQUAL "Xcode") - set(CMake_TEST_XCODE_SWIFT 1) - endif() - endif() - endif() if(CMAKE_Swift_COMPILER OR CMake_TEST_XCODE_SWIFT) ADD_TEST_MACRO(SwiftOnly SwiftOnly) if(CMake_TEST_XCODE_SWIFT) -- cgit v0.12 From 4451a1f54fd2c1ba6aed4c38c23cb45e09f6be1d Mon Sep 17 00:00:00 2001 From: Brad King Date: Fri, 21 Oct 2022 10:37:48 -0400 Subject: Tests: Factor out a CMake_TEST_Swift variable for Swift test conditions --- Tests/CMakeLists.txt | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/Tests/CMakeLists.txt b/Tests/CMakeLists.txt index 04db70b..6ca08d0 100644 --- a/Tests/CMakeLists.txt +++ b/Tests/CMakeLists.txt @@ -343,6 +343,11 @@ if(BUILD_TESTING) endif() endif() endif() + if(NOT DEFINED CMake_TEST_Swift) + if(CMAKE_Swift_COMPILER OR CMake_TEST_XCODE_SWIFT) + set(CMake_TEST_Swift 1) + endif() + endif() # Use 1500 or CTEST_TEST_TIMEOUT for long test timeout value, # whichever is greater. @@ -390,7 +395,7 @@ if(BUILD_TESTING) ADD_TEST_MACRO(MissingSourceFile MissingSourceFile) set_tests_properties(MissingSourceFile PROPERTIES PASS_REGULAR_EXPRESSION "CMake Error at CMakeLists.txt:3 \\(add_executable\\):[ \r\n]*Cannot find source file:[ \r\n]*DoesNotExist/MissingSourceFile.c") - if(CMAKE_Swift_COMPILER OR CMake_TEST_XCODE_SWIFT) + if(CMake_TEST_Swift) ADD_TEST_MACRO(SwiftOnly SwiftOnly) if(CMake_TEST_XCODE_SWIFT) ADD_TEST_MACRO(SwiftMix SwiftMix) -- cgit v0.12 From 2345139ab5b93cea4cf331af8d840ed5f943dbeb Mon Sep 17 00:00:00 2001 From: Evan Wilde Date: Tue, 27 Sep 2022 15:30:46 -0700 Subject: CheckSourceCompiles: Add support for Swift Plumb through swift `check_source_compiles` support. Add tests to check that valid swift sources compile and invalid sources don't. --- Modules/Internal/CheckSourceCompiles.cmake | 3 +++ Tests/RunCMake/CMakeLists.txt | 3 ++- .../CheckSourceCompiles/CheckSourceCompilesSwift.cmake | 15 +++++++++++++++ Tests/RunCMake/CheckSourceCompiles/RunCMakeTest.cmake | 4 ++++ 4 files changed, 24 insertions(+), 1 deletion(-) create mode 100644 Tests/RunCMake/CheckSourceCompiles/CheckSourceCompilesSwift.cmake diff --git a/Modules/Internal/CheckSourceCompiles.cmake b/Modules/Internal/CheckSourceCompiles.cmake index eadf3da..2d43a76 100644 --- a/Modules/Internal/CheckSourceCompiles.cmake +++ b/Modules/Internal/CheckSourceCompiles.cmake @@ -34,6 +34,9 @@ function(CMAKE_CHECK_SOURCE_COMPILES _lang _source _var) elseif(_lang STREQUAL "OBJCXX") set(_lang_textual "Objective-C++") set(_lang_ext "mm") + elseif(_lang STREQUAL "Swift") + set(_lang_textual "Swift") + set(_lang_ext "swift") else() message (SEND_ERROR "check_source_compiles: ${_lang}: unknown language.") return() diff --git a/Tests/RunCMake/CMakeLists.txt b/Tests/RunCMake/CMakeLists.txt index 6c5ab7f..3f92829 100644 --- a/Tests/RunCMake/CMakeLists.txt +++ b/Tests/RunCMake/CMakeLists.txt @@ -752,7 +752,8 @@ add_RunCMake_test(CheckCompilerFlag -DCMake_TEST_CUDA=${CMake_TEST_CUDA} add_RunCMake_test(CheckSourceCompiles -DCMake_TEST_CUDA=${CMake_TEST_CUDA} -DCMake_TEST_ISPC=${CMake_TEST_ISPC} -DCMAKE_Fortran_COMPILER_ID=${CMAKE_Fortran_COMPILER_ID} - -DCMake_TEST_HIP=${CMake_TEST_HIP}) + -DCMake_TEST_HIP=${CMake_TEST_HIP} + -DCMake_TEST_Swift=${CMake_TEST_Swift}) add_RunCMake_test(CheckSourceRuns -DCMake_TEST_CUDA=${CMake_TEST_CUDA} -DCMAKE_Fortran_COMPILER_ID=${CMAKE_Fortran_COMPILER_ID} -DCMake_TEST_HIP=${CMake_TEST_HIP}) diff --git a/Tests/RunCMake/CheckSourceCompiles/CheckSourceCompilesSwift.cmake b/Tests/RunCMake/CheckSourceCompiles/CheckSourceCompilesSwift.cmake new file mode 100644 index 0000000..767fa69 --- /dev/null +++ b/Tests/RunCMake/CheckSourceCompiles/CheckSourceCompilesSwift.cmake @@ -0,0 +1,15 @@ +enable_language(Swift) +include(CheckSourceCompiles) + +set(Swift 1) # test that this is tolerated + +check_source_compiles(Swift "baz()" SHOULD_FAIL) + +if(SHOULD_FAIL) + message(SEND_ERROR "invalid Swift source didn't fail.") +endif() + +check_source_compiles(Swift "print(\"Hello, CMake\")" SHOULD_BUILD) +if(NOT SHOULD_BUILD) + message(SEND_ERROR "Test failed for valid Swift source.") +endif() diff --git a/Tests/RunCMake/CheckSourceCompiles/RunCMakeTest.cmake b/Tests/RunCMake/CheckSourceCompiles/RunCMakeTest.cmake index df77d3d..2ed3e36 100644 --- a/Tests/RunCMake/CheckSourceCompiles/RunCMakeTest.cmake +++ b/Tests/RunCMake/CheckSourceCompiles/RunCMakeTest.cmake @@ -31,3 +31,7 @@ endif() if(CMake_TEST_HIP) run_cmake(CheckSourceCompilesHIP) endif() + +if(CMake_TEST_Swift) + run_cmake(CheckSourceCompilesSwift) +endif() -- cgit v0.12 From 3fc971c38b3aba4d2b216d31a1e6ced9cda2bf4a Mon Sep 17 00:00:00 2001 From: Evan Wilde Date: Fri, 14 Oct 2022 10:18:52 -0700 Subject: CheckSourceCompiles: For Swift executable, name source 'main.swift' Xcode uses its own heuristics to determine whether or not to accept top-level code in a source file while Ninja uses the swift driver heuristics. With the Swift driver, if the module contains a single file, that file will be parsed as a top-level code context. With Xcode, the single file will only be parsed as top-level code if the name of that file is 'main.swift'. To ensure more consistent behavior between the two generators, if we're building Swift and the try-compile target type is executable or undefined, we name the file `main.swift` to ensure that both will handle the single file as top-level code. --- Modules/Internal/CheckSourceCompiles.cmake | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/Modules/Internal/CheckSourceCompiles.cmake b/Modules/Internal/CheckSourceCompiles.cmake index 2d43a76..bf5a82d 100644 --- a/Modules/Internal/CheckSourceCompiles.cmake +++ b/Modules/Internal/CheckSourceCompiles.cmake @@ -9,7 +9,7 @@ cmake_policy(SET CMP0057 NEW) # if() supports IN_LIST function(CMAKE_CHECK_SOURCE_COMPILES _lang _source _var) if(NOT DEFINED "${_var}") - + set(_lang_filename "src") if(_lang STREQUAL "C") set(_lang_textual "C") set(_lang_ext "c") @@ -37,6 +37,10 @@ function(CMAKE_CHECK_SOURCE_COMPILES _lang _source _var) elseif(_lang STREQUAL "Swift") set(_lang_textual "Swift") set(_lang_ext "swift") + if (NOT DEFINED CMAKE_TRY_COMPILE_TARGET_TYPE + OR CMAKE_TRY_COMPILE_TARGET_TYPE STREQUAL "EXECUTABLE") + set(_lang_filename "main") + endif() else() message (SEND_ERROR "check_source_compiles: ${_lang}: unknown language.") return() @@ -95,7 +99,7 @@ function(CMAKE_CHECK_SOURCE_COMPILES _lang _source _var) endif() string(APPEND _source "\n") try_compile(${_var} - SOURCE_FROM_VAR "src.${_SRC_EXT}" _source + SOURCE_FROM_VAR "${_lang_filename}.${_SRC_EXT}" _source COMPILE_DEFINITIONS -D${_var} ${CMAKE_REQUIRED_DEFINITIONS} ${CHECK_${LANG}_SOURCE_COMPILES_ADD_LINK_OPTIONS} ${CHECK_${LANG}_SOURCE_COMPILES_ADD_LIBRARIES} -- cgit v0.12