-
Notifications
You must be signed in to change notification settings - Fork 782
/
Copy pathGhosttyExe.zig
120 lines (102 loc) · 3.82 KB
/
GhosttyExe.zig
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
const Ghostty = @This();
const std = @import("std");
const Config = @import("Config.zig");
const SharedDeps = @import("SharedDeps.zig");
/// The primary Ghostty executable.
exe: *std.Build.Step.Compile,
/// The install step for the executable.
install_step: *std.Build.Step.InstallArtifact,
pub fn init(b: *std.Build, cfg: *const Config, deps: *const SharedDeps) !Ghostty {
const exe: *std.Build.Step.Compile = b.addExecutable(.{
.name = "ghostty",
.root_source_file = b.path("src/main.zig"),
.target = cfg.target,
.optimize = cfg.optimize,
.strip = cfg.strip,
});
const install_step = b.addInstallArtifact(exe, .{});
// Set PIE if requested
if (cfg.pie) exe.pie = true;
// Add the shared dependencies
_ = try deps.add(exe);
// Check for possible issues
try checkNixShell(exe, cfg);
// Patch our rpath if that option is specified.
if (cfg.patch_rpath) |rpath| {
if (rpath.len > 0) {
const run = std.Build.Step.Run.create(b, "patchelf rpath");
run.addArgs(&.{ "patchelf", "--set-rpath", rpath });
run.addArtifactArg(exe);
install_step.step.dependOn(&run.step);
}
}
// OS-specific
switch (cfg.target.result.os.tag) {
.windows => {
exe.subsystem = .Windows;
exe.addWin32ResourceFile(.{
.file = b.path("dist/windows/ghostty.rc"),
});
},
else => {},
}
return .{
.exe = exe,
.install_step = install_step,
};
}
/// Add the ghostty exe to the install target.
pub fn install(self: *const Ghostty) void {
const b = self.install_step.step.owner;
b.getInstallStep().dependOn(&self.install_step.step);
}
/// If we're in NixOS but not in the shell environment then we issue
/// a warning because the rpath may not be setup properly. This doesn't modify
/// our build in any way but addresses a common build-from-source issue
/// for a subset of users.
fn checkNixShell(exe: *std.Build.Step.Compile, cfg: *const Config) !void {
// Non-Linux doesn't have rpath issues.
if (cfg.target.result.os.tag != .linux) return;
// When cross-compiling, we don't need to worry about matching our
// Nix shell rpath since the resulting binary will be run on a
// separate system.
if (!cfg.target.query.isNativeCpu()) return;
if (!cfg.target.query.isNativeOs()) return;
// Verify we're in NixOS
std.fs.accessAbsolute("/etc/NIXOS", .{}) catch return;
// If we're in a nix shell, not a problem
if (cfg.env.get("IN_NIX_SHELL") != null) return;
try exe.step.addError(
"\x1b[" ++ color_map.get("yellow").? ++
"\x1b[" ++ color_map.get("d").? ++
\\Detected building on and for NixOS outside of the Nix shell environment.
\\
\\The resulting ghostty binary will likely fail on launch because it is
\\unable to dynamically load the windowing libs (X11, Wayland, etc.).
\\We highly recommend running only within the Nix build environment
\\and the resulting binary will be portable across your system.
\\
\\To run in the Nix build environment, use the following command.
\\Append any additional options like (`-Doptimize` flags). The resulting
\\binary will be in zig-out as usual.
\\
\\ nix develop -c zig build
\\
++
"\x1b[0m",
.{},
);
}
/// ANSI escape codes for colored log output
const color_map = std.StaticStringMap([]const u8).initComptime(.{
&.{ "black", "30m" },
&.{ "blue", "34m" },
&.{ "b", "1m" },
&.{ "d", "2m" },
&.{ "cyan", "36m" },
&.{ "green", "32m" },
&.{ "magenta", "35m" },
&.{ "red", "31m" },
&.{ "white", "37m" },
&.{ "yellow", "33m" },
});