1 # Distributed under the OSI-approved BSD 3-Clause License. See accompanying
2 # file Copyright.txt or https://cmake.org/licensing for details.
4 if(CMAKE_Swift_COMPILER_FORCED)
5 # The compiler configuration was forced by the user.
6 # Assume the user has configured all compiler information.
7 set(CMAKE_Swift_COMPILER_WORKS TRUE)
11 include(CMakeTestCompilerCommon)
13 # Remove any cached result from an older CMake version.
14 # We now store this in CMakeSwiftCompiler.cmake.
15 unset(CMAKE_Swift_COMPILER_WORKS CACHE)
17 # This file is used by EnableLanguage in cmGlobalGenerator to
18 # determine that the selected C++ compiler can actually compile
19 # and link the most basic of programs. If not, a fatal error
20 # is set and cmake stops processing commands and will not generate
21 # any makefiles or projects.
22 if(NOT CMAKE_Swift_COMPILER_WORKS)
23 PrintTestCompilerStatus("Swift")
24 # Clear result from normal variable.
25 unset(CMAKE_Swift_COMPILER_WORKS)
26 # Puts test result in cache variable.
27 set(__CMAKE_Swift_TEST_SOURCE "print(\"CMake\")\n")
28 try_compile(CMAKE_Swift_COMPILER_WORKS
29 SOURCE_FROM_VAR main.swift __CMAKE_Swift_TEST_SOURCE
30 OUTPUT_VARIABLE __CMAKE_Swift_COMPILER_OUTPUT)
31 # Move result from cache to normal variable.
32 set(CMAKE_Swift_COMPILER_WORKS ${CMAKE_Swift_COMPILER_WORKS})
33 unset(CMAKE_Swift_COMPILER_WORKS CACHE)
34 set(Swift_TEST_WAS_RUN 1)
37 if(NOT CMAKE_Swift_COMPILER_WORKS)
38 PrintTestCompilerResult(CHECK_FAIL "broken")
39 file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log
40 "Determining if the Swift compiler works failed with "
41 "the following output:\n${__CMAKE_Swift_COMPILER_OUTPUT}\n\n")
42 string(REPLACE "\n" "\n " _output "${__CMAKE_Swift_COMPILER_OUTPUT}")
43 message(FATAL_ERROR "The Swift compiler\n \"${CMAKE_Swift_COMPILER}\"\n"
44 "is not able to compile a simple test program.\nIt fails "
45 "with the following output:\n ${_output}\n\n"
46 "CMake will not be able to correctly generate this project.")
48 if(Swift_TEST_WAS_RUN)
49 PrintTestCompilerResult(CHECK_PASS "works")
50 file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeOutput.log
51 "Determining if the Swift compiler works passed with "
52 "the following output:\n${__CMAKE_Swift_COMPILER_OUTPUT}\n\n")
55 # Unlike C and CXX we do not yet detect any information about the Swift ABI.
56 # However, one of the steps done for C and CXX as part of that detection is
57 # to initialize the implicit include directories. That is relevant here.
58 set(CMAKE_Swift_IMPLICIT_INCLUDE_DIRECTORIES "${_CMAKE_Swift_IMPLICIT_INCLUDE_DIRECTORIES_INIT}")
60 # Re-configure to save learned information.
61 configure_file(${CMAKE_ROOT}/Modules/CMakeSwiftCompiler.cmake.in
62 ${CMAKE_PLATFORM_INFO_DIR}/CMakeSwiftCompiler.cmake @ONLY)
63 include(${CMAKE_PLATFORM_INFO_DIR}/CMakeSwiftCompiler.cmake)
66 unset(__CMAKE_Swift_TEST_SOURCE)
67 unset(__CMAKE_Swift_COMPILER_OUTPUT)