Skip to content

Commit b65e47d

Browse files
committed
working locally i think
1 parent 147a920 commit b65e47d

File tree

2 files changed

+84
-36
lines changed

2 files changed

+84
-36
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
/.zig-cache/
2+
/.lp-cache/
23
/zig-out/
34
/v8/
45
result

build.zig

Lines changed: 83 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,13 @@ const V8_VERSION: []const u8 = "14.0.365.4";
44

55
const LazyPath = std.Build.LazyPath;
66

7-
fn addDepotToolsToPath(b: *std.Build, step: *std.Build.Step.Run, depot_tools: *std.Build.Dependency) void {
8-
const old_path = step.getEnvMap().get("PATH") orelse "";
9-
const depot_tools_abs_path = b.pathFromRoot(depot_tools.path("").getPath(b));
10-
const new_path = b.fmt("{s}:{s}", .{ depot_tools_abs_path, old_path });
11-
step.setEnvironmentVariable("PATH", new_path);
7+
fn getDepotToolExePath(b: *std.Build, depot_tools_dir: []const u8, executable: []const u8) []const u8 {
8+
return b.fmt("{s}/{s}", .{ b.pathFromRoot(depot_tools_dir), executable });
9+
}
10+
11+
fn addDepotToolsToPath(b: *std.Build, step: *std.Build.Step.Run, depot_tools_dir: []const u8) void {
12+
const depot_tools_absolute_path = b.pathFromRoot(depot_tools_dir);
13+
step.addPathDir(depot_tools_absolute_path);
1214
}
1315

1416
pub fn build(b: *std.Build) !void {
@@ -22,25 +24,30 @@ pub fn build(b: *std.Build) !void {
2224
b.option(bool, "inspector_subtype", "Export default valueSubtype and descriptionForValueSubtype") orelse true,
2325
);
2426

25-
const cache_root = b.option([]const u8, "v8_cache_root", "Root directory for V8 cache") orelse
26-
(b.cache_root.path orelse ".zig-cache");
27+
const cache_root = b.option([]const u8, "cache_root", "Root directory for the V8 and depot_tools cache") orelse ".lp-cache";
28+
std.fs.cwd().access(cache_root, .{}) catch {
29+
try std.fs.cwd().makeDir(cache_root);
30+
};
31+
2732
const prebuilt_v8_path = b.option([]const u8, "prebuilt_v8_path", "Path to prebuilt libc_v8.a");
2833

2934
const v8_dir = b.fmt("{s}/v8-{s}", .{ cache_root, V8_VERSION });
35+
const depot_tools_dir = b.fmt("{s}/depot_tools-{s}", .{ cache_root, V8_VERSION });
3036

3137
const built_v8 = if (prebuilt_v8_path) |path| blk: {
3238
// Use prebuilt_v8 if available.
3339
const wf = b.addWriteFiles();
3440
_ = wf.addCopyFile(.{ .cwd_relative = path }, "libc_v8.a");
3541
break :blk wf;
3642
} else blk: {
37-
const bootstrapped_v8 = try bootstrapV8(b, v8_dir);
43+
const bootstrapped_depot_tools = try bootstrapDepotTools(b, depot_tools_dir);
44+
const bootstrapped_v8 = try bootstrapV8(b, bootstrapped_depot_tools, v8_dir, depot_tools_dir);
3845

3946
const prepare_step = b.step("prepare-v8", "Prepare V8 source code");
4047
prepare_step.dependOn(&bootstrapped_v8.step);
4148

4249
// Otherwise, go through build process.
43-
break :blk try buildV8(b, v8_dir, bootstrapped_v8, target, optimize);
50+
break :blk try buildV8(b, v8_dir, depot_tools_dir, bootstrapped_v8, target, optimize);
4451
};
4552

4653
const build_step = b.step("build-v8", "Build v8");
@@ -109,8 +116,46 @@ pub fn build(b: *std.Build) !void {
109116
}
110117
}
111118

112-
fn bootstrapV8(b: *std.Build, v8_dir: []const u8) !*std.Build.Step.Run {
119+
fn bootstrapDepotTools(b: *std.Build, depot_tools_dir: []const u8) !*std.Build.Step.Run {
113120
const depot_tools = b.dependency("depot_tools", .{});
121+
const marker_file = b.fmt("{s}/.bootstrap-complete", .{depot_tools_dir});
122+
123+
const needs_full_bootstrap = blk: {
124+
std.fs.cwd().access(marker_file, .{}) catch break :blk true;
125+
break :blk false;
126+
};
127+
128+
if (!needs_full_bootstrap) {
129+
std.debug.print("Using cached depot_tools bootstrap from {s}\n", .{depot_tools_dir});
130+
const noop = b.addSystemCommand(&.{"true"});
131+
return noop;
132+
}
133+
134+
std.debug.print("Bootstrapping depot_tools {s} in {s} (this will take a while)...\n", .{ V8_VERSION, depot_tools_dir });
135+
136+
const copy_depot_tools = b.addSystemCommand(&.{ "cp", "-r" });
137+
copy_depot_tools.addDirectoryArg(depot_tools.path(""));
138+
copy_depot_tools.addArg(depot_tools_dir);
139+
140+
const ensure_bootstrap = b.addSystemCommand(&.{
141+
getDepotToolExePath(b, depot_tools_dir, "ensure_bootstrap"),
142+
});
143+
ensure_bootstrap.setCwd(.{ .cwd_relative = depot_tools_dir });
144+
addDepotToolsToPath(b, ensure_bootstrap, depot_tools_dir);
145+
ensure_bootstrap.step.dependOn(&copy_depot_tools.step);
146+
147+
const create_marker = b.addSystemCommand(&.{ "touch", marker_file });
148+
create_marker.step.dependOn(&ensure_bootstrap.step);
149+
150+
return create_marker;
151+
}
152+
153+
fn bootstrapV8(
154+
b: *std.Build,
155+
bootstrapped_depot_tools: *std.Build.Step.Run,
156+
v8_dir: []const u8,
157+
depot_tools_dir: []const u8,
158+
) !*std.Build.Step.Run {
114159
const marker_file = b.fmt("{s}/.bootstrap-complete", .{v8_dir});
115160

116161
// Check if already bootstrapped
@@ -200,6 +245,7 @@ fn bootstrapV8(b: *std.Build, v8_dir: []const u8) !*std.Build.Step.Run {
200245

201246
// Create cache directory
202247
const mkdir = b.addSystemCommand(&.{ "mkdir", "-p", v8_dir });
248+
mkdir.step.dependOn(&bootstrapped_depot_tools.step);
203249

204250
// Write .gclient file
205251
const gclient_content = b.fmt(
@@ -252,23 +298,22 @@ fn bootstrapV8(b: *std.Build, v8_dir: []const u8) !*std.Build.Step.Run {
252298
write_gclient_args.addArg(b.fmt("echo '# Generated by Zig build system' > {s}/build/config/gclient_args.gni", .{v8_dir}));
253299
write_gclient_args.step.dependOn(&mkdir_build_config.step);
254300

255-
var depot_tools_init = std.Build.Step.Run.create(b, "initialize depot_tools");
256-
depot_tools_init.addFileArg(depot_tools.path("ensure_bootstrap"));
257-
addDepotToolsToPath(b, depot_tools_init, depot_tools);
258-
depot_tools_init.step.dependOn(&write_gclient_args.step);
259-
260301
// Run gclient sync
261-
var gclient_sync = std.Build.Step.Run.create(b, "run gclient sync");
262-
gclient_sync.addFileArg(depot_tools.path("gclient"));
263-
gclient_sync.addArgs(&.{"sync"});
302+
const gclient_sync = b.addSystemCommand(&.{
303+
getDepotToolExePath(b, depot_tools_dir, "gclient"),
304+
"sync",
305+
});
264306
gclient_sync.setCwd(.{ .cwd_relative = v8_dir });
265-
addDepotToolsToPath(b, gclient_sync, depot_tools);
266-
gclient_sync.step.dependOn(&depot_tools_init.step);
307+
addDepotToolsToPath(b, gclient_sync, depot_tools_dir);
308+
gclient_sync.step.dependOn(&write_gclient_args.step);
267309

268310
// Run clang update
269-
const clang_update = b.addSystemCommand(&.{ "python3", "tools/clang/scripts/update.py" });
311+
const clang_update = b.addSystemCommand(&.{
312+
getDepotToolExePath(b, depot_tools_dir, "python-bin/python3"),
313+
"tools/clang/scripts/update.py",
314+
});
270315
clang_update.setCwd(.{ .cwd_relative = v8_dir });
271-
addDepotToolsToPath(b, clang_update, depot_tools);
316+
addDepotToolsToPath(b, clang_update, depot_tools_dir);
272317
clang_update.step.dependOn(&gclient_sync.step);
273318

274319
// Create marker file
@@ -280,13 +325,13 @@ fn bootstrapV8(b: *std.Build, v8_dir: []const u8) !*std.Build.Step.Run {
280325

281326
fn buildV8(
282327
b: *std.Build,
283-
v8_cache: []const u8,
328+
v8_dir: []const u8,
329+
depot_tools_dir: []const u8,
284330
bootstrapped_v8: *std.Build.Step.Run,
285331
target: std.Build.ResolvedTarget,
286332
optimize: std.builtin.OptimizeMode,
287333
) !*std.Build.Step.WriteFile {
288-
const depot_tools = b.dependency("depot_tools", .{});
289-
const v8_dir: LazyPath = .{ .cwd_relative = v8_cache };
334+
const v8_dir_lazy_path: LazyPath = .{ .cwd_relative = v8_dir };
290335

291336
const allocator = b.allocator;
292337

@@ -326,31 +371,33 @@ fn buildV8(
326371

327372
const out_dir = b.fmt("out/{s}/{s}", .{ @tagName(tag), if (is_debug) "debug" else "release" });
328373

329-
var gn_run = std.Build.Step.Run.create(b, "run gn");
330-
gn_run.addFileArg(depot_tools.path("gn"));
331-
gn_run.addArgs(&.{
374+
const gn_run = b.addSystemCommand(&.{
375+
getDepotToolExePath(b, depot_tools_dir, "gn"),
332376
"--root=.",
333377
"--root-target=//zig",
334378
"--dotfile=zig/.gn",
335379
"gen",
336380
out_dir,
337381
b.fmt("--args={s}", .{gn_args.items}),
338382
});
339-
gn_run.setCwd(v8_dir);
340-
addDepotToolsToPath(b, gn_run, depot_tools);
383+
gn_run.setCwd(v8_dir_lazy_path);
384+
addDepotToolsToPath(b, gn_run, depot_tools_dir);
341385
gn_run.step.dependOn(&bootstrapped_v8.step);
342386

343-
var ninja_run = std.Build.Step.Run.create(b, "run ninja");
344-
ninja_run.addFileArg(depot_tools.path("ninja"));
345-
ninja_run.addArgs(&.{ "-C", out_dir, "c_v8" });
346-
ninja_run.setCwd(v8_dir);
347-
addDepotToolsToPath(b, ninja_run, depot_tools);
387+
const ninja_run = b.addSystemCommand(&.{
388+
getDepotToolExePath(b, depot_tools_dir, "ninja"),
389+
"-C",
390+
out_dir,
391+
"c_v8",
392+
});
393+
ninja_run.setCwd(v8_dir_lazy_path);
394+
addDepotToolsToPath(b, ninja_run, depot_tools_dir);
348395
ninja_run.step.dependOn(&gn_run.step);
349396

350397
const wf = b.addWriteFiles();
351398
wf.step.dependOn(&ninja_run.step);
352399
const libc_v8_path = b.fmt("{s}/obj/zig/libc_v8.a", .{out_dir});
353-
_ = wf.addCopyFile(v8_dir.path(b, libc_v8_path), "libc_v8.a");
400+
_ = wf.addCopyFile(v8_dir_lazy_path.path(b, libc_v8_path), "libc_v8.a");
354401

355402
return wf;
356403
}

0 commit comments

Comments
 (0)