-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.zig
121 lines (101 loc) · 3.45 KB
/
build.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
121
const std = @import("std");
pub fn build(b: *std.Build) void {
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});
const name = "toyskkserv";
const deps = &[_]Dep{
.{ .name = "zig-cli" },
.{ .name = "zon_get_fields" },
.{ .name = "percent_encoding" },
.{ .name = "jdz_allocator" },
.{ .name = "network" },
.{ .name = "chroma" },
.{ .name = "temp" },
.{
.name = "euc-jis-2004-zig",
.module = "euc-jis-2004",
},
.{
.name = "btree-zig",
.module = "btree_c_zig",
.link = "btree-zig",
},
};
const test_deps = &[_]Dep{
.{ .name = "protest" },
};
prepareExe(name, b, target, optimize, deps);
const combined = deps ++ test_deps;
prepareTestExe(b, target, optimize, combined);
}
fn getBuildOptions(b: *std.Build) *std.Build.Step.Options {
const options = b.addOptions();
const version = b.run(&[_][]const u8{
"git",
"describe",
"--tags",
"--abbrev=0",
});
options.addOption([]const u8, "version", std.mem.trim(u8, version, " \n"));
const commit = b.run(&[_][]const u8{
"git",
"rev-parse",
"HEAD",
})[0..8];
options.addOption([]const u8, "commit", std.mem.trim(u8, commit, " \n"));
return options;
}
fn prepareExe(name: []const u8, b: *std.Build, target: std.Build.ResolvedTarget, optimize: std.builtin.OptimizeMode, deps: []const Dep) void {
const exe = b.addExecutable(.{
.name = name,
.root_source_file = b.path("src/main.zig"),
.target = target,
.optimize = optimize,
});
const opts = getBuildOptions(b);
opts.addOption([]const u8, "name", name);
exe.root_module.addOptions("build_options", opts);
for (deps) |d| {
const module = d.module orelse d.name;
const dep = b.dependency(d.name, .{ .target = target, .optimize = optimize });
const mod = dep.module(module);
exe.root_module.addImport(d.name, mod);
if (d.link) |l| {
exe.linkLibrary(dep.artifact(l));
}
}
b.installArtifact(exe);
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 app");
run_step.dependOn(&run_cmd.step);
}
fn prepareTestExe(b: *std.Build, target: std.Build.ResolvedTarget, optimize: std.builtin.OptimizeMode, deps: []const Dep) void {
const exe = b.addTest(.{
.root_source_file = b.path("src/test.zig"),
.target = target,
.optimize = optimize,
});
const protest_mod = b.dependency("protest", .{ .target = target, .optimize = optimize }).module("protest");
exe.root_module.addImport("protest", protest_mod);
for (deps) |d| {
const module = d.module orelse d.name;
const dep = b.dependency(d.name, .{ .target = target, .optimize = optimize });
const mod = dep.module(module);
exe.root_module.addImport(d.name, mod);
if (d.link) |l| {
exe.linkLibrary(dep.artifact(l));
}
}
const run_exe_unit_tests = b.addRunArtifact(exe);
const test_step = b.step("test", "Run unit tests");
test_step.dependOn(&run_exe_unit_tests.step);
}
const Dep = struct {
name: []const u8,
module: ?[]const u8 = null,
link: ?[]const u8 = null,
};