-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.zig
More file actions
193 lines (165 loc) · 6.35 KB
/
build.zig
File metadata and controls
193 lines (165 loc) · 6.35 KB
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
const std = @import("std");
pub fn build(b: *std.Build) void {
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});
const version_str = "1.0.0";
const app_name = "myapp";
const binary_name = app_name;
// Attempt to inject current git commit hash, fall back to "unknown"
var git_commit_buf: [32]u8 = undefined;
const git_commit = blk: {
const child_res = std.process.Child.run(.{
.allocator = b.allocator,
.argv = &.{ "git", "rev-parse", "--short", "HEAD" },
.max_output_bytes = 64,
}) catch break :blk "unknown";
if (child_res.stdout.len == 0) break :blk "unknown";
const trimmed = std.mem.trim(u8, child_res.stdout, "\n");
if (trimmed.len > git_commit_buf.len) break :blk "unknown";
@memcpy(git_commit_buf[0..trimmed.len], trimmed);
break :blk git_commit_buf[0..trimmed.len];
};
// Build options
// Build options – TUI disabled by default so fresh clones build everywhere
const enable_tui = b.option(bool, "enable-tui", "Enable TUI support with ncurses (default: false)") orelse false;
const ncurses_prefix = b.option([]const u8, "ncurses-prefix", "Override ncurses prefix (e.g. /usr/local/opt/ncurses)");
const aro_dep = b.dependency("aro", .{
.target = target,
.optimize = optimize,
});
const exe = b.addExecutable(.{
.name = binary_name,
.root_module = b.createModule(.{
.root_source_file = null,
.target = target,
.optimize = optimize,
}),
});
// Ensure local headers are discoverable regardless of include style
exe.addIncludePath(.{ .cwd_relative = "src" });
// Base source files
const base_sources = [_][]const u8{
"src/main.c",
"src/core/error.c",
"src/core/config.c",
"src/utils/logging.c",
"src/utils/memory.c",
"src/utils/colors.c",
"src/io/input.c",
"src/io/output.c",
"src/cli/help.c",
"src/cli/args.c",
};
// Base flags
const base_flags = [_][]const u8{
"-Wall",
"-Wextra",
"-std=c23",
"-D_GNU_SOURCE",
};
// Add base sources
for (base_sources) |src| {
var flags = std.ArrayList([]const u8).init(b.allocator);
flags.appendSlice(&base_flags) catch @panic("OOM");
flags.append(b.fmt("-DAPP_VERSION=\"{s}\"", .{version_str})) catch @panic("OOM");
flags.append(b.fmt("-DAPP_NAME=\"{s}\"", .{app_name})) catch @panic("OOM");
flags.append(b.fmt("-DAPP_GIT_COMMIT=\"{s}\"", .{git_commit})) catch @panic("OOM");
flags.append("-DAPP_BUILD_DATE=\"reproducible\"") catch @panic("OOM");
if (enable_tui) {
flags.append("-DENABLE_TUI=1") catch @panic("OOM");
}
exe.addCSourceFile(.{
.file = b.path(src),
.flags = flags.items,
});
flags.deinit();
}
// Add TUI source if enabled
if (enable_tui) {
var flags = std.ArrayList([]const u8).init(b.allocator);
flags.appendSlice(&base_flags) catch @panic("OOM");
flags.append(b.fmt("-DAPP_VERSION=\"{s}\"", .{version_str})) catch @panic("OOM");
flags.append(b.fmt("-DAPP_NAME=\"{s}\"", .{app_name})) catch @panic("OOM");
flags.append(b.fmt("-DAPP_GIT_COMMIT=\"{s}\"", .{git_commit})) catch @panic("OOM");
flags.append("-DAPP_BUILD_DATE=\"reproducible\"") catch @panic("OOM");
flags.append("-DENABLE_TUI=1") catch @panic("OOM");
// TUI sources
const tui_sources = [_][]const u8{
"src/tui/tui.c",
"src/tui/tui_progress.c",
};
for (tui_sources) |src| {
exe.addCSourceFile(.{
.file = b.path(src),
.flags = flags.items,
});
}
flags.deinit();
}
exe.linkLibC();
// NCurses / PDCurses configuration (only if TUI is enabled)
if (enable_tui) {
// Allow caller to override ncurses install prefix explicitly.
if (ncurses_prefix) |pref| {
exe.addIncludePath(.{ .cwd_relative = b.fmt("{s}/include", .{pref}) });
exe.addLibraryPath(.{ .cwd_relative = b.fmt("{s}/lib", .{pref}) });
}
if (target.result.os.tag == .windows) {
// Prefer PDCurses on Windows
exe.linkSystemLibrary("pdcurses");
} else {
exe.linkSystemLibrary("ncurses");
}
}
b.installArtifact(exe);
// Install Aro headers
b.installDirectory(.{
.source_dir = aro_dep.path("include"),
.install_dir = .prefix,
.install_subdir = "include",
});
// Run command
const run_cmd = b.addRunArtifact(exe);
run_cmd.step.dependOn(b.getInstallStep());
if (b.args) |args| {
run_cmd.addArgs(args);
}
const run_step = b.step("run", "Run the application");
run_step.dependOn(&run_cmd.step);
// Test command
const test_exe = b.addTest(.{
.root_module = b.createModule(.{
.root_source_file = b.path("test/main.zig"),
.target = target,
.optimize = optimize,
}),
});
const test_cmd = b.addRunArtifact(test_exe);
test_cmd.step.dependOn(b.getInstallStep());
const test_step = b.step("test", "Run test suite");
test_step.dependOn(&test_cmd.step);
// Clean command – cross-platform
const clean_cmd = if (target.result.os.tag == .windows)
b.addSystemCommand(&.{ "cmd", "/C", "rmdir", "/S", "/Q", "zig-out", "&&", "rmdir", "/S", "/Q", ".zig-cache" })
else
b.addSystemCommand(&.{ "rm", "-rf", "zig-out", ".zig-cache" });
const clean_step = b.step("clean", "Clean build artifacts");
clean_step.dependOn(&clean_cmd.step);
// Format commands
const fmt_step = b.step("fmt", "Format all source files");
const fmt = b.addFmt(.{
.paths = &.{ "build.zig", "src", "test" },
.check = false,
});
fmt_step.dependOn(&fmt.step);
const fmt_check_step = b.step("fmt-check", "Check source formatting");
const fmt_check = b.addFmt(.{
.paths = &.{ "build.zig", "src", "test" },
.check = true,
});
fmt_check_step.dependOn(&fmt_check.step);
// Check command
const check_step = b.step("check", "Run all checks");
check_step.dependOn(fmt_check_step);
check_step.dependOn(test_step);
}