Windows

依赖

Swift 有以下一般依赖关系:

  • Git(由 Swift Package Manager 使用)

  • Python1 (由调试器 - lldb 使用)

Windows 具有以下特定于平台的附加依赖项:

  • Windows SDK(提供 Windows 标头和导入库)

  • Visual Studio(为其他标头提供Visual C++ SDK/Build Tools)

安装说明

使用 Windows 程序包管理器安装

The Windows 程序包管理器 可以在 App Store 中找到或 直接安装.

使用Scoop安装

# Optional: Needed to run a remote script the first time
Set-ExecutionPolicy RemoteSigned -Scope CurrentUser
# Command for installing scoop
Invoke-RestMethod -Url 'get.scoop.sh' | Invoke-Expression
  1. 安装所需的依赖项:

    无法通过 Windows 程序包管理器安装平台依赖项,因为安装规则不会安装所需的组件。它们将通过Visual Studio安装程序安装。

    使用 Winget (Windows Package Manager):

    winget install Git.Git
    winget install Python.Python.3.10
    
    curl -sOL https://aka.ms/vs/16/release/vs_community.exe
    start /w vs_community.exe --passive --wait --norestart --nocache ^
      --installPath "%ProgramFiles(x86)%\Microsoft Visual Studio\2019\Community" ^
      --add Microsoft.VisualStudio.Component.Windows10SDK.19041 ^
      --add Microsoft.VisualStudio.Component.VC.Tools.x86.x64
    del /q vs_community.exe
    

    With Scoop:

    # Scoop already comes pre-installed with Git, so no need to re-install it.
    scoop bucket add versions
    scoop install python310
    
    curl -sOL https://aka.ms/vs/16/release/vs_community.exe
    start /w vs_community.exe --passive --wait --norestart --nocache ^
      --installPath "%ProgramFiles(x86)%\Microsoft Visual Studio\2019\Community" ^
      --add Microsoft.VisualStudio.Component.Windows10SDK.19041 ^
      --add Microsoft.VisualStudio.Component.VC.Tools.x86.x64
    del /q vs_community.exe
    

    启动一个新的命令提示符/Powershell 提示符并安装 Python 库 6。

    pip install six
    
  2. 安装 Swift:

    Swift可以直接通过官方安装程序安装,使用Windows Package Manager安装,也可以使用Scoop安装。请注意,Windows Package Manager版本可能落后于正式版本,Scoop不会有此问题。

    • 使用官方安装程序
      1. 下载 最新的软件包版本.
      2. 运行包安装程序。
    • 使用 Windows 程序包管理器:
      winget install Swift.Toolchain
    • Using Scoop:
      scoop install swift

Swift 工具链将安装在: %SystemDrive%\Library\Developer\Toolchains\unknown-Asserts-development.xctoolchain
兼容的 Swift SDK 将安装在: %SystemDrive%\Library\Developer\Platforms\Windows.platform\Developer\SDKs\Windows.sdk .

传统安装

注意:低于 5.4.2 的 Swift 需要传统的安装过程。

Swift 已经在 Visual Studio 2019 中进行了测试。您需要使用以下组件安装 Visual Studio。Swift 的安装程序可在下载部分找到。Windows 上的工具链将安装到 。
%SystemDrive%\Library\Developer\Toolchains

需要以下 Visual Studio 组件:

元件 Visual Studio ID
MSVC v142 - VS 2019 C++ x64/x86 构建工具(最新) Microsoft.VisualStudio.Component.VC.Tools.x86.x64
Windows 10 SDK (10.0.17763.0)2 Microsoft.VisualStudio.Component.Windows10SDK.17763

建议使用以下其他 Visual Studio 组件:

元件 Visual Studio ID
Git for Windows Microsoft.VisualStudio.Component.Git
Python 3 64-bit (3.7.8)2 Component.CPython.x64

建议使用以下附加的 Visual Studio 组件:

元件 Visual Studio ID
C++ CMake tools for Windows Microsoft.VisualStudio.Component.VC.CMake.Project

支持文件

注意:仅 5.4.2 之前的版本才需要执行此操作

必须使用 才能运行以下步骤。从Visual Studio运行脚本,该脚本设置必要的环境变量以查找系统标头。x64 Native Tools for VS2019 Command Promptx64 Native Tools for VS2019 Command PromptDevEnv

为了使 Windows SDK 可供 Swift 访问,有必要将一些文件部署到 Windows SDK 中。以下内容将修改您的 Visual Studio 安装,因此需要从(提升的)“管理员”运行。x86 Native Tools for VS2019 Command Prompt

copy /Y %SDKROOT%\usr\share\ucrt.modulemap "%UniversalCRTSdkDir%\Include\%UCRTVersion%\ucrt\module.modulemap"
copy /Y %SDKROOT%\usr\share\visualc.modulemap "%VCToolsInstallDir%\include\module.modulemap"
copy /Y %SDKROOT%\usr\share\visualc.apinotes "%VCToolsInstallDir%\include\visualc.apinotes"
copy /Y %SDKROOT%\usr\share\winsdk.modulemap "%UniversalCRTSdkDir%\Include\%UCRTVersion%\um\module.modulemap"

因为它正在将文件安装到 Visual Studio 映像中,所以每次更新 Visual Studio 时都需要复制这些文件。

Visual Studio 更新后修复

如果 Visual Studio 已更新,您可能需要修复安装。请参阅Microsoft有关修复已安装程序的说明

Windows 上的代码签名

  1. GnuPG.org 安装 GPG

  2. 如果您是第一次下载 Swift 软件包,请将 PGP 密钥导入到密钥环中:

    $ gpg.exe --keyserver hkp://keyserver.ubuntu.com `
          --receive-keys `
          'A62A E125 BBBF BB96 A6E0  42EC 925C C1CC ED3D 1561' `
          '8A74 9566 2C3C D4AE 18D9  5637 FAF6 989E 1BC1 6FEA'
    

    或者:

    $ wget https://swift.org/keys/all-keys.asc -UseBasicParsing | `
    Select-Object -Expand Content | gpg.exe --import -
    

    如果您过去导入过密钥,请跳过此步骤。

  3. 验证 PGP 签名。

    Windows 的安装程序使用 GnuPG 使用 Swift 开源项目的密钥之一进行签名。强烈建议每个人在使用该软件之前验证签名。.exe

    首先,刷新密钥以下载新的密钥吊销证书(如果有):

    $ gpg.exe --keyserver hkp://keyserver.ubuntu.com --refresh-keys Swift
    

    然后,使用签名文件验证存档是否完好无损:

    $ gpg.exe --verify swift-<VERSION>-<PLATFORM>.exe.sig
    ...
    gpg: Good signature from "Swift Automatic Signing Key #3 <swift-infrastructure@swift.org>"
    

    如果由于没有公钥 () 而无法验证,请按照下面的活动签名密钥中的说明将密钥导入密钥环。

    您可能会看到一条警告:

    gpg: WARNING: This key is not certified with a trusted signature!
    gpg:          There is no indication that the signature belongs to the owner.
    

    此警告表示此密钥与您之间没有信任 Web 中的路径。只要按照上述步骤从受信任的源检索密钥,警告就无害。

    如果验证失败并报告“错误签名”,请不要使用下载的工具链。相反,请通过电子邮件 swift-infrastructure@forums.swift.org 提供尽可能多的详细信息,以便我们调查问题。gpg

活动签名密钥

Swift 项目使用一组密钥进行快照构建,并为每个正式版本使用单独的密钥。我们使用 4096 位 RSA 密钥。

以下键用于对工具链包进行签名:

  • Swift Automatic Signing Key #3 <swift-infrastructure@swift.org>

    下载 https://swift.org/keys/automatic-signing-key-4.asc
    指纹 8A74 9566 2C3C D4AE 18D9 5637 FAF6 989E 1BC1 6FEA
    Long Id FAF6989E1BC16FEA

    要导入密钥,请运行:

    $ gpg.exe --keyserver hkp://keyserver.ubuntu.com `
          --receive-keys `
          '8A74 9566 2C3C D4AE 18D9  5637 FAF6 989E 1BC1 6FEA'
    

    或者:

    $ wget https://swift.org/keys/automatic-signing-key-4.asc -UseBasicParsing | `
     Select-Object -Expand Content | gpg.exe --import -
    
  • Swift 5.x Release Signing Key <swift-infrastructure@swift.org>

    下载 https://swift.org/keys/release-key-swift-5.x.asc
    指纹 A62A E125 BBBF BB96 A6E0 42EC 925C C1CC ED3D 1561
    Long Id 925CC1CCED3D1561

    要导入密钥,请运行:

    $ gpg.exe --keyserver hkp://keyserver.ubuntu.com `
          --receive-keys `
          'A62A E125 BBBF BB96 A6E0  42EC 925C C1CC ED3D 1561'
    

    或者:

    $ wget https://swift.org/keys/release-key-swift-5.x.asc -UseBasicParsing | `
     Select-Object -Expand Content | gpg.exe --import -