CMakeDetermineSwiftCompiler.cmake 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. if (NOT _CMAKE_TOOLCHAIN_LOCATION)
  61. get_filename_component(_CMAKE_TOOLCHAIN_LOCATION "${CMAKE_Swift_COMPILER}" PATH)
  62. endif ()
  63. set(_CMAKE_PROCESSING_LANGUAGE "Swift")
  64. include(CMakeFindBinUtils)
  65. unset(_CMAKE_PROCESSING_LANGUAGE)
  66. # configure variables set in this file for fast reload later on
  67. configure_file(${CMAKE_ROOT}/Modules/CMakeSwiftCompiler.cmake.in
  68. ${CMAKE_PLATFORM_INFO_DIR}/CMakeSwiftCompiler.cmake @ONLY)
  69. set(CMAKE_Swift_COMPILER_ENV_VAR "SWIFTC")