gpt4 book ai didi

swift - 排除 Swift 包中的文件

转载 作者:可可西里 更新时间:2023-11-01 01:44:42 26 4
gpt4 key购买 nike

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

有没有办法在 Package.swift 中执行此操作,或者有其他方法可以在 Swift Package Manager 中为特定平台定位几个文件?

最佳答案

Is there a way to do this in Package.swift ... ?

Package.swift也可以使用 Swifty,因为包声明文件本身就是一个 .swift 文件。

这里有一些使用 Swift 5.3 的例子 Package Manager Conditional Target Dependencies SE-0273 条件时间

// swift-tools-version:5.3
import PackageDescription

// ...
targets: [
.target(
name: "BKSecurity",
dependencies: [
.product(name: "Crypto", condition: .when(platforms: [.linux])),
"BKFoundation"
]),
// 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"
]
),
// 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)
]),
]
//...
)

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

Package.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
}(),
)

另见

关于swift - 排除 Swift 包中的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57619273/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com