Why Swift Package Manager does not support weak linking (-weak_framework SwiftUI)

Currently in SwiftPM with Swift5.1, it has only these methods in linker flags control:

public struct LinkerSetting : Encodable {
    public static func linkedLibrary(_ library: String, _ condition: PackageDescription.BuildSettingCondition? = nil) -> PackageDescription.LinkerSetting

    public static func linkedFramework(_ framework: String, _ condition: PackageDescription.BuildSettingCondition? = nil) -> PackageDescription.LinkerSetting

    public static func unsafeFlags(_ flags: [String], _ condition: PackageDescription.BuildSettingCondition? = nil) -> PackageDescription.LinkerSetting
}

None of these 3 options is useful for real world App use case.

The unsafeFlags can not be used in a framework, because Xcode will complain that:

The package product 'SDWebImageSwiftUI' cannot be used as a dependency of this target because it uses unsafe build flags.
3 Likes

You don't need to explicitly weak link system frameworks like SwiftUI. If your package has a deployment target lower than macOS 10.15 / iOS 13.0 / tvOS 13.0 / watchOS 6.0, then SwiftUI will automatically be weak linked.

If you find some configuration where this does NOT occur, please file a bug report and attach a sample project which reproduces the issue if possible.

The problem is not only SwiftUI. The problem exist on all the cases, like Combine, like LinkPresentation, like RealityKit.

I’ve already says, using the min deployment target version have Side-Effect. which is not a framework author want and have to avoid. I don’t think mixing these two things (weak linking and OS compatibility), for a Package Manager, is a good design

1 Like

And currently in Xcode 11.2.1, Yes it’s s Bug that SwiftUI is automatically weak linking if the min deployment target is iOS 12. But Combine framework is not.

If you only change that, you will still get a dyld crash when startup because Combine symbol is not loaded on iOS 12. You have a manually specify -weak_framework Combine in Other Linker Flags.

I think I’ve already fired Radars, if not, I can fire again. Another anooying problem on Xcode is that the Linked Libaraies and Framework does not showing Combine, which menas I can not add Combine and choose thr framework into Optional(this is the same effect as using -weak_framework Combine). Not a big point but just annoying if some new developers don’t know what a linker do.

2 Likes