-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.zig
270 lines (236 loc) · 8.86 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
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
const std = @import("std");
const CrossTarget = std.zig.CrossTarget;
const assert = std.debug.assert;
pub fn build(b: *std.Build) void {
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});
// -------------------------- Obtain all dependencies of this project --------------------------
const deps = dependencies(b, target, optimize);
// -------------------------- Creates a step for static library build --------------------------
const lib = b.addStaticLibrary(.{
.name = "rdb",
.root_source_file = b.path("src/root.zig"),
.target = target,
.optimize = optimize,
});
Dep.apply_deps(deps, &lib.root_module);
b.installArtifact(lib);
// -------------------------- Creates a step for shared library build --------------------------
const shared_lib = b.addStaticLibrary(.{
.name = "rdb",
.root_source_file = b.path("src/root.zig"),
.target = target,
.optimize = optimize,
});
Dep.apply_deps(deps, &shared_lib.root_module);
b.installArtifact(shared_lib);
// -------------------------- Fast check if compiles --------------------------
check(b, b.step("check", "check if compiles"), .{ .deps = deps, .target = target, .optimize = optimize });
// -------------------------- test run or build --------------------------
run_tests(b, b.step("test", "run tests"), b.step("test:build", "build test"), .{
.deps = deps,
.target = target,
.optimize = optimize,
});
// -------------------------- bindings --------------------------
bindings_go(b, b.step("bindings:go", "build libs for go"));
cli(b, target, lib, deps);
}
/// fast compile check for easy development
fn check(
b: *std.Build,
step_check: *std.Build.Step,
opts: struct {
deps: []const ?Dep,
target: std.Build.ResolvedTarget,
optimize: std.builtin.OptimizeMode,
},
) void {
const lib = b.addStaticLibrary(.{
.name = "rdb",
.root_source_file = b.path("src/root.zig"),
.target = opts.target,
.optimize = opts.optimize,
});
const test_lib = b.addTest(.{
.name = "rdb",
.root_source_file = b.path("src/root.zig"),
.target = opts.target,
.optimize = opts.optimize,
});
Dep.apply_deps(opts.deps, &lib.root_module);
Dep.apply_deps(opts.deps, &test_lib.root_module);
step_check.dependOn(&lib.step);
step_check.dependOn(&test_lib.step);
}
fn run_tests(
b: *std.Build,
run_test_step: *std.Build.Step,
build_test_step: *std.Build.Step,
opts: struct {
deps: []const ?Dep,
target: std.Build.ResolvedTarget,
optimize: std.builtin.OptimizeMode,
},
) void {
const test_filters = b.option([]const []const u8, "test-filter", "Skip tests that do not match any filter") orelse &[0][]const u8{};
const sanitize_thread = b.option(bool, "sanitize_thread", "Enable Thread Sanitizer");
const lib_unit_tests = b.addTest(.{
.root_source_file = b.path("src/root.zig"),
.test_runner = b.path("test_runner.zig"),
.target = opts.target,
.optimize = opts.optimize,
.filters = test_filters,
.sanitize_thread = if (sanitize_thread) |sanitize| sanitize else false,
});
Dep.apply_deps(opts.deps, &lib_unit_tests.root_module);
const run_lib_unit_tests = b.addRunArtifact(lib_unit_tests);
run_lib_unit_tests.has_side_effects = true;
run_test_step.dependOn(&run_lib_unit_tests.step);
build_test_step.dependOn(&b.addInstallArtifact(lib_unit_tests, .{}).step);
}
fn bench(
b: *std.Build,
rdb: *std.Build.Step.Compile,
deps: []const ?Dep,
target: std.Build.ResolvedTarget,
optimize: std.builtin.OptimizeMode,
) void {
const exe = b.addExecutable(.{
.name = "bench",
.root_source_file = b.path("test/bench.zig"),
.optimize = optimize,
.target = target,
});
Dep.apply_deps(deps, &exe.root_module);
exe.root_module.addImport(rdb.name, &rdb.root_module);
b.installArtifact(exe);
}
const Dep = struct {
dep: *std.Build.Dependency,
import_name: []const u8,
module_name: []const u8,
pub fn init(dep: *std.Build.Dependency, name: []const u8) Dep {
return Dep{ .dep = dep, .import_name = name, .module_name = name };
}
pub fn init_with_mod_name(dep: *std.Build.Dependency, import_name: []const u8, mod_name: []const u8) Dep {
return Dep{ .dep = dep, .import_name = import_name, .module_name = mod_name };
}
pub fn apply(self: Dep, mod: *std.Build.Module) void {
mod.addImport(self.import_name, self.dep.module(self.module_name));
}
pub fn apply_deps(deps: []const ?Dep, mod: *std.Build.Module) void {
for (deps) |ndep| {
if (ndep) |dep| {
dep.apply(mod);
}
}
}
};
inline fn dependencies(
b: *std.Build,
target: std.Build.ResolvedTarget,
optimize: std.builtin.OptimizeMode,
) []const ?Dep {
const jdz_allocator = b.dependency("jdz_allocator", .{
.target = target,
.optimize = optimize,
});
const zart = b.dependency("zart", .{
.target = target,
.optimize = optimize,
});
const zli = b.dependency("zli", .{ .target = target, .optimize = optimize });
return &[_]?Dep{
Dep.init(jdz_allocator, "jdz_allocator"),
Dep.init(zart, "zart"),
Dep.init(zli, "zli"),
};
}
const platforms = .{
.{ "x86_64-linux", "x86_64_v3+aes" },
.{ "aarch64-linux", "baseline+aes+neon" },
.{ "x86_64-windows", "x86_64_v3+aes" },
// .{ "x86_64-macos", "x86_64_v3+aes" },
// .{ "aarch64-macos", "baseline+aes+neon" },
};
fn bindings_go(b: *std.Build, step_bindings_go: *std.Build.Step) void {
const header = CopyFile.create(b, b.path("rdb.h"), "bindings/go/pkg/native/rdb.h");
step_bindings_go.dependOn(&header.step);
inline for (platforms) |platform| {
const name = platform[0];
const cross_target = CrossTarget.parse(.{
.arch_os_abi = name,
.cpu_features = platform[1],
}) catch unreachable;
const resolved_target = b.resolveTargetQuery(cross_target);
const lib = b.addStaticLibrary(.{
.name = "rdb",
.root_source_file = b.path("src/root.zig"),
.target = resolved_target,
.optimize = .ReleaseFast,
});
const deps = dependencies(b, resolved_target, .ReleaseFast);
Dep.apply_deps(deps, &lib.root_module);
step_bindings_go.dependOn(&b.addInstallFile(
lib.getEmittedBin(),
b.pathJoin(&.{ "../bindings/go/pkg/native/", name, lib.out_filename }),
).step);
}
}
const CopyFile = struct {
step: std.Build.Step,
generated_file: std.Build.GeneratedFile,
source: std.Build.LazyPath,
destination: []const u8,
pub fn create(b: *std.Build, source: std.Build.LazyPath, destination: []const u8) *CopyFile {
const ret = b.allocator.create(CopyFile) catch @panic("OOM");
ret.* = CopyFile{
.source = source,
.destination = destination,
.step = std.Build.Step.init(.{
.id = .custom,
.owner = b,
.name = b.fmt("copy {s} to {s}", .{ source.getDisplayName(), std.fs.path.basename(destination) }),
.makeFn = make,
}),
.generated_file = .{ .step = &ret.step },
};
ret.source.addStepDependencies(&ret.step);
return ret;
}
fn make(step: *std.Build.Step, _: std.Progress.Node) !void {
const b = step.owner;
const self: *CopyFile = @fieldParentPtr("step", step);
const source_path = self.source.getPath2(b, step);
_ = try std.fs.Dir.updateFile(
b.build_root.handle,
source_path,
b.build_root.handle,
self.destination,
.{},
);
self.generated_file.path = self.destination;
}
};
fn cli(b: *std.Build, target: std.Build.ResolvedTarget, rdb: *std.Build.Step.Compile, deps: []const ?Dep) void {
const cli_artifact = b.addExecutable(.{
.name = "rdbcli",
.root_source_file = b.path("cli/main.zig"),
.optimize = .ReleaseFast,
.target = target,
});
Dep.apply_deps(deps, &cli_artifact.root_module);
const cli_run_artifact = b.addRunArtifact(cli_artifact);
// This allows the user to pass arguments to the application in the build
// command itself, like this: `zig build run -- arg1 arg2 etc`
if (b.args) |args| {
cli_run_artifact.addArgs(args);
}
cli_artifact.root_module.addImport("rdb", &rdb.root_module);
const cli_step = b.step("cli", "install the cli");
const cli_run = b.step("cli:run", "run the cli");
cli_step.dependOn(&b.addInstallArtifact(cli_artifact, .{}).step);
cli_run.dependOn(&cli_run_artifact.step);
//
}