authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2024-08-23 12:57:55-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2024-08-23 19:23:38-07:00
log9848318725345e4516d68e4f4537f602078d59ad
tree0f1746127373a75442bce8fdab7f916c333bfbe4
parent5dd2bb525d1f19969c450c2b99a71f866a4f01ff

fix autodocs regression FTBFS

regressed in dffc8c44f9a01aa05ea364ffdc71509d15bc2601 since there is no test coverage for the `zig std` command yet. closes #21180

3 files changed, 60 insertions(+), 39 deletions(-)

lib/compiler/std-docs.zig+51-30
...@@ -4,6 +4,7 @@ const mem = std.mem;...@@ -4,6 +4,7 @@ const mem = std.mem;
4const io = std.io;4const io = std.io;
5const Allocator = std.mem.Allocator;5const Allocator = std.mem.Allocator;
6const assert = std.debug.assert;6const assert = std.debug.assert;
7const Cache = std.Build.Cache;
78
8fn usage() noreturn {9fn usage() noreturn {
9 io.getStdOut().writeAll(10 io.getStdOut().writeAll(
...@@ -232,9 +233,18 @@ fn serveWasm(...@@ -232,9 +233,18 @@ fn serveWasm(
232233
233 // Do the compilation every request, so that the user can edit the files234 // Do the compilation every request, so that the user can edit the files
234 // and see the changes without restarting the server.235 // and see the changes without restarting the server.
235 const wasm_binary_path = try buildWasmBinary(arena, context, optimize_mode);236 const wasm_base_path = try buildWasmBinary(arena, context, optimize_mode);
237 const bin_name = try std.zig.binNameAlloc(arena, .{
238 .root_name = autodoc_root_name,
239 .target = std.zig.system.resolveTargetQuery(std.Build.parseTargetQuery(.{
240 .arch_os_abi = autodoc_arch_os_abi,
241 .cpu_features = autodoc_cpu_features,
242 }) catch unreachable) catch unreachable,
243 .output_mode = .Exe,
244 });
236 // std.http.Server does not have a sendfile API yet.245 // std.http.Server does not have a sendfile API yet.
237 const file_contents = try std.fs.cwd().readFileAlloc(gpa, wasm_binary_path, 10 * 1024 * 1024);246 const bin_path = try wasm_base_path.join(arena, bin_name);
247 const file_contents = try bin_path.root_dir.handle.readFileAlloc(gpa, bin_path.sub_path, 10 * 1024 * 1024);
238 defer gpa.free(file_contents);248 defer gpa.free(file_contents);
239 try request.respond(file_contents, .{249 try request.respond(file_contents, .{
240 .extra_headers = &.{250 .extra_headers = &.{
...@@ -244,37 +254,42 @@ fn serveWasm(...@@ -244,37 +254,42 @@ fn serveWasm(
244 });254 });
245}255}
246256
257const autodoc_root_name = "autodoc";
258const autodoc_arch_os_abi = "wasm32-freestanding";
259const autodoc_cpu_features = "baseline+atomics+bulk_memory+multivalue+mutable_globals+nontrapping_fptoint+reference_types+sign_ext";
260
247fn buildWasmBinary(261fn buildWasmBinary(
248 arena: Allocator,262 arena: Allocator,
249 context: *Context,263 context: *Context,
250 optimize_mode: std.builtin.OptimizeMode,264 optimize_mode: std.builtin.OptimizeMode,
251) ![]const u8 {265) !Cache.Path {
252 const gpa = context.gpa;266 const gpa = context.gpa;
253267
254 var argv: std.ArrayListUnmanaged([]const u8) = .{};268 var argv: std.ArrayListUnmanaged([]const u8) = .{};
255269
256 try argv.appendSlice(arena, &.{270 try argv.appendSlice(arena, &.{
257 context.zig_exe_path,271 context.zig_exe_path, //
258 "build-exe",272 "build-exe", //
259 "-fno-entry",273 "-fno-entry", //
260 "-O",274 "-O", @tagName(optimize_mode), //
261 @tagName(optimize_mode),275 "-target", autodoc_arch_os_abi, //
262 "-target",276 "-mcpu", autodoc_cpu_features, //
263 "wasm32-freestanding",277 "--cache-dir", context.global_cache_path, //
264 "-mcpu",278 "--global-cache-dir", context.global_cache_path, //
265 "baseline+atomics+bulk_memory+multivalue+mutable_globals+nontrapping_fptoint+reference_types+sign_ext",279 "--name", autodoc_root_name, //
266 "--cache-dir",280 "-rdynamic", //
267 context.global_cache_path,281 "--dep", "Walk", //
268 "--global-cache-dir",282 try std.fmt.allocPrint(
269 context.global_cache_path,283 arena,
270 "--name",284 "-Mroot={s}/docs/wasm/main.zig",
271 "autodoc",285 .{context.zig_lib_directory},
272 "-rdynamic",286 ),
273 "--dep",287 try std.fmt.allocPrint(
274 "Walk",288 arena,
275 try std.fmt.allocPrint(arena, "-Mroot={s}/docs/wasm/main.zig", .{context.zig_lib_directory}),289 "-MWalk={s}/docs/wasm/Walk.zig",
276 try std.fmt.allocPrint(arena, "-MWalk={s}/docs/wasm/Walk.zig", .{context.zig_lib_directory}),290 .{context.zig_lib_directory},
277 "--listen=-",291 ),
292 "--listen=-", //
278 });293 });
279294
280 var child = std.process.Child.init(argv.items, gpa);295 var child = std.process.Child.init(argv.items, gpa);
...@@ -293,7 +308,7 @@ fn buildWasmBinary(...@@ -293,7 +308,7 @@ fn buildWasmBinary(
293 try sendMessage(child.stdin.?, .exit);308 try sendMessage(child.stdin.?, .exit);
294309
295 const Header = std.zig.Server.Message.Header;310 const Header = std.zig.Server.Message.Header;
296 var result: ?[]const u8 = null;311 var result: ?Cache.Path = null;
297 var result_error_bundle = std.zig.ErrorBundle.empty;312 var result_error_bundle = std.zig.ErrorBundle.empty;
298313
299 const stdout = poller.fifo(.stdout);314 const stdout = poller.fifo(.stdout);
...@@ -330,13 +345,19 @@ fn buildWasmBinary(...@@ -330,13 +345,19 @@ fn buildWasmBinary(
330 .extra = extra_array,345 .extra = extra_array,
331 };346 };
332 },347 },
333 .emit_bin_path => {348 .emit_digest => {
334 const EbpHdr = std.zig.Server.Message.EmitBinPath;349 const EmitDigest = std.zig.Server.Message.EmitDigest;
335 const ebp_hdr = @as(*align(1) const EbpHdr, @ptrCast(body));350 const emit_digest = @as(*align(1) const EmitDigest, @ptrCast(body));
336 if (!ebp_hdr.flags.cache_hit) {351 if (!emit_digest.flags.cache_hit) {
337 std.log.info("source changes detected; rebuilt wasm component", .{});352 std.log.info("source changes detected; rebuilt wasm component", .{});
338 }353 }
339 result = try arena.dupe(u8, body[@sizeOf(EbpHdr)..]);354 const digest = body[@sizeOf(EmitDigest)..][0..Cache.bin_digest_len];
355 result = .{
356 .root_dir = Cache.Directory.cwd(),
357 .sub_path = try std.fs.path.join(arena, &.{
358 context.global_cache_path, "o" ++ std.fs.path.sep_str ++ Cache.binToHex(digest.*),
359 }),
360 };
340 },361 },
341 else => {}, // ignore other messages362 else => {}, // ignore other messages
342 }363 }
lib/std/Build/Fuzz/WebServer.zig+4-4
...@@ -304,13 +304,13 @@ fn buildWasmBinary(...@@ -304,13 +304,13 @@ fn buildWasmBinary(
304 };304 };
305 },305 },
306 .emit_digest => {306 .emit_digest => {
307 const EbpHdr = std.zig.Server.Message.EmitDigest;307 const EmitDigest = std.zig.Server.Message.EmitDigest;
308 const ebp_hdr = @as(*align(1) const EbpHdr, @ptrCast(body));308 const ebp_hdr = @as(*align(1) const EmitDigest, @ptrCast(body));
309 if (!ebp_hdr.flags.cache_hit) {309 if (!ebp_hdr.flags.cache_hit) {
310 log.info("source changes detected; rebuilt wasm component", .{});310 log.info("source changes detected; rebuilt wasm component", .{});
311 }311 }
312 const digest = body[@sizeOf(EbpHdr)..][0..Cache.bin_digest_len];312 const digest = body[@sizeOf(EmitDigest)..][0..Cache.bin_digest_len];
313 result = Path{313 result = .{
314 .root_dir = ws.global_cache_directory,314 .root_dir = ws.global_cache_directory,
315 .sub_path = try arena.dupe(u8, "o" ++ std.fs.path.sep_str ++ Cache.binToHex(digest.*)),315 .sub_path = try arena.dupe(u8, "o" ++ std.fs.path.sep_str ++ Cache.binToHex(digest.*)),
316 };316 };
lib/std/Build/Step.zig+5-5
...@@ -534,11 +534,11 @@ fn zigProcessUpdate(s: *Step, zp: *ZigProcess, watch: bool) !?Path {...@@ -534,11 +534,11 @@ fn zigProcessUpdate(s: *Step, zp: *ZigProcess, watch: bool) !?Path {
534 }534 }
535 },535 },
536 .emit_digest => {536 .emit_digest => {
537 const EbpHdr = std.zig.Server.Message.EmitDigest;537 const EmitDigest = std.zig.Server.Message.EmitDigest;
538 const ebp_hdr = @as(*align(1) const EbpHdr, @ptrCast(body));538 const emit_digest = @as(*align(1) const EmitDigest, @ptrCast(body));
539 s.result_cached = ebp_hdr.flags.cache_hit;539 s.result_cached = emit_digest.flags.cache_hit;
540 const digest = body[@sizeOf(EbpHdr)..][0..Cache.bin_digest_len];540 const digest = body[@sizeOf(EmitDigest)..][0..Cache.bin_digest_len];
541 result = Path{541 result = .{
542 .root_dir = b.cache_root,542 .root_dir = b.cache_root,
543 .sub_path = try arena.dupe(u8, "o" ++ std.fs.path.sep_str ++ Cache.binToHex(digest.*)),543 .sub_path = try arena.dupe(u8, "o" ++ std.fs.path.sep_str ++ Cache.binToHex(digest.*)),
544 };544 };