Home > Software engineering >  How can you make the target products platform specific in swift package manager
How can you make the target products platform specific in swift package manager

Time:12-08

I have the following general architecture. An app that imports a Swift Package for all my model data which is then using with Firestore or direct REST to firestore depending on platform. I use REST for the WatchOS piece since firestore-swift still doesn't support WatchOS.

My Swift Package has a dependency to Firestore which is not an issue. However, I only want the targets in the target section to be available to iOS and MacOS, and not included on WatchOS. Can I specify this in the Package manifest file?

Here is my general diagram of what I am trying to do.

enter image description here

My package manifest looks like this. Note at the bottom the two commented out lines, which I would like to include only if the target is iOS or MacOS.

// swift-tools-version:5.5
// The swift-tools-version declares the minimum version of Swift required to build this package.

import PackageDescription

let package = Package(
    name: "EMobileLibrary",
    platforms: [.iOS(.v15), .watchOS(.v8), .macOS(.v12)],
    products: [
        // Products define the executables and libraries a package produces, and make them visible to other packages.
        .library(
            name: "EMobileLibrary",
            targets: ["EMobileLibrary"]),
    ],
    dependencies: [
        // Dependencies declare other packages that this package depends on.
        .package(name: "AppDevWithSwiftLibrary", url: "https://github.com/directoryname/AppDevWithSwiftLibrary", from: "1.11.6"),
        .package(
            name: "Firebase",
            url: "https://github.com/firebase/firebase-ios-sdk.git",
            .upToNextMajor(from: "8.10.0")
          ),
    ],
    targets: [
        // Targets are the basic building blocks of a package. A target can define a module or a test suite.
        // Targets can depend on other targets in this package, and on products in packages this package depends on.
        .target(
            name: "EMobileLibrary",
            dependencies: [
                .product(name: "AppDevWithSwiftLibrary", package: "AppDevWithSwiftLibrary"),
//                .product(name: "FirebaseFirestore", package: "Firebase"),
//                .product(name: "FirebaseFirestoreSwift-Beta", package: "Firebase")
            ]
        ),
        .testTarget(
            name: "EMobileLibraryTests",
            dependencies: ["EMobileLibrary"]),
    ]
)

CodePudding user response:

Define a dummy wrapper target that has platform dependent dependencies.

Here's an example excerpt from https://github.com/firebase/firebase-ios-sdk/blob/master/Package.swift that defines FirebaseDynamicLinks as only available on iOS, but not tvOS, macOS, or watchOS:

    .library(
      name: "FirebaseDynamicLinks",
      targets: ["FirebaseDynamicLinksTarget"]
    ),
.
.
.
    .target(
      name: "FirebaseDynamicLinksTarget",
      dependencies: [.target(name: "FirebaseDynamicLinks",
                             condition: .when(platforms: [.iOS]))],
      path: "SwiftPM-PlatformExclude/FirebaseDynamicLinksWrap"
    ),

    .target(
      name: "FirebaseDynamicLinks",
       ....
  • Related