From 5dc98da9a098c64fec752c34d78cb6fbb0a2bf62 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Fri, 24 Mar 2023 19:39:50 -0700 Subject: [PATCH 1/6] build compiles at least --- build.zig | 52 ++++++----------------- flake.lock | 6 +-- pkg/tracy/build.zig | 2 +- src/build/LibtoolStep.zig | 55 ++++++++---------------- src/build/LipoStep.zig | 50 +++++++--------------- src/build/XCFrameworkStep.zig | 78 +++++++++++++---------------------- 6 files changed, 75 insertions(+), 168 deletions(-) diff --git a/build.zig b/build.zig index ae439db96..cde0978a8 100644 --- a/build.zig +++ b/build.zig @@ -222,7 +222,7 @@ pub fn build(b: *std.build.Builder) !void { break :is_nixos if (std.fs.accessAbsolute("/etc/NIXOS", .{})) true else |_| false; }; if (is_nixos and env.get("IN_NIX_SHELL") == null) { - const notice = b.addLog( + try exe.step.addError( "\x1b[" ++ color_map.get("yellow").? ++ "\x1b[" ++ color_map.get("b").? ++ "WARNING: " ++ @@ -244,8 +244,6 @@ pub fn build(b: *std.build.Builder) !void { "\x1b[0m", .{}, ); - - exe.step.dependOn(¬ice.step); } // If we're installing, we get the install step so we can add @@ -327,7 +325,7 @@ pub fn build(b: *std.build.Builder) !void { .sources = lib_list.items, }); libtool.step.dependOn(&lib.step); - b.default_step.dependOn(&libtool.step); + b.default_step.dependOn(libtool.step); break :lib libtool; }; @@ -355,7 +353,7 @@ pub fn build(b: *std.build.Builder) !void { .sources = lib_list.items, }); libtool.step.dependOn(&lib.step); - b.default_step.dependOn(&libtool.step); + b.default_step.dependOn(libtool.step); break :lib libtool; }; @@ -363,22 +361,22 @@ pub fn build(b: *std.build.Builder) !void { const static_lib_universal = LipoStep.create(b, .{ .name = "ghostty", .out_name = "libghostty.a", - .input_a = .{ .generated = &static_lib_aarch64.out_path }, - .input_b = .{ .generated = &static_lib_x86_64.out_path }, + .input_a = static_lib_aarch64.output, + .input_b = static_lib_x86_64.output, }); - static_lib_universal.step.dependOn(&static_lib_aarch64.step); - static_lib_universal.step.dependOn(&static_lib_x86_64.step); + static_lib_universal.step.dependOn(static_lib_aarch64.step); + static_lib_universal.step.dependOn(static_lib_x86_64.step); // The xcframework wraps our ghostty library so that we can link // it to the final app built with Swift. const xcframework = XCFrameworkStep.create(b, .{ .name = "GhosttyKit", .out_path = "macos/GhosttyKit.xcframework", - .library = .{ .generated = &static_lib_universal.out_path }, + .library = static_lib_universal.output, .headers = .{ .path = "include" }, }); - xcframework.step.dependOn(&static_lib_universal.step); - b.default_step.dependOn(&xcframework.step); + xcframework.step.dependOn(static_lib_universal.step); + b.default_step.dependOn(xcframework.step); } // wasm @@ -477,24 +475,11 @@ pub fn build(b: *std.build.Builder) !void { .target = target, }); { - if (emit_test_exe) { - const main_test_exe = b.addTest(.{ - .name = "ghostty-test", - .kind = .test_exe, - .root_source_file = .{ .path = "src/main.zig" }, - .target = target, - }); - main_test_exe.install(); - } + if (emit_test_exe) main_test.install(); main_test.setFilter(test_filter); _ = try addDeps(b, main_test, true); main_test.addOptions("build_options", exe_options); - - var before = b.addLog("\x1b[" ++ color_map.get("cyan").? ++ "\x1b[" ++ color_map.get("b").? ++ "[{s} tests]" ++ "\x1b[" ++ color_map.get("d").? ++ " ----" ++ "\x1b[0m", .{"ghostty"}); - var after = b.addLog("\x1b[" ++ color_map.get("d").? ++ "–––---\n\n" ++ "\x1b[0m", .{}); - test_step.dependOn(&before.step); test_step.dependOn(&main_test.step); - test_step.dependOn(&after.step); } // Named package dependencies don't have their tests run by reference, @@ -510,7 +495,6 @@ pub fn build(b: *std.build.Builder) !void { var buf: [256]u8 = undefined; var test_run = b.addTest(.{ .name = try std.fmt.bufPrint(&buf, "{s}-test", .{name}), - .kind = .@"test", .root_source_file = module.source_file, .target = target, }); @@ -521,21 +505,9 @@ pub fn build(b: *std.build.Builder) !void { // try test_.packages.appendSlice(children); // } - var before = b.addLog("\x1b[" ++ color_map.get("cyan").? ++ "\x1b[" ++ color_map.get("b").? ++ "[{s} tests]" ++ "\x1b[" ++ color_map.get("d").? ++ " ----" ++ "\x1b[0m", .{name}); - var after = b.addLog("\x1b[" ++ color_map.get("d").? ++ "–––---\n\n" ++ "\x1b[0m", .{}); - test_step.dependOn(&before.step); test_step.dependOn(&test_run.step); - test_step.dependOn(&after.step); - if (emit_test_exe) { - const test_exe = b.addTest(.{ - .name = try std.fmt.bufPrint(&buf, "{s}-test", .{name}), - .kind = .test_exe, - .root_source_file = module.source_file, - .target = target, - }); - test_exe.install(); - } + if (emit_test_exe) test_run.install(); } } } diff --git a/flake.lock b/flake.lock index e3e3b2783..0386c1be1 100644 --- a/flake.lock +++ b/flake.lock @@ -126,11 +126,11 @@ "nixpkgs": "nixpkgs_2" }, "locked": { - "lastModified": 1678494092, - "narHash": "sha256-0vB5xFl3qoQmyH3l2goAbdzxcU4KwHqdN1SrQsBfT7k=", + "lastModified": 1679669017, + "narHash": "sha256-FUvrE/4HKsrl/CedvQuoJa+vDr9lM7elI28cJs9uSHs=", "owner": "mitchellh", "repo": "zig-overlay", - "rev": "5f38868e7af88a575d6dfd48ee213afcd96bed82", + "rev": "810cecd27319cb79feb4a8e79ac7ad40c7826235", "type": "github" }, "original": { diff --git a/pkg/tracy/build.zig b/pkg/tracy/build.zig index debef1df6..7894f00c3 100644 --- a/pkg/tracy/build.zig +++ b/pkg/tracy/build.zig @@ -47,7 +47,7 @@ pub fn buildTracy( lib.addIncludePath(root); lib.addCSourceFile(try std.fs.path.join( - lib.builder.allocator, + b.allocator, &.{ root, "TracyClient.cpp" }, ), flags.items); diff --git a/src/build/LibtoolStep.zig b/src/build/LibtoolStep.zig index 1d114f097..adb2e77a2 100644 --- a/src/build/LibtoolStep.zig +++ b/src/build/LibtoolStep.zig @@ -4,8 +4,8 @@ const LibtoolStep = @This(); const std = @import("std"); const Step = std.build.Step; +const RunStep = std.build.RunStep; const FileSource = std.build.FileSource; -const GeneratedFile = std.build.GeneratedFile; pub const Options = struct { /// The name of this step. @@ -19,47 +19,26 @@ pub const Options = struct { sources: []FileSource, }; -step: Step, -builder: *std.Build, +/// The step to depend on. +step: *Step, -/// Resulting binary -out_path: GeneratedFile, +/// The output file from the libtool run. +output: FileSource, -/// See Options -name: []const u8, -out_name: []const u8, -sources: []FileSource, +/// Run libtool against a list of library files to combine into a single +/// static library. +pub fn create(b: *std.Build, opts: Options) *LibtoolStep { + const self = b.allocator.create(LibtoolStep) catch @panic("OOM"); + + const run_step = RunStep.create(b, b.fmt("libtool {s}", .{opts.name})); + run_step.addArgs(&.{ "libtool", "-static", "-o" }); + const output = run_step.addOutputFileArg(opts.out_name); + for (opts.sources) |source| run_step.addFileSourceArg(source); -pub fn create(builder: *std.Build, opts: Options) *LibtoolStep { - const self = builder.allocator.create(LibtoolStep) catch @panic("OOM"); self.* = .{ - .step = Step.init(.custom, builder.fmt("lipo {s}", .{opts.name}), builder.allocator, make), - .builder = builder, - .name = opts.name, - .out_path = .{ .step = &self.step }, - .out_name = opts.out_name, - .sources = opts.sources, + .step = &run_step.step, + .output = output, }; + return self; } - -fn make(step: *Step) !void { - const self = @fieldParentPtr(LibtoolStep, "step", step); - - // We use a RunStep here to ease our configuration. - const run = std.build.RunStep.create(self.builder, self.builder.fmt( - "libtool {s}", - .{self.name}, - )); - run.addArgs(&.{ - "libtool", - "-static", - "-o", - }); - try run.argv.append(.{ .output = .{ - .generated_file = &self.out_path, - .basename = self.out_name, - } }); - for (self.sources) |source| run.addFileSourceArg(source); - try run.step.make(); -} diff --git a/src/build/LipoStep.zig b/src/build/LipoStep.zig index f073ca15f..49187f307 100644 --- a/src/build/LipoStep.zig +++ b/src/build/LipoStep.zig @@ -4,8 +4,8 @@ const LipoStep = @This(); const std = @import("std"); const Step = std.build.Step; +const RunStep = std.build.RunStep; const FileSource = std.build.FileSource; -const GeneratedFile = std.build.GeneratedFile; pub const Options = struct { /// The name of the xcframework to create. @@ -19,46 +19,24 @@ pub const Options = struct { input_b: FileSource, }; -step: Step, -builder: *std.build.Builder, +step: *Step, /// Resulting binary -out_path: GeneratedFile, +output: FileSource, -/// See Options -name: []const u8, -out_name: []const u8, -input_a: FileSource, -input_b: FileSource, +pub fn create(b: *std.Build, opts: Options) *LipoStep { + const self = b.allocator.create(LipoStep) catch @panic("OOM"); + + const run_step = RunStep.create(b, b.fmt("lipo {s}", .{opts.name})); + run_step.addArgs(&.{ "lipo", "-create", "-output" }); + const output = run_step.addOutputFileArg(opts.out_name); + run_step.addFileSourceArg(opts.input_a); + run_step.addFileSourceArg(opts.input_b); -pub fn create(builder: *std.build.Builder, opts: Options) *LipoStep { - const self = builder.allocator.create(LipoStep) catch @panic("OOM"); self.* = .{ - .step = Step.init(.custom, builder.fmt("lipo {s}", .{opts.name}), builder.allocator, make), - .builder = builder, - .name = opts.name, - .out_path = .{ .step = &self.step }, - .out_name = opts.out_name, - .input_a = opts.input_a, - .input_b = opts.input_b, + .step = &run_step.step, + .output = output, }; + return self; } - -fn make(step: *Step) !void { - const self = @fieldParentPtr(LipoStep, "step", step); - - // We use a RunStep here to ease our configuration. - const run = std.build.RunStep.create(self.builder, self.builder.fmt( - "lipo {s}", - .{self.name}, - )); - run.addArgs(&.{ "lipo", "-create", "-output" }); - try run.argv.append(.{ .output = .{ - .generated_file = &self.out_path, - .basename = self.out_name, - } }); - run.addFileSourceArg(self.input_a); - run.addFileSourceArg(self.input_b); - try run.step.make(); -} diff --git a/src/build/XCFrameworkStep.zig b/src/build/XCFrameworkStep.zig index 120609bf4..36fdbebc6 100644 --- a/src/build/XCFrameworkStep.zig +++ b/src/build/XCFrameworkStep.zig @@ -5,7 +5,8 @@ const XCFrameworkStep = @This(); const std = @import("std"); const Step = std.build.Step; -const GeneratedFile = std.build.GeneratedFile; +const RunStep = std.build.RunStep; +const FileSource = std.build.FileSource; pub const Options = struct { /// The name of the xcframework to create. @@ -21,61 +22,38 @@ pub const Options = struct { headers: std.build.FileSource, }; -step: Step, -builder: *std.build.Builder, +step: *Step, -/// See Options -name: []const u8, -out_path: []const u8, -library: std.build.FileSource, -headers: std.build.FileSource, +pub fn create(b: *std.Build, opts: Options) *XCFrameworkStep { + const self = b.allocator.create(XCFrameworkStep) catch @panic("OOM"); -pub fn create(builder: *std.build.Builder, opts: Options) *XCFrameworkStep { - const self = builder.allocator.create(XCFrameworkStep) catch @panic("OOM"); - self.* = .{ - .step = Step.init(.custom, builder.fmt( - "xcframework {s}", - .{opts.name}, - ), builder.allocator, make), - .builder = builder, - .name = opts.name, - .out_path = opts.out_path, - .library = opts.library, - .headers = opts.headers, + // We have to delete the old xcframework first since we're writing + // to a static path. + const run_delete = run: { + const run = RunStep.create(b, b.fmt("xcframework delete {s}", .{opts.name})); + run.has_side_effects = true; + run.addArgs(&.{ "rm", "-rf", opts.out_path }); + break :run run; }; - return self; -} -fn make(step: *Step) !void { - const self = @fieldParentPtr(XCFrameworkStep, "step", step); - - // TODO: use the zig cache system when it is in the stdlib - // https://github.com/ziglang/zig/pull/14571 - const output_path = self.out_path; - - // We use a RunStep here to ease our configuration. - { - const run = std.build.RunStep.create(self.builder, self.builder.fmt( - "xcframework delete {s}", - .{self.name}, - )); - run.condition = .always; - run.addArgs(&.{ "rm", "-rf", output_path }); - try run.step.make(); - } - { - const run = std.build.RunStep.create(self.builder, self.builder.fmt( - "xcframework {s}", - .{self.name}, - )); - run.condition = .always; + // Then we run xcodebuild to create the framework. + const run_create = run: { + const run = RunStep.create(b, b.fmt("xcframework {s}", .{opts.name})); + run.has_side_effects = true; run.addArgs(&.{ "xcodebuild", "-create-xcframework" }); run.addArg("-library"); - run.addFileSourceArg(self.library); + run.addFileSourceArg(opts.library); run.addArg("-headers"); - run.addFileSourceArg(self.headers); + run.addFileSourceArg(opts.headers); run.addArg("-output"); - run.addArg(output_path); - try run.step.make(); - } + run.addArg(opts.out_path); + break :run run; + }; + run_create.step.dependOn(&run_delete.step); + + self.* = .{ + .step = &run_create.step, + }; + + return self; } From 7a6826ee0e43e08c849e8e69ff2a983a9efd5d17 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Fri, 24 Mar 2023 19:51:17 -0700 Subject: [PATCH 2/6] build: run tests --- build.zig | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/build.zig b/build.zig index cde0978a8..548ed621f 100644 --- a/build.zig +++ b/build.zig @@ -479,7 +479,9 @@ pub fn build(b: *std.build.Builder) !void { main_test.setFilter(test_filter); _ = try addDeps(b, main_test, true); main_test.addOptions("build_options", exe_options); - test_step.dependOn(&main_test.step); + + const test_run = main_test.run(); + test_step.dependOn(&test_run.step); } // Named package dependencies don't have their tests run by reference, @@ -492,22 +494,21 @@ pub fn build(b: *std.build.Builder) !void { if (std.mem.eql(u8, name, "build_options")) continue; if (std.mem.eql(u8, name, "glfw")) continue; - var buf: [256]u8 = undefined; - var test_run = b.addTest(.{ - .name = try std.fmt.bufPrint(&buf, "{s}-test", .{name}), + const test_exe = b.addTest(.{ + .name = b.fmt("{s}-test", .{name}), .root_source_file = module.source_file, .target = target, }); + if (emit_test_exe) test_exe.install(); - _ = try addDeps(b, test_run, true); + _ = try addDeps(b, test_exe, true); // if (pkg.dependencies) |children| { // test_.packages = std.ArrayList(std.build.Pkg).init(b.allocator); // try test_.packages.appendSlice(children); // } + const test_run = test_exe.run(); test_step.dependOn(&test_run.step); - - if (emit_test_exe) test_run.install(); } } } From 19106575eb2f4fcc4dadfd32ff4fa683867c1ac3 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Fri, 24 Mar 2023 20:17:25 -0700 Subject: [PATCH 3/6] use new build struct --- build.zig | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/build.zig b/build.zig index 548ed621f..9faf504cb 100644 --- a/build.zig +++ b/build.zig @@ -1,7 +1,6 @@ const std = @import("std"); const builtin = @import("builtin"); const fs = std.fs; -const Builder = std.build.Builder; const LibExeObjStep = std.build.LibExeObjStep; const RunStep = std.build.RunStep; const apprt = @import("src/apprt.zig"); @@ -54,7 +53,7 @@ var flatpak: bool = false; var app_runtime: apprt.Runtime = .none; var font_backend: font.Backend = .freetype; -pub fn build(b: *std.build.Builder) !void { +pub fn build(b: *std.Build) !void { const optimize = b.standardOptimizeOption(.{}); const target = target: { var result = b.standardTargetOptions(.{}); @@ -518,7 +517,7 @@ const FileSourceList = std.ArrayList(std.build.FileSource); /// Adds and links all of the primary dependencies for the exe. fn addDeps( - b: *std.build.Builder, + b: *std.Build, step: *std.build.LibExeObjStep, static: bool, ) !FileSourceList { @@ -735,7 +734,7 @@ fn addDeps( } fn benchSteps( - b: *std.build.Builder, + b: *std.Build, target: std.zig.CrossTarget, optimize: std.builtin.Mode, install: bool, @@ -777,7 +776,7 @@ fn benchSteps( } fn conformanceSteps( - b: *std.build.Builder, + b: *std.Build, target: std.zig.CrossTarget, optimize: std.builtin.Mode, ) !std.StringHashMap(*LibExeObjStep) { From 3be86cc79aa8de2ed7d88cefe79562e0c7687dd8 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Fri, 24 Mar 2023 20:19:04 -0700 Subject: [PATCH 4/6] don't prefix warning --- build.zig | 2 -- 1 file changed, 2 deletions(-) diff --git a/build.zig b/build.zig index 9faf504cb..14b05343c 100644 --- a/build.zig +++ b/build.zig @@ -223,8 +223,6 @@ pub fn build(b: *std.Build) !void { if (is_nixos and env.get("IN_NIX_SHELL") == null) { try exe.step.addError( "\x1b[" ++ color_map.get("yellow").? ++ - "\x1b[" ++ color_map.get("b").? ++ - "WARNING: " ++ "\x1b[" ++ color_map.get("d").? ++ \\Detected building on and for NixOS outside of the Nix shell enviornment. \\ From 9016222da93fa6486fc53dd9a89e71e127bf843d Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Fri, 24 Mar 2023 20:25:54 -0700 Subject: [PATCH 5/6] update zig bins for flatpak --- com.mitchellh.ghostty.yml | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/com.mitchellh.ghostty.yml b/com.mitchellh.ghostty.yml index 9982abac3..a3d525da3 100644 --- a/com.mitchellh.ghostty.yml +++ b/com.mitchellh.ghostty.yml @@ -33,13 +33,13 @@ modules: - cp -r ./* /app/tmp/zig sources: - type: archive - url: https://ziglang.org/builds/zig-linux-x86_64-0.11.0-dev.1817+f6c934677.tar.xz - sha256: 5c401d9839f83355059b375ad15fda6ab11ef120ce497c02485ee3105678c06c + url: https://ziglang.org/builds/zig-linux-x86_64-0.11.0-dev.2247+38ee46dda.tar.xz + sha256: 2eef41bd10ccfa4c31c00fd3c9373275c430eeefaf78ba275da129a4e8e2a25e only-arches: - x86_64 - type: archive - url: https://ziglang.org/builds/zig-linux-aarch64-0.11.0-dev.1817+f6c934677.tar.xz - sha256: 9c986f35f1c393aed91b252bed35a1f65a8c7b9f5d1a8d8a4fe41f84b4de1759 + url: https://ziglang.org/builds/zig-linux-aarch64-0.11.0-dev.2247+38ee46dda.tar.xz + sha256: f7b1a46a8904c30154c9e902784744c8c4f67d9a2ad204fe53e78115b6c04d72 only-arches: - aarch64 From 86c5b04ff932e9a7554fdb38cf945ee64fbc9c20 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Fri, 24 Mar 2023 20:33:36 -0700 Subject: [PATCH 6/6] copy mac binary must depend on binary --- build.zig | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/build.zig b/build.zig index 14b05343c..7d75e51d8 100644 --- a/build.zig +++ b/build.zig @@ -291,8 +291,11 @@ pub fn build(b: *std.Build) !void { // App (Mac) if (target.isDarwin()) { - const bin_path = try std.fmt.allocPrint(b.allocator, "{s}/bin/ghostty", .{b.install_path}); - b.installFile(bin_path, "Ghostty.app/Contents/MacOS/ghostty"); + const bin_install = b.addInstallFile( + .{ .generated = &exe.output_path_source }, + "Ghostty.app/Contents/MacOS/ghostty", + ); + b.getInstallStep().dependOn(&bin_install.step); b.installFile("dist/macos/Info.plist", "Ghostty.app/Contents/Info.plist"); b.installFile("dist/macos/Ghostty.icns", "Ghostty.app/Contents/Resources/Ghostty.icns"); }