1# Distributed under the OSI-approved BSD 3-Clause License.  See accompanying
2# file Copyright.txt or https://cmake.org/licensing for details.
3
4if(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)
8  return()
9endif()
10
11include(CMakeTestCompilerCommon)
12
13# Remove any cached result from an older CMake version.
14# We now store this in CMakeSwiftCompiler.cmake.
15unset(CMAKE_Swift_COMPILER_WORKS CACHE)
16
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.
22if(NOT CMAKE_Swift_COMPILER_WORKS)
23  PrintTestCompilerStatus("Swift")
24  file(WRITE ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/main.swift
25    "print(\"CMake\")\n")
26  # Clear result from normal variable.
27  unset(CMAKE_Swift_COMPILER_WORKS)
28  # Puts test result in cache variable.
29  try_compile(CMAKE_Swift_COMPILER_WORKS ${CMAKE_BINARY_DIR}
30    ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/main.swift
31    OUTPUT_VARIABLE __CMAKE_Swift_COMPILER_OUTPUT)
32  # Move result from cache to normal variable.
33  set(CMAKE_Swift_COMPILER_WORKS ${CMAKE_Swift_COMPILER_WORKS})
34  unset(CMAKE_Swift_COMPILER_WORKS CACHE)
35  set(Swift_TEST_WAS_RUN 1)
36endif()
37
38if(NOT CMAKE_Swift_COMPILER_WORKS)
39  PrintTestCompilerResult(CHECK_FAIL "broken")
40  file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log
41    "Determining if the Swift compiler works failed with "
42    "the following output:\n${__CMAKE_Swift_COMPILER_OUTPUT}\n\n")
43  string(REPLACE "\n" "\n  " _output "${__CMAKE_Swift_COMPILER_OUTPUT}")
44  message(FATAL_ERROR "The Swift compiler\n  \"${CMAKE_Swift_COMPILER}\"\n"
45    "is not able to compile a simple test program.\nIt fails "
46    "with the following output:\n  ${_output}\n\n"
47    "CMake will not be able to correctly generate this project.")
48else()
49  if(Swift_TEST_WAS_RUN)
50    PrintTestCompilerResult(CHECK_PASS "works")
51    file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeOutput.log
52      "Determining if the Swift compiler works passed with "
53      "the following output:\n${__CMAKE_Swift_COMPILER_OUTPUT}\n\n")
54  endif()
55
56  # Unlike C and CXX we do not yet detect any information about the Swift ABI.
57  # However, one of the steps done for C and CXX as part of that detection is
58  # to initialize the implicit include directories.  That is relevant here.
59  set(CMAKE_Swift_IMPLICIT_INCLUDE_DIRECTORIES "${_CMAKE_Swift_IMPLICIT_INCLUDE_DIRECTORIES_INIT}")
60
61  # Re-configure to save learned information.
62  configure_file(${CMAKE_ROOT}/Modules/CMakeSwiftCompiler.cmake.in
63                 ${CMAKE_PLATFORM_INFO_DIR}/CMakeSwiftCompiler.cmake @ONLY)
64  include(${CMAKE_PLATFORM_INFO_DIR}/CMakeSwiftCompiler.cmake)
65endif()
66
67unset(__CMAKE_Swift_COMPILER_OUTPUT)
68