Lyrebird 2.0.0 — Linux support
Consolidated release: the cross-platform Linux port (engine, generators, CLI, REPL, NRT, portable DSP), live ALSA audio + ALSA MIDI + POSIX OSC + portable WAV codec, the GTK4 GUI (LyrebirdUI on the SwiftOpenUI backend), and the Linux live-coding scratchpad (LiveCompile). Apple platforms unchanged; all additive. See RELEASES.md and LINUX_README.md. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
@@ -4,7 +4,7 @@
|
||||
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
#if os(macOS)
|
||||
#if os(macOS) || os(Linux)
|
||||
|
||||
/// Errors raised while staging compile artifacts into Application Support.
|
||||
enum ArtifactStagingError: Error {
|
||||
|
||||
@@ -14,7 +14,7 @@
|
||||
|
||||
import Foundation
|
||||
|
||||
#if os(macOS)
|
||||
#if os(macOS) || os(Linux)
|
||||
|
||||
/// Handle to one successfully compiled and loaded snippet.
|
||||
///
|
||||
|
||||
@@ -13,7 +13,7 @@
|
||||
// The editor (`LyrebirdScratchView`) renders diagnostics in its
|
||||
// margin gutter using `(line, column)` directly.
|
||||
|
||||
#if os(macOS)
|
||||
#if os(macOS) || os(Linux)
|
||||
|
||||
public struct Diagnostic: Equatable, Sendable {
|
||||
/// 1-based line number in the **user's** snippet body. May be `nil` when the
|
||||
|
||||
@@ -22,7 +22,7 @@
|
||||
|
||||
import Foundation
|
||||
|
||||
#if os(macOS)
|
||||
#if os(macOS) || os(Linux)
|
||||
|
||||
public enum DiagnosticParser {
|
||||
|
||||
|
||||
@@ -8,7 +8,7 @@
|
||||
//
|
||||
// Severity classifier for a parsed swiftc diagnostic line.
|
||||
|
||||
#if os(macOS)
|
||||
#if os(macOS) || os(Linux)
|
||||
|
||||
public enum DiagnosticSeverity: String, Equatable, Sendable {
|
||||
case error
|
||||
|
||||
@@ -12,7 +12,7 @@
|
||||
// (line, column); build errors carry no span → `line: nil` with the message
|
||||
// alone.
|
||||
|
||||
#if os(macOS)
|
||||
#if os(macOS) || os(Linux)
|
||||
import LyrebirdGraphDSLEmbedded
|
||||
|
||||
/// Maps interpreted-graph-DSL errors to editor diagnostics.
|
||||
|
||||
@@ -6,9 +6,9 @@
|
||||
|
||||
// LyrebirdLiveCompile
|
||||
//
|
||||
// One parsed code-completion candidate from `sourcekit-lsp`. macOS only.
|
||||
// One parsed code-completion candidate from `sourcekit-lsp`. macOS/Linux only.
|
||||
|
||||
#if os(macOS)
|
||||
#if os(macOS) || os(Linux)
|
||||
|
||||
/// One code-completion candidate returned by sourcekit-lsp.
|
||||
public struct LSPCompletionItem: Sendable, Equatable {
|
||||
|
||||
@@ -12,7 +12,7 @@
|
||||
|
||||
import Foundation
|
||||
|
||||
#if os(macOS)
|
||||
#if os(macOS) || os(Linux)
|
||||
|
||||
public enum LiveCompileError: Error, CustomStringConvertible, Equatable, Sendable {
|
||||
/// `xcrun -f swiftc` and the fallback paths all failed.
|
||||
|
||||
@@ -9,14 +9,16 @@
|
||||
// `LiveCompiler.compile(source:)` shells out to `swiftc -emit-library`, then
|
||||
// `dlopen`s the resulting dylib and resolves the snippet entry symbol. No UI,
|
||||
// no `SynthesisEngine` binding (those are handled by the harness and session
|
||||
// layers). macOS only — iOS cannot exec swiftc.
|
||||
// layers). macOS/Linux only — iOS cannot exec swiftc.
|
||||
|
||||
import Foundation
|
||||
#if os(macOS)
|
||||
import Darwin
|
||||
#if canImport(Darwin)
|
||||
import Darwin // dlopen / dlsym / dlerror
|
||||
#elseif canImport(Glibc)
|
||||
import Glibc // dlopen / dlsym / dlerror on Linux
|
||||
#endif
|
||||
|
||||
#if os(macOS)
|
||||
#if os(macOS) || os(Linux)
|
||||
|
||||
/// Symbol the snippet preamble must export. Snippets declare:
|
||||
///
|
||||
@@ -26,10 +28,37 @@ import Darwin
|
||||
/// ```
|
||||
public let lyrebirdSnippetEntrySymbol: String = "lyrebird_snippet_entry"
|
||||
|
||||
/// Platform shared-library extension: `dylib` (Mach-O) on macOS, `so` (ELF) on
|
||||
/// Linux. A snippet is `swiftc -emit-library`'d to `lib<name>.<ext>`, and the
|
||||
/// resident Lyrebird library is `libLyrebirdDynamic.<ext>`.
|
||||
#if os(macOS)
|
||||
let livecompileSharedLibraryExtension: String = "dylib"
|
||||
#else
|
||||
let livecompileSharedLibraryExtension: String = "so"
|
||||
#endif
|
||||
|
||||
/// Search `$PATH` for an executable named `name`, returning the first hit or
|
||||
/// `nil`. Covers toolchains installed outside the hard-coded locations — most
|
||||
/// notably a swiftly install (`~/.local/share/swiftly/bin`) on Linux, which puts
|
||||
/// both `swiftc` and `sourcekit-lsp` on `$PATH` but not in `/usr/bin`.
|
||||
func livecompileExecutableOnPath(_ name: String) -> String? {
|
||||
guard let pathEnv: String = ProcessInfo.processInfo.environment["PATH"] else { return nil }
|
||||
for dir: Substring in pathEnv.split(separator: ":") where !dir.isEmpty {
|
||||
let base: String = String(dir)
|
||||
let candidate: String = base.hasSuffix("/") ? base + name : base + "/" + name
|
||||
if FileManager.default.isExecutableFile(atPath: candidate) {
|
||||
return candidate
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
/// One-shot snippet compiler. Stateless — every `compile(source:)` allocates a
|
||||
/// fresh temp dir, dylib name, and `dlopen` handle.
|
||||
/// fresh temp dir, shared-library name, and `dlopen` handle.
|
||||
public enum LiveCompiler {
|
||||
|
||||
static var sharedLibraryExtension: String { livecompileSharedLibraryExtension }
|
||||
|
||||
/// Compile `source` to a dylib and resolve the snippet entry symbol.
|
||||
///
|
||||
/// - Parameters:
|
||||
@@ -52,7 +81,7 @@ public enum LiveCompiler {
|
||||
let workDir: URL = FileManager.default.temporaryDirectory
|
||||
.appendingPathComponent("LyrebirdLiveCompile-\(snippetID)", isDirectory: true)
|
||||
let sourcePath: String = workDir.appendingPathComponent("Snippet.swift").path
|
||||
let dylibPath: String = workDir.appendingPathComponent("lib\(moduleName).dylib").path
|
||||
let dylibPath: String = workDir.appendingPathComponent("lib\(moduleName).\(sharedLibraryExtension)").path
|
||||
|
||||
do {
|
||||
try FileManager.default.createDirectory(
|
||||
@@ -112,11 +141,17 @@ public enum LiveCompiler {
|
||||
|
||||
/// First-success search:
|
||||
/// 1. `xcrun -f swiftc` (honours `DEVELOPER_DIR` and the user's active Xcode).
|
||||
/// 2. Hard-coded toolchain paths.
|
||||
/// 2. An executable `swiftc` on `$PATH` — covers a swiftly install
|
||||
/// (`~/.local/share/swiftly/bin`) and any Linux toolchain layout that
|
||||
/// isn't in the hard-coded list.
|
||||
/// 3. Hard-coded toolchain paths.
|
||||
static func findSwiftc() throws -> String {
|
||||
if let xcrun: String = swiftcViaXcrun() {
|
||||
return xcrun
|
||||
}
|
||||
if let onPath: String = swiftcOnPath() {
|
||||
return onPath
|
||||
}
|
||||
let candidates: [String] = [
|
||||
"/usr/bin/swiftc",
|
||||
"/usr/local/bin/swiftc",
|
||||
@@ -128,6 +163,11 @@ public enum LiveCompiler {
|
||||
throw LiveCompileError.swiftcNotFound
|
||||
}
|
||||
|
||||
/// Search `$PATH` for an executable `swiftc`. Returns the first hit, or `nil`.
|
||||
static func swiftcOnPath() -> String? {
|
||||
livecompileExecutableOnPath("swiftc")
|
||||
}
|
||||
|
||||
/// `xcrun --show-sdk-path` — returns the macOS SDK path used by the user's
|
||||
/// active Xcode. `nil` when xcrun is missing or returns non-zero.
|
||||
static func sdkPathViaXcrun() -> String? {
|
||||
|
||||
@@ -12,7 +12,7 @@
|
||||
// `CompiledSnippet` so the dylibs stay mapped — see the leak-by-design note in
|
||||
// `CompiledSnippet.swift`.
|
||||
|
||||
#if os(macOS)
|
||||
#if os(macOS) || os(Linux)
|
||||
import Foundation
|
||||
import LyrebirdSynth
|
||||
|
||||
@@ -665,6 +665,13 @@ public final class LiveSession: @unchecked Sendable {
|
||||
for url: URL in LiveSession.discoverSPMModuleMaps(in: importPaths) {
|
||||
args += ["-Xcc", "-fmodule-map-file=\(url.path)"]
|
||||
}
|
||||
#if os(Linux)
|
||||
// Source-only system-library module maps (CALSA, LyrebirdC) — see the
|
||||
// helper. Without CALSA's, a Linux `import LyrebirdSynth` fails to resolve.
|
||||
for url: URL in LiveSession.discoverSourceSystemModuleMaps(in: importPaths) {
|
||||
args += ["-Xcc", "-fmodule-map-file=\(url.path)"]
|
||||
}
|
||||
#endif
|
||||
return args
|
||||
}
|
||||
|
||||
@@ -690,7 +697,15 @@ public final class LiveSession: @unchecked Sendable {
|
||||
args += ["-Xlinker", "-rpath", "-Xlinker", path.path]
|
||||
}
|
||||
} else {
|
||||
#if os(macOS)
|
||||
args += ["-Xlinker", "-undefined", "-Xlinker", "dynamic_lookup"]
|
||||
#else
|
||||
// ELF has no Mach-O flat-namespace deferred lookup: a snippet resolves
|
||||
// Lyrebird symbols by NEEDED-linking libLyrebirdDynamic.so, which needs
|
||||
// it present in a -L path (i.e. hasDylib == true). Reaching here on
|
||||
// Linux means no such library was found; emit no link flag so the link
|
||||
// fails loudly rather than adding a macOS-only one.
|
||||
#endif
|
||||
}
|
||||
return args
|
||||
}
|
||||
@@ -754,6 +769,44 @@ public final class LiveSession: @unchecked Sendable {
|
||||
return found
|
||||
}
|
||||
|
||||
#if os(Linux)
|
||||
/// Linux only: locate the `CALSA` **system-library** module map, which lives
|
||||
/// only in the package *source* tree (`Sources/CALSA/module.modulemap`),
|
||||
/// never in the build dir — a `.systemLibrary` target emits no
|
||||
/// `<CTarget>.build/module.modulemap`. `LyrebirdSynth` transitively imports
|
||||
/// `CALSA` on Linux (the ALSA driver), so a snippet's `import LyrebirdSynth`
|
||||
/// fails with `missing required module 'CALSA'` unless the map is surfaced.
|
||||
/// macOS never hits this (Core Audio, no CALSA import). `LyrebirdC` is a
|
||||
/// regular C target and IS covered by `discoverSPMModuleMaps` (build-tree
|
||||
/// map) — adding its source map too would `redefinition of module 'LyrebirdC'`.
|
||||
/// Walks up from each import path (bounded) to the package root.
|
||||
static func discoverSourceSystemModuleMaps(in importPaths: [URL]) -> [URL] {
|
||||
let maxWalk: Int = 8
|
||||
let relativeMaps: [String] = [
|
||||
"Sources/CALSA/module.modulemap",
|
||||
]
|
||||
var found: [URL] = []
|
||||
var visited: Set<String> = Set<String>()
|
||||
for start: URL in importPaths {
|
||||
var cur: URL = start
|
||||
for _ in 0..<maxWalk {
|
||||
for rel: String in relativeMaps {
|
||||
let probe: URL = cur.appendingPathComponent(rel)
|
||||
if FileManager.default.fileExists(atPath: probe.path),
|
||||
!visited.contains(probe.path) {
|
||||
visited.insert(probe.path)
|
||||
found.append(probe)
|
||||
}
|
||||
}
|
||||
let parent: URL = cur.deletingLastPathComponent()
|
||||
if parent == cur { break }
|
||||
cur = parent
|
||||
}
|
||||
}
|
||||
return found
|
||||
}
|
||||
#endif
|
||||
|
||||
// MARK: - Build-dir discovery
|
||||
|
||||
/// Best-effort discovery of the SPM `.build/<triple>/<config>` directory
|
||||
@@ -781,7 +834,10 @@ public final class LiveSession: @unchecked Sendable {
|
||||
// (→ libLyrebirdDynamic.dylib); the module/target is `LyrebirdSynth`
|
||||
// (LyrebirdSynth.swiftmodule). The consumer-facing `Lyrebird` product
|
||||
// is static and produces no dylib, so it is not a marker here.
|
||||
let markers: [String] = ["libLyrebirdDynamic.dylib", "LyrebirdSynth.swiftmodule"]
|
||||
let markers: [String] = [
|
||||
"libLyrebirdDynamic.\(livecompileSharedLibraryExtension)",
|
||||
"LyrebirdSynth.swiftmodule",
|
||||
]
|
||||
for start: URL in candidates {
|
||||
var cur: URL = start
|
||||
for _ in 0..<maxWalk {
|
||||
@@ -805,7 +861,9 @@ public final class LiveSession: @unchecked Sendable {
|
||||
/// already resolved in the running process.
|
||||
public static func hasLyrebirdDylib(in buildDir: URL) -> Bool {
|
||||
FileManager.default.fileExists(
|
||||
atPath: buildDir.appendingPathComponent("libLyrebirdDynamic.dylib").path
|
||||
atPath: buildDir.appendingPathComponent(
|
||||
"libLyrebirdDynamic.\(livecompileSharedLibraryExtension)"
|
||||
).path
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
@@ -19,7 +19,7 @@
|
||||
|
||||
import Foundation
|
||||
|
||||
#if os(macOS)
|
||||
#if os(macOS) || os(Linux)
|
||||
|
||||
extension LiveSession {
|
||||
|
||||
@@ -178,6 +178,16 @@ extension LiveSession {
|
||||
// the temp dir is deleted.
|
||||
stageClangModuleMaps(from: src, writeInto: staging, finalRoot: dest)
|
||||
|
||||
#if os(Linux)
|
||||
// 3.5. CALSA (the ALSA system library) has no `<Module>.build/` map in the
|
||||
// build dir — its module map lives only in the package source — so the
|
||||
// steps above miss it and a snippet's `import LyrebirdSynth` can't resolve
|
||||
// CALSA from the staged root. Copy the source module dir (map + relative
|
||||
// include/) into a self-contained `CALSA.build/` that `discoverSPMModuleMaps`
|
||||
// then finds at the staged location. macOS never imports CALSA.
|
||||
stageCALSAModuleMap(from: src, writeInto: staging)
|
||||
#endif
|
||||
|
||||
// Receipt: store the source stamp so the next call can skip an unchanged
|
||||
// source.
|
||||
let stampText: String = String(srcStamp.timeIntervalSince1970)
|
||||
@@ -218,10 +228,29 @@ extension LiveSession {
|
||||
includingPropertiesForKeys: nil
|
||||
) else { return [] }
|
||||
return entries.filter {
|
||||
$0.pathExtension == "dylib" && $0.lastPathComponent.hasPrefix("libLyrebird")
|
||||
$0.pathExtension == livecompileSharedLibraryExtension
|
||||
&& $0.lastPathComponent.hasPrefix("libLyrebird")
|
||||
}
|
||||
}
|
||||
|
||||
#if os(Linux)
|
||||
/// Copy the CALSA source module dir (`Sources/CALSA/`: `module.modulemap` +
|
||||
/// `include/calsa-shim.h`) into `staging/CALSA.build/`, so the staged root is a
|
||||
/// self-contained compile root for a snippet that transitively imports CALSA.
|
||||
/// The map's header path is relative, so it resolves verbatim at the staged
|
||||
/// location. Best-effort: if the source map isn't found (e.g. a detached build
|
||||
/// dir), staging proceeds without it and the snippet compile surfaces the gap.
|
||||
private static func stageCALSAModuleMap(from src: URL, writeInto staging: URL) {
|
||||
let fm: FileManager = FileManager.default
|
||||
guard let calsaMap: URL = LiveSession.discoverSourceSystemModuleMaps(in: [src]).first else {
|
||||
return
|
||||
}
|
||||
let calsaSrcDir: URL = calsaMap.deletingLastPathComponent()
|
||||
let stagedDir: URL = staging.appendingPathComponent("CALSA.build", isDirectory: true)
|
||||
try? fm.copyItem(at: calsaSrcDir, to: stagedDir)
|
||||
}
|
||||
#endif
|
||||
|
||||
/// A `dest` is safe to replace when it doesn't exist, is empty, or looks like
|
||||
/// a prior staging root (has a receipt or a `Modules/` dir).
|
||||
private static func isReplaceableStagingDest(_ dest: URL) -> Bool {
|
||||
|
||||
@@ -11,7 +11,7 @@
|
||||
// cases (toolchain missing, dlopen failure, etc.) pass through under
|
||||
// `.compilerError`.
|
||||
|
||||
#if os(macOS)
|
||||
#if os(macOS) || os(Linux)
|
||||
|
||||
public enum LiveSessionError: Error, CustomStringConvertible, Sendable {
|
||||
/// Snippet failed to compile. `diagnostics` are already line-mapped via the
|
||||
|
||||
@@ -13,7 +13,7 @@
|
||||
// across the Swift and DSL paths. The only difference is speed: a graph edit
|
||||
// re-evaluates in microseconds instead of a swiftc round-trip.
|
||||
|
||||
#if os(macOS)
|
||||
#if os(macOS) || os(Linux)
|
||||
import LyrebirdSynth
|
||||
import LyrebirdGraphDSLEmbedded
|
||||
|
||||
|
||||
@@ -17,9 +17,9 @@
|
||||
// that file, which is what lets `import LyrebirdSynth` and the injected
|
||||
// `context: SnippetContext` resolve for an otherwise free-standing file.
|
||||
//
|
||||
// macOS only.
|
||||
// macOS/Linux only.
|
||||
|
||||
#if os(macOS)
|
||||
#if os(macOS) || os(Linux)
|
||||
import Foundation
|
||||
|
||||
/// Provides code completion for a live-coding editor buffer.
|
||||
@@ -92,6 +92,13 @@ public actor SnippetCompletionService {
|
||||
for url: URL in LiveSession.discoverSPMModuleMaps(in: session.importPaths) {
|
||||
args += ["-Xcc", "-fmodule-map-file=\(url.path)"]
|
||||
}
|
||||
#if os(Linux)
|
||||
// CALSA's system-library module map is source-only (see LiveSession);
|
||||
// completion needs it too, or `import LyrebirdSynth` fails to resolve.
|
||||
for url: URL in LiveSession.discoverSourceSystemModuleMaps(in: session.importPaths) {
|
||||
args += ["-Xcc", "-fmodule-map-file=\(url.path)"]
|
||||
}
|
||||
#endif
|
||||
for path: URL in session.librarySearchPaths {
|
||||
args += ["-L", path.path]
|
||||
}
|
||||
@@ -106,8 +113,16 @@ public actor SnippetCompletionService {
|
||||
}
|
||||
|
||||
private static func sdkPath() -> String? {
|
||||
// Linux needs no `-sdk`, and `xcrun` does not exist there. Crucially,
|
||||
// guard the spawn: on Linux, `Process.run()` throws when the launch path
|
||||
// is missing, and a subsequent `waitUntilExit()` on a process that never
|
||||
// started BLOCKS FOREVER (it is a no-op on macOS) — the completion hang.
|
||||
// The macOS xcrun helpers in LiveCompiler avoid this by checking
|
||||
// `isExecutableFile(atPath:)` first; do the same here.
|
||||
let xcrunPath: String = "/usr/bin/xcrun"
|
||||
guard FileManager.default.isExecutableFile(atPath: xcrunPath) else { return nil }
|
||||
let p: Process = Process()
|
||||
p.executableURL = URL(fileURLWithPath: "/usr/bin/xcrun")
|
||||
p.executableURL = URL(fileURLWithPath: xcrunPath)
|
||||
p.arguments = ["--show-sdk-path", "--sdk", "macosx"]
|
||||
let out: Pipe = Pipe()
|
||||
p.standardOutput = out
|
||||
@@ -123,12 +138,20 @@ public actor SnippetCompletionService {
|
||||
/// Minimum-deployment target triple. Arch matches the running process;
|
||||
/// the OS floor matches the package's `macOS 15` minimum.
|
||||
private static func targetTriple() -> String {
|
||||
#if os(macOS)
|
||||
#if arch(arm64)
|
||||
let arch: String = "arm64"
|
||||
return "arm64-apple-macosx15.0"
|
||||
#else
|
||||
let arch: String = "x86_64"
|
||||
return "x86_64-apple-macosx15.0"
|
||||
#endif
|
||||
#else
|
||||
// Linux GNU triple; the Swift/LLVM spelling of arm64 is `aarch64`.
|
||||
#if arch(arm64)
|
||||
return "aarch64-unknown-linux-gnu"
|
||||
#else
|
||||
return "x86_64-unknown-linux-gnu"
|
||||
#endif
|
||||
#endif
|
||||
return "\(arch)-apple-macosx15.0"
|
||||
}
|
||||
|
||||
/// Launch sourcekit-lsp and open the (empty) document. Call once before the
|
||||
|
||||
@@ -16,7 +16,7 @@
|
||||
// synth that the host will track for re-evaluation cleanup, and report back via
|
||||
// the test-friendly `reportSampleRate(_:)` / `reportMessage(_:)` helpers.
|
||||
|
||||
#if os(macOS)
|
||||
#if os(macOS) || os(Linux)
|
||||
import LyrebirdSynth
|
||||
|
||||
public final class SnippetContext {
|
||||
|
||||
@@ -8,7 +8,7 @@
|
||||
//
|
||||
// C ABI signature of the snippet's exported entry point.
|
||||
|
||||
#if os(macOS)
|
||||
#if os(macOS) || os(Linux)
|
||||
|
||||
/// Snippets export this symbol via `@_cdecl("lyrebird_snippet_entry")`. The host
|
||||
/// passes a pointer to a `LiveSession` (or harness context) here; snippets need
|
||||
|
||||
@@ -12,7 +12,7 @@
|
||||
// from the opaque pointer the host hands in. The user's body is dropped in as
|
||||
// the function body.
|
||||
|
||||
#if os(macOS)
|
||||
#if os(macOS) || os(Linux)
|
||||
|
||||
public enum SnippetHarness {
|
||||
|
||||
|
||||
@@ -12,9 +12,9 @@
|
||||
// `initialize` / `initialized`, `textDocument/didOpen` / `didChange`, and
|
||||
// `textDocument/completion`.
|
||||
//
|
||||
// macOS only — the whole live-compile surface shells out to the toolchain.
|
||||
// macOS/Linux only — the whole live-compile surface shells out to the toolchain.
|
||||
|
||||
#if os(macOS)
|
||||
#if os(macOS) || os(Linux)
|
||||
import Foundation
|
||||
|
||||
/// Nanoseconds in one second — for `Task.sleep`.
|
||||
@@ -80,8 +80,22 @@ public final class SourceKitLSPClient: @unchecked Sendable {
|
||||
/// Resolve `sourcekit-lsp` from the active toolchain via `xcrun`, falling
|
||||
/// back to the conventional Xcode path.
|
||||
private static func resolveExecutable() -> URL {
|
||||
// Guard the xcrun spawn: `xcrun` is macOS-only, and on Linux
|
||||
// `Process.run()` throws for a missing launch path while a following
|
||||
// `waitUntilExit()` on the never-started process BLOCKS FOREVER (a no-op
|
||||
// on macOS). Skip straight to the conventional path there, where the
|
||||
// toolchain ships `/usr/bin/sourcekit-lsp`.
|
||||
let xcrunPath: String = "/usr/bin/xcrun"
|
||||
guard FileManager.default.isExecutableFile(atPath: xcrunPath) else {
|
||||
// No xcrun (Linux): prefer a `sourcekit-lsp` on $PATH (swiftly, a
|
||||
// custom toolchain, …) over the conventional /usr/bin location.
|
||||
if let onPath: String = livecompileExecutableOnPath("sourcekit-lsp") {
|
||||
return URL(fileURLWithPath: onPath)
|
||||
}
|
||||
return URL(fileURLWithPath: "/usr/bin/sourcekit-lsp")
|
||||
}
|
||||
let xcrun: Process = Process()
|
||||
xcrun.executableURL = URL(fileURLWithPath: "/usr/bin/xcrun")
|
||||
xcrun.executableURL = URL(fileURLWithPath: xcrunPath)
|
||||
xcrun.arguments = ["--find", "sourcekit-lsp"]
|
||||
let out: Pipe = Pipe()
|
||||
xcrun.standardOutput = out
|
||||
|
||||
@@ -6,9 +6,9 @@
|
||||
|
||||
// LyrebirdLiveCompile
|
||||
//
|
||||
// Errors surfaced by ``SourceKitLSPClient``. macOS only.
|
||||
// Errors surfaced by ``SourceKitLSPClient``. macOS/Linux only.
|
||||
|
||||
#if os(macOS)
|
||||
#if os(macOS) || os(Linux)
|
||||
|
||||
/// A failure from the `sourcekit-lsp` client.
|
||||
public enum SourceKitLSPError: Error, CustomStringConvertible {
|
||||
|
||||
@@ -12,7 +12,7 @@
|
||||
// few hundred ms after ⌘↩, then the swap lands exactly on the downbeat rather
|
||||
// than whenever the compile happened to finish.
|
||||
|
||||
#if os(macOS)
|
||||
#if os(macOS) || os(Linux)
|
||||
import Foundation
|
||||
import LyrebirdSynth
|
||||
|
||||
|
||||
@@ -7,14 +7,14 @@
|
||||
// LyrebirdLiveCompile — snippet source formatting.
|
||||
//
|
||||
// Shells out to the toolchain's `swift-format` (the same way `LiveCompiler`
|
||||
// shells to `swiftc`) to pretty-print a snippet. macOS only — iOS cannot exec
|
||||
// subprocesses. Writes the source to a temp file and runs `swift-format format
|
||||
// shells to `swiftc`) to pretty-print a snippet. macOS/Linux only — iOS cannot
|
||||
// exec subprocesses. Writes the source to a temp file and runs `swift-format format
|
||||
// <file>`, capturing stdout — avoids the write-stdin/read-stdout pipe deadlock
|
||||
// a streaming approach risks on large output.
|
||||
|
||||
import Foundation
|
||||
|
||||
#if os(macOS)
|
||||
#if os(macOS) || os(Linux)
|
||||
|
||||
/// Stateless `swift-format` wrapper.
|
||||
public enum SwiftFormatter {
|
||||
@@ -72,14 +72,20 @@ public enum SwiftFormatter {
|
||||
label: "net.realizedsound.lyrebird.swift-format-drain",
|
||||
attributes: .concurrent
|
||||
)
|
||||
var outData: Data = Data()
|
||||
var errData: Data = Data()
|
||||
// Box the drained bytes in reference cells: on Linux the `DispatchQueue`
|
||||
// closures are `@Sendable` and cannot mutate a captured `var`. Each
|
||||
// closure writes its own box (no shared mutation), and `group.wait()`
|
||||
// fences before the reads below.
|
||||
let outBox: SwiftFormatterDataBox = SwiftFormatterDataBox()
|
||||
let errBox: SwiftFormatterDataBox = SwiftFormatterDataBox()
|
||||
group.enter()
|
||||
drainQueue.async { outData = outHandle.readDataToEndOfFile(); group.leave() }
|
||||
drainQueue.async { outBox.data = outHandle.readDataToEndOfFile(); group.leave() }
|
||||
group.enter()
|
||||
drainQueue.async { errData = errHandle.readDataToEndOfFile(); group.leave() }
|
||||
drainQueue.async { errBox.data = errHandle.readDataToEndOfFile(); group.leave() }
|
||||
group.wait()
|
||||
process.waitUntilExit()
|
||||
let outData: Data = outBox.data
|
||||
let errData: Data = errBox.data
|
||||
|
||||
guard process.terminationStatus == 0 else {
|
||||
let stderr: String = String(data: errData, encoding: .utf8) ?? ""
|
||||
@@ -137,4 +143,13 @@ public enum SwiftFormatter {
|
||||
}
|
||||
}
|
||||
|
||||
/// Reference cell for one drained pipe's bytes, so the concurrent drain closures
|
||||
/// write through a reference instead of mutating a captured `var` (which the
|
||||
/// Linux `@Sendable` `DispatchQueue` closures forbid). `@unchecked Sendable`:
|
||||
/// each instance is written by exactly one closure and read only after
|
||||
/// `DispatchGroup.wait()`.
|
||||
private final class SwiftFormatterDataBox: @unchecked Sendable {
|
||||
var data: Data = Data()
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
#if os(macOS)
|
||||
#if os(macOS) || os(Linux)
|
||||
|
||||
/// Errors raised while formatting snippet source with `swift-format`.
|
||||
public enum SwiftFormatterError: Error {
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
#if os(macOS)
|
||||
#if os(macOS) || os(Linux)
|
||||
|
||||
/// Result of probing for a usable Swift toolchain (the Swift scratchpad compiles
|
||||
/// snippets by shelling out to `swiftc`, which requires Xcode or the Command Line
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
#if os(macOS)
|
||||
#if os(macOS) || os(Linux)
|
||||
import Foundation
|
||||
|
||||
/// Detects whether a usable Swift toolchain is present, so the app can warn the user
|
||||
|
||||
@@ -12,7 +12,7 @@
|
||||
// from each swiftc-reported line so error rows point back at the row the user
|
||||
// typed.
|
||||
|
||||
#if os(macOS)
|
||||
#if os(macOS) || os(Linux)
|
||||
|
||||
public struct WrappedSnippet: Equatable {
|
||||
public let source: String
|
||||
|
||||
Reference in New Issue
Block a user