Files
Lyrebird/docs/windows-support-plan.md
T
josh b6fdee8bfc 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>
2026-09-10 21:09:11 -07:00

226 lines
10 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# Windows Support Plan
> Companion to `docs/cross-platform-feasibility.md`. That doc took the
> `LyrebirdSynth` core to a clean Linux cross-compile; this one is the actionable
> plan to bring the same core up on **Windows 11**, verified locally in a
> Parallels VM. Branch: `feasibility/cross-platform-linux-windows`.
## Why Windows is close, not a fresh port
The Linux work forced a platform-neutral design — `LyrebirdDSP` (`@_exported`
Accelerate on Apple / portable fallbacks off Apple), the portable WAV codec, and
`#if canImport(...)` framework guards. Most of that lights up on Windows for
free. `LyrebirdDSP`'s math shim already carries a `#elseif canImport(ucrt)`
branch, `OSStatus` is shimmed for any non-Darwin platform, and libdispatch +
`Synchronization.Atomic` both ship for Windows on Swift 6.x.
**The whole incremental Windows job is: 2 small platform-abstraction branches +
a local verify loop + (later) a WASAPI driver.**
## Environment: Parallels Windows 11 (Apple Silicon → ARM64)
On an Apple Silicon Mac, Parallels runs **Windows 11 on ARM64**, so the Swift
target is `aarch64-unknown-windows-msvc`. Swift.org ships ARM64 Windows
toolchains, but ARM64 Windows Swift is **less battle-tested than x86_64** — expect
a few more rough edges (some may be toolchain bugs, not our code). (If this were
an Intel Mac it'd be x64 Windows = the most-tested path.)
### Step 1 — Install the toolchain (inside the VM)
1. Install **Visual Studio** (MSVC + Windows 11 SDK). Swift.org requires
**"2022 or newer"; Community edition is sufficient.** VS 2022 is the
documented/tested baseline; **VS 2026 Community should work** (Swift finds VS
via `vswhere`, version-agnostic) but is not officially validated against the
current Swift 6.3.x toolchain — if `swift build` can't find the MSVC
environment, install **VS 2022 Build Tools alongside** (they coexist) as the
fallback.
- In the VS Installer: **"Desktop development with C++"** workload, plus these
components — **`Windows 11 SDK`**, **`VC.Tools.ARM64`** (the ARM64 MSVC
toolset — REQUIRED for `aarch64-windows`, easy to miss), and
`VC.Tools.x86.x64`.
- Enable **Developer Mode** (Windows Settings → For developers).
2. Install the **Swift toolchain**:
- `winget install --id Swift.Toolchain -e`
- (No `swiftly` on Windows yet — winget/MSI is the path.)
3. New terminal, verify: `swift --version` (expect Swift 6.3.x, target
`aarch64-unknown-windows-msvc`). Confirm a hello-world `swift build` works
before Lyrebird, to prove the MSVC env is wired.
### Step 2 — Get the code into the VM (native FS, NOT a shared folder)
Building from a Parallels **shared Mac folder** hits path/case/CRLF/file-lock
quirks under SwiftPM. Clone into the VM's own filesystem instead:
```
git clone <this-repo-url> C:\src\Lyrebird
cd C:\src\Lyrebird
git checkout feasibility/cross-platform-linux-windows
```
(Or add the Mac repo as a remote over the Parallels network share and `git pull`
from it — just don't *build* on the share.)
Set `git config core.autocrlf false` in the clone so the SPDX headers / fixtures
don't get CRLF-mangled.
### Step 3 — First build + the verify loop
```
swift build --target LyrebirdSynth 2>&1 | tee build.log
```
Expect errors. Copy `build.log` back to the Mac side (paste into the session).
The loop, exactly as we did for Linux:
1. You run `swift build` in the VM, paste the break-set.
2. I categorize + fix on the Mac, commit.
3. You `git pull`, rebuild, paste again.
4. Repeat until `LyrebirdSynth` builds, then `swift test`.
## The two KNOWN code gaps (ready to fix)
Both are self-contained platform-abstraction files built for exactly this. Fixes
below are ready to drop in; they are **blind until the VM verifies them**.
### Gap 1 — `Sources/Lyrebird/Utilities/MonotonicClock.swift`
The `#else` branch uses POSIX `clock_gettime(CLOCK_MONOTONIC)` / `timespec`,
which Windows UCRT does **not** provide. Add a WinSDK branch using
`QueryPerformanceCounter` / `QueryPerformanceFrequency`.
```swift
#if canImport(Darwin)
import Darwin
#elseif canImport(Glibc)
import Glibc
#elseif canImport(Musl)
import Musl
#elseif canImport(WinSDK)
import WinSDK
#endif
enum LyrebirdMonotonicClock {
#if canImport(Darwin)
// ... existing mach_absolute_time path (unchanged) ...
#elseif canImport(WinSDK)
static let ticksToNanos: Double = {
var freq: LARGE_INTEGER = LARGE_INTEGER()
QueryPerformanceFrequency(&freq)
return 1_000_000_000.0 / Double(freq.QuadPart) // QPC ticks → ns
}()
@inline(__always)
static func ticks() -> UInt64 {
var counter: LARGE_INTEGER = LARGE_INTEGER()
QueryPerformanceCounter(&counter)
return UInt64(counter.QuadPart)
}
#else
// ... existing clock_gettime(CLOCK_MONOTONIC) path (Linux) ...
#endif
}
```
QPC is monotonic, high-resolution, allocation-free → RT-safe. Note it returns a
tick count (not ns), so `ticksToNanos` scales by `1e9/frequency` — same
tick+factor shape as the Apple mach path (the Linux path returns ns directly and
uses factor 1.0).
### Gap 2 — `Sources/Lyrebird/Engine/RTThreadIdentity.swift`
Uses pthread TLS (`pthread_key_create`/`getspecific`/`setspecific`). Windows has
no pthread. Use Fiber-Local Storage (`FlsAlloc`/`FlsGetValue`/`FlsSetValue`) from
WinSDK — the Windows analogue of a TLS slot, and the pattern Swift's own runtime
uses. Keep `markCurrentThreadAsRT()` allocation-free (it runs on the render
thread).
```swift
#elseif canImport(WinSDK)
import WinSDK
// _key: DWORD from FlsAlloc(nil) (lazy, non-RT — via warmUp()).
// markCurrentThreadAsRT(): FlsSetValue(_key, sentinel) if FlsGetValue(_key) != sentinel
// isCurrentThreadRT: FlsGetValue(_key) == sentinel
```
`FlsGetValue`/`FlsSetValue` are O(1) pointer ops, no allocation → RT-safe, same
contract as the pthread path. Sentinel = a non-null `UnsafeMutableRawPointer`
(bit pattern 1), exactly like the pthread version.
> Design note: RTThreadIdentity is deliberately the one file that keeps a local
> platform `#if` (it's threading plumbing, not a DSP primitive). Adding the
> WinSDK branch keeps that boundary — generators stay platform-free.
## Step 4 — Expected further break-set (unknowns to watch)
Beyond the 2 known gaps, the VM build will likely surface:
- **Foundation gaps.** Windows Foundation is less complete than Linux's. Watch
`FileHandle` (the WAV codec's `seek`/`write(contentsOf:)`/`read(upToCount:)`),
`FileManager.createFile(atPath:)`, `URL.path` (backslash paths), `Data`.
- **Path separators.** WAV codec + any file I/O uses `url.path`; on Windows that's
`C:\...`. Should be handled by Foundation, but test round-trips.
- **CRLF / text.** MusicXML + any text parsing — line-ending assumptions.
- **`Thread.sleep` / `DispatchQueue`** — should work (libdispatch ships for
Windows) but the DiskOut BG-drain loop is worth a smoke test.
- **`Synchronization.Atomic`** — expected fine on 6.x; confirm.
- **FoundationXML** — MusicXML uses `#if canImport(FoundationXML)`; verify the
module exists on Windows (it may, may not — `canImport` handles either way,
but if `XMLParser` isn't in plain Foundation *or* FoundationXML on Windows,
MusicXML needs guarding).
## Step 5 — Milestone: `LyrebirdSynth` builds + tests on Windows
Same bar as Linux: `swift build --target LyrebirdSynth` clean, then
`swift test --no-parallel` green (or the DSP/WAV subset first:
`swift test --filter "ScalarDSPParityTests|WavCodecTests"`). The DSP parity tests
are Apple-only (`#if canImport(Accelerate)`), so on Windows they compile away —
Windows relies on the *same portable cores* Linux exercises, plus the WAV
round-trip tests which DO run everywhere.
## Step 6 — Live audio + MIDI (after headless core is green)
- **Audio out:** `WindowsAudioDriver: AudioDriver` — **WASAPI** (built-in,
~10–30 ms; shared or exclusive mode) first; **ASIO** later for low latency
(<10 ms) — note the Steinberg ASIO SDK is **license-restricted / not
redistributable**, a legal step, and users install their own ASIO driver.
- **MIDI:** WinMM (legacy, simple) or WinRT MIDI (modern).
- NRT-to-WAV already works via the portable codec, so offline render is
available on Windows the moment the core builds.
## Step 7 — CI (optional, later)
A GitHub Actions `windows-latest` lane (`winget install Swift.Toolchain` + VS
Build Tools) locks in the Windows build so it can't silently regress. The
Parallels loop is for *bring-up*; CI is for *keeping it green*. (Note:
`windows-latest` runners are x86_64 — if the local VM is ARM64, CI covers the
x64 triple and the VM covers ARM64; together that's good coverage.)
## Quick-start checklist (when you return)
- [ ] VM: install VS 2022 Build Tools (Desktop C++ workload) + Swift toolchain
- [ ] VM: `git clone` the repo natively (not a shared folder), checkout this branch, `core.autocrlf false`
- [ ] VM: `swift build --target LyrebirdSynth 2>&1 | tee build.log`
- [ ] Paste `build.log` into the session
- [ ] (Mac) land Gap 1 + Gap 2 fixes, iterate the break-set to zero
- [ ] VM: `swift test --no-parallel`
- [ ] Then: WASAPI driver, MIDI, CI
## What's already committed vs pending
- Committed on this branch: the entire portable core (Linux-verified), incl. the
`ucrt` math branch and `OSStatus` shim that Windows reuses.
- **Both WinSDK branches are now written (blind, awaiting VM verification):**
- `MonotonicClock.swift` — `#elseif canImport(WinSDK)` using
`QueryPerformanceCounter` / `QueryPerformanceFrequency`.
- `RTThreadIdentity.swift` — `#elseif canImport(WinSDK)` using
`TlsAlloc` / `TlsGetValue` / `TlsSetValue` (chose `Tls*` over the plan's
original `Fls*` — `Tls*` is the direct thread-local analogue; `Fls*` is
fiber-scoped). Refactored the pthread call sites through inline
`_tlsGet`/`_tlsSet` helpers so the RT methods are platform-agnostic;
macOS + Linux re-verified (build + 22 targeted tests) — no regression.
- Neither is verified on Windows yet — that's the first thing the VM build
checks. Likely correction points: exact WinSDK symbol names / `LARGE_INTEGER`
field access (`.QuadPart`), `DWORD.max` vs a `TLS_OUT_OF_INDEXES` constant,
and whether `QueryPerformance*`/`Tls*` are imported into Swift as-is.
- **Still pending:** the unknown Foundation/path break-set (Step 4) — only the VM
can surface it.