排除 Swift 包中的文件

Tru*_*an1 8 swift swift-package-manager xcode11

要排除文件的整个部分,我可以使用宏来定位平台,例如#if os(iOS) || os(watchOS).

有没有办法Package.swift在 .

l -*_*c l 8

有没有办法在Package.swift...中做到这一点?

Swifty 的东西也适用Package.swift因为包声明文件本身就是一个.swift文件。

以下是一些使用 Swift 5.3 Package Manager Conditional Target Dependencies SE-0273 conditionwhen.

// swift-tools-version:5.3
import PackageDescription

// ...
    targets: [
        .target(
            name: "BKSecurity",
            dependencies: [
                .product(name: "Crypto", condition: .when(platforms: [.linux])),
                "BKFoundation"
        ]),
Run Code Online (Sandbox Code Playgroud)
// swift-tools-version:5.3
import PackageDescription

// ...
    targets: [
      .target(
        name: "CombineShim",
        dependencies: [
          .product(name: "OpenCombine", 
                   package: "OpenCombine",
                   condition: .when(platforms: [.wasi, .linux])
        )]
      ),
      .target(
        name: "TokamakShim",
        dependencies: [
          .target(name: "TokamakDOM", condition: .when(platforms: [.wasi])),
          "SomeCommonDependency"
        ]
      ),
Run Code Online (Sandbox Code Playgroud)
// swift-tools-version:5.3
import PackageDescription

let supportsCoreAudio: BuildSettingCondition = 
        .when(platforms: [.iOS, .macOS, .tvOS, .watchOS])
let supportsALSA: BuildSettingCondition = 
        .when(platforms: [.linux])

let package = Package(
    name: "portaudio",
// ...
  targets: [
    .target(
      name: "libportaudio",
      dependencies: [],
      cSettings: [
        .define("PA_USE_COREAUDIO", supportsCoreAudio),
        .define("PA_USE_ALSA", supportsALSA)
      ],
      linkerSettings: [
        .linkedLibrary("asound", supportsALSA),
        .linkedFramework("CoreAudio", supportsCoreAudio),
        .linkedFramework("CoreServices", supportsCoreAudio),
        .linkedFramework("CoreFoundation", supportsCoreAudio),
        .linkedFramework("AudioUnit", supportsCoreAudio),
        .linkedFramework("AudioToolbox", supportsCoreAudio)
    ]),
  ]
//...
)
Run Code Online (Sandbox Code Playgroud)

请注意,#if os(…)可以在Package.swift. 但是,Package.swift在构建平台的上下文中评估、构建和执行。因此,#if os(…)目标平台构建平台(例如 macOS、Linux 或 Windows)时,在上下文中很有用。

包.swift

import PackageDescription

let package = Package(
    // ...
    targets: {
        var targets: [Target] = [
            .testTarget(
                name: "QuickTests",
                dependencies: [ "Quick", "Nimble" ],
                exclude: ["SomeFile.ext"]
            ),
        ]
#if os(macOS)
        // macOS build platform
        targets.append(contentsOf: [
            .target(name: "QuickSpecBase", dependencies: []),
            .target(name: "Quick", dependencies: [ "QuickSpecBase" ]),
        ])
#else
        // not macOS build platform, e.g. linux
        targets.append(contentsOf: [
            .target(name: "Quick", dependencies: []),
        ])
#endif
        return targets
    }(),
)
Run Code Online (Sandbox Code Playgroud)

也可以看看

  • 目前我并不完全肯定,但我怀疑 `Package.swift` 中的 `#if os(macOS)` 可能指的是 Xcode 运行的操作系统,而不是它正在编译的目标操作系统。但我对此并不确定。 (3认同)
  • 排除列表可以是目录,例如“QuickTests” (2认同)