From 5a29dd3ef5e2fdf47963ded980251f314a0c6b95 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Mon, 22 Sep 2025 08:35:44 -0700 Subject: [PATCH] build: make build_options generally available --- src/build/Config.zig | 18 +++++++++++++ src/build/GhosttyZig.zig | 33 +++++++++++------------- src/build/SharedDeps.zig | 15 +---------- src/terminal/build_options.zig | 46 +++++++++++++++++----------------- 4 files changed, 56 insertions(+), 56 deletions(-) diff --git a/src/build/Config.zig b/src/build/Config.zig index 97a98b752..244af8d93 100644 --- a/src/build/Config.zig +++ b/src/build/Config.zig @@ -8,6 +8,7 @@ const builtin = @import("builtin"); const ApprtRuntime = @import("../apprt/runtime.zig").Runtime; const FontBackend = @import("../font/backend.zig").Backend; const RendererBackend = @import("../renderer/backend.zig").Backend; +const TerminalBuildOptions = @import("../terminal/build_options.zig").Options; const XCFramework = @import("GhosttyXCFramework.zig"); const WasmTarget = @import("../os/wasm/target.zig").Target; const expandPath = @import("../os/path.zig").expand; @@ -490,6 +491,23 @@ pub fn addOptions(self: *const Config, step: *std.Build.Step.Options) !void { ); } +/// Returns the build options for the terminal module. This assumes a +/// Ghostty executable being built. Callers should modify this as needed. +pub fn terminalOptions(self: *const Config) TerminalBuildOptions { + return .{ + .artifact = .ghostty, + .simd = self.simd, + .oniguruma = true, + .slow_runtime_safety = switch (self.optimize) { + .Debug => true, + .ReleaseSafe, + .ReleaseSmall, + .ReleaseFast, + => false, + }, + }; +} + /// Returns a baseline CPU target retaining all the other CPU configs. pub fn baselineTarget(self: *const Config) std.Build.ResolvedTarget { // Set our cpu model as baseline. There may need to be other modifications diff --git a/src/build/GhosttyZig.zig b/src/build/GhosttyZig.zig index 56727f9ae..f175eb957 100644 --- a/src/build/GhosttyZig.zig +++ b/src/build/GhosttyZig.zig @@ -6,8 +6,6 @@ const std = @import("std"); const Config = @import("Config.zig"); const SharedDeps = @import("SharedDeps.zig"); -const vt_options = @import("../terminal/build_options.zig"); - vt: *std.Build.Module, pub fn init( @@ -15,6 +13,18 @@ pub fn init( cfg: *const Config, deps: *const SharedDeps, ) !GhosttyZig { + // General build options + const general_options = b.addOptions(); + try cfg.addOptions(general_options); + + // Terminal module build options + var vt_options = cfg.terminalOptions(); + vt_options.artifact = .lib; + // We presently don't allow Oniguruma in our Zig module at all. + // We should expose this as a build option in the future so we can + // conditionally do this. + vt_options.oniguruma = false; + const vt = b.addModule("ghostty-vt", .{ .root_source_file = b.path("src/lib_vt.zig"), .target = cfg.target, @@ -24,23 +34,8 @@ pub fn init( .link_libc = if (cfg.simd) true else null, .link_libcpp = if (cfg.simd) true else null, }); - vt_options.addOptions(b, vt, .{ - .artifact = .lib, - .simd = cfg.simd, - - // We presently don't allow Oniguruma in our Zig module at all. - // We should expose this as a build option in the future so we can - // conditionally do this. - .oniguruma = false, - - .slow_runtime_safety = switch (cfg.optimize) { - .Debug => true, - .ReleaseSafe, - .ReleaseSmall, - .ReleaseFast, - => false, - }, - }); + vt.addOptions("build_options", general_options); + vt_options.add(b, vt); // We always need unicode tables deps.unicode_tables.addModuleImport(vt); diff --git a/src/build/SharedDeps.zig b/src/build/SharedDeps.zig index cf84b3e0c..4f3e291fa 100644 --- a/src/build/SharedDeps.zig +++ b/src/build/SharedDeps.zig @@ -108,20 +108,7 @@ pub fn add( step.root_module.addOptions("build_options", self.options); // Every exe needs the terminal options - { - const vt_options = @import("../terminal/build_options.zig"); - vt_options.addOptions(b, step.root_module, .{ - .artifact = .ghostty, - .simd = self.config.simd, - .slow_runtime_safety = switch (optimize) { - .Debug => true, - .ReleaseSafe, - .ReleaseSmall, - .ReleaseFast, - => false, - }, - }); - } + self.config.terminalOptions().add(b, step.root_module); // Freetype _ = b.systemIntegrationOption("freetype", .{}); // Shows it in help diff --git a/src/terminal/build_options.zig b/src/terminal/build_options.zig index be5335f47..1b0449bbf 100644 --- a/src/terminal/build_options.zig +++ b/src/terminal/build_options.zig @@ -2,7 +2,7 @@ const std = @import("std"); pub const Options = struct { /// The target artifact to build. This will gate some functionality. - artifact: Artifact = .ghostty, + artifact: Artifact, /// Whether Oniguruma regex support is available. If this isn't /// available, some features will be disabled. This may be outdated, @@ -11,17 +11,36 @@ pub const Options = struct { /// - Kitty graphics protocol /// - Tmux control mode /// - oniguruma: bool = true, + oniguruma: bool, /// Whether to build SIMD-accelerated code paths. This pulls in more /// build-time dependencies and adds libc as a runtime dependency, /// but results in significant performance improvements. - simd: bool = true, + simd: bool, /// True if we should enable the "slow" runtime safety checks. These /// are runtime safety checks that are slower than typical and should /// generally be disabled in production builds. - slow_runtime_safety: bool = false, + slow_runtime_safety: bool, + + /// Add the required build options for the terminal module. + pub fn add( + self: Options, + b: *std.Build, + m: *std.Build.Module, + ) void { + const opts = b.addOptions(); + opts.addOption(Artifact, "artifact", self.artifact); + opts.addOption(bool, "oniguruma", self.oniguruma); + opts.addOption(bool, "simd", self.simd); + opts.addOption(bool, "slow_runtime_safety", self.slow_runtime_safety); + + // These are synthesized based on other options. + opts.addOption(bool, "kitty_graphics", self.oniguruma); + opts.addOption(bool, "tmux_control_mode", self.oniguruma); + + m.addOptions("terminal_options", opts); + } }; pub const Artifact = enum { @@ -31,22 +50,3 @@ pub const Artifact = enum { /// libghostty-vt, Zig module lib, }; - -/// Add the required build options for the terminal module. -pub fn addOptions( - b: *std.Build, - m: *std.Build.Module, - v: Options, -) void { - const opts = b.addOptions(); - opts.addOption(Artifact, "artifact", v.artifact); - opts.addOption(bool, "oniguruma", v.oniguruma); - opts.addOption(bool, "simd", v.simd); - opts.addOption(bool, "slow_runtime_safety", v.slow_runtime_safety); - - // These are synthesized based on other options. - opts.addOption(bool, "kitty_graphics", v.oniguruma); - opts.addOption(bool, "tmux_control_mode", v.oniguruma); - - m.addOptions("terminal_options", opts); -}