diff --git a/lib/compiler/std-docs.zig b/lib/compiler/std-docs.zig index 93a04a28e5..d86be67702 100644 --- a/lib/compiler/std-docs.zig +++ b/lib/compiler/std-docs.zig @@ -177,6 +177,26 @@ fn serveSourcesTar(request: *std.http.Server.Request, context: *Context) !void { try w.writeFile(file); try w.writeByteNTimes(0, padding); } + + { + // Since this command is JIT compiled, the builtin module available in + // this source file corresponds to the user's host system. + const builtin_zig = @embedFile("builtin"); + + var file_header = std.tar.output.Header.init(); + file_header.typeflag = .regular; + try file_header.setPath("builtin", "builtin.zig"); + try file_header.setSize(builtin_zig.len); + try file_header.updateChecksum(); + try w.writeAll(std.mem.asBytes(&file_header)); + try w.writeAll(builtin_zig); + const padding = p: { + const remainder = builtin_zig.len % 512; + break :p if (remainder > 0) 512 - remainder else 0; + }; + try w.writeByteNTimes(0, padding); + } + // intentionally omitting the pointless trailer //try w.writeByteNTimes(0, 512 * 2); try response.end(); diff --git a/src/Compilation.zig b/src/Compilation.zig index 5bbca51ede..460a1e5d9e 100644 --- a/src/Compilation.zig +++ b/src/Compilation.zig @@ -3693,6 +3693,9 @@ fn workerDocsCopy(comp: *Compilation, wg: *WaitGroup) void { } fn docsCopyFallible(comp: *Compilation) anyerror!void { + const zcu = comp.module orelse + return comp.lockAndSetMiscFailure(.docs_copy, "no Zig code to document", .{}); + const emit = comp.docs_emit.?; var out_dir = emit.directory.handle.makeOpenPath(emit.sub_path, .{}) catch |err| { return comp.lockAndSetMiscFailure( @@ -3723,7 +3726,25 @@ fn docsCopyFallible(comp: *Compilation) anyerror!void { }; defer tar_file.close(); - const root = comp.root_mod.root; + var seen_table: std.AutoArrayHashMapUnmanaged(*Package.Module, void) = .{}; + defer seen_table.deinit(comp.gpa); + + try seen_table.put(comp.gpa, zcu.main_mod, {}); + try seen_table.put(comp.gpa, zcu.std_mod, {}); + + var i: usize = 0; + while (i < seen_table.count()) : (i += 1) { + const mod = seen_table.keys()[i]; + try comp.docsCopyModule(mod, tar_file); + + const deps = mod.deps.values(); + try seen_table.ensureUnusedCapacity(comp.gpa, deps.len); + for (deps) |dep| seen_table.putAssumeCapacity(dep, {}); + } +} + +fn docsCopyModule(comp: *Compilation, module: *Package.Module, tar_file: std.fs.File) !void { + const root = module.root; const sub_path = if (root.sub_path.len == 0) "." else root.sub_path; var mod_dir = root.root_dir.handle.openDir(sub_path, .{ .iterate = true }) catch |err| { return comp.lockAndSetMiscFailure(.docs_copy, "unable to open directory '{}': {s}", .{ @@ -3762,7 +3783,7 @@ fn docsCopyFallible(comp: *Compilation) anyerror!void { var file_header = std.tar.output.Header.init(); file_header.typeflag = .regular; - try file_header.setPath(comp.root_name, entry.path); + try file_header.setPath(module.fully_qualified_name, entry.path); try file_header.setSize(stat.size); try file_header.updateChecksum();