Swift Package Manager Logo

Implementing Swift Package Manager –Example 6

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

Three Layers Of Packages, With A One-Level-Down Dynamic Framework

In this example, we include a package, represented as a static library, that embeds a dylib, that embeds another static library (“dylib sandwich”).

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 6.0.0. You can check out that version, and reproduce the results here.

Here is the source code for the Package_D.swift file, Version 2.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: 2.0.0\n" + Package_C(indent: inIndent + 1).text + "\n" + Package_A(indent: inIndent + 1).text
    }
}

Here is the source for the Package_D, Version 2.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", 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"
            ]
        )
    ]
)

Note that it is almost the same as the Version 1.0.0 file, except that now, there is no “type: .dynamic” in the product list.

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 6.0.0:

import Foundation
import Package_D

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

When we run that, we get something like this:

PackageConsumer, Version 6.0.0
	Package_D, Version: 2.0.0
		Package_C, Version: 2.0.0
			Package_A, Version: 2.0.0
		Package_A, Version: 2.0.0

Again, we need to disable Hardened Runtime, in order to run this in Xcode. Building via swift build works.