CMakeDetermineSwiftCompiler.cmake 5.7 KB

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