From a9180ccf9a9e845658ba455e2dbc2447b18d2654 Mon Sep 17 00:00:00 2001 From: Saleem Abdulrasool Date: Tue, 23 Apr 2019 10:05:25 -0700 Subject: Tests: add a check for the Swift compiler This ensures that the tests only run when the Swift compiler is present. --- Tests/CMakeLists.txt | 10 ++++-- Tests/CheckSwift.cmake | 61 +++++++++++++++++++++++++++++++++ Tests/RunCMake/CMakeLists.txt | 2 +- Tests/RunCMake/Swift/RunCMakeTest.cmake | 4 +++ 4 files changed, 74 insertions(+), 3 deletions(-) create mode 100644 Tests/CheckSwift.cmake diff --git a/Tests/CMakeLists.txt b/Tests/CMakeLists.txt index d992986..a0f90b2 100644 --- a/Tests/CMakeLists.txt +++ b/Tests/CMakeLists.txt @@ -22,6 +22,7 @@ macro(ADD_TEST_MACRO NAME) endmacro() include(${CMAKE_CURRENT_SOURCE_DIR}/CheckFortran.cmake) +include(${CMAKE_CURRENT_SOURCE_DIR}/CheckSwift.cmake) # Fake a user home directory to avoid polluting the real one. if(DEFINED ENV{HOME} AND NOT CTEST_NO_TEST_HOME) @@ -365,11 +366,16 @@ if(BUILD_TESTING) ((NOT CMAKE_OSX_SDKPRODUCT STREQUAL "Mac OS X") OR (NOT CMAKE_OSX_SDKVERSION VERSION_LESS 10.10))) if(CMAKE_GENERATOR STREQUAL "Xcode") - ADD_TEST_MACRO(SwiftMix SwiftMix) - ADD_TEST_MACRO(SwiftOnly SwiftOnly) + 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) + ADD_TEST_MACRO(SwiftMix SwiftMix) + endif() + endif() if(CMAKE_Fortran_COMPILER) ADD_TEST_MACRO(FortranOnly FortranOnly) endif() diff --git a/Tests/CheckSwift.cmake b/Tests/CheckSwift.cmake new file mode 100644 index 0000000..fcbae7e --- /dev/null +++ b/Tests/CheckSwift.cmake @@ -0,0 +1,61 @@ +if(NOT CMAKE_GENERATOR MATCHES "Xcode|Ninja") + set(CMAKE_Swift_COMPILER "") + return() +endif() + +if(NOT DEFINED CMAKE_Swift_COMPILER) + set(_desc "Looking for a Swift compiler") + message(STATUS ${_desc}) + + file(REMOVE_RECURSE ${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/CheckSwift) + + file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/CheckSwift/CMakeLists.txt" + "cmake_minimum_required(VERSION 3.14) +project(CheckSwift Swift) +file(WRITE \"\${CMAKE_CURRENT_BINARY_DIR}/result.cmake\" + \"set(CMAKE_Swift_COMPILER \\\"\${CMAKE_Swift_COMPILER}\\\")\\n\" + \"set(CMAKE_Swift_FLAGS \\\"\${CMAKE_Swift_FLAGS}\\\")\\n\") +") + + if(CMAKE_GENERATOR_INSTANCE) + set(_D_CMAKE_GENERATOR_INSTANCE "-DCMAKE_GENERATOR_INSTANCE:INTERNAL=${CMAKE_GENERATOR_INSTANCE}") + else() + set(_D_CMAKE_GENERATOR_INSTANCE "") + endif() + + execute_process(WORKING_DIRECTORY + ${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/CheckSwift + COMMAND + ${CMAKE_COMMAND} . -G ${CMAKE_GENERATOR} + -A "${CMAKE_GENERATOR_PLATFORM}" + -T "${CMAKE_GENERATOR_TOOLSET}" + ${_D_CMAKE_GENERATOR_INSTANCE} + TIMEOUT + 60 + OUTPUT_VARIABLE + output + ERROR_VARIABLE + output + RESULT_VARIABLE + result) + + include(${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/CheckSwift/result.cmake + OPTIONAL) + if(CMAKE_Swift_COMPILER AND "${result}" STREQUAL "0") + file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeOutput.log + "${_desc} passed with the following output:\n" + "${output}\n") + else() + file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log + "${_desc} failed with the following output:\n" + "${output}\n") + endif() + + message(STATUS "${_desc} - ${CMAKE_Swift_COMPILER}") + + set(CMAKE_Swift_COMPILER "${CMAKE_Swift_COMPILER}" CACHE FILEPATH "Swift compiler") + set(CMAKE_Swift_FLAGS "${CMAKE_Swift_FLAGS}" CACHE STRING "Swift flags") + + mark_as_advanced(CMAKE_Swift_COMPILER) + mark_as_advanced(CMAKE_Swift_FLAGS) +endif() diff --git a/Tests/RunCMake/CMakeLists.txt b/Tests/RunCMake/CMakeLists.txt index d57138b..0ccfca8 100644 --- a/Tests/RunCMake/CMakeLists.txt +++ b/Tests/RunCMake/CMakeLists.txt @@ -205,7 +205,7 @@ if(UNIX AND CMAKE_SHARED_LIBRARY_RUNTIME_C_FLAG AND CMAKE_EXECUTABLE_FORMAT STRE add_RunCMake_test(RuntimePath) endif() add_RunCMake_test(ScriptMode) -add_RunCMake_test(Swift) +add_RunCMake_test(Swift -DCMAKE_Swift_COMPILER=${CMAKE_Swift_COMPILER}) add_RunCMake_test(TargetObjects) add_RunCMake_test(TargetSources) add_RunCMake_test(ToolchainFile) diff --git a/Tests/RunCMake/Swift/RunCMakeTest.cmake b/Tests/RunCMake/Swift/RunCMakeTest.cmake index 4864295..de99042 100644 --- a/Tests/RunCMake/Swift/RunCMakeTest.cmake +++ b/Tests/RunCMake/Swift/RunCMakeTest.cmake @@ -4,6 +4,10 @@ if(RunCMake_GENERATOR STREQUAL Xcode) if(XCODE_BELOW_6_1) run_cmake(XcodeTooOld) endif() +elseif(RunCMake_GENERATOR STREQUAL Ninja) + if(CMAKE_Swift_COMPILER) + # Add Ninja-specific Swift tests here. + endif() else() run_cmake(NotSupported) endif() -- cgit v0.12