Skip to content

Instantly share code, notes, and snippets.

@liamnichols
Last active April 6, 2022 12:26
Show Gist options
  • Save liamnichols/92f8fdcf2864d0fd1619a18828acafb8 to your computer and use it in GitHub Desktop.
Save liamnichols/92f8fdcf2864d0fd1619a18828acafb8 to your computer and use it in GitHub Desktop.

StaticSwiftSyntaxParser

A small wrapper package that helps to replace SwiftSyntax's dependency on lib_internalSwiftSyntaxParser.dylib with StaticInternalSwiftSyntaxParser.

Why?

When deploying command line tools such as periphery, to keep them as portable as possible, it is much more appropriate to statically link any library dependencies, something that swift-syntax does not currently do. By statically linking the lib_internalSwiftSyntaxParser dependency, a command line tool's installation becomes much simpler since there is no longer a need to ensure that dynamic libraries are copied into the location that ws specified by the r_path.

Usage

To use this package, include it as a dependency alongside SwiftSyntax and proceed to importing SwiftSyntaxParser in your code like normal:

let package = Package(
    name: "Periphery",
    products: [
        .executable(name: "periphery", targets: ["Frontend"])
    ],
    dependencies: [
        .package(name: "SwiftSyntax", url: "https://github.com/apple/swift-syntax", .exact("0.50600.1")),
        .package(name: "StaticSwiftSyntaxParser", url: "https://github.com/peripheryapp/static-swift-syntax-parser.git", .exact("5.6"))
    ],
    targets: [
        .target(
            name: "Frontend",
            dependencies: [
                .product(name: "SwiftSyntax", package: "SwiftSyntax"),
                .product(name: "StaticSwiftSyntaxParser", package: "StaticSwiftSyntaxParser")
            ]
        )
    ]
)

While you don't have to do anything with StaticSwiftSyntaxParser in your code, the presence of the dependency will link StaticInternalSwiftSyntaxParser and ensure that the now redundant dylib is stripped.

@_exported import SwiftSyntaxParser
// swift-tools-version:5.3
// The swift-tools-version declares the minimum version of Swift required to build this package.
import PackageDescription
let package = Package(
name: "StaticSwiftSyntaxParser",
products: [
.library(name: "StaticSwiftSyntaxParser", type: .static, targets: ["StaticSwiftSyntaxParser"])
],
dependencies: [
.package(name: "SwiftSyntax", url: "https://github.com/apple/swift-syntax", .exact("0.50600.1"))
],
targets: [
.target(
name: "StaticSwiftSyntaxParser",
dependencies: [
.product(name: "SwiftSyntaxParser", package: "SwiftSyntax"),
.target(name: "lib_InternalSwiftSyntaxParser"),
],
path: ".",
exclude: ["README.md"],
sources: ["export.swift"],
linkerSettings: [
.unsafeFlags(["-Xlinker", "-dead_strip_dylibs"])
]
),
.binaryTarget(
name: "lib_InternalSwiftSyntaxParser",
url: "https://github.com/keith/StaticInternalSwiftSyntaxParser/releases/download/5.6/lib_InternalSwiftSyntaxParser.xcframework.zip",
checksum: "88d748f76ec45880a8250438bd68e5d6ba716c8042f520998a438db87083ae9d"
)
]
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment