| ... | ... | @@ -4,6 +4,7 @@ const mem = std.mem; |
| 4 | 4 | const io = std.io; |
| 5 | 5 | const Allocator = std.mem.Allocator; |
| 6 | 6 | const assert = std.debug.assert; |
| 7 | const Cache = std.Build.Cache; |
| 7 | 8 | |
| 8 | 9 | fn usage() noreturn { |
| 9 | 10 | io.getStdOut().writeAll( |
| ... | ... | @@ -232,9 +233,18 @@ fn serveWasm( |
| 232 | 233 | |
| 233 | 234 | // Do the compilation every request, so that the user can edit the files |
| 234 | 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 | 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 | 248 | defer gpa.free(file_contents); |
| 239 | 249 | try request.respond(file_contents, .{ |
| 240 | 250 | .extra_headers = &.{ |
| ... | ... | @@ -244,37 +254,42 @@ fn serveWasm( |
| 244 | 254 | }); |
| 245 | 255 | } |
| 246 | 256 | |
| 257 | const autodoc_root_name = "autodoc"; |
| 258 | const autodoc_arch_os_abi = "wasm32-freestanding"; |
| 259 | const autodoc_cpu_features = "baseline+atomics+bulk_memory+multivalue+mutable_globals+nontrapping_fptoint+reference_types+sign_ext"; |
| 260 | |
| 247 | 261 | fn buildWasmBinary( |
| 248 | 262 | arena: Allocator, |
| 249 | 263 | context: *Context, |
| 250 | 264 | optimize_mode: std.builtin.OptimizeMode, |
| 251 | | ) ![]const u8 { |
| 265 | ) !Cache.Path { |
| 252 | 266 | const gpa = context.gpa; |
| 253 | 267 | |
| 254 | 268 | var argv: std.ArrayListUnmanaged([]const u8) = .{}; |
| 255 | 269 | |
| 256 | 270 | try argv.appendSlice(arena, &.{ |
| 257 | | context.zig_exe_path, |
| 258 | | "build-exe", |
| 259 | | "-fno-entry", |
| 260 | | "-O", |
| 261 | | @tagName(optimize_mode), |
| 262 | | "-target", |
| 263 | | "wasm32-freestanding", |
| 264 | | "-mcpu", |
| 265 | | "baseline+atomics+bulk_memory+multivalue+mutable_globals+nontrapping_fptoint+reference_types+sign_ext", |
| 266 | | "--cache-dir", |
| 267 | | context.global_cache_path, |
| 268 | | "--global-cache-dir", |
| 269 | | context.global_cache_path, |
| 270 | | "--name", |
| 271 | | "autodoc", |
| 272 | | "-rdynamic", |
| 273 | | "--dep", |
| 274 | | "Walk", |
| 275 | | try std.fmt.allocPrint(arena, "-Mroot={s}/docs/wasm/main.zig", .{context.zig_lib_directory}), |
| 276 | | try std.fmt.allocPrint(arena, "-MWalk={s}/docs/wasm/Walk.zig", .{context.zig_lib_directory}), |
| 277 | | "--listen=-", |
| 271 | context.zig_exe_path, // |
| 272 | "build-exe", // |
| 273 | "-fno-entry", // |
| 274 | "-O", @tagName(optimize_mode), // |
| 275 | "-target", autodoc_arch_os_abi, // |
| 276 | "-mcpu", autodoc_cpu_features, // |
| 277 | "--cache-dir", context.global_cache_path, // |
| 278 | "--global-cache-dir", context.global_cache_path, // |
| 279 | "--name", autodoc_root_name, // |
| 280 | "-rdynamic", // |
| 281 | "--dep", "Walk", // |
| 282 | try std.fmt.allocPrint( |
| 283 | arena, |
| 284 | "-Mroot={s}/docs/wasm/main.zig", |
| 285 | .{context.zig_lib_directory}, |
| 286 | ), |
| 287 | try std.fmt.allocPrint( |
| 288 | arena, |
| 289 | "-MWalk={s}/docs/wasm/Walk.zig", |
| 290 | .{context.zig_lib_directory}, |
| 291 | ), |
| 292 | "--listen=-", // |
| 278 | 293 | }); |
| 279 | 294 | |
| 280 | 295 | var child = std.process.Child.init(argv.items, gpa); |
| ... | ... | @@ -293,7 +308,7 @@ fn buildWasmBinary( |
| 293 | 308 | try sendMessage(child.stdin.?, .exit); |
| 294 | 309 | |
| 295 | 310 | const Header = std.zig.Server.Message.Header; |
| 296 | | var result: ?[]const u8 = null; |
| 311 | var result: ?Cache.Path = null; |
| 297 | 312 | var result_error_bundle = std.zig.ErrorBundle.empty; |
| 298 | 313 | |
| 299 | 314 | const stdout = poller.fifo(.stdout); |
| ... | ... | @@ -330,13 +345,19 @@ fn buildWasmBinary( |
| 330 | 345 | .extra = extra_array, |
| 331 | 346 | }; |
| 332 | 347 | }, |
| 333 | | .emit_bin_path => { |
| 334 | | const EbpHdr = std.zig.Server.Message.EmitBinPath; |
| 335 | | const ebp_hdr = @as(*align(1) const EbpHdr, @ptrCast(body)); |
| 336 | | if (!ebp_hdr.flags.cache_hit) { |
| 348 | .emit_digest => { |
| 349 | const EmitDigest = std.zig.Server.Message.EmitDigest; |
| 350 | const emit_digest = @as(*align(1) const EmitDigest, @ptrCast(body)); |
| 351 | if (!emit_digest.flags.cache_hit) { |
| 337 | 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 | 362 | else => {}, // ignore other messages |
| 342 | 363 | } |