@@ -4,11 +4,12 @@ const V8_VERSION: []const u8 = "14.0.365.4";
44
55const 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}" , .{ depot_tools_dir , executable });
9+ }
10+
11+ fn addDepotToolsToPath (step : * std.Build.Step.Run , depot_tools_dir : []const u8 ) void {
12+ step .addPathDir (depot_tools_dir );
1213}
1314
1415pub fn build (b : * std.Build ) ! void {
@@ -22,25 +23,30 @@ pub fn build(b: *std.Build) !void {
2223 b .option (bool , "inspector_subtype" , "Export default valueSubtype and descriptionForValueSubtype" ) orelse true ,
2324 );
2425
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" );
26+ const cache_root = b .option ([]const u8 , "cache_root" , "Root directory for the V8 and depot_tools cache" ) orelse b .pathFromRoot (".lp-cache" );
27+ std .fs .cwd ().access (cache_root , .{}) catch {
28+ try std .fs .cwd ().makeDir (cache_root );
29+ };
30+
2731 const prebuilt_v8_path = b .option ([]const u8 , "prebuilt_v8_path" , "Path to prebuilt libc_v8.a" );
2832
2933 const v8_dir = b .fmt ("{s}/v8-{s}" , .{ cache_root , V8_VERSION });
34+ const depot_tools_dir = b .fmt ("{s}/depot_tools-{s}" , .{ cache_root , V8_VERSION });
3035
3136 const built_v8 = if (prebuilt_v8_path ) | path | blk : {
3237 // Use prebuilt_v8 if available.
3338 const wf = b .addWriteFiles ();
3439 _ = wf .addCopyFile (.{ .cwd_relative = path }, "libc_v8.a" );
3540 break :blk wf ;
3641 } else blk : {
37- const bootstrapped_v8 = try bootstrapV8 (b , v8_dir );
42+ const bootstrapped_depot_tools = try bootstrapDepotTools (b , depot_tools_dir );
43+ const bootstrapped_v8 = try bootstrapV8 (b , bootstrapped_depot_tools , v8_dir , depot_tools_dir );
3844
3945 const prepare_step = b .step ("prepare-v8" , "Prepare V8 source code" );
4046 prepare_step .dependOn (& bootstrapped_v8 .step );
4147
4248 // Otherwise, go through build process.
43- break :blk try buildV8 (b , v8_dir , bootstrapped_v8 , target , optimize );
49+ break :blk try buildV8 (b , v8_dir , depot_tools_dir , bootstrapped_v8 , target , optimize );
4450 };
4551
4652 const build_step = b .step ("build-v8" , "Build v8" );
@@ -109,8 +115,46 @@ pub fn build(b: *std.Build) !void {
109115 }
110116}
111117
112- fn bootstrapV8 (b : * std.Build , v8_dir : []const u8 ) ! * std.Build.Step.Run {
118+ fn bootstrapDepotTools (b : * std.Build , depot_tools_dir : []const u8 ) ! * std.Build.Step.Run {
113119 const depot_tools = b .dependency ("depot_tools" , .{});
120+ const marker_file = b .fmt ("{s}/.bootstrap-complete" , .{depot_tools_dir });
121+
122+ const needs_full_bootstrap = blk : {
123+ std .fs .cwd ().access (marker_file , .{}) catch break :blk true ;
124+ break :blk false ;
125+ };
126+
127+ if (! needs_full_bootstrap ) {
128+ std .debug .print ("Using cached depot_tools bootstrap from {s}\n " , .{depot_tools_dir });
129+ const noop = b .addSystemCommand (&.{"true" });
130+ return noop ;
131+ }
132+
133+ std .debug .print ("Bootstrapping depot_tools {s} in {s} (this will take a while)...\n " , .{ V8_VERSION , depot_tools_dir });
134+
135+ const copy_depot_tools = b .addSystemCommand (&.{ "cp" , "-r" });
136+ copy_depot_tools .addDirectoryArg (depot_tools .path ("" ));
137+ copy_depot_tools .addArg (depot_tools_dir );
138+
139+ const ensure_bootstrap = b .addSystemCommand (&.{
140+ getDepotToolExePath (b , depot_tools_dir , "ensure_bootstrap" ),
141+ });
142+ ensure_bootstrap .setCwd (.{ .cwd_relative = depot_tools_dir });
143+ addDepotToolsToPath (ensure_bootstrap , depot_tools_dir );
144+ ensure_bootstrap .step .dependOn (& copy_depot_tools .step );
145+
146+ const create_marker = b .addSystemCommand (&.{ "touch" , marker_file });
147+ create_marker .step .dependOn (& ensure_bootstrap .step );
148+
149+ return create_marker ;
150+ }
151+
152+ fn bootstrapV8 (
153+ b : * std.Build ,
154+ bootstrapped_depot_tools : * std.Build.Step.Run ,
155+ v8_dir : []const u8 ,
156+ depot_tools_dir : []const u8 ,
157+ ) ! * std.Build.Step.Run {
114158 const marker_file = b .fmt ("{s}/.bootstrap-complete" , .{v8_dir });
115159
116160 // Check if already bootstrapped
@@ -200,6 +244,7 @@ fn bootstrapV8(b: *std.Build, v8_dir: []const u8) !*std.Build.Step.Run {
200244
201245 // Create cache directory
202246 const mkdir = b .addSystemCommand (&.{ "mkdir" , "-p" , v8_dir });
247+ mkdir .step .dependOn (& bootstrapped_depot_tools .step );
203248
204249 // Write .gclient file
205250 const gclient_content = b .fmt (
@@ -253,16 +298,21 @@ fn bootstrapV8(b: *std.Build, v8_dir: []const u8) !*std.Build.Step.Run {
253298 write_gclient_args .step .dependOn (& mkdir_build_config .step );
254299
255300 // Run gclient sync
256- var gclient_sync = std .Build .Step .Run .create (b , "run gclient sync" );
257- gclient_sync .addFileArg (depot_tools .path ("gclient" ));
258- gclient_sync .addArgs (&.{"sync" });
301+ const gclient_sync = b .addSystemCommand (&.{
302+ getDepotToolExePath (b , depot_tools_dir , "gclient" ),
303+ "sync" ,
304+ });
259305 gclient_sync .setCwd (.{ .cwd_relative = v8_dir });
260- addDepotToolsToPath (b , gclient_sync , depot_tools );
306+ addDepotToolsToPath (gclient_sync , depot_tools_dir );
261307 gclient_sync .step .dependOn (& write_gclient_args .step );
262308
263309 // Run clang update
264- const clang_update = b .addSystemCommand (&.{ "python3" , "tools/clang/scripts/update.py" });
310+ const clang_update = b .addSystemCommand (&.{
311+ getDepotToolExePath (b , depot_tools_dir , "python-bin/python3" ),
312+ "tools/clang/scripts/update.py" ,
313+ });
265314 clang_update .setCwd (.{ .cwd_relative = v8_dir });
315+ addDepotToolsToPath (clang_update , depot_tools_dir );
266316 clang_update .step .dependOn (& gclient_sync .step );
267317
268318 // Create marker file
@@ -274,13 +324,13 @@ fn bootstrapV8(b: *std.Build, v8_dir: []const u8) !*std.Build.Step.Run {
274324
275325fn buildV8 (
276326 b : * std.Build ,
277- v8_cache : []const u8 ,
327+ v8_dir : []const u8 ,
328+ depot_tools_dir : []const u8 ,
278329 bootstrapped_v8 : * std.Build.Step.Run ,
279330 target : std.Build.ResolvedTarget ,
280331 optimize : std.builtin.OptimizeMode ,
281332) ! * std.Build.Step.WriteFile {
282- const depot_tools = b .dependency ("depot_tools" , .{});
283- const v8_dir : LazyPath = .{ .cwd_relative = v8_cache };
333+ const v8_dir_lazy_path : LazyPath = .{ .cwd_relative = v8_dir };
284334
285335 const allocator = b .allocator ;
286336
@@ -320,31 +370,33 @@ fn buildV8(
320370
321371 const out_dir = b .fmt ("out/{s}/{s}" , .{ @tagName (tag ), if (is_debug ) "debug" else "release" });
322372
323- var gn_run = std .Build .Step .Run .create (b , "run gn" );
324- gn_run .addFileArg (depot_tools .path ("gn" ));
325- gn_run .addArgs (&.{
373+ const gn_run = b .addSystemCommand (&.{
374+ getDepotToolExePath (b , depot_tools_dir , "gn" ),
326375 "--root=." ,
327376 "--root-target=//zig" ,
328377 "--dotfile=zig/.gn" ,
329378 "gen" ,
330379 out_dir ,
331380 b .fmt ("--args={s}" , .{gn_args .items }),
332381 });
333- gn_run .setCwd (v8_dir );
334- addDepotToolsToPath (b , gn_run , depot_tools );
382+ gn_run .setCwd (v8_dir_lazy_path );
383+ addDepotToolsToPath (gn_run , depot_tools_dir );
335384 gn_run .step .dependOn (& bootstrapped_v8 .step );
336385
337- var ninja_run = std .Build .Step .Run .create (b , "run ninja" );
338- ninja_run .addFileArg (depot_tools .path ("ninja" ));
339- ninja_run .addArgs (&.{ "-C" , out_dir , "c_v8" });
340- ninja_run .setCwd (v8_dir );
341- addDepotToolsToPath (b , ninja_run , depot_tools );
386+ const ninja_run = b .addSystemCommand (&.{
387+ getDepotToolExePath (b , depot_tools_dir , "ninja" ),
388+ "-C" ,
389+ out_dir ,
390+ "c_v8" ,
391+ });
392+ ninja_run .setCwd (v8_dir_lazy_path );
393+ addDepotToolsToPath (ninja_run , depot_tools_dir );
342394 ninja_run .step .dependOn (& gn_run .step );
343395
344396 const wf = b .addWriteFiles ();
345397 wf .step .dependOn (& ninja_run .step );
346398 const libc_v8_path = b .fmt ("{s}/obj/zig/libc_v8.a" , .{out_dir });
347- _ = wf .addCopyFile (v8_dir .path (b , libc_v8_path ), "libc_v8.a" );
399+ _ = wf .addCopyFile (v8_dir_lazy_path .path (b , libc_v8_path ), "libc_v8.a" );
348400
349401 return wf ;
350402}
0 commit comments