Skip to content

Commit 536b97d

Browse files
authored
Merge pull request #172 from lightpanda-io/fix-caching-again
build: improve artifact freshness tracking
2 parents eddea9d + b84c47b commit 536b97d

1 file changed

Lines changed: 41 additions & 51 deletions

File tree

build.zig

Lines changed: 41 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,17 @@ const V8_VERSION: []const u8 = "14.0.365.4";
44

55
const LazyPath = std.Build.LazyPath;
66

7+
// Shim files staged into the V8 checkout. `src` is relative to this repo,
8+
// `dest` is relative to the V8 source tree root. Any file added here is
9+
// automatically copied during bootstrap and watched by the freshness check.
10+
const StagedFile = struct { src: []const u8, dest: []const u8 };
11+
const staged_files = [_]StagedFile{
12+
.{ .src = "src/binding.cpp", .dest = "binding.cpp" },
13+
.{ .src = "src/inspector.h", .dest = "inspector.h" },
14+
.{ .src = "build-tools/BUILD.gn", .dest = "zig/BUILD.gn" },
15+
.{ .src = "build-tools/.gn", .dest = "zig/.gn" },
16+
};
17+
718
fn getDepotToolExePath(b: *std.Build, depot_tools_dir: []const u8, executable: []const u8) []const u8 {
819
return b.fmt("{s}/{s}", .{ depot_tools_dir, executable });
920
}
@@ -235,9 +246,6 @@ fn bootstrapV8(
235246

236247
if (!needs_full_bootstrap) {
237248
const needs_source_update = blk: {
238-
if (needs_full_bootstrap) break :blk false;
239-
240-
// Check if marker exists
241249
const marker_stat = std.fs.cwd().statFile(marker_file) catch break :blk true;
242250
const marker_mtime = marker_stat.mtime;
243251

@@ -282,32 +290,19 @@ fn bootstrapV8(
282290
};
283291

284292
if (needs_source_update) {
285-
// Just needs the bindings to be updated, will reuse cached dir.
286293
std.debug.print("Updating source files in V8 bootstrap\n", .{});
287294

288-
// Just copy the updated files
289-
const copy_binding = b.addSystemCommand(&.{"cp"});
290-
copy_binding.addFileArg(b.path("src/binding.cpp"));
291-
copy_binding.addArg(b.fmt("{s}/binding.cpp", .{v8_dir}));
292-
293-
const copy_inspector = b.addSystemCommand(&.{"cp"});
294-
copy_inspector.addFileArg(b.path("src/inspector.h"));
295-
copy_inspector.addArg(b.fmt("{s}/inspector.h", .{v8_dir}));
296-
copy_inspector.step.dependOn(&copy_binding.step);
297-
298-
const copy_build_gn = b.addSystemCommand(&.{"cp"});
299-
copy_build_gn.addFileArg(b.path("build-tools/BUILD.gn"));
300-
copy_build_gn.addArg(b.fmt("{s}/zig/BUILD.gn", .{v8_dir}));
301-
copy_build_gn.step.dependOn(&copy_inspector.step);
302-
303-
const copy_gn = b.addSystemCommand(&.{"cp"});
304-
copy_gn.addFileArg(b.path("build-tools/.gn"));
305-
copy_gn.addArg(b.fmt("{s}/zig/.gn", .{v8_dir}));
306-
copy_gn.step.dependOn(&copy_build_gn.step);
295+
var prev_step: *std.Build.Step = undefined;
296+
for (staged_files, 0..) |f, i| {
297+
const cp = b.addSystemCommand(&.{"cp"});
298+
cp.addFileArg(b.path(f.src));
299+
cp.addArg(b.fmt("{s}/{s}", .{ v8_dir, f.dest }));
300+
if (i > 0) cp.step.dependOn(prev_step);
301+
prev_step = &cp.step;
302+
}
307303

308-
// Touch marker to update timestamp
309304
const update_marker = b.addSystemCommand(&.{ "touch", marker_file });
310-
update_marker.step.dependOn(&copy_gn.step);
305+
update_marker.step.dependOn(prev_step);
311306

312307
return .{ .step = &update_marker.step, .needs_build = true };
313308
} else {
@@ -342,34 +337,23 @@ fn bootstrapV8(
342337
write_gclient.addArg(b.fmt("echo '{s}' > {s}/.gclient", .{ gclient_content, v8_dir }));
343338
write_gclient.step.dependOn(&mkdir.step);
344339

345-
// Copy binding files
346-
const copy_binding = b.addSystemCommand(&.{"cp"});
347-
copy_binding.addFileArg(b.path("src/binding.cpp"));
348-
copy_binding.addArg(b.fmt("{s}/binding.cpp", .{v8_dir}));
349-
copy_binding.step.dependOn(&write_gclient.step);
350-
351-
const copy_inspector = b.addSystemCommand(&.{"cp"});
352-
copy_inspector.addFileArg(b.path("src/inspector.h"));
353-
copy_inspector.addArg(b.fmt("{s}/inspector.h", .{v8_dir}));
354-
copy_inspector.step.dependOn(&copy_binding.step);
355-
356-
// Create zig directory and copy build files
357-
const mkdir_zig = b.addSystemCommand(&.{ "mkdir", "-p", b.fmt("{s}/zig", .{v8_dir}) });
358-
mkdir_zig.step.dependOn(&copy_inspector.step);
359-
360-
const copy_build_gn = b.addSystemCommand(&.{"cp"});
361-
copy_build_gn.addFileArg(b.path("build-tools/BUILD.gn"));
362-
copy_build_gn.addArg(b.fmt("{s}/zig/BUILD.gn", .{v8_dir}));
363-
copy_build_gn.step.dependOn(&mkdir_zig.step);
364-
365-
const copy_gn = b.addSystemCommand(&.{"cp"});
366-
copy_gn.addFileArg(b.path("build-tools/.gn"));
367-
copy_gn.addArg(b.fmt("{s}/zig/.gn", .{v8_dir}));
368-
copy_gn.step.dependOn(&copy_build_gn.step);
340+
var prev_stage_step: *std.Build.Step = &write_gclient.step;
341+
for (staged_files) |f| {
342+
if (std.fs.path.dirname(f.dest)) |parent| {
343+
const mkdir_parent = b.addSystemCommand(&.{ "mkdir", "-p", b.fmt("{s}/{s}", .{ v8_dir, parent }) });
344+
mkdir_parent.step.dependOn(prev_stage_step);
345+
prev_stage_step = &mkdir_parent.step;
346+
}
347+
const cp = b.addSystemCommand(&.{"cp"});
348+
cp.addFileArg(b.path(f.src));
349+
cp.addArg(b.fmt("{s}/{s}", .{ v8_dir, f.dest }));
350+
cp.step.dependOn(prev_stage_step);
351+
prev_stage_step = &cp.step;
352+
}
369353

370354
// Create gclient_args.gni
371355
const mkdir_build_config = b.addSystemCommand(&.{ "mkdir", "-p", b.fmt("{s}/build/config", .{v8_dir}) });
372-
mkdir_build_config.step.dependOn(&copy_gn.step);
356+
mkdir_build_config.step.dependOn(prev_stage_step);
373357

374358
const write_gclient_args = b.addSystemCommand(&.{ "sh", "-c" });
375359
write_gclient_args.addArg(b.fmt("echo '# Generated by Zig build system' > {s}/build/config/gclient_args.gni", .{v8_dir}));
@@ -426,8 +410,14 @@ fn buildV8(
426410
const libc_v8_path = b.fmt("{s}/obj/zig/libc_v8.a", .{out_dir});
427411
const full_libc_v8_lazy_path = v8_dir_lazy_path.path(b, libc_v8_path);
428412

413+
// Bootstrap marker is shared across profiles, so compare staged sources
414+
// directly against this profile's libc_v8.a to track freshness per-profile.
429415
const needs_build = bootstrapped_v8.needs_build or blk: {
430-
std.fs.cwd().access(b.fmt("{s}/{s}", .{ v8_dir, libc_v8_path }), .{}) catch break :blk true;
416+
const lib_stat = std.fs.cwd().statFile(b.fmt("{s}/{s}", .{ v8_dir, libc_v8_path })) catch break :blk true;
417+
for (staged_files) |f| {
418+
const src_stat = std.fs.cwd().statFile(b.fmt("{s}/{s}", .{ v8_dir, f.dest })) catch break :blk true;
419+
if (src_stat.mtime > lib_stat.mtime) break :blk true;
420+
}
431421
break :blk false;
432422
};
433423

0 commit comments

Comments
 (0)