Hi Swift Community,
I'm working on a program or script that can analyze a Swift Package Manager (SPM) dependency graph and return all the system libraries declared in .systemLibrary
targets across the entire graph.
For example, given a Package.swift
like this:
import PackageDescription
let package = Package(
name: "libNFCTest",
dependencies: [
// Dependencies declare other packages that this package depends on.
],
targets: [
.systemLibrary(
name: "libnfc",
pkgConfig: "libnfc",
providers: [
.apt(["libnfc-dev", "libopenssl", "libsqlite-dev"]),
.brew(["libnfc", "libopenssl", "libsqlite-dev"])
]
),
.target(
name: "libNFCTest",
dependencies: ["libnfc"]
),
.testTarget(
name: "libNFCTestTests",
dependencies: ["libNFCTest"]
),
]
)
I want to generate a structure like this:
[
"libnfc":
[
"apt": ["libnfc-dev", "libopenssl", "libsqlite-dev"],
"brew": ["libnfc", "libopenssl", "libsqlite-dev"]
]
"libfoo":
[
"apt": ["libbar", "liboo", "libsdl"]
]
]
This map should include system libraries from all packages in the dependency graph, not just the current package. Each provider (apt
, brew
, choco
, etc.) would serve as the key, and its corresponding value would be an array of system library or package names.
Goal:
I aim to use this information to create a program that can:
- Prompt the user during deployment to install missing system libraries or system packages.
- Or simply just informationally listing all the system libraries and their respective providers
- Handle the dependency graph holistically, extracting and consolidating all system libraries across all dependencies.
Question:
- How can I effectively traverse the SPM dependency graph to collect all
.systemLibrary
targets across all packages? - Is there a recommended way or existing tooling to parse and extract the
providers
metadata programmatically? - Any pitfalls or best practices to be aware of when implementing such a program?
Unfortunately, I've tried:
swift package show-dependencies --format json
and looking at
Package.resolved
However, it doesn't list out systemLibrary
values.
I'm open to ideas, suggestions, or even pointers to similar implementations! Your insights would be greatly appreciated.
Thanks in advance!