Merge topic 'cuda_add_12.8_new_sm_support'
[kiteware-cmake.git] / Modules / CMakeDetermineSwiftCompiler.cmake
blob723942465284b701b9bfea83e24e1e36589aa54f
1 # Distributed under the OSI-approved BSD 3-Clause License.  See accompanying
2 # file Copyright.txt or https://cmake.org/licensing for details.
4 include(${CMAKE_ROOT}/Modules/CMakeDetermineCompiler.cmake)
6 # Local system-specific compiler preferences for this language.
7 include(Platform/${CMAKE_SYSTEM_NAME}-Determine-Swift OPTIONAL)
8 include(Platform/${CMAKE_SYSTEM_NAME}-Swift OPTIONAL)
9 if(NOT CMAKE_Swift_COMPILER_NAMES)
10   set(CMAKE_Swift_COMPILER_NAMES swiftc)
11 endif()
13 if("${CMAKE_GENERATOR}" STREQUAL "Xcode")
14   if(XCODE_VERSION VERSION_LESS 6.1)
15     message(FATAL_ERROR "Swift language not supported by Xcode ${XCODE_VERSION}")
16   endif()
17   set(CMAKE_Swift_COMPILER_XCODE_TYPE sourcecode.swift)
18   execute_process(COMMAND xcrun --find swiftc
19     OUTPUT_VARIABLE _xcrun_out OUTPUT_STRIP_TRAILING_WHITESPACE
20     ERROR_VARIABLE _xcrun_err RESULT_VARIABLE _xcrun_result)
21   if(_xcrun_result EQUAL 0 AND EXISTS "${_xcrun_out}")
22     set(CMAKE_Swift_COMPILER "${_xcrun_out}")
23   else()
24     _cmake_find_compiler_path(Swift)
25   endif()
26 elseif("${CMAKE_GENERATOR}" MATCHES "^Ninja")
27   if(CMAKE_Swift_COMPILER)
28     _cmake_find_compiler_path(Swift)
29   else()
30     set(CMAKE_Swift_COMPILER_INIT NOTFOUND)
32     if(NOT $ENV{SWIFTC} STREQUAL "")
33       get_filename_component(CMAKE_Swift_COMPILER_INIT $ENV{SWIFTC} PROGRAM
34         PROGRAM_ARGS CMAKE_Swift_FLAGS_ENV_INIT)
35       if(CMAKE_Swift_FLAGS_ENV_INIT)
36         set(CMAKE_Swift_COMPILER_ARG1 "${CMAKE_Swift_FLAGS_ENV_INIT}" CACHE
37           STRING "Arguments to the Swift compiler")
38       endif()
39       if(NOT EXISTS ${CMAKE_Swift_COMPILER_INIT})
40         message(FATAL_ERROR "Could not find compiler set in environment variable SWIFTC\n$ENV{SWIFTC}.\n${CMAKE_Swift_COMPILER_INIT}")
41       endif()
42     endif()
44     if(NOT CMAKE_Swift_COMPILER_INIT)
45       set(CMAKE_Swift_COMPILER_LIST swiftc ${_CMAKE_TOOLCHAIN_PREFIX}swiftc)
46     endif()
48     _cmake_find_compiler(Swift)
49   endif()
50   mark_as_advanced(CMAKE_Swift_COMPILER)
51 else()
52   message(FATAL_ERROR "Swift language not supported by \"${CMAKE_GENERATOR}\" generator")
53 endif()
55 # Build a small source file to identify the compiler.
56 if(NOT CMAKE_Swift_COMPILER_ID_RUN)
57   set(CMAKE_Swift_COMPILER_ID_RUN 1)
59   if("${CMAKE_GENERATOR}" STREQUAL "Xcode")
60     list(APPEND CMAKE_Swift_COMPILER_ID_MATCH_VENDORS Apple)
61     set(CMAKE_Swift_COMPILER_ID_MATCH_VENDOR_REGEX_Apple "com.apple.xcode.tools.swift.compiler")
62   endif()
64   # Try to identify the compiler.
65   set(CMAKE_Swift_COMPILER_ID)
66   include(${CMAKE_ROOT}/Modules/CMakeDetermineCompilerId.cmake)
67   CMAKE_DETERMINE_COMPILER_ID(Swift "" CompilerId/main.swift)
68 endif()
70 # Check if we are using the old compiler driver.
71 if(CMAKE_GENERATOR STREQUAL "Xcode")
72   # For Xcode, we can decide driver kind simply by Swift version.
73   if(CMAKE_Swift_COMPILER_VERSION VERSION_GREATER_EQUAL 5.5)
74     set(CMAKE_Swift_COMPILER_USE_OLD_DRIVER FALSE)
75   else()
76     set(CMAKE_Swift_COMPILER_USE_OLD_DRIVER TRUE)
77   endif()
78 elseif(NOT DEFINED CMAKE_Swift_COMPILER_USE_OLD_DRIVER)
79   # Dry-run a WMO build to identify the compiler driver.
81   # Create a clean directory in which to run the test.
82   set(CMAKE_Swift_COMPILER_DRIVER_TEST_DIR ${CMAKE_PLATFORM_INFO_DIR}/SwiftCompilerDriver)
83   file(REMOVE_RECURSE "${CMAKE_Swift_COMPILER_DRIVER_TEST_DIR}")
84   file(MAKE_DIRECTORY "${CMAKE_Swift_COMPILER_DRIVER_TEST_DIR}")
86   # Create a Swift file and an arbitrary linker resource.
87   file(WRITE ${CMAKE_Swift_COMPILER_DRIVER_TEST_DIR}/main.swift "print(\"Hello\")\n")
88   file(WRITE ${CMAKE_Swift_COMPILER_DRIVER_TEST_DIR}/lib.in "\n")
90   # Honor user-specified compiler flags.
91   if(DEFINED CMAKE_Swift_FLAGS)
92     separate_arguments(_CMAKE_Swift_COMPILER_FLAGS_LIST NATIVE_COMMAND "${CMAKE_Swift_FLAGS}")
93   else()
94     separate_arguments(_CMAKE_Swift_COMPILER_FLAGS_LIST NATIVE_COMMAND "${CMAKE_Swift_FLAGS_INIT}")
95   endif()
96   set(_CMAKE_Swift_COMPILER_CHECK_COMMAND "${CMAKE_Swift_COMPILER}" ${_CMAKE_Swift_COMPILER_FLAGS_LIST} -wmo main.swift lib.in "-###")
97   unset(_CMAKE_Swift_COMPILER_FLAGS_LIST)
99   # Execute in dry-run mode so no compilation will be actually performed.
100   execute_process(COMMAND ${_CMAKE_Swift_COMPILER_CHECK_COMMAND}
101     WORKING_DIRECTORY "${CMAKE_Swift_COMPILER_DRIVER_TEST_DIR}"
102     OUTPUT_VARIABLE _CMAKE_Swift_COMPILER_CHECK_OUTPUT)
104   # Check the first frontend execution.  It is on the first line of output.
105   # The old driver treats all inputs as Swift sources while the new driver
106   # can identify "lib.in" as a linker resource.
107   if("${_CMAKE_Swift_COMPILER_CHECK_OUTPUT}" MATCHES "^[^\n]* lib\\.in")
108     set(CMAKE_Swift_COMPILER_USE_OLD_DRIVER TRUE)
109   else()
110     set(CMAKE_Swift_COMPILER_USE_OLD_DRIVER FALSE)
111   endif()
113   # Record the check results in the configure log.
114   list(TRANSFORM _CMAKE_Swift_COMPILER_CHECK_COMMAND PREPEND "\"")
115   list(TRANSFORM _CMAKE_Swift_COMPILER_CHECK_COMMAND APPEND "\"")
116   list(JOIN _CMAKE_Swift_COMPILER_CHECK_COMMAND " " _CMAKE_Swift_COMPILER_CHECK_COMMAND)
117   string(REPLACE "\n" "\n  " _CMAKE_Swift_COMPILER_CHECK_OUTPUT "  ${_CMAKE_Swift_COMPILER_CHECK_OUTPUT}")
118   message(CONFIGURE_LOG
119     "Detected CMAKE_Swift_COMPILER_USE_OLD_DRIVER=\"${CMAKE_Swift_COMPILER_USE_OLD_DRIVER}\" from:\n"
120     "  ${_CMAKE_Swift_COMPILER_CHECK_COMMAND}\n"
121     "with output:\n"
122     "${_CMAKE_Swift_COMPILER_CHECK_OUTPUT}"
123     )
125   unset(_CMAKE_Swift_COMPILER_CHECK_COMMAND)
126   unset(_CMAKE_Swift_COMPILER_CHECK_OUTPUT)
127 endif()
129 if (NOT _CMAKE_TOOLCHAIN_LOCATION)
130   get_filename_component(_CMAKE_TOOLCHAIN_LOCATION "${CMAKE_Swift_COMPILER}" PATH)
131 endif ()
133 set(_CMAKE_PROCESSING_LANGUAGE "Swift")
134 include(CMakeFindBinUtils)
135 unset(_CMAKE_PROCESSING_LANGUAGE)
137 # configure variables set in this file for fast reload later on
138 configure_file(${CMAKE_ROOT}/Modules/CMakeSwiftCompiler.cmake.in
139                ${CMAKE_PLATFORM_INFO_DIR}/CMakeSwiftCompiler.cmake @ONLY)
141 set(CMAKE_Swift_COMPILER_ENV_VAR "SWIFTC")