Skip to content

Commit ac001fc

Browse files
committed
first build.zig refactoring
Use more modules
1 parent c70b387 commit ac001fc

File tree

1 file changed

+42
-40
lines changed

1 file changed

+42
-40
lines changed

build.zig

Lines changed: 42 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,13 @@ pub fn build(b: *std.Build) !void {
4848
// compile and install
4949
const bench = b.addExecutable(.{
5050
.name = "zig-js-runtime-bench",
51-
.root_source_file = .{ .path = "src/main_bench.zig" },
51+
.root_source_file = b.path("src/main_bench.zig"),
5252
.single_threaded = true,
5353
.target = target,
5454
.optimize = mode,
5555
});
5656

57-
try common(bench, mode, options);
57+
try common(b, &bench.root_module, options);
5858
if (mode == .ReleaseSafe) {
5959
// remove debug info
6060
// TODO: check if mandatory in release-safe
@@ -78,11 +78,11 @@ pub fn build(b: *std.Build) !void {
7878
// compile and install
7979
const shell = b.addExecutable(.{
8080
.name = "zig-js-runtime-shell",
81-
.root_source_file = .{ .path = "src/main_shell.zig" },
81+
.root_source_file = b.path("src/main_shell.zig"),
8282
.target = target,
8383
.optimize = mode,
8484
});
85-
try common(shell, mode, options);
85+
try common(b, &shell.root_module, options);
8686
try pkgs.add_shell(shell);
8787
if (mode == .ReleaseSafe) {
8888
// remove debug info
@@ -107,13 +107,13 @@ pub fn build(b: *std.Build) !void {
107107

108108
// compile
109109
const tests = b.addTest(.{
110-
.root_source_file = .{ .path = "src/run_tests.zig" },
110+
.root_source_file = b.path("src/run_tests.zig"),
111111
.target = target,
112112
.optimize = mode,
113113
});
114-
try common(tests, mode, options);
114+
try common(b, &tests.root_module, options);
115115
tests.root_module.single_threaded = true;
116-
tests.test_runner = .{ .path = "src/test_runner.zig" };
116+
tests.test_runner = b.path("src/test_runner.zig");
117117
const run_tests = b.addRunArtifact(tests);
118118

119119
// step
@@ -149,15 +149,15 @@ pub fn buildOptions(b: *std.Build) !Options {
149149
}
150150

151151
fn common(
152-
step: *std.Build.Step.Compile,
153-
mode: std.builtin.Mode,
152+
b: *std.Build,
153+
m: *std.Build.Module,
154154
options: Options,
155155
) !void {
156-
step.root_module.addOptions("jsruntime_build_options", options.opts);
157-
step.root_module.addImport("tigerbeetle-io", pkgs.tigerbeetle_io(step));
156+
m.addOptions("jsruntime_build_options", options.opts);
157+
m.addImport("tigerbeetle-io", pkgs.tigerbeetle_io(b));
158158
if (options.engine == .v8) {
159-
try pkgs.v8(step, mode);
160-
step.root_module.addImport("v8", pkgs.zig_v8(step));
159+
try pkgs.v8(m);
160+
m.addImport("v8", pkgs.zig_v8(b));
161161
}
162162
}
163163

@@ -167,37 +167,35 @@ pub fn packages(comptime vendor_path: []const u8) type {
167167

168168
const vendor = vendor_path ++ "vendor";
169169

170-
fn tigerbeetle_io(step: *std.Build.Step.Compile) *std.Build.Module {
171-
return step.step.owner.createModule(.{
172-
.root_source_file = .{ .path = vendor ++ "/tigerbeetle-io/io.zig" },
170+
fn tigerbeetle_io(b: *std.Build) *std.Build.Module {
171+
return b.createModule(.{
172+
.root_source_file = b.path(vendor ++ "/tigerbeetle-io/io.zig"),
173173
});
174174
}
175175

176-
fn zig_v8(step: *std.Build.Step.Compile) *std.Build.Module {
177-
const mod = step.step.owner.createModule(.{
178-
.root_source_file = .{ .path = vendor ++ "/zig-v8/src/v8.zig" },
176+
fn zig_v8(b: *std.Build) *std.Build.Module {
177+
const mod = b.createModule(.{
178+
.root_source_file = b.path(vendor ++ "/zig-v8/src/v8.zig"),
179179
.link_libc = false,
180180
.link_libcpp = false,
181181
});
182182

183-
mod.addIncludePath(.{ .path = vendor ++ "/zig-v8/src" });
183+
mod.addIncludePath(b.path(vendor ++ "/zig-v8/src"));
184184

185185
return mod;
186186
}
187187

188-
fn v8(step: *std.Build.Step.Compile, mode: std.builtin.Mode) !void {
189-
const mode_str: []const u8 = if (mode == .Debug) "debug" else "release";
190-
// step.linkLibC(); // TODO: do we need to link libc?
191-
188+
fn v8(mod: *std.Build.Module) !void {
189+
const mode_str: []const u8 = if (mod.optimize.? == .Debug) "debug" else "release";
192190
// FIXME: we are tied to native v8 builds, currently:
193191
// - aarch64-macos
194192
// - x86_64-linux
195-
const os = step.root_module.resolved_target.?.result.os.tag;
196-
const arch = step.root_module.resolved_target.?.result.cpu.arch;
193+
const os = mod.resolved_target.?.result.os.tag;
194+
const arch = mod.resolved_target.?.result.cpu.arch;
197195
switch (os) {
198196
.linux => blk: {
199197
// TODO: why do we need it? It should be linked already when we built v8
200-
step.linkLibCpp();
198+
mod.link_libcpp = true;
201199
break :blk;
202200
},
203201
.macos => blk: {
@@ -211,15 +209,15 @@ pub fn packages(comptime vendor_path: []const u8) type {
211209
}
212210

213211
const lib_path = try std.fmt.allocPrint(
214-
step.step.owner.allocator,
212+
mod.owner.allocator,
215213
"{s}vendor/v8/{s}-{s}/{s}/libc_v8.a",
216214
.{ vendor_path, @tagName(arch), @tagName(os), mode_str },
217215
);
218-
step.addObjectFile(.{ .path = lib_path });
216+
mod.addObjectFile(mod.owner.path(lib_path));
219217
}
220218

221219
pub fn add_shell(step: *std.Build.Step.Compile) !void {
222-
step.addIncludePath(.{ .path = vendor ++ "/linenoise-mob" });
220+
step.addIncludePath(step.root_module.owner.path(vendor ++ "/linenoise-mob"));
223221
const lib = step.step.owner.addStaticLibrary(.{
224222
.name = "linenoise",
225223
.target = step.root_module.resolved_target.?,
@@ -229,27 +227,31 @@ pub fn packages(comptime vendor_path: []const u8) type {
229227
// TODO: use mode to add debug/release flags
230228
const cflags = &.{};
231229
lib.addCSourceFile(.{
232-
.file = .{ .path = vendor ++ "/linenoise-mob/linenoise.c" },
230+
.file = step.root_module.owner.path(vendor ++ "/linenoise-mob/linenoise.c"),
233231
.flags = cflags,
234232
});
235233
step.linkLibrary(lib);
236234
}
237235

238-
pub fn add(
239-
step: *std.Build.Step.Compile,
236+
pub fn module(
237+
b: *std.Build,
240238
options: Options,
241-
) !void {
242-
const jsruntime_mod = step.step.owner.createModule(.{
243-
.root_source_file = .{ .path = vendor_path ++ "/src/api.zig" },
239+
mode: std.builtin.Mode,
240+
target: std.Build.ResolvedTarget,
241+
) !*std.Build.Module {
242+
const mod = b.createModule(.{
243+
.root_source_file = b.path(vendor_path ++ "/src/api.zig"),
244+
.optimize = mode,
245+
.target = target,
244246
.imports = &[_]std.Build.Module.Import{
245247
.{ .name = "jsruntime_build_options", .module = options.opts.createModule() },
246-
.{ .name = "tigerbeetle-io", .module = Self.tigerbeetle_io(step) },
247-
.{ .name = "v8", .module = Self.zig_v8(step) },
248+
.{ .name = "tigerbeetle-io", .module = Self.tigerbeetle_io(b) },
249+
.{ .name = "v8", .module = Self.zig_v8(b) },
248250
},
249251
});
250-
try Self.v8(step, step.root_module.optimize.?);
252+
try Self.v8(mod);
251253

252-
step.root_module.addImport("jsruntime", jsruntime_mod);
254+
return mod;
253255
}
254256
};
255257
}

0 commit comments

Comments
 (0)