Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support dynamic linking on Linux via Artifactbundles #8249

Open
wants to merge 16 commits into
base: main
Choose a base branch
from

Conversation

tayloraswift
Copy link
Member

@tayloraswift tayloraswift commented Jan 26, 2025

proof-of-concept for enabling self-managed dynamic library dependencies on Linux!

Motivation:

see Forums discussion, #5714

Modifications:

  • adds an ArtifactType called “library”, which can be used in info.json inside an Artifactbundle. variant/triple checking is not implemented yet, and we expect this to be very limited and geared towards self-managed deployment use cases, as we intentionally do not want to design a compatibility checking system that covers all possible host–target combinations of “Linux”.
{
    "schemaVersion": "1.0",
    "artifacts": {
        "MyLibrary": {
            "type": "library",
            "version": "0.0.0",
            "variants": [{ "path": "MyLibrary" }]
        }
    }
}
  • adds logic to Sources/Build/BuildPlan/BuildPlan+Swift.swift that injects the necessary .swiftmodule search paths to the swiftc build command.

  • adds logic to Sources/Build/BuildPlan/BuildPlan+Product.swift that populates the necessary linker flags to compile products that include dependencies on non-XCFramework binary targets

  • does a small amount of refactoring around the additions i made, in an effort to leave the code base in a slightly better shape than i found it in

Result:

SwiftPM can now dynamically link binary library dependencies into executables on Linux, although the executables of course will require the shared libraries to be installed in the expected locations to actually run.

sample manifest:

// swift-tools-version:6.0
import PackageDescription

let package = Package(
    name: "DynamicLinkingTest",
    products: [
        // .library(name: "MyLibrary", type: .dynamic, targets: ["MyLibrary"]),
        .executable(name: "Client", targets: ["Client"]),
    ],
    dependencies: [
    ],
    targets: [
        .binaryTarget(
            name: "MyLibrary",
            path: "test.artifactbundle"),

        .executableTarget(name: "Client",
            dependencies: [
                .target(name: "MyLibrary"),
            ]),

        // .target(name: "MyLibrary",
        //     swiftSettings: [
        //         .unsafeFlags(["-enable-library-evolution"]),
        //     ]),
    ]
)

sample Artifactbundle

- test.artifactbundle
    - MyLibrary
        - libMyLibrary.so
        - MyLibrary.swiftmodule
    - info.json

@tayloraswift tayloraswift force-pushed the dynamic-libraries-for-linux branch from 14dd2a6 to d01f6ba Compare January 27, 2025 22:22
@tayloraswift tayloraswift marked this pull request as ready for review February 1, 2025 04:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant