Swift Package Manager: two similar targets different only with the dependencies - ios

I'm migrating a library built and tested as an Xcode Project to Swift Package Manager. The project contains 3 targets: the library itself, the same library but with different dependency and the test (application) target.
So far, porting the library was easy.
Now I'm interested in building the same library with a different dependency (mocks) which could be tested.
In practice, the Package.swift file looks like this:
// swift-tools-version:5.4
// The swift-tools-version declares the minimum version of Swift required to build this package.
import PackageDescription
let package = Package(
name: "TargetLibrary",
platforms: [
.iOS(.v14), .macOS(.v10_15)
],
products: [
.library(
name: "TargetLibrary",
targets: ["TargetLibrary"]
),
],
dependencies: [
.package(
url: "ssh://git#github.com/DependentLib",
.upToNextMinor(from: "1.3.18")
),
],
targets: [
.target(
name: "TargetLibrary",
dependencies: [
// Using the "main" dependency for the "TargetLibrary"
.product(name: "DependentLib", package: "DependentLib"),
],
path: "src",
sources: [
"sourcefiles"
],
publicHeadersPath: "SwiftPackage/include",
cxxSettings: [
.headerSearchPath("lib"),
]
),
// This target is virtually identical, to the previous one,
// the only difference is that it is using a different dependency library
// from the same package
.target(
name: "TargetLibraryWithMock",
dependencies: [
// Using the "mock" dependency for this library
.product(name: "DependentLibMock", package: "DependentLib"),
],
path: "src",
sources: [
"sourcefiles"
],
publicHeadersPath: "SwiftPackage/include",
cxxSettings: [
.headerSearchPath("lib"),
]
),
// Now testing the "TargetLibraryWithMock"
.testTarget()....
],
cxxLanguageStandard: .cxx14
)
Now, of course, if I just duplicate two targets, I'll get a target overlapping sources error
So, the question is:
How can I build two targets different only by the dependencies they include?
It looks like target dependency condition could have helped here
dependencies: [
.target(name: "DependentLib", condition: .when(configuration: .build)),
.target(name: "DependentLibMock", condition: .when(configuration: .test)),
]
but only platform-specific conditionals exist now. What could be solutions to this problem?
Graphical representation of the desired result:

Related

Is it possible to have conditional dependencies in a Package.swift target depending on the platform?

I am trying to build a Swift package, where one of the targets must depend on a different version of a library depending on whether it is being built for iOS or MacOS.
For iOS, the dependency is packaged as an .xcframework, whereas for MacOS it is packaged as a system library.
So my dependencies package looks like this:
// swift-tools-version:5.6
import PackageDescription
let package = Package(
name: "LibFoo",
products: [
.library(name: "LibFooFramework", targets: ["LibFooFramework"]),
.library(name: "ClibFoo", targets: ["ClibFoo"])
],
targets: [
.binaryTarget(
name: "LibFooFramework",
path: "path/to/LibFooFramework.xcframework"
),
.systemLibrary(
name: "ClibFoo",
pkgConfig: "libFoo"
)
]
)
So then in my client package, I want to use one of these dependencies depending on the target platform.
I have tried this:
// swift-tools-version:5.6
import PackageDescription
#if os(iOS)
let conditionalTarget: Target = .target(
name: "MyLib",
dependencies: [
.product(name: "LibFooFramework", package: "LibFooFramework")
]
)
#else
let CVulkanUtilsTarget: Target = .target(
name: "MyLib",
dependencies: [
.product(name: "ClibFoo", package: "LibFoo")
]
)
#endif
let package = Package(
name: "MyLib",
products: [
.library(name: "MyLib", targets: ["MyLib"]),
],
dependencies: [
.package(url: "https://github.com/spencerkohan/LibFoo", branch:"master")
],
targets: [
.library(
name: "LibFooFramework",
path: "path/to/LibFooFramework.xcframework"
),
]
)
But it seems that the package manager always takes the else branch, even if the package is being built for iOS. I guess this is because the os target of the Package.swift will always be the build environment and not the target.
But is there any way to achieve this type of conditional dependency?

Swift Package Resolution failed because multiple targets named a same framework

I have 2 local Swift Packages: LibA and LibB.
Both LibA and LibB depends on a same framework(AmazonIVSPlayer).
I want to add both of the into my Project but I got the below error:
Multiple targets named 'AmazonIVSPlayer' in LibA and LibB
the Package.swift of both libraries are like below:
import PackageDescription
let package = Package(
name: "LibA",
products: [
// Products define the executables and libraries a package produces, and make them visible to other packages.
.library(
name: "LibA",
targets: ["LibA","AmazonIVSPlayern"]),
],
dependencies: [
// Dependencies declare other packages that this package depends on.
// .package(url: /* package url */, from: "1.0.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: "LibA",
dependencies: []),
.binaryTarget(
name: "AmazonIVSPlayer",
url: "https://player.live-video.net/1.8.1/AmazonIVSPlayer.xcframework.zip",
checksum: "8256f9f580fdb09b156afad43cd17dd120091c794e848b27aad83c1a098ecc7f")
]
)
I read
Swift Package Manager: "multiple targets named..."
Swift Package Manager (SPM) and Cocoapod Dependency Conflict
Swift packages and conflicting dependencies
https://forums.swift.org/t/multiple-target-issue-with-spm/16696
https://www.reddit.com/r/swift/comments/d4wwbk/question_about_dependency_conflicts_in_swift
Since none of them offer any solution and all posts are old, I am wondering is there any new way to solve this issue?
I am also searching for a solution for this problem. The only way I found to work around this, is to create a git repository, put the xcframework (in your case AmazonIVSPlayer) there and a Package.swift. The Package.swift in the newly created repo would look like that:
let package = Package(
name: "AmazonIVSPlayer",
products: [
.library(
name: "AmazonIVSPlayer",
targets: ["AmazonIVSPlayer"]
)
],
targets: [
.binaryTarget(name: "AmazonIVSPlayer", path: "AmazonIVSPlayer.xcframework")
]
)
Then remove the binaryTarget from LibA and LibB and add the following to the dependencies:
.package(url: "https://github.com/foo/bar.git", from: "1.8.1")
Don't forget to create a tag for the version number.
Then add the following to the target dependencies:
.product(name: "AmazonIVSPlayer", package: "bar"),
That way you can use AmazonIVSPlayer in LibA and LibB and can add both Libs to the same project without getting an error. Not the easiest solution, but the only one which works for me.
The error you are getting indicating that you are using two products that have module that conflicts with another products module name. The solution you could try:
If the module causing the name conflict are same (contain the same code and files), then you can create a separate package for the module, in this case a separate package for AmazonIVSPlayer could be created:
import PackageDescription
let package = Package(
name: "AmazonIVSPlayer",
products: [
.library(
name: "AmazonIVSPlayer",
targets: ["AmazonIVSPlayern"]),
],
dependencies: [],
targets: [
.binaryTarget(
name: "AmazonIVSPlayer",
url: "https://player.live-video.net/1.8.1/AmazonIVSPlayer.xcframework.zip",
checksum: "8256f9f580fdb09b156afad43cd17dd120091c794e848b27aad83c1a098ecc7f")
]
)
Alternatively, you can combine the package manifest of LibA and LibB into a single package and expose both of them as multiple products:
import PackageDescription
let package = Package(
name: "LibAB",
products: [
.library(
name: "LibA",
targets: ["LibA","AmazonIVSPlayern"]),
.library(
name: "LibB",
targets: ["LibB","AmazonIVSPlayern"]),
],
dependencies: [],
targets: [
.target(
name: "LibA",
dependencies: []),
.target(
name: "LibB",
dependencies: []),
.binaryTarget(
name: "AmazonIVSPlayer",
url: "https://player.live-video.net/1.8.1/AmazonIVSPlayer.xcframework.zip",
checksum: "8256f9f580fdb09b156afad43cd17dd120091c794e848b27aad83c1a098ecc7f")
]
)
Now for the scenarios where both modules are actually different, with swift 5.7 you will be able to differentiate them with module aliases. So with your current package description, when consuming:
targets: [
.executableTarget(
name: "App",
dependencies: [
.product(name: "LibA", package: "LibA"),
.product(name: "LibB", package: "LibB", moduleAliases: ["AmazonIVSPlayer": "AmazonIVSPlayerFromLibB"]),
])
]
When importing module you have to provide the alias name that you have specified:
import AmazonIVSPlayer // imports from LibA
import AmazonIVSPlayerFromLibB // imports from LibB
I've just edited project.pbxproj, and find the target that has the framework added with SPM inside packageProductDependencies list, and I've copied that line to the target that's missing that.
Something like:
name = "Target 1";
38D300792549B0C600FDEFA8 /* UIKit Extensions */,
Copied that line of the framework in the other target
name = "Target 2";
38D300792549B0C600FDEFA8 /* UIKit Extensions */,

Firebase [Swift Package Manager]: no such module FirebaseRemoteConfig

After I read the instructions listed here for how to add Firebase as a dependency to a Swift package, I couldn't get it to work, here's my Package.swift manifest:
import PackageDescription
let package = Package(
name: "MyPackage",
products: [
// Products define the executables and libraries a package produces, and make them visible to other packages.
.library(
name: "MyPackage",
targets: ["MyPackage"]),
],
dependencies: [
// Dependencies declare other packages that this package depends on.
.package(name: "Firebase",
url: "https://github.com/firebase/firebase-ios-sdk.git",
.upToNextMajor(from: "8.0.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: "MyPackage",
dependencies: [.product(name: "FirebaseRemoteConfig", package: "Firebase")]),
.testTarget(
name: "MyPackageTests",
dependencies: ["MyPackage"]),
]
)
the package graph resolves without problems, but when I try to add my own code in Sources/MyPackage.swift starting with import FirebaseRemoteConfig the compiler complains with:
No such model 'FirebaseRemoteConfig'.
what's went wrong with my setup ?
I figured out the solution, but unfortunately it's not documented on Firebase Docs, I have to add the .platforms array in the Package.swift manifest specifying a version that supports FirebaseRemoteConfig, for example:
platforms: [
.iOS(.v13)
]

Import local SPM Library in local SPM package

I have a local SPM package that contains 2 libraries, and I want to import one of those libraries in another local SPM package :
File containing the libraries:
let package = Package(
name: "LocationService",
platforms: [.iOS(.v13)],
products: [
.library(
name: "LocationService",
type: .dynamic,
targets: ["LocationService"]),
.library(
name: "LocationLiveClient",
type: .dynamic,
targets: ["LocationLiveClient"]),
],
targets: [
.target(
name: "LocationService",
dependencies: []),
.target(
name: "LocationLiveClient",
dependencies: ["LocationService"],
path: "Sources/LocationLiveClient"),
]
)
File importing the libraries:
let package = Package(
name: "HomePage",
products: [
.library(
name: "HomePage",
type: .dynamic,
targets: ["HomePage"])
],
dependencies: [
.package(path: "../RouterService"),
.package(path: "../LocationService/Sources/LocationLiveClient"),
],
targets: [
.target(
name: "HomePage",
dependencies: ["RouterService", "LocationLiveClient"])
]
)
There are a couple of issues to resolve here.
(If a requirement of your design is to use 'dynamic' linking, then this approach may not work for you.)
type: .dynamic:
Unless you absolutely need to guarantee how library linking is achieved, it is recommended that you leave this as the default value of nil (just remove the line). This allows the swift package manager to determine how to best link the libraries (the default being 'static').
.package(path: "../LocationService/Sources/LocationLiveClient"),
LocationLiveClient is a product & target of the LocationService package. In the dependencies here, a reference to the package as a whole should be made. So change this to .package(path: "../LocationService"),
dependencies: ["RouterService", "LocationLiveClient"])
Once the change to depend on the whole location service package, the compiler needs a little extra information. You can update your target dependencies to specifically use the LocationLiveClient library in the LocationService package: .product(name: "LocationLiveClient", package: "LocationService").
With those changes in place you end up with a Package definition like this:
let package = Package(
name: "HomePage",
products: [
.library(
name: "HomePage",
targets: ["HomePage"]),
],
dependencies: [
.package(path: "../RouterService"),
.package(path: "../LocationService"),
],
targets: [
.target(
name: "HomePage",
dependencies: [
"RouterService",
.product(name: "LocationLiveClient", package: "LocationService")
]
),
]
)
You should then be able to import LocationLiveClient as expected.
Side note: Assuming your 'LocationService' package has the following folder structure, then you can safely remove path: "Sources/LocationLiveClient" from your LocationLiveClient target definition.
LocationService
-> Sources
-> LocationService
-> LocationLiveClient

How do I declare the Siesta Swift package as a dependency of another Swift package?

I'm trying to use the Swift package Siesta as a dependency for the package I'm building and reference it in my package code. I've identified how to import the package into my project in my Package.swift file which is simple enough:
dependencies: [
.package(url: "https://github.com/bustoutsolutions/siesta", from: "1.5.1")
],
This causes the package to be copied into my package just fine. The problem I'm having is actually linking it up to my package so I can import it and reference it in code. I know I need to actually link it up to my target
I've read some other package files and because the package name for Siesta is like this
let package = Package(
name: "Siesta",
And the products it declares are like this
products: [
.library(name: "Siesta", targets: ["Siesta"]),
.library(name: "SiestaUI", targets: ["SiestaUI"]),
.library(name: "Siesta_Alamofire", targets: ["Siesta_Alamofire"]),
],
I should be able to just do this in my package file's target to use it
.target(
name: "MyTarget",
dependencies: [.product(name: "Siesta", package: "Siesta")]),
But when I try to build my package, I get an error:
/Users/blahblah/Desktop/MyPackage/Package.swift: unknown package 'Siesta' in dependencies of target 'MyTarget'
And not only that, all the targets for my single run scheme on my package go missing and I can't build again without discarding all my local version control changes. What's going on here?
With Swift tools version 5.2 you have to provide a name argument when declaring your package dependency.
.package(name: "Siesta", url: "https://github.com/bustoutsolutions/siesta", from: "1.5.1")
A working example of a Package.swift file:
// swift-tools-version:5.2
// The swift-tools-version declares the minimum version of Swift required to build this package.
import PackageDescription
let package = Package(
name: "MyPackage",
products: [
.library(
name: "MyLibrary",
targets: ["MyTarget"]),
],
dependencies: [
// make sure to provide a `name` argument here
.package(name: "Siesta", url: "https://github.com/bustoutsolutions/siesta", from: "1.5.1")
],
targets: [
.target(
name: "MyTarget",
dependencies: [
.product(name: "Siesta", package: "Siesta")
]),
]
)
Source: https://forums.swift.org/t/package-names-in-swift-5-2/34886/6

Resources