Skip to content

Commit 76b1cd1

Browse files
authored
Merge pull request #162 from lightpanda-io/fix-caching-performance
build: optimize caching by skipping hashing and copying of libc_v8.a
2 parents c9299ec + d8e39dc commit 76b1cd1

File tree

1 file changed

+68
-53
lines changed

1 file changed

+68
-53
lines changed

build.zig

Lines changed: 68 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -87,22 +87,23 @@ pub fn build(b: *std.Build) !void {
8787

8888
const built_v8 = if (prebuilt_v8_path) |path| blk: {
8989
// Use prebuilt_v8 if available.
90-
const wf = b.addWriteFiles();
91-
_ = wf.addCopyFile(.{ .cwd_relative = path }, "libc_v8.a");
92-
break :blk wf;
90+
break :blk BuiltV8{
91+
.step = b.step("prebuilt_v8", "Use prebuilt v8"),
92+
.libc_v8_path = .{ .cwd_relative = path },
93+
};
9394
} else blk: {
9495
const bootstrapped_depot_tools = try bootstrapDepotTools(b, depot_tools_dir);
9596
const bootstrapped_v8 = try bootstrapV8(b, bootstrapped_depot_tools, v8_dir, depot_tools_dir);
9697

9798
const prepare_step = b.step("prepare-v8", "Prepare V8 source code");
98-
prepare_step.dependOn(&bootstrapped_v8.step);
99+
prepare_step.dependOn(bootstrapped_v8.step);
99100

100101
// Otherwise, go through build process.
101102
break :blk try buildV8(b, v8_dir, depot_tools_dir, bootstrapped_v8, target, gn_args);
102103
};
103104

104105
const build_step = b.step("build-v8", "Build v8");
105-
build_step.dependOn(&built_v8.step);
106+
build_step.dependOn(built_v8.step);
106107

107108
b.getInstallStep().dependOn(build_step);
108109

@@ -116,7 +117,7 @@ pub fn build(b: *std.Build) !void {
116117
});
117118
v8_module.addIncludePath(b.path("src"));
118119
v8_module.addImport("default_exports", build_opts.createModule());
119-
v8_module.addObjectFile(built_v8.getDirectory().path(b, "libc_v8.a"));
120+
v8_module.addObjectFile(built_v8.libc_v8_path);
120121

121122
switch (target.result.os.tag) {
122123
.macos => {
@@ -141,15 +142,7 @@ pub fn build(b: *std.Build) !void {
141142
});
142143
tests.root_module.addImport("default_exports", build_opts.createModule());
143144

144-
const release_dir = if (optimize == .Debug) "debug" else "release";
145-
const os = switch (target.result.os.tag) {
146-
.linux => "linux",
147-
.macos => "macos",
148-
.ios => "ios",
149-
else => return error.UnsupportedPlatform,
150-
};
151-
152-
tests.addObjectFile(b.path(b.fmt("v8/out/{s}/{s}/obj/zig/libc_v8.a", .{ os, release_dir })));
145+
tests.addObjectFile(built_v8.libc_v8_path);
153146
tests.addIncludePath(b.path("src"));
154147

155148
switch (target.result.os.tag) {
@@ -167,7 +160,12 @@ pub fn build(b: *std.Build) !void {
167160
}
168161
}
169162

170-
fn bootstrapDepotTools(b: *std.Build, depot_tools_dir: []const u8) !*std.Build.Step.Run {
163+
const V8BootstrapResult = struct {
164+
step: *std.Build.Step,
165+
needs_build: bool,
166+
};
167+
168+
fn bootstrapDepotTools(b: *std.Build, depot_tools_dir: []const u8) !*std.Build.Step {
171169
const depot_tools = b.dependency("depot_tools", .{});
172170
const marker_file = b.fmt("{s}/.bootstrap-complete", .{depot_tools_dir});
173171

@@ -179,7 +177,7 @@ fn bootstrapDepotTools(b: *std.Build, depot_tools_dir: []const u8) !*std.Build.S
179177
if (!needs_full_bootstrap) {
180178
std.debug.print("Using cached depot_tools bootstrap from {s}\n", .{depot_tools_dir});
181179
const noop = b.addSystemCommand(&.{"true"});
182-
return noop;
180+
return &noop.step;
183181
}
184182

185183
std.debug.print("Bootstrapping depot_tools {s} in {s} (this will take a while)...\n", .{ V8_VERSION, depot_tools_dir });
@@ -214,15 +212,15 @@ fn bootstrapDepotTools(b: *std.Build, depot_tools_dir: []const u8) !*std.Build.S
214212
const create_marker = b.addSystemCommand(&.{ "touch", marker_file });
215213
create_marker.step.dependOn(&ensure_bootstrap.step);
216214

217-
return create_marker;
215+
return &create_marker.step;
218216
}
219217

220218
fn bootstrapV8(
221219
b: *std.Build,
222-
bootstrapped_depot_tools: *std.Build.Step.Run,
220+
bootstrapped_depot_tools: *std.Build.Step,
223221
v8_dir: []const u8,
224222
depot_tools_dir: []const u8,
225-
) !*std.Build.Step.Run {
223+
) !V8BootstrapResult {
226224
const marker_file = b.fmt("{s}/.bootstrap-complete", .{v8_dir});
227225

228226
// Check if already bootstrapped
@@ -299,20 +297,20 @@ fn bootstrapV8(
299297
const update_marker = b.addSystemCommand(&.{ "touch", marker_file });
300298
update_marker.step.dependOn(&copy_gn.step);
301299

302-
return update_marker;
300+
return .{ .step = &update_marker.step, .needs_build = true };
303301
} else {
304302
// Cached V8 is still valid.
305303
std.debug.print("Using cached V8 bootstrap from {s}\n", .{v8_dir});
306304
const noop = b.addSystemCommand(&.{"true"});
307-
return noop;
305+
return .{ .step = &noop.step, .needs_build = false };
308306
}
309307
}
310308

311309
std.debug.print("Bootstrapping V8 {s} in {s} (this will take a while)...\n", .{ V8_VERSION, v8_dir });
312310

313311
// Create cache directory
314312
const mkdir = b.addSystemCommand(&.{ "mkdir", "-p", v8_dir });
315-
mkdir.step.dependOn(&bootstrapped_depot_tools.step);
313+
mkdir.step.dependOn(bootstrapped_depot_tools);
316314

317315
// Write .gclient file
318316
const gclient_content = b.fmt(
@@ -387,49 +385,66 @@ fn bootstrapV8(
387385
const create_marker = b.addSystemCommand(&.{ "touch", marker_file });
388386
create_marker.step.dependOn(&clang_update.step);
389387

390-
return create_marker;
388+
return .{ .step = &create_marker.step, .needs_build = true };
391389
}
392390

391+
const BuiltV8 = struct {
392+
step: *std.Build.Step,
393+
libc_v8_path: LazyPath,
394+
};
395+
393396
fn buildV8(
394397
b: *std.Build,
395398
v8_dir: []const u8,
396399
depot_tools_dir: []const u8,
397-
bootstrapped_v8: *std.Build.Step.Run,
400+
bootstrapped_v8: V8BootstrapResult,
398401
target: std.Build.ResolvedTarget,
399402
gn_args: GnArgs,
400-
) !*std.Build.Step.WriteFile {
403+
) !BuiltV8 {
401404
const v8_dir_lazy_path: LazyPath = .{ .cwd_relative = v8_dir };
402405

403406
const args_string = try gn_args.asString(b, target);
404407
const out_dir = b.fmt("out/{s}/{s}", .{ @tagName(target.result.os.tag), if (gn_args.is_debug) "debug" else "release" });
408+
const libc_v8_path = b.fmt("{s}/obj/zig/libc_v8.a", .{out_dir});
409+
const full_libc_v8_lazy_path = v8_dir_lazy_path.path(b, libc_v8_path);
405410

406-
const gn_run = b.addSystemCommand(&.{
407-
getDepotToolExePath(b, depot_tools_dir, "gn"),
408-
"--root=.",
409-
"--root-target=//zig",
410-
"--dotfile=zig/.gn",
411-
"gen",
412-
out_dir,
413-
b.fmt("--args={s}", .{args_string}),
414-
});
415-
gn_run.setCwd(v8_dir_lazy_path);
416-
addDepotToolsToPath(gn_run, depot_tools_dir);
417-
gn_run.step.dependOn(&bootstrapped_v8.step);
418-
419-
const ninja_run = b.addSystemCommand(&.{
420-
getDepotToolExePath(b, depot_tools_dir, "autoninja"),
421-
"-C",
422-
out_dir,
423-
"c_v8",
424-
});
425-
ninja_run.setCwd(v8_dir_lazy_path);
426-
addDepotToolsToPath(ninja_run, depot_tools_dir);
427-
ninja_run.step.dependOn(&gn_run.step);
411+
const needs_build = bootstrapped_v8.needs_build or blk: {
412+
std.fs.cwd().access(b.fmt("{s}/{s}", .{ v8_dir, libc_v8_path }), .{}) catch break :blk true;
413+
break :blk false;
414+
};
428415

429-
const wf = b.addWriteFiles();
430-
wf.step.dependOn(&ninja_run.step);
431-
const libc_v8_path = b.fmt("{s}/obj/zig/libc_v8.a", .{out_dir});
432-
_ = wf.addCopyFile(v8_dir_lazy_path.path(b, libc_v8_path), "libc_v8.a");
416+
const final_step = b.step("build_v8_core", "Build V8 core");
417+
418+
if (needs_build) {
419+
const gn_run = b.addSystemCommand(&.{
420+
getDepotToolExePath(b, depot_tools_dir, "gn"),
421+
"--root=.",
422+
"--root-target=//zig",
423+
"--dotfile=zig/.gn",
424+
"gen",
425+
out_dir,
426+
b.fmt("--args={s}", .{args_string}),
427+
});
428+
gn_run.setCwd(v8_dir_lazy_path);
429+
addDepotToolsToPath(gn_run, depot_tools_dir);
430+
gn_run.step.dependOn(bootstrapped_v8.step);
431+
432+
const ninja_run = b.addSystemCommand(&.{
433+
getDepotToolExePath(b, depot_tools_dir, "autoninja"),
434+
"-C",
435+
out_dir,
436+
"c_v8",
437+
});
438+
ninja_run.setCwd(v8_dir_lazy_path);
439+
addDepotToolsToPath(ninja_run, depot_tools_dir);
440+
ninja_run.step.dependOn(&gn_run.step);
441+
final_step.dependOn(&ninja_run.step);
442+
} else {
443+
final_step.dependOn(bootstrapped_v8.step);
444+
}
433445

434-
return wf;
446+
return BuiltV8{
447+
.step = final_step,
448+
.libc_v8_path = full_libc_v8_lazy_path,
449+
};
435450
}

0 commit comments

Comments
 (0)