如何为 Windows 平台构建 Swift 包?需要哪些特殊配置?

解读

在国内面试中,这道题表面问“跨平台构建”,实则考察候选人对 Swift 工具链、ABI、VC++ 生态与 CI 流程的综合掌握。面试官希望听到:

  1. 你清楚 Windows 上没有 Xcode,必须依赖官方 Snapshot 工具链;
  2. 你能区分“能编译 Swift 源码”与“能产出可交付的 Swift Package”之间的鸿沟;
  3. 你知道如何把 Swift 运行时、WinSDK、Visual Studio 环境变量、SPM 三元组、模块映射、CI 缓存全部串起来,最终让 swift build -c release 在 Windows 跑通。

一句话:这道题考的是“工程落地能力”,不是“语法糖”。

知识点

  1. Windows Swift 官方工具链(swift.org 下载的 exe 安装包)自带 clang、lld、swift-driver,但不含 WinSDK 与 MSVC 运行时,必须本地先装 Visual Studio 2019/2022(带“使用 C++ 的桌面开发”工作负载)。
  2. Swift Package Manager 在 Windows 使用 MSVC 作为链接器,因此需要 vcvars64.bat 环境变量;否则出现找不到 kernel32.lib、msvcrt.lib 等错误。
  3. 三元组(triple)必须显式指定为 x86_64-unknown-windows-msvc;SPM 默认会误用 unknown-windows-gnu,导致链接失败。
  4. 若依赖 C/C++ 目标,需在 Package.swift 中写 systemLibrary 或 target.cxxSettings,并保证头文件路径对 WinSDK 友好;中文路径必须 UTF-8 编码,否则 clang-cl 报错。
  5. Swift 5.9+ 起支持 swift-sdk 概念,可把 Windows SDK 与运行时打包成可迁移 bundle,解决 CI 镜像分发问题;国内公司常用自托管 GitLab Runner,需缓存 C:\Library\Developer\Platforms\Windows.platform 目录,避免每次拉 2 GB 工具链。
  6. 若产物需要脱机部署,必须把 swiftCore.dll、swiftCRT.dll 与 msvcp140.dll 一并拷贝到可执行文件同级目录;国内甲方常因“缺 DLL”拒收交付物,这一步务必写进 Package.swift 的 postBuildCommand 脚本。
  7. 调试符号:Windows 使用 PDB,SPM 默认生成 DWARF,需在 buildSettings 里加 -debug-info-format=codeview -g 才能被 Visual Studio Debugger 识别。
  8. 并发模型:Swift Concurrency 在 Windows 依赖 IOCP,官方已移植,但 Grand Central Dispatch 尚未完整,若代码依赖 DispatchQueue 需改用 swift-async-algorithms 或 StructuredConcurrency 方案。

答案

步骤如下,缺一步都可能在国内 Windows 交付场景被客户打回:

  1. 安装 Visual Studio 2022 社区版,勾选“使用 C++ 的桌面开发”与“Windows 11 SDK 10.0.22621”。
  2. 下载 swift-5.9.2-RELEASE-windows10.exe 并安装到 C:\Library\Developer\Toolchains\unknown-Asserts-development.xctoolchain,安装器会自动写 PATH 与 SDKROOT。
  3. 打开 “x64 Native Tools Command Prompt for VS 2022”,执行:
    swift --version
    
    确认打印 Target: x86_64-unknown-windows-msvc。
  4. 进入 Package 根目录,先清理旧缓存:
    swift package clean
    swift package reset
    
  5. 显式指定 triple 构建:
    swift build -c release -Xswiftc -target -Xswiftc x86_64-unknown-windows-msvc
    
    若依赖 C 模块,再加 -Xcc -I"%WindowsSdkDir%Include\10.0.22621.0\ucrt"。
  6. 运行单元测试:
    swift test -c release -Xswiftc -target -Xswiftc x86_64-unknown-windows-msvc
    
    确保所有用例通过,否则客户验收不签字。
  7. 收集运行时依赖:
    swift run -c release --package-path Tools helper\collect-dlls.swift
    
    把输出目录 Redist\ 连同 .exe 一并打包,交付前在干净 Win10 虚拟机验证双击即可运行。
  8. CI 集成:在 .gitlab-ci.yml 中复用 mcr.microsoft.com/windows/servercore:ltsc2022 镜像,预装 VS Build Tools 与 Swift 工具链,缓存 C:\Library\Developer 与 %LOCALAPPDATA%\cache\org.swift.swiftpm,可将构建时间从 30 min 降到 5 min。

拓展思考

  1. 如果客户要求静态链接 Swift 运行时以减少 DLL 分发,可尝试 -static-swift-stdlib,但 Windows 版静态库目前只开放 swiftCore.lib,swiftCRT.lib 仍动态,需评估体积与许可风险。
  2. 国内部分军工、政企项目强制离线编译,可提前在能联网的机器制作 swift-sdk bundle,把 WinSDK、ucrt、vcruntime、Swift 模块全部打包,拷贝到内网后通过 swift build --experimental-swift-sdk <path> 完成零外网构建。
  3. 若团队同时维护 iOS 与 Windows 双端,建议把公共逻辑拆成纯 Swift Target,避免使用 FoundationProcessInfo、NSFileHandle 等 Darwin 专属 API;可引入 swift-system 与 swift-win32 做条件编译 #if os(Windows),实现“单一代码仓库、双平台交付”,降低人力成本。