-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.zig
More file actions
134 lines (119 loc) · 4.44 KB
/
build.zig
File metadata and controls
134 lines (119 loc) · 4.44 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
/// To run the tests, run:
/// `zig build test`
///
/// To automatically build as source files are edited:
/// `zig build --watch --summary all`
///
const std = @import("std");
const c_source_files = [_][:0]const u8{
"src/lndir.c",
"src/string_list.c",
"src/dir_walker.c",
};
const c_main_file = "src/main.c";
pub fn build(b: *std.Build) void {
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});
const debug = b.option(bool, "debug", "enable debug printing") orelse (optimize == .Debug);
const version = read_version(b.allocator) catch @panic("Error reading version from build.zig.zon");
const test_filters: []const []const u8 = b.option(
[]const []const u8,
"test-filter",
"Skip tests that do not match any of the specified filters",
) orelse &.{};
// c exe
const exe_mod = b.createModule(.{
.target = target,
.optimize = optimize,
});
const WerrorIfDebug = if (optimize == .Debug) "-Werror" else "";
exe_mod.addCSourceFiles(.{
.files = &c_source_files,
.flags = &.{WerrorIfDebug},
});
exe_mod.addCSourceFile(.{ .file = b.path(c_main_file) });
exe_mod.link_libc = true;
exe_mod.linkSystemLibrary("uring", .{ .preferred_link_mode = .dynamic });
exe_mod.addCMacro("VERSION", version);
if (debug) exe_mod.addCMacro("DEBUG", "");
const c_exe = b.addExecutable(.{
.name = "lndir",
.root_module = exe_mod,
});
b.installArtifact(c_exe);
// run steps
const run_cmd = b.addRunArtifact(c_exe);
run_cmd.step.dependOn(b.getInstallStep());
if (b.args) |args| {
run_cmd.addArgs(args);
}
const run_step = b.step("run", "Run the app");
run_step.dependOn(&run_cmd.step);
// test steps
const test_mod = b.createModule(.{
.root_source_file = b.path("src/tests.zig"),
.target = target,
.optimize = optimize,
});
test_mod.addCSourceFiles(.{ .files = &c_source_files });
test_mod.link_libc = true;
test_mod.linkSystemLibrary("uring", .{ .preferred_link_mode = .dynamic });
const string_mod = translate_c_file(b, optimize, target, "src/string_list.h");
const lndir_mod = translate_c_file(b, optimize, target, "src/lndir.h");
test_mod.addImport("string_list", string_mod);
test_mod.addImport("lndir", lndir_mod);
const exe_unit_tests = b.addTest(.{ .root_module = test_mod, .filters = test_filters });
const run_exe_unit_tests = b.addRunArtifact(exe_unit_tests);
const test_step = b.step("test", "Run unit tests");
test_step.dependOn(&run_exe_unit_tests.step);
}
/// Reads the version string from build.zig.zon
/// @import would also work, but this function checks the string at runtime,
/// so modifying the version doesn't trigger recompiling the build script
fn read_version(allocator: std.mem.Allocator) ![]const u8 {
var threaded = std.Io.Threaded.init_single_threaded;
const io = threaded.io();
const cwd = std.Io.Dir.cwd();
const zon_file = try std.Io.Dir.openFile(cwd, io, "build.zig.zon", .{ .mode = .read_only });
var zon_buf: [4096]u8 = undefined;
var reader = zon_file.reader(io, &zon_buf);
const interface = &reader.interface;
const zon_data = try interface.allocRemainingAlignedSentinel(allocator, .unlimited, .@"8", 0);
const ZonVersion = struct {
version: []const u8,
};
const zon_version = try std.zon.parse.fromSliceAlloc(
ZonVersion,
allocator,
zon_data,
null,
.{ .ignore_unknown_fields = true },
);
const version = blk: {
var buffer: std.ArrayList(u8) = .empty;
defer buffer.deinit(allocator);
try buffer.ensureTotalCapacity(allocator, 32);
buffer.appendAssumeCapacity('\"');
buffer.appendSliceAssumeCapacity(zon_version.version);
buffer.appendSliceAssumeCapacity(" (z)\"");
break :blk (try buffer.toOwnedSlice(allocator));
};
return version;
}
fn translate_c_file(
b: *std.Build,
opt: std.builtin.OptimizeMode,
target: std.Build.ResolvedTarget,
path: []const u8,
) *std.Build.Module {
const translate_c = b.addTranslateC(.{
.root_source_file = b.path(path),
.optimize = opt,
.target = target,
});
translate_c.addIncludePath(b.path("src/"));
const translate_c_mod = translate_c.createModule();
translate_c.link_libc = true;
translate_c_mod.link_libc = true;
return translate_c_mod;
}