Framework with multiple targets and shared sources

1.8k views Asked by At

I have a framework with two targets (A and B) with the following file structure:

FooFramework
|__ A
     |__ Foo.swift
     |__ Bar.swift
     |__ Foobar.swift
|__ B
     |__ Barfoo.swift

B basically has a subset of A's sources (where B is set as a target), e.g. B only uses Foo.swift and Bar.swift, but not Foobar.swift.

Now I have an app that needs both framework A and B (in separate targets targets).

At this point I'm unsure how to setup the Package.swift file for this setup to work. Whenever I open the Package.swift in Xcode and try to build framework B, the compiler is complaining about missing types in Barfoo.swift, because they are included in Bar.swift.

This is how I've setup the Package.swift file:

// swift-tools-version:5.3

import PackageDescription

let package = Package(
    name: "Framework",
    platforms: [
        .iOS(.v11),
        .macOS(.v10_15)
    ],
    products: [
        .library(
            name: "A",
            targets: ["A"]),
        .library(
            name: "B",
            targets: ["B"]),
    ],
    dependencies: [
        .package(name: "Dependency", url: "https://github.com/...", .exact("1.0.0"))
    ],
    targets: [
        .target(
            name: "A",
            dependencies: [
                "Dependency",
            ],
            path: "A",
            exclude: [
                "Info.plist",
            ],
            resources: [
                .process("fonts"),
            ]
        ),
        .target(
            name: "B",
            dependencies: ["A"],
            path: "B",
            exclude: [
                "Info.plist",
            ],
            resources: [
                .process("../A/fonts"),
            ]),
        .testTarget(
            name: "ATests",
            dependencies: ["A"],
            path: "ATests",
            exclude: [
                "Info.plist"
            ]
        )
    ]
)

Related sources, but unfortunately unhelpful:

0

There are 0 answers