Using Custom Metadata for PackageDescription in SPM


Is there a way to import a custom framework/library in Package.swift manifest file? i.e

import PackageDescription
import CustomPackageSupport

We are using SPM for modularization of a large existing project and it is expected to be broken into several swift packages containing dozens of products & targets so instead of create modules manually CustomPackageSupport will help us create targets/products for each package and we will avoid lot of boilerplate code (and manual typing of urls, names etc..)

Is there a way to import a non-standard framework/library in Package.swift manifest like that?

The manifest only knows about the standard and core libraries from the toolchain; attempting to import anything else will fail.

What you can do instead is enclose part of the manifest with // ↓ This section is automated and // ↑ This section is automated. or some other unique token. Then you can write a tool (or eventually a command plugin) that locates that section and replaces its contents with whatever its algorithm determines should be there.

I used this strategy for a long while to automatically trim manifests for Windows back when the Windows platform support was incomplete and its feature set was still constrained.

Just wanted to echo what @SDGGiesbrecht mentioned.

A great SDK for Swift metaprogramming is Sourcery.

While I agree with others that code generation is probably the more fruitful route here, this should be possible in principle, at least as long as you are using swift build and not Xcode.

There's the -Xmanifest parameter which allows passing additional flags to the compilation of the manifest and that should be usable for passing search paths to a library/framework that you have previously built.

A more declarative approach for something like this could also eventually arrives via new plugin types.

1 Like

Plugin is an interesting idea which I haven't explore yet.. but just to be sure you are referring to plugin library type?

Also, ours is an Xcode project which references to independent swift packages.

Thanks for your input. This is interesting idea to explore. I will definitely look into this. Do you know of any reference open source project that does something like this?

Sorry if I wasn't clear enough, this could be interesting design space for plugins at some point in the future, there's no way to utilize plugins here today.

1 Like