I'd like for my cross-platform project to use functionality from swift-system, but I'm not sure of the best way to do it.
Right now, my idea is to have a target with a conditional dependency on the swift-system package, then for code within that target to use #canImport to decide which library they actually want to use:
let package = Package(
name: "mylib",
products: [
// Includes everything.
.library(name: "MyLib", targets: ["MyLib"]),
// Base functionality which doesn't require swift-system.
.library(name: "MyLibCore", targets: ["MyLibCore"]),
],
dependencies: [
.package(url: "https://github.com/apple/swift-system", .upToNextMinor(from: "0.0.2")),
],
targets: [
// Everything - Core + System extras.
.target(
name: "MyLib",
dependencies: ["MyLibCore", "MyLibSystemExtras"]
),
// Base functionality.
.target(
name: "MyLibCore"
),
// Swift-system additions.
.target(
name: "MyLibSystemExtras",
dependencies: [
"MyLibCore",
.product(
name: "SystemPackage",
package: "swift-system",
condition: .when(platforms: [.linux, .windows, .wasi, .android, ...every other non-Apple platform... ])
)
]
),
]
)
Then, within MyLibSystemExtras, I'd write:
#if canImport(System)
import System
#else
import SystemPackage
#endif
extension FilePath { ... }
Are there any drawbacks to this approach? I think it should do the right thing, but I can't find any "official" guidance in the swift-system repository.