Swift Package Manager Logo

Implementing Swift Package Manager –Example 5

This entry is part 11 of 14 in the series Swift Package Manager

Three Layers Of Packages, With Dynamic Frameworks

In this example, we include a package, represented as a dylib, that embeds another dylib, that embeds a static library.

This has the same issue as Example 3, where dynamic frameworks can present challenges, when using the Hardened Runtime.

Here is the GitHub link to the PackageConsumer project Version 5.0.0. You can check out that version, and reproduce the results here.

Fig. 1: The Hierarchy

Here is the source code for the Package_D.swift file, Version 1.0.0:

import Foundation
import Package_A
import Package_C

public struct Package_D: PackageProtocol {
    public let indent: Int
    public let text: String
    public init(indent inIndent: Int = 0) {
        indent = inIndent
        let prefix = String(repeating: "\t", count: inIndent)
        text =  "\(prefix)Package_D, Version: 1.0.0\n" + Package_C(indent: inIndent + 1).text + "\n" + Package_A(indent: inIndent + 1).text
    }
}

Note that we also include (and print) Package_A. This is merely to demonstrate that we have the ability to access modules that are dependencies of dependencies.

Here is the source for the Package_D, Version 1.0.0 Package.swift file:

import PackageDescription

let package = Package(
    name: "Package_D",
    platforms: [
        .iOS(.v11),
        .tvOS(.v11),
        .macOS(.v10_14),
        .watchOS(.v5)
    ],
    products: [
        .library(name: "Package-D", type: .dynamic, targets: ["Package_D"])
    ],
    dependencies: [
        .package(name: "Package_C", url: "git@github.com:LittleGreenViper/SMPArticle-Package_C.git", from: "2.0.0")
    ],
    targets: [
        .target(
            name: "Package_D",
            dependencies: [
                .product(name: "Package-C", package: "Package_C")
            ]
        ),
        .testTarget(
            name: "Package_DTests",
            dependencies: [
                "Package_D"
            ]
        )
    ]
)

Here is the source code for the Package_C.swift file, Version 2.0.0:

import Foundation
import Package_A

public struct Package_C: PackageProtocol {
    public let indent: Int
    public let text: String
    public init(indent inIndent: Int = 0) {
        indent = inIndent
        let prefix = String(repeating: "\t", count: inIndent)
        text =  "\(prefix)Package_C, Version: 2.0.0\n" + Package_A(indent: inIndent + 1).text
    }
}

Here is the source code for Package_A, version 2.0.0:

import Foundation

public protocol PackageProtocol {
    var text: String { get }
    var indent: Int { get }
}

public struct Package_A: PackageProtocol {
    public let indent: Int
    public let text: String
    public init(indent inIndent: Int = 0) {
        indent = inIndent
        let prefix = String(repeating: "\t", count: inIndent)
        text =  "\(prefix)Package_A, Version: 2.0.0"
    }
}

Here is the source for PackageConsumer.swift, Version 5.0.0:

import Foundation
import Package_D

struct PackageConsumer {
    let text: String
    init(text intext: String = "PackageConsumer, Version 5.0.0") {
        text = intext + "\n" + Package_D(indent: 1).text
    }
}

Running that, gives us something like this:

PackageConsumer, Version 5.0.0
	Package_D, Version: 1.0.0
		Package_C, Version: 2.0.0
			Package_A, Version: 2.0.0
		Package_A, Version: 2.0.0

Remember that we need to turn Hardened Runtime off, in order to run it in Xcode.