ghostty/src/build/GhosttyLib.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

111 lines
2.9 KiB
Zig

const GhosttyLib = @This();
const std = @import("std");
const Config = @import("Config.zig");
const SharedDeps = @import("SharedDeps.zig");
const LibtoolStep = @import("LibtoolStep.zig");
const LipoStep = @import("LipoStep.zig");
/// The step that generates the file.
step: *std.Build.Step,
/// The final static library file
output: std.Build.LazyPath,
pub fn initStatic(
b: *std.Build,
deps: *const SharedDeps,
) !GhosttyLib {
const lib = b.addStaticLibrary(.{
.name = "ghostty",
.root_source_file = b.path("src/main_c.zig"),
.target = deps.config.target,
.optimize = deps.config.optimize,
});
lib.bundle_compiler_rt = true;
lib.linkLibC();
// Add our dependencies. Get the list of all static deps so we can
// build a combined archive if necessary.
var lib_list = try deps.add(lib);
try lib_list.append(lib.getEmittedBin());
if (!deps.config.target.result.isDarwin()) return .{
.step = &lib.step,
.output = lib.getEmittedBin(),
};
// Create a static lib that contains all our dependencies.
const libtool = LibtoolStep.create(b, .{
.name = "ghostty",
.out_name = "libghostty-fat.a",
.sources = lib_list.items,
});
libtool.step.dependOn(&lib.step);
return .{
.step = libtool.step,
.output = libtool.output,
};
}
pub fn initShared(
b: *std.Build,
deps: *const SharedDeps,
) !GhosttyLib {
const lib = b.addSharedLibrary(.{
.name = "ghostty",
.root_source_file = b.path("src/main_c.zig"),
.target = deps.config.target,
.optimize = deps.config.optimize,
.strip = deps.config.strip,
});
_ = try deps.add(lib);
return .{
.step = &lib.step,
.output = lib.getEmittedBin(),
};
}
pub fn initMacOSUniversal(
b: *std.Build,
original_deps: *const SharedDeps,
) !GhosttyLib {
const aarch64 = try initStatic(b, &try original_deps.retarget(
b,
Config.genericMacOSTarget(b, .aarch64),
));
const x86_64 = try initStatic(b, &try original_deps.retarget(
b,
Config.genericMacOSTarget(b, .x86_64),
));
const universal = LipoStep.create(b, .{
.name = "ghostty",
.out_name = "libghostty.a",
.input_a = aarch64.output,
.input_b = x86_64.output,
});
return .{
.step = universal.step,
.output = universal.output,
};
}
pub fn install(self: *const GhosttyLib, name: []const u8) void {
const b = self.step.owner;
const lib_install = b.addInstallLibFile(self.output, name);
b.getInstallStep().dependOn(&lib_install.step);
}
pub fn installHeader(self: *const GhosttyLib) void {
const b = self.step.owner;
const header_install = b.addInstallHeaderFile(
b.path("include/ghostty.h"),
"ghostty.h",
);
b.getInstallStep().dependOn(&header_install.step);
}