ghostty/src/build/GitVersion.zig
Mitchell Hashimoto 8bf5c4ed7f This is a major refactor of build.zig.
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.
2025-01-07 19:47:43 -08:00

76 lines
2.3 KiB
Zig

const Version = @This();
const std = @import("std");
/// The short hash (7 characters) of the latest commit.
short_hash: []const u8,
/// True if there was a diff at build time.
changes: bool,
/// The tag -- if any -- that this commit is a part of.
tag: ?[]const u8,
/// The branch that was checked out at the time of the build.
branch: []const u8,
/// Initialize the version and detect it from the Git environment. This
/// allocates using the build allocator and doesn't free.
pub fn detect(b: *std.Build) !Version {
// Execute a bunch of git commands to determine the automatic version.
var code: u8 = 0;
const branch: []const u8 = b.runAllowFail(
&[_][]const u8{ "git", "-C", b.build_root.path orelse ".", "rev-parse", "--abbrev-ref", "HEAD" },
&code,
.Ignore,
) catch |err| switch (err) {
error.FileNotFound => return error.GitNotFound,
error.ExitCodeFailure => return error.GitNotRepository,
else => return err,
};
const short_hash = short_hash: {
const output = b.runAllowFail(
&[_][]const u8{ "git", "-C", b.build_root.path orelse ".", "log", "--pretty=format:%h", "-n", "1" },
&code,
.Ignore,
) catch |err| switch (err) {
error.FileNotFound => return error.GitNotFound,
else => return err,
};
break :short_hash std.mem.trimRight(u8, output, "\r\n ");
};
const tag = b.runAllowFail(
&[_][]const u8{ "git", "-C", b.build_root.path orelse ".", "describe", "--exact-match", "--tags" },
&code,
.Ignore,
) catch |err| switch (err) {
error.FileNotFound => return error.GitNotFound,
error.ExitCodeFailure => "", // expected
else => return err,
};
_ = b.runAllowFail(&[_][]const u8{
"git",
"-C",
b.build_root.path orelse ".",
"diff",
"--quiet",
"--exit-code",
}, &code, .Ignore) catch |err| switch (err) {
error.FileNotFound => return error.GitNotFound,
error.ExitCodeFailure => {}, // expected
else => return err,
};
const changes = code != 0;
return .{
.short_hash = short_hash,
.changes = changes,
.tag = if (tag.len > 0) std.mem.trimRight(u8, tag, "\r\n ") else null,
.branch = std.mem.trimRight(u8, branch, "\r\n "),
};
}