Home > Software design >  How can I conditionally depend on a system library for desktop vs iOS?
How can I conditionally depend on a system library for desktop vs iOS?

Time:07-31

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?

I.e. can I do something like this?

#if !os(iOS)
import CGLFW
#endif

Or else how would I achieve this?

CodePudding user response:

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!

  • Related