build: build ReleaseFast and baseline CPU by default for system packages

This makes `--system` imply `-Doptimize=ReleaseFast` and
`-Dcpu=baseline`. These can still be overridden by the user by setting
the same flags explicitly.

The `--system` flag is used to build system packages and its a safe
assumption that in those cases, Ghostty is being built for general use
amongst many users, so the default should be optimized for
performance and compatibility.

This was always documented in our PACKAGING.md, but is now automatic.
The PACKAGING.md file has been updated to reflect this.
push-lvtrmyqqqpkn
Mitchell Hashimoto 2025-08-21 11:03:18 -07:00
parent d725f2346f
commit a6206632d4
No known key found for this signature in database
GPG Key ID: 523D5DC389D273BC
3 changed files with 47 additions and 22 deletions

View File

@ -300,7 +300,11 @@ jobs:
# GhosttyKit is the framework that is built from Zig for our native # GhosttyKit is the framework that is built from Zig for our native
# Mac app to access. # Mac app to access.
- name: Build GhosttyKit - name: Build GhosttyKit
run: nix develop -c zig build --system ${{ steps.deps.outputs.deps }} -Demit-macos-app=false run: |
nix develop -c zig build \
--system ${{ steps.deps.outputs.deps }} \
-Doptimize=Debug \
-Demit-macos-app=false
# The native app is built with native Xcode tooling. This also does # The native app is built with native Xcode tooling. This also does
# codesigning. IMPORTANT: this must NOT run in a Nix environment. # codesigning. IMPORTANT: this must NOT run in a Nix environment.
@ -350,11 +354,11 @@ jobs:
- name: Build All - name: Build All
run: | run: |
nix develop -c zig build --system ${{ steps.deps.outputs.deps }} -Demit-macos-app=false -Drenderer=metal -Dfont-backend=freetype nix develop -c zig build --system ${{ steps.deps.outputs.deps }} -Demit-macos-app=false -Doptimize=Debug -Drenderer=metal -Dfont-backend=freetype
nix develop -c zig build --system ${{ steps.deps.outputs.deps }} -Demit-macos-app=false -Drenderer=metal -Dfont-backend=coretext nix develop -c zig build --system ${{ steps.deps.outputs.deps }} -Demit-macos-app=false -Doptimize=Debug -Drenderer=metal -Dfont-backend=coretext
nix develop -c zig build --system ${{ steps.deps.outputs.deps }} -Demit-macos-app=false -Drenderer=metal -Dfont-backend=coretext_freetype nix develop -c zig build --system ${{ steps.deps.outputs.deps }} -Demit-macos-app=false -Doptimize=Debug -Drenderer=metal -Dfont-backend=coretext_freetype
nix develop -c zig build --system ${{ steps.deps.outputs.deps }} -Demit-macos-app=false -Drenderer=metal -Dfont-backend=coretext_harfbuzz nix develop -c zig build --system ${{ steps.deps.outputs.deps }} -Demit-macos-app=false -Doptimize=Debug -Drenderer=metal -Dfont-backend=coretext_harfbuzz
nix develop -c zig build --system ${{ steps.deps.outputs.deps }} -Demit-macos-app=false -Drenderer=metal -Dfont-backend=coretext_noshape nix develop -c zig build --system ${{ steps.deps.outputs.deps }} -Demit-macos-app=false -Doptimize=Debug -Drenderer=metal -Dfont-backend=coretext_noshape
build-snap: build-snap:
strategy: strategy:
@ -500,7 +504,10 @@ jobs:
# This relies on the cache being populated by the commands above. # This relies on the cache being populated by the commands above.
- name: Test System Build - name: Test System Build
run: nix develop -c zig build --system ${ZIG_GLOBAL_CACHE_DIR}/p run: |
nix develop -c zig build \
--system ${ZIG_GLOBAL_CACHE_DIR}/p \
-Doptimize=Debug
test-gtk: test-gtk:
strategy: strategy:

View File

@ -81,9 +81,7 @@ ZIG_GLOBAL_CACHE_DIR=/tmp/offline-cache ./nix/build-support/fetch-zig-cache.sh
DESTDIR=/tmp/ghostty \ DESTDIR=/tmp/ghostty \
zig build \ zig build \
--prefix /usr \ --prefix /usr \
--system /tmp/offline-cache/p \ --system /tmp/offline-cache/p
-Doptimize=ReleaseFast \
-Dcpu=baseline
``` ```
The build options are covered in the next section, but this will build The build options are covered in the next section, but this will build
@ -96,8 +94,8 @@ to wherever the package manager expects it).
### Build Options ### Build Options
Ghostty uses the Zig build system. You can see all available build options by Ghostty uses the Zig build system. You can see all available build options by
running `zig build --help`. The following are options that are particularly running `zig build --help`. The following are options are very important
relevant to package maintainers: and should always be used:
- `--prefix`: The installation prefix. Combine with the `DESTDIR` environment - `--prefix`: The installation prefix. Combine with the `DESTDIR` environment
variable to install to a temporary directory for packaging. variable to install to a temporary directory for packaging.
@ -106,17 +104,20 @@ relevant to package maintainers:
any package fetching from the internet. This flag also triggers all any package fetching from the internet. This flag also triggers all
dependencies to be dynamically linked by default. This flag also makes dependencies to be dynamically linked by default. This flag also makes
the binary a PIE (Position Independent Executable) by default (override the binary a PIE (Position Independent Executable) by default (override
with `-Dpie`). with `-Dpie`). This flag also enables a default baseline CPU and enables
all optimizations.
Additional options packagers may find useful:
- `-Doptimize=ReleaseFast`: Build with optimizations enabled and safety checks - `-Doptimize=ReleaseFast`: Build with optimizations enabled and safety checks
disabled. This is the recommended build mode for distribution. I'd prefer disabled. This is the recommended build mode for distribution. I'd prefer
a safe build but terminal emulators are performance-sensitive and the a safe build but terminal emulators are performance-sensitive and the
safe build is currently too slow. I plan to improve this in the future. safe build is currently too slow. This is the default when `--system`
Other build modes are available: `Debug`, `ReleaseSafe`, and `ReleaseSmall`. is specified.
- `-Dcpu=baseline`: Build for the "baseline" CPU of the target architecture. - `-Dcpu=baseline`: Build for the "baseline" CPU of the target architecture.
This avoids building for newer CPU features that may not be available on This avoids building for newer CPU features that may not be available on
all target machines. all target machines. This is the default when `--system` is specified.
- `-Dtarget=$arch-$os-$abi`: Build for a specific target triple. This is - `-Dtarget=$arch-$os-$abi`: Build for a specific target triple. This is
often necessary for system packages to specify a specific minimum Linux often necessary for system packages to specify a specific minimum Linux

View File

@ -66,11 +66,33 @@ emit_webdata: bool = false,
env: std.process.EnvMap, env: std.process.EnvMap,
pub fn init(b: *std.Build) !Config { pub fn init(b: *std.Build) !Config {
// This is set to true when we're building a system package. For now
// this is trivially detected using the "system_package_mode" bool
// but we may want to make this more sophisticated in the future.
const system_package = b.graph.system_package_mode;
// When we're system packaging, we change our default optimization
// to ReleaseFast if it wasn't explicitly set. We do this because we
// assume we're building for a release if we're packaging. We use this
// over `preferred_optimize_mode` because it still lets people change
// it via the `--release` flag.
if (system_package) b.release_mode = switch (b.release_mode) {
.off, .any => .fast,
.fast, .safe, .small => b.release_mode,
};
// Setup our standard Zig target and optimize options, i.e. // Setup our standard Zig target and optimize options, i.e.
// `-Doptimize` and `-Dtarget`. // `-Doptimize` and `-Dtarget`.
const optimize = b.standardOptimizeOption(.{}); const optimize = b.standardOptimizeOption(.{});
const target = target: { const target = target: {
var result = b.standardTargetOptions(.{}); var result = b.standardTargetOptions(.{
// If we're system packaging, we assume we're creating a package
// for general use by other people, so we want to default to
// a baseline CPU.
.default_target = if (system_package) .{
.cpu_model = .baseline,
} else .{},
});
// If we're building for macOS and we're on macOS, we need to // If we're building for macOS and we're on macOS, we need to
// use a generic target to workaround compilation issues. // use a generic target to workaround compilation issues.
@ -89,11 +111,6 @@ pub fn init(b: *std.Build) !Config {
break :target result; break :target result;
}; };
// This is set to true when we're building a system package. For now
// this is trivially detected using the "system_package_mode" bool
// but we may want to make this more sophisticated in the future.
const system_package = b.graph.system_package_mode;
// This specifies our target wasm runtime. For now only one semi-usable // This specifies our target wasm runtime. For now only one semi-usable
// one exists so this is hardcoded. // one exists so this is hardcoded.
const wasm_target: WasmTarget = .browser; const wasm_target: WasmTarget = .browser;