mirror of
https://github.com/ghostty-org/ghostty.git
synced 2025-07-14 07:46:12 +03:00

The major idea behind the refactor is to split the `build.zig` file up into distinct `src/build/*.zig` files. By doing so, we can improve readability of the primary `build.zig` while also enabling better reuse of steps. Our `build.zig` is now less than 150 lines of code (of course, it calls into a lot more lines but they're neatly organized now). Improvements: * `build.zig` is less than 150 lines of readable code. * Help strings and unicode table generators are only run once when multiple artifacts are built since the results are the same regardless of target. * Metal lib is only built once per architecture (rather than once per artifact) * Resources (shell integration, terminfo, etc.) and docs are only built/installed for artifacts that need them Breaking changes: * Removed broken wasm build (@gabydd will re-add) * Removed conformance files, shell scripts are better and we don't run these anymore * Removed macOS app bundle creation, we don't use this anymore since we use Xcode ## Some History Our `build.zig` hasn't been significantly refactored since the project started, when Zig was _version 0.10_. Since then, the build system has changed significantly. We've only ever duct taped the `build.zig` as we needed to support new Zig versions, new features, etc. It was a mess. The major improvement is adapting the entire Ghostty `build.zig` to the Step and LazyPath changes introduced way back in Zig 0.12. This lets us better take advantage of parallelism and the dependency graph so that steps are only executed as they're needed. As such, you can see in the build.zig that we initialize a lot of things, but unless a final target (i.e. install, run) references those steps, _they'll never be executed_. This lets us clean up a lot.
70 lines
2.3 KiB
Zig
70 lines
2.3 KiB
Zig
//! GhosttyBench generates all the Ghostty benchmark helper binaries.
|
|
const GhosttyBench = @This();
|
|
|
|
const std = @import("std");
|
|
const Config = @import("Config.zig");
|
|
const SharedDeps = @import("SharedDeps.zig");
|
|
|
|
steps: []*std.Build.Step.Compile,
|
|
|
|
pub fn init(
|
|
b: *std.Build,
|
|
deps: *const SharedDeps,
|
|
) !GhosttyBench {
|
|
var steps = std.ArrayList(*std.Build.Step.Compile).init(b.allocator);
|
|
errdefer steps.deinit();
|
|
|
|
// Open the directory ./src/bench
|
|
const c_dir_path = b.pathFromRoot("src/bench");
|
|
var c_dir = try std.fs.cwd().openDir(c_dir_path, .{ .iterate = true });
|
|
defer c_dir.close();
|
|
|
|
// Go through and add each as a step
|
|
var c_dir_it = c_dir.iterate();
|
|
while (try c_dir_it.next()) |entry| {
|
|
// Get the index of the last '.' so we can strip the extension.
|
|
const index = std.mem.lastIndexOfScalar(u8, entry.name, '.') orelse continue;
|
|
if (index == 0) continue;
|
|
|
|
// If it doesn't end in 'zig' then ignore
|
|
if (!std.mem.eql(u8, entry.name[index + 1 ..], "zig")) continue;
|
|
|
|
// Name of the conformance app and full path to the entrypoint.
|
|
const name = entry.name[0..index];
|
|
|
|
// Executable builder.
|
|
const bin_name = try std.fmt.allocPrint(b.allocator, "bench-{s}", .{name});
|
|
const c_exe = b.addExecutable(.{
|
|
.name = bin_name,
|
|
.root_source_file = b.path("src/main.zig"),
|
|
.target = deps.config.target,
|
|
|
|
// We always want our benchmarks to be in release mode.
|
|
.optimize = .ReleaseFast,
|
|
});
|
|
c_exe.linkLibC();
|
|
|
|
// Update our entrypoint
|
|
var enum_name: [64]u8 = undefined;
|
|
@memcpy(enum_name[0..name.len], name);
|
|
std.mem.replaceScalar(u8, enum_name[0..name.len], '-', '_');
|
|
|
|
var buf: [64]u8 = undefined;
|
|
const new_deps = try deps.changeEntrypoint(b, std.meta.stringToEnum(
|
|
Config.ExeEntrypoint,
|
|
try std.fmt.bufPrint(&buf, "bench_{s}", .{enum_name[0..name.len]}),
|
|
).?);
|
|
|
|
_ = try new_deps.add(c_exe);
|
|
|
|
try steps.append(c_exe);
|
|
}
|
|
|
|
return .{ .steps = steps.items };
|
|
}
|
|
|
|
pub fn install(self: *const GhosttyBench) void {
|
|
const b = self.steps[0].step.owner;
|
|
for (self.steps) |step| b.installArtifact(step);
|
|
}
|