Consume Swift Package for Multiple Targets and Platforms in a Project

How can you make the target products platform specific in swift package manager

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",
....

Add dependencies to binary targets in Swift Package Manager

I think the real issue here is that the dependencies don't need to be a part of your modules's public interface. You would need to replace all instances of import for the dependencies in your code to @_implementationOnly import

E.g.

@_implementationOnly import CocoaLumberjack

You can read more about @_implementationOnly here

Sharing code across test targets when using the Swift Package Manager

I faced the same issue and solved it by defining a Target (not Test Target) instead for Common.

My Package.swift file:

// ...
.target(name: "Common", dependencies: ["App"], path: "Tests/Common"),
.testTarget(name: "AppTests", dependencies: ["App", "Common"]),
.testTarget(name: "IntegrationTests", dependencies: ["App", "Common"])
// ...


Related Topics



Leave a reply



Submit