feat(macos): port Ghostty's macOS code paths (Metal, CoreText, Darwin)

Ports the macOS-specific Zig code behind cfg(target_os = "macos"):
- renderer: a GraphicsApi trait with generic::Renderer<G>; Metal backend
  (all renderer/metal/* files, shaders.metal verbatim, runtime library
  compile, IOSurface layer, CVDisplayLink), shadertoy -> MSL via naga;
  OpenGL remains the backend elsewhere.
- font: CoreText face, shaper and discovery with build.rs backend
  selection (coretext default; coretext-freetype / coretext-harfbuzz /
  coretext-noshape features), no fontconfig/freetype/harfbuzz on Apple.
- input: KeymapDarwin via UCKeyTranslate, option-as-alt, mac keycodes.
- os: macos, mach, cf_release_thread, NSProcessInfo args and macOS
  branches; config: macOS defaults, keybinds, Application Support paths.
- termio/core: login(1) wrapping, openpty/execvpe, Darwin child exit
  handling, os_log logging.

Compile-checked for aarch64/x86_64-apple-darwin (clippy -D warnings
clean) using the cross setup in docs/rewrite/notes/macos-M1.md; cannot
run here. The embedded runtime/C API (embedded.zig) is the next step.

Also includes the cross-platform integration edits shared by the crash,
verification, Windows and macOS work (Cargo target-gating, thread
crash-context hooks, apprt-gtk surface/IME refactor, termio/os cfg
gates), a cfg fix so ghostty-config builds on wasm32, and an allow for
the intentional recursion in the crash overflow test.

Test Plan:
- cargo test --workspace (3224 passed)
- cargo clippy --workspace --all-targets -- -D warnings
- cargo check --target aarch64-apple-darwin -p ghostty-renderer (with
  the documented cross env)
This commit is contained in:
ddidderr committed 2026-10-12 06:02:39 +02:00
1 parent 3c8fae1089
commit 452b782149
124 files changed
+14936 -1242

No files matched your search

+90 -7
View File
@@ -1,9 +1,19 @@
//! Font backend selection. Port of `font/backend.zig`.
//!
//! On Linux (and other non-wasm targets) [`OPTIONS`] selects
//! [`Backend::FontconfigFreetype`] (fontconfig discovery, FreeType rendering,
//! HarfBuzz shaping). The other variants are kept for fidelity with the Zig
//! enum and its helper predicates.
//! [`OPTIONS`] selects the backend for the target (`build.rs` emits the
//! matching `cfg`s):
//!
//! * Linux and other non-Apple, non-wasm targets:
//! [`Backend::FontconfigFreetype`] (fontconfig discovery, FreeType
//! rendering, HarfBuzz shaping).
//! * Apple targets: [`Backend::Coretext`] by default. macOS also supports
//! [`Backend::CoretextFreetype`], [`Backend::CoretextHarfbuzz`] and
//! [`Backend::CoretextNoshape`], selected with the cargo features
//! `coretext-freetype`, `coretext-harfbuzz` and `coretext-noshape`
//! (Zig: `-Dfont-backend=...`). "There is no scenario that is the default"
//! for those; they're only used by people who want to self-compile Ghostty
//! and prefer the freetype aesthetic.
//! * wasm32: [`Backend::WebCanvas`].
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum Backend {
@@ -31,7 +41,9 @@ impl Backend {
// with `wasm_target == .browser`).
if cfg!(target_arch = "wasm32") {
Backend::WebCanvas
} else if cfg!(target_os = "macos") {
} else if cfg!(target_vendor = "apple") {
// macOS also supports "coretext_freetype" but there is no
// scenario that is the default.
Backend::Coretext
} else {
Backend::FontconfigFreetype
@@ -76,11 +88,45 @@ pub struct Options {
pub backend: Backend,
}
#[cfg(not(target_arch = "wasm32"))]
#[cfg(all(not(target_arch = "wasm32"), not(target_vendor = "apple")))]
pub const OPTIONS: Options = Options {
backend: Backend::FontconfigFreetype,
};
#[cfg(all(target_vendor = "apple", feature = "coretext-freetype"))]
pub const OPTIONS: Options = Options {
backend: Backend::CoretextFreetype,
};
#[cfg(all(
target_vendor = "apple",
feature = "coretext-harfbuzz",
not(feature = "coretext-freetype")
))]
pub const OPTIONS: Options = Options {
backend: Backend::CoretextHarfbuzz,
};
#[cfg(all(
target_vendor = "apple",
feature = "coretext-noshape",
not(feature = "coretext-freetype"),
not(feature = "coretext-harfbuzz")
))]
pub const OPTIONS: Options = Options {
backend: Backend::CoretextNoshape,
};
#[cfg(all(
target_vendor = "apple",
not(feature = "coretext-freetype"),
not(feature = "coretext-harfbuzz"),
not(feature = "coretext-noshape")
))]
pub const OPTIONS: Options = Options {
backend: Backend::Coretext,
};
#[cfg(target_arch = "wasm32")]
pub const OPTIONS: Options = Options {
backend: Backend::WebCanvas,
@@ -99,7 +145,44 @@ mod tests {
assert!(!b.has_coretext());
}
#[cfg(not(target_arch = "wasm32"))]
#[test]
fn coretext_capabilities() {
let b = Backend::Coretext;
assert!(!b.has_freetype());
assert!(!b.has_fontconfig());
assert!(!b.has_harfbuzz());
assert!(b.has_coretext());
let b = Backend::CoretextFreetype;
assert!(b.has_freetype());
assert!(!b.has_fontconfig());
assert!(b.has_harfbuzz());
assert!(b.has_coretext());
let b = Backend::CoretextHarfbuzz;
assert!(!b.has_freetype());
assert!(b.has_harfbuzz());
assert!(b.has_coretext());
let b = Backend::CoretextNoshape;
assert!(!b.has_freetype());
assert!(!b.has_harfbuzz());
assert!(b.has_coretext());
}
#[test]
fn default_backend_per_target() {
let b = Backend::default_for_target();
if cfg!(target_vendor = "apple") {
assert_eq!(b, Backend::Coretext);
} else if cfg!(target_arch = "wasm32") {
assert_eq!(b, Backend::WebCanvas);
} else {
assert_eq!(b, Backend::FontconfigFreetype);
}
}
#[cfg(all(not(target_arch = "wasm32"), not(target_vendor = "apple")))]
#[test]
fn linux_backend_capabilities() {
let b = OPTIONS.backend;