fix autodocs regression FTBFS

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

closes #21180
This commit is contained in:
Andrew Kelley 2024-08-23 12:57:55 -07:00
parent 5dd2bb525d
commit 9848318725
3 changed files with 60 additions and 39 deletions

View File

@ -4,6 +4,7 @@ const mem = std.mem;
const io = std.io;
const Allocator = std.mem.Allocator;
const assert = std.debug.assert;
const Cache = std.Build.Cache;
fn usage() noreturn {
io.getStdOut().writeAll(
@ -232,9 +233,18 @@ fn serveWasm(
// Do the compilation every request, so that the user can edit the files
// and see the changes without restarting the server.
const wasm_binary_path = try buildWasmBinary(arena, context, optimize_mode);
const wasm_base_path = try buildWasmBinary(arena, context, optimize_mode);
const bin_name = try std.zig.binNameAlloc(arena, .{
.root_name = autodoc_root_name,
.target = std.zig.system.resolveTargetQuery(std.Build.parseTargetQuery(.{
.arch_os_abi = autodoc_arch_os_abi,
.cpu_features = autodoc_cpu_features,
}) catch unreachable) catch unreachable,
.output_mode = .Exe,
});
// std.http.Server does not have a sendfile API yet.
const file_contents = try std.fs.cwd().readFileAlloc(gpa, wasm_binary_path, 10 * 1024 * 1024);
const bin_path = try wasm_base_path.join(arena, bin_name);
const file_contents = try bin_path.root_dir.handle.readFileAlloc(gpa, bin_path.sub_path, 10 * 1024 * 1024);
defer gpa.free(file_contents);
try request.respond(file_contents, .{
.extra_headers = &.{
@ -244,37 +254,42 @@ fn serveWasm(
});
}
const autodoc_root_name = "autodoc";
const autodoc_arch_os_abi = "wasm32-freestanding";
const autodoc_cpu_features = "baseline+atomics+bulk_memory+multivalue+mutable_globals+nontrapping_fptoint+reference_types+sign_ext";
fn buildWasmBinary(
arena: Allocator,
context: *Context,
optimize_mode: std.builtin.OptimizeMode,
) ![]const u8 {
) !Cache.Path {
const gpa = context.gpa;
var argv: std.ArrayListUnmanaged([]const u8) = .{};
try argv.appendSlice(arena, &.{
context.zig_exe_path,
"build-exe",
"-fno-entry",
"-O",
@tagName(optimize_mode),
"-target",
"wasm32-freestanding",
"-mcpu",
"baseline+atomics+bulk_memory+multivalue+mutable_globals+nontrapping_fptoint+reference_types+sign_ext",
"--cache-dir",
context.global_cache_path,
"--global-cache-dir",
context.global_cache_path,
"--name",
"autodoc",
"-rdynamic",
"--dep",
"Walk",
try std.fmt.allocPrint(arena, "-Mroot={s}/docs/wasm/main.zig", .{context.zig_lib_directory}),
try std.fmt.allocPrint(arena, "-MWalk={s}/docs/wasm/Walk.zig", .{context.zig_lib_directory}),
"--listen=-",
context.zig_exe_path, //
"build-exe", //
"-fno-entry", //
"-O", @tagName(optimize_mode), //
"-target", autodoc_arch_os_abi, //
"-mcpu", autodoc_cpu_features, //
"--cache-dir", context.global_cache_path, //
"--global-cache-dir", context.global_cache_path, //
"--name", autodoc_root_name, //
"-rdynamic", //
"--dep", "Walk", //
try std.fmt.allocPrint(
arena,
"-Mroot={s}/docs/wasm/main.zig",
.{context.zig_lib_directory},
),
try std.fmt.allocPrint(
arena,
"-MWalk={s}/docs/wasm/Walk.zig",
.{context.zig_lib_directory},
),
"--listen=-", //
});
var child = std.process.Child.init(argv.items, gpa);
@ -293,7 +308,7 @@ fn buildWasmBinary(
try sendMessage(child.stdin.?, .exit);
const Header = std.zig.Server.Message.Header;
var result: ?[]const u8 = null;
var result: ?Cache.Path = null;
var result_error_bundle = std.zig.ErrorBundle.empty;
const stdout = poller.fifo(.stdout);
@ -330,13 +345,19 @@ fn buildWasmBinary(
.extra = extra_array,
};
},
.emit_bin_path => {
const EbpHdr = std.zig.Server.Message.EmitBinPath;
const ebp_hdr = @as(*align(1) const EbpHdr, @ptrCast(body));
if (!ebp_hdr.flags.cache_hit) {
.emit_digest => {
const EmitDigest = std.zig.Server.Message.EmitDigest;
const emit_digest = @as(*align(1) const EmitDigest, @ptrCast(body));
if (!emit_digest.flags.cache_hit) {
std.log.info("source changes detected; rebuilt wasm component", .{});
}
result = try arena.dupe(u8, body[@sizeOf(EbpHdr)..]);
const digest = body[@sizeOf(EmitDigest)..][0..Cache.bin_digest_len];
result = .{
.root_dir = Cache.Directory.cwd(),
.sub_path = try std.fs.path.join(arena, &.{
context.global_cache_path, "o" ++ std.fs.path.sep_str ++ Cache.binToHex(digest.*),
}),
};
},
else => {}, // ignore other messages
}

View File

@ -304,13 +304,13 @@ fn buildWasmBinary(
};
},
.emit_digest => {
const EbpHdr = std.zig.Server.Message.EmitDigest;
const ebp_hdr = @as(*align(1) const EbpHdr, @ptrCast(body));
const EmitDigest = std.zig.Server.Message.EmitDigest;
const ebp_hdr = @as(*align(1) const EmitDigest, @ptrCast(body));
if (!ebp_hdr.flags.cache_hit) {
log.info("source changes detected; rebuilt wasm component", .{});
}
const digest = body[@sizeOf(EbpHdr)..][0..Cache.bin_digest_len];
result = Path{
const digest = body[@sizeOf(EmitDigest)..][0..Cache.bin_digest_len];
result = .{
.root_dir = ws.global_cache_directory,
.sub_path = try arena.dupe(u8, "o" ++ std.fs.path.sep_str ++ Cache.binToHex(digest.*)),
};

View File

@ -534,11 +534,11 @@ fn zigProcessUpdate(s: *Step, zp: *ZigProcess, watch: bool) !?Path {
}
},
.emit_digest => {
const EbpHdr = std.zig.Server.Message.EmitDigest;
const ebp_hdr = @as(*align(1) const EbpHdr, @ptrCast(body));
s.result_cached = ebp_hdr.flags.cache_hit;
const digest = body[@sizeOf(EbpHdr)..][0..Cache.bin_digest_len];
result = Path{
const EmitDigest = std.zig.Server.Message.EmitDigest;
const emit_digest = @as(*align(1) const EmitDigest, @ptrCast(body));
s.result_cached = emit_digest.flags.cache_hit;
const digest = body[@sizeOf(EmitDigest)..][0..Cache.bin_digest_len];
result = .{
.root_dir = b.cache_root,
.sub_path = try arena.dupe(u8, "o" ++ std.fs.path.sep_str ++ Cache.binToHex(digest.*)),
};