# Adding platform-specific dependency to multi-platform Swift Package

**URL:** <https://forums.swift.org/t/adding-platform-specific-dependency-to-multi-platform-swift-package/49645>\
**Category:** Using Swift\
**Tags:** packagemanager\
**Created:** [June 15, 2021, 2:05pm UTC](https://forums.swift.org/t/adding-platform-specific-dependency-to-multi-platform-swift-package/49645 "2021-06-15T14:05:57Z")\
**Posts on this page:** 18\
**Page:** 1

<div class="post-metadata">

**Author:** ![darrarski](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/darrarski/32/6548_2.png) [@darrarski](https://forums.swift.org/u/darrarski)\
**Post date:** [June 15, 2021, 2:05pm UTC](https://forums.swift.org/t/adding-platform-specific-dependency-to-multi-platform-swift-package/49645/1 "2021-06-15T14:05:57Z")

</div>

Hello,

I am struggling with setting up a Swift Package that supports multiple platforms ( **iOS** and **tvOS** in my case) and also depends on a platform-specific library ( **iOS** in my case).

Despite adding a `condition` property to the platform-specific dependency, I can only build my library for the platform supported by the dependency.

My `Package.swift` looks like this:

```swift
// swift-tools-version:5.4
import PackageDescription

let package = Package(
  name: "Demo",
  platforms: [
    .iOS(.v14),
    .tvOS(.v14),
  ],
  products: [
    .library(
      name: "Demo",
      targets: ["Demo"]
    ),
  ],
  dependencies: [
    .package(
      name: "Firebase",
      url: "https://github.com/firebase/firebase-ios-sdk.git",
      .exact("8.1.1")
    ),
  ],
  targets: [
    .target(
      name: "Demo",
      dependencies: [
        .product(
          name: "FirebaseAnalytics",
          package: "Firebase",
          condition: .when(platforms: [.iOS])
        ),
      ]
    ),
    .testTarget(
      name: "DemoTests",
      dependencies: [
        .target(name: "Demo")
      ]
    ),
  ]
)

```

Note that `FirebaseAnalytics` only supports **iOS** platform, but not **tvOS**.

My "Demo" library builds fine for **iOS** , but when trying to build it for **tvOS** , I am getting the following error:

> [...]/FirebaseAnalytics.xcframework:1:1: While building for tvOS Simulator, no library for this platform was found in '[...]/FirebaseAnalytics.xcframework'.

Not sure if such configuration is not supported, or I am doing something wrong here.

I've created an example project that demonstrates the issue:

> **[GitHub - darrarski/spm-platform-specific-dependecy-demo: Demo of...](https://github.com/darrarski/spm-platform-specific-dependecy-demo)**
>
> Demo of multi-platform Swift Package with platform-specific dependency - GitHub - darrarski/spm-platform-specific-dependecy-demo: Demo of multi-platform Swift Package with platform-specific dependency

I am using Apple Swift version 5.4 (swiftlang-1205.0.26.9 clang-1205.0.19.55) that comes with Xcode 12.5 (12E262).

All feedback and suggestions will be appreciated!

---

<div class="post-metadata">

**Author:** ![Jon\_Shier](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/jon_shier/32/921_2.png) [@Jon\_Shier](https://forums.swift.org/u/Jon_Shier)\
**Post date:** [June 15, 2021, 2:50pm UTC](https://forums.swift.org/t/adding-platform-specific-dependency-to-multi-platform-swift-package/49645/2 "2021-06-15T14:50:30Z")

</div>

A typical approach is to conditionalize your `dependencies` array so that it's empty on `tvOS`.

```swift
#if os(tvOS)
let dependencies: [Dependency] = []
#else
let dependencies: [Dependency] = ...
#endif

```

I'm not sure that's the right type for the array but you get the idea.

---

<div class="post-metadata">

**Author:** ![darrarski](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/darrarski/32/6548_2.png) [@darrarski](https://forums.swift.org/u/darrarski)\
**Post date:** [June 15, 2021, 2:55pm UTC](https://forums.swift.org/t/adding-platform-specific-dependency-to-multi-platform-swift-package/49645/3 "2021-06-15T14:55:52Z")

</div>

That’s interesting. Thanks! I will definitely try it out, although I am not sure if this will work inside Package.swift file.

---

<div class="post-metadata">

**Author:** ![Jon\_Shier](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/jon_shier/32/921_2.png) [@Jon\_Shier](https://forums.swift.org/u/Jon_Shier)\
**Post date:** [June 15, 2021, 2:57pm UTC](https://forums.swift.org/t/adding-platform-specific-dependency-to-multi-platform-swift-package/49645/4 "2021-06-15T14:57:20Z")

</div>

Oops, yeah, I keep forgetting it's conditioned on the running OS, not the targeted one. So this would work for Linux vs. Windows vs. macOS, but not for tvOS vs. iOS. What you're doing is supposed to work I think.

---

<div class="post-metadata">

**Author:** ![darrarski](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/darrarski/32/6548_2.png) [@darrarski](https://forums.swift.org/u/darrarski)\
**Post date:** [June 15, 2021, 8:39pm UTC](https://forums.swift.org/t/adding-platform-specific-dependency-to-multi-platform-swift-package/49645/5 "2021-06-15T20:39:16Z")

</div>

Unfortunately, using `#if os(...)` / `#endif` won't help in this case as the platform is defined by the machine I am working on (macOS) and not the target platform I would like to build for (iOS/tvOS).

I have pushed updated demo project to a separate branch. It's closer to what I am working on. My app's source code is modularized using libraries defined in Swift Package, similarly to what PointFree does in their [ISOWORDS](http://github.com/pointfreeco/isowords/) game.

> **[GitHub - darrarski/spm-platform-specific-dependecy-demo at app-example](https://github.com/darrarski/spm-platform-specific-dependecy-demo/tree/app-example)**
>
> app-example

---

<div class="post-metadata">

**Author:** ![SDGGiesbrecht](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/sdggiesbrecht/32/4768_2.png) [@SDGGiesbrecht](https://forums.swift.org/u/SDGGiesbrecht)\
**Post date:** [June 15, 2021, 10:21pm UTC](https://forums.swift.org/t/adding-platform-specific-dependency-to-multi-platform-swift-package/49645/6 "2021-06-15T22:21:34Z")

</div>

Your manifest looks correct to me. (`#if os(...)` is an old workaround with bad side effects and should not be used in manifests anymore.)

I use it all the time, so I can also confirm that in most situations it works properly (including with 5.4 and 12.5).

Whatever is going wrong here must only be triggered by narrower set of conditions. For example, I have never attempted to conditionally use a binary dependency. Maybe there is a bug in the way the two features overlap? Anyway, it would be helpful to have a minimal demonstration where the dependency is less complicated than Firebase.

Regardless, it is supposed to work, so you can file a bug at [bugs.swift.org](http://bugs.swift.org) if you can reproduce it in a native command line build (`swift build`), or with the Feedback Assistant if only Xcode has trouble.

---

<div class="post-metadata">

**Author:** ![Peter-Schorn](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/peter-schorn/32/11183_2.png) [@Peter-Schorn](https://forums.swift.org/u/Peter-Schorn)\
**Post date:** [June 16, 2021, 2:52am UTC](https://forums.swift.org/t/adding-platform-specific-dependency-to-multi-platform-swift-package/49645/7 "2021-06-16T02:52:55Z")

</div>

This definitely looks like a bug to me. Your use case is exactly what conditional dependencies are for.

---

<div class="post-metadata">

**Author:** ![exevil](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/exevil/32/18989_2.png) [@exevil](https://forums.swift.org/u/exevil)\
**Post date:** [July 29, 2021, 7:53pm UTC](https://forums.swift.org/t/adding-platform-specific-dependency-to-multi-platform-swift-package/49645/8 "2021-07-29T19:53:45Z")

</div>

Encountered the same issue while trying to list binary target conditionally as a dependency.

---

<div class="post-metadata">

**Author:** ![rosskimes](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/rosskimes/32/626_2.png) [@rosskimes](https://forums.swift.org/u/rosskimes)\
**Post date:** [October 15, 2021, 1:25pm UTC](https://forums.swift.org/t/adding-platform-specific-dependency-to-multi-platform-swift-package/49645/9 "2021-10-15T13:25:49Z")

</div>

Was a bug ever filed for this issue? I just ran in to it myself.

---

<div class="post-metadata">

**Author:** ![brianmichel](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/brianmichel/32/21244_2.png) [@brianmichel](https://forums.swift.org/u/brianmichel)\
**Post date:** [February 8, 2022, 3:47am UTC](https://forums.swift.org/t/adding-platform-specific-dependency-to-multi-platform-swift-package/49645/10 "2022-02-08T03:47:46Z")

</div>

Hey folks, I filed a bug for this issue [[SR-15836] Platform Conditionals Don't Apply To Binary Frameworks · Issue #4351 · apple/swift-package-manager · GitHub](https://bugs.swift.org/browse/SR-15836) as I recently ran into the same problem. Does anyone have any workarounds?

---

<div class="post-metadata">

**Author:** ![loganblevins](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/loganblevins/32/23250_2.png) [@loganblevins](https://forums.swift.org/u/loganblevins)\
**Post date:** [May 12, 2022, 9:48pm UTC](https://forums.swift.org/t/adding-platform-specific-dependency-to-multi-platform-swift-package/49645/11 "2022-05-12T21:48:32Z")

</div>

Bumping this as well.

Just ran into this exact issue when pulling in a package to a tvOS project with conditional package dependency set to iOS only.

Builds for iOS but does not for tvOS. Binary target with a reference xcframework for iOS only is the root issue as it still tries to be linked even though my package manifest says not to on tvOS.

On Swift 5.5

Any updates???

---

<div class="post-metadata">

**Author:** ![loganblevins](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/loganblevins/32/23250_2.png) [@loganblevins](https://forums.swift.org/u/loganblevins)\
**Post date:** [May 12, 2022, 9:50pm UTC](https://forums.swift.org/t/adding-platform-specific-dependency-to-multi-platform-swift-package/49645/12 "2022-05-12T21:50:46Z")

</div>

The only workaround I can imagine that might work, involves creating a target for iOS and a target for tvOS. Each target might be able to conditionally depend on platform and thus only the respective target gets linked for proper platform.

Again, this is hypothetical but worth a shot. I plan on taking this route to workaround the SPM bug if possible.

---

<div class="post-metadata">

**Author:** ![dmcgloin](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/dmcgloin/32/7717_2.png) [@dmcgloin](https://forums.swift.org/u/dmcgloin)\
**Post date:** [May 21, 2022, 12:55am UTC](https://forums.swift.org/t/adding-platform-specific-dependency-to-multi-platform-swift-package/49645/13 "2022-05-21T00:55:34Z")

</div>

Hey, did this strategy work for you?

---

<div class="post-metadata">

**Author:** ![loganblevins](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/loganblevins/32/23250_2.png) [@loganblevins](https://forums.swift.org/u/loganblevins)\
**Post date:** [May 21, 2022, 1:40am UTC](https://forums.swift.org/t/adding-platform-specific-dependency-to-multi-platform-swift-package/49645/14 "2022-05-21T01:40:46Z")

</div>

Unfortunately no. I ended up resorting to an entirely independent package for tvOS.

---

<div class="post-metadata">

**Author:** ![harshvishu](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/harshvishu/32/23227_2.png) [@harshvishu](https://forums.swift.org/u/harshvishu)\
**Post date:** [September 17, 2022, 3:03am UTC](https://forums.swift.org/t/adding-platform-specific-dependency-to-multi-platform-swift-package/49645/15 "2022-09-17T03:03:31Z")

</div>

And the issue still exists. Does anyone found any solution or workaround?

---

<div class="post-metadata">

**Author:** ![importRyan](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/importryan/32/18176_2.png) [@importRyan](https://forums.swift.org/u/importRyan)\
**Post date:** [October 8, 2022, 5:13am UTC](https://forums.swift.org/t/adding-platform-specific-dependency-to-multi-platform-swift-package/49645/16 "2022-10-08T05:13:41Z")

</div>

The workaround I used this week was:

**Clients package**

- Interface target
- Implementation target (imports the iOS only binary)
- iOS implementations target (@\_exports all single platform binaries)

**Features package**

- Feature target (imports the interface)
- Dependencies target (imports all interfaces, no implementations)

**App**

- Add Clients.iOS Implementations target to Frameworks and Libraries
- Where dependencies are declared: #if os(iOS) import iOS Implementations target

I wish there was another way.

---

<div class="post-metadata">

**Author:** ![loganblevins](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/loganblevins/32/23250_2.png) [@loganblevins](https://forums.swift.org/u/loganblevins)\
**Post date:** [August 1, 2023, 3:36am UTC](https://forums.swift.org/t/adding-platform-specific-dependency-to-multi-platform-swift-package/49645/17 "2023-08-01T03:36:25Z")

</div>

This is the way

---

<div class="post-metadata">

**Author:** ![thejohnlima](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/thejohnlima/32/37755_2.png) [@thejohnlima](https://forums.swift.org/u/thejohnlima)\
**Post date:** [October 15, 2024, 1:27am UTC](https://forums.swift.org/t/adding-platform-specific-dependency-to-multi-platform-swift-package/49645/19 "2024-10-15T01:27:09Z")

</div>

I faced a similar issue today, but my case was using binary target.  
I will post my solution here, since this post helped me to find the solution for my case.

```swift
// swift-tools-version: 5.10

import PackageDescription

let package = Package(
    name: "MyPackage",
    platforms: [
        .iOS(.v16),
        .tvOS(.v16),
    ],
    products: [
        .library(
            name: "MyPackage",
            targets: ["MyPackage"]
        ),
    ],
    targets: [
        .target(
            name: "MyPackage",
            dependencies: [
                .target(name: "MyFramework-iOS", condition: .when(platforms: [.iOS])),
                .target(name: "MyFramework-tvOS", condition: .when(platforms: [.tvOS]))
            ]
        ),
        .binaryTarget(
            name: "MyFramework-iOS",
            url: "https://mywebsetup/binaries/2.222.22/iOS/MyFramework.xcframework.zip",
            checksum: "255gjf6ab5dcc44a7c542344648e14e0f039a3121ab1144d676797d3c094b0bf1"
        ),
        .binaryTarget(
            name: "MyFramework-tvOS",
            url: "https://mywebsetup/binaries/2.222.22/tvOS/MyFramework-tvOS.xcframework.zip",
            checksum: "5aa0fd2cc75c17bcf1c226efe6e3681a1a8f84f4b10af2077c1af587318cc6g0"
        )
    ]
)

```
