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

1.4k Views Asked by At

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://[email protected]/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:

enter image description here

0

There are 0 best solutions below