简体   繁体   中英

How can I conditionally depend on a system library for desktop vs iOS?

I'm working on a Swift package that should depend on GLFW only on desktop (MacOS or Linux), but not for iOS, since GLFW does not build for iOS.

How would I go about this?

I have a system library defined like so:

import PackageDescription

let package = Package(
    name: "GLFWSystemLibrary",
    products: [
        .library(name: "CGLFW3", targets: ["CGLFW3"]),
    ],
    dependencies: [],
    targets: [
        .systemLibrary(
            name: "CGLFW3",
            pkgConfig: "glfw3",
            providers: [
                .brew(["glfw"]),
                .apt(["libglfw3"])
            ]
        )
    ]
)

And my build fails when I try to include this in an iOS project.

Is it enough to just conditionally include CGLFW in the libraries which depend on this, or else how would I make a dependency conditional in Swift Package Manager?

Ie can I do something like this?

#if !os(iOS)
import CGLFW
#endif

Or else how would I achieve this?

Your code totally works, Additionally: you can also use:

#if canImport(AppKit)
#endif

Or even:

#if canImport(CGLFW)
#endif

It depends on what you prefer!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM