authorgravatar for xq@random-projects.netFelix "xq" Queißner <xq@random-projects.net> 2023-07-25 13:26:32+02:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-07-30 11:18:50-07:00
log5c0181841081170a118d8e50af2a09f5006f59e1
tree085e2f6ce612c883aeb328611b510462ddcf55b6
parentce95a3b153e9f3e83232e641c26a41e7dbd01165

Introduces `Compile.getEmittedX()` functions, drops `Compile.emit_X`. Resolves #14971


22 files changed, 289 insertions(+), 163 deletions(-)

build.zig-5
......@@ -59,7 +59,6 @@ pub fn build(b: *std.Build) !void {
5959 .target = target,
6060 });
6161 autodoc_test.overrideZigLibDir(.{ .path = "lib" });
62 autodoc_test.emit_bin = .no_emit; // https://github.com/ziglang/zig/issues/16351
6362 const install_std_docs = b.addInstallDirectory(.{
6463 .source_dir = autodoc_test.getEmittedDocs(),
6564 .install_dir = .prefix,
......@@ -196,10 +195,6 @@ pub fn build(b: *std.Build) !void {
196195 exe.pie = pie;
197196 exe.sanitize_thread = sanitize_thread;
198197 exe.entitlements = entitlements;
199 // TODO -femit-bin/-fno-emit-bin should be inferred by the build system
200 // based on whether or not the exe is run or installed.
201 // https://github.com/ziglang/zig/issues/16351
202 if (no_bin) exe.emit_bin = .no_emit;
203198
204199 exe.build_id = b.option(
205200 std.Build.Step.Compile.BuildId,
lib/std/Build.zig+3-49
......@@ -19,6 +19,8 @@ const NativeTargetInfo = std.zig.system.NativeTargetInfo;
1919const Sha256 = std.crypto.hash.sha2.Sha256;
2020const Build = @This();
2121
22const build_util = @import("Build/util.zig");
23
2224pub const Cache = @import("Build/Cache.zig");
2325
2426/// deprecated: use `Step.Compile`.
......@@ -1679,7 +1681,7 @@ pub const LazyPath = union(enum) {
16791681 .generated => |gen| return gen.path orelse {
16801682 std.debug.getStderrMutex().lock();
16811683 const stderr = std.io.getStdErr();
1682 dumpBadGetPathHelp(gen.step, stderr, src_builder, asking_step) catch {};
1684 build_util.dumpBadGetPathHelp(gen.step, stderr, src_builder, asking_step) catch {};
16831685 @panic("misconfigured build script");
16841686 },
16851687 }
......@@ -1694,54 +1696,6 @@ pub const LazyPath = union(enum) {
16941696 }
16951697};
16961698
1697/// In this function the stderr mutex has already been locked.
1698fn dumpBadGetPathHelp(
1699 s: *Step,
1700 stderr: fs.File,
1701 src_builder: *Build,
1702 asking_step: ?*Step,
1703) anyerror!void {
1704 const w = stderr.writer();
1705 try w.print(
1706 \\getPath() was called on a GeneratedFile that wasn't built yet.
1707 \\ source package path: {s}
1708 \\ Is there a missing Step dependency on step '{s}'?
1709 \\
1710 , .{
1711 src_builder.build_root.path orelse ".",
1712 s.name,
1713 });
1714
1715 const tty_config = std.io.tty.detectConfig(stderr);
1716 tty_config.setColor(w, .red) catch {};
1717 try stderr.writeAll(" The step was created by this stack trace:\n");
1718 tty_config.setColor(w, .reset) catch {};
1719
1720 const debug_info = std.debug.getSelfDebugInfo() catch |err| {
1721 try w.print("Unable to dump stack trace: Unable to open debug info: {s}\n", .{@errorName(err)});
1722 return;
1723 };
1724 const ally = debug_info.allocator;
1725 std.debug.writeStackTrace(s.getStackTrace(), w, ally, debug_info, tty_config) catch |err| {
1726 try stderr.writer().print("Unable to dump stack trace: {s}\n", .{@errorName(err)});
1727 return;
1728 };
1729 if (asking_step) |as| {
1730 tty_config.setColor(w, .red) catch {};
1731 try stderr.writeAll(" The step that is missing a dependency on the above step was created by this stack trace:\n");
1732 tty_config.setColor(w, .reset) catch {};
1733
1734 std.debug.writeStackTrace(as.getStackTrace(), w, ally, debug_info, tty_config) catch |err| {
1735 try stderr.writer().print("Unable to dump stack trace: {s}\n", .{@errorName(err)});
1736 return;
1737 };
1738 }
1739
1740 tty_config.setColor(w, .red) catch {};
1741 try stderr.writeAll(" Hope that helps. Proceeding to panic.\n");
1742 tty_config.setColor(w, .reset) catch {};
1743}
1744
17451699/// Allocates a new string for assigning a value to a named macro.
17461700/// If the value is omitted, it is set to 1.
17471701/// `name` and `value` need not live longer than the function call.
lib/std/Build/Step.zig+4-1
......@@ -423,7 +423,10 @@ pub fn evalZigProcess(
423423 });
424424 }
425425
426 if (s.cast(Compile)) |compile| if (compile.emit_bin == .no_emit) return result;
426 if (s.cast(Compile)) |compile| {
427 if (compile.generated_bin == null) // TODO(xq): How to handle this properly?!
428 return result;
429 }
427430
428431 return result orelse return s.fail(
429432 "the following command failed to communicate the compilation result:\n{s}",
lib/std/Build/Step/Compile.zig+181-81
......@@ -21,6 +21,8 @@ const InstallDir = std.Build.InstallDir;
2121const GeneratedFile = std.Build.GeneratedFile;
2222const Compile = @This();
2323
24const build_util = @import("../util.zig");
25
2426pub const base_id: Step.Id = .compile;
2527
2628step: Step,
......@@ -46,14 +48,6 @@ framework_dirs: ArrayList(LazyPath),
4648frameworks: StringHashMap(FrameworkLinkInfo),
4749verbose_link: bool,
4850verbose_cc: bool,
49emit_asm: EmitOption = .default,
50emit_bin: EmitOption = .default,
51emit_implib: EmitOption = .default,
52emit_llvm_bc: EmitOption = .default,
53emit_llvm_ir: EmitOption = .default,
54// Lots of things depend on emit_h having a consistent path,
55// so it is not an EmitOption for now.
56emit_h: bool = false,
5751bundle_compiler_rt: ?bool = null,
5852single_threaded: ?bool,
5953stack_protector: ?bool = null,
......@@ -87,6 +81,9 @@ export_symbol_names: []const []const u8 = &.{},
8781
8882root_src: ?LazyPath,
8983out_h_filename: []const u8,
84out_ll_filename: []const u8,
85out_bc_filename: []const u8,
86out_asm_filename: []const u8,
9087out_lib_filename: []const u8,
9188out_pdb_filename: []const u8,
9289modules: std.StringArrayHashMap(*Module),
......@@ -210,12 +207,16 @@ use_lld: ?bool,
210207/// otherwise.
211208expect_errors: []const []const u8 = &.{},
212209
213output_path_source: GeneratedFile,
214output_lib_path_source: GeneratedFile,
215output_h_path_source: GeneratedFile,
216output_pdb_path_source: GeneratedFile,
217output_dirname_source: GeneratedFile,
210emit_directory: GeneratedFile,
211
218212generated_docs: ?*GeneratedFile,
213generated_asm: ?*GeneratedFile,
214generated_bin: ?*GeneratedFile,
215generated_pdb: ?*GeneratedFile,
216generated_implib: ?*GeneratedFile,
217generated_llvm_bc: ?*GeneratedFile,
218generated_llvm_ir: ?*GeneratedFile,
219generated_h: ?*GeneratedFile,
219220
220221pub const CSourceFiles = struct {
221222 files: []const []const u8,
......@@ -373,22 +374,6 @@ pub const Kind = enum {
373374
374375pub const Linkage = enum { dynamic, static };
375376
376pub const EmitOption = union(enum) {
377 default: void,
378 no_emit: void,
379 emit: void,
380 emit_to: []const u8,
381
382 fn getArg(self: @This(), b: *std.Build, arg_name: []const u8) ?[]const u8 {
383 return switch (self) {
384 .no_emit => b.fmt("-fno-{s}", .{arg_name}),
385 .default => null,
386 .emit => b.fmt("-f{s}", .{arg_name}),
387 .emit_to => |path| b.fmt("-f{s}={s}", .{ arg_name, path }),
388 };
389 }
390};
391
392377pub fn create(owner: *std.Build, options: Options) *Compile {
393378 const name = owner.dupe(options.name);
394379 const root_src: ?LazyPath = if (options.root_source_file) |rsrc| rsrc.dupe(owner) else null;
......@@ -454,6 +439,9 @@ pub fn create(owner: *std.Build, options: Options) *Compile {
454439 .version = options.version,
455440 .out_filename = out_filename,
456441 .out_h_filename = owner.fmt("{s}.h", .{name}),
442 .out_ll_filename = owner.fmt("{s}.bc", .{name}),
443 .out_bc_filename = owner.fmt("{s}.ll", .{name}),
444 .out_asm_filename = owner.fmt("{s}.s", .{name}),
457445 .out_lib_filename = undefined,
458446 .out_pdb_filename = owner.fmt("{s}.pdb", .{name}),
459447 .major_only_filename = null,
......@@ -480,12 +468,16 @@ pub fn create(owner: *std.Build, options: Options) *Compile {
480468 .installed_path = null,
481469 .force_undefined_symbols = StringHashMap(void).init(owner.allocator),
482470
483 .output_path_source = GeneratedFile{ .step = &self.step },
484 .output_lib_path_source = GeneratedFile{ .step = &self.step },
485 .output_h_path_source = GeneratedFile{ .step = &self.step },
486 .output_pdb_path_source = GeneratedFile{ .step = &self.step },
487 .output_dirname_source = GeneratedFile{ .step = &self.step },
471 .emit_directory = GeneratedFile{ .step = &self.step },
472
488473 .generated_docs = null,
474 .generated_asm = null,
475 .generated_bin = null,
476 .generated_pdb = null,
477 .generated_implib = null,
478 .generated_llvm_bc = null,
479 .generated_llvm_ir = null,
480 .generated_h = null,
489481
490482 .target_info = target_info,
491483
......@@ -692,6 +684,7 @@ pub fn isStaticLibrary(self: *Compile) bool {
692684}
693685
694686pub fn producesPdbFile(self: *Compile) bool {
687 // TODO: Is this right? Isn't PDB for *any* PE/COFF file?
695688 if (!self.target.isWindows() and !self.target.isUefi()) return false;
696689 if (self.target.getObjectFormat() == .c) return false;
697690 if (self.strip == true or (self.strip == null and self.optimize == .ReleaseSmall)) return false;
......@@ -968,54 +961,75 @@ pub fn setLibCFile(self: *Compile, libc_file: ?LazyPath) void {
968961 self.libc_file = if (libc_file) |f| f.dupe(b) else null;
969962}
970963
971pub const getOutputSource = getEmittedBin; // DEPRECATED, use getEmittedBin
972
973/// Returns the generated executable, library or object file.
974/// To run an executable built with zig build, use `run`, or create an install step and invoke it.
975pub fn getEmittedBin(self: *Compile) LazyPath {
976 return .{ .generated = &self.output_path_source };
964fn getEmittedFileGeneric(self: *Compile, output_file: *?*GeneratedFile) LazyPath {
965 if (output_file.*) |g| {
966 return .{ .generated = g };
967 }
968 const arena = self.step.owner.allocator;
969 const generated_file = arena.create(GeneratedFile) catch @panic("OOM");
970 generated_file.* = .{ .step = &self.step };
971 output_file.* = generated_file;
972 return .{ .generated = generated_file };
977973}
978974
979975pub const getOutputDirectorySource = getEmitDirectory; // DEPRECATED, use getEmitDirectory
980976
977/// Returns the path to the output directory.
981978pub fn getEmitDirectory(self: *Compile) LazyPath {
982 return .{ .generated = &self.output_dirname_source };
979 return .{ .generated = &self.emit_directory };
980}
981
982pub const getOutputSource = getEmittedBin; // DEPRECATED, use getEmittedBin
983
984/// Returns the path to the generated executable, library or object file.
985/// To run an executable built with zig build, use `run`, or create an install step and invoke it.
986pub fn getEmittedBin(self: *Compile) LazyPath {
987 return self.getEmittedFileGeneric(&self.generated_bin);
983988}
984989
985990pub const getOutputLibSource = getEmittedImplib; // DEPRECATED, use getEmittedImplib
986991
987/// Returns the generated import library. This function can only be called for libraries.
992/// Returns the path to the generated import library. This function can only be called for libraries.
988993pub fn getEmittedImplib(self: *Compile) LazyPath {
989994 assert(self.kind == .lib);
990 return .{ .generated = &self.output_lib_path_source };
995 return self.getEmittedFileGeneric(&self.generated_implib);
991996}
992997
993998pub const getOutputHSource = getEmittedH; // DEPRECATED, use getEmittedH
994999
995/// Returns the generated header file.
996/// This function can only be called for libraries or object files which have `emit_h` set.
1000/// Returns the path to the generated header file.
1001/// This function can only be called for libraries or objects.
9971002pub fn getEmittedH(self: *Compile) LazyPath {
9981003 assert(self.kind != .exe and self.kind != .@"test");
999 assert(self.emit_h);
1000 return .{ .generated = &self.output_h_path_source };
1004 return self.getEmittedFileGeneric(&self.generated_h);
10011005}
10021006
10031007pub const getOutputPdbSource = getEmittedPdb; // DEPRECATED, use getEmittedPdb
10041008
10051009/// Returns the generated PDB file. This function can only be called for Windows and UEFI.
10061010pub fn getEmittedPdb(self: *Compile) LazyPath {
1007 // TODO: Is this right? Isn't PDB for *any* PE/COFF file?
1008 assert(self.target.isWindows() or self.target.isUefi());
1009 return .{ .generated = &self.output_pdb_path_source };
1011 assert(self.producesPdbFile());
1012 return self.getEmittedFileGeneric(&self.generated_pdb);
10101013}
10111014
1015/// Returns the path to the generated documentation directory.
10121016pub fn getEmittedDocs(self: *Compile) LazyPath {
1013 if (self.generated_docs) |g| return .{ .generated = g };
1014 const arena = self.step.owner.allocator;
1015 const generated_file = arena.create(GeneratedFile) catch @panic("OOM");
1016 generated_file.* = .{ .step = &self.step };
1017 self.generated_docs = generated_file;
1018 return .{ .generated = generated_file };
1017 return self.getEmittedFileGeneric(&self.generated_docs);
1018}
1019
1020/// Returns the path to the generated assembly code.
1021pub fn getEmittedAsm(self: *Compile) LazyPath {
1022 return self.getEmittedFileGeneric(&self.generated_asm);
1023}
1024
1025/// Returns the path to the generated LLVM IR.
1026pub fn getEmittedLlvmIr(self: *Compile) LazyPath {
1027 return self.getEmittedFileGeneric(&self.generated_llvm_ir);
1028}
1029
1030/// Returns the path to the generated LLVM BC.
1031pub fn getEmittedLlvmBc(self: *Compile) LazyPath {
1032 return self.getEmittedFileGeneric(&self.generated_llvm_bc);
10191033}
10201034
10211035pub fn addAssemblyFile(self: *Compile, source: LazyPath) void {
......@@ -1149,6 +1163,12 @@ pub fn setExecCmd(self: *Compile, args: []const ?[]const u8) void {
11491163}
11501164
11511165fn linkLibraryOrObject(self: *Compile, other: *Compile) void {
1166 _ = other.getEmittedBin(); // Force emission of the binary
1167
1168 if (other.kind == .lib and other.target.isWindows()) { // TODO(xq): Is this the correct logic here?
1169 _ = other.getEmittedImplib(); // Force emission of the binary
1170 }
1171
11521172 self.step.dependOn(&other.step);
11531173 self.link_objects.append(.{ .other_step = other }) catch @panic("OOM");
11541174 self.include_dirs.append(.{ .other_step = other }) catch @panic("OOM");
......@@ -1268,6 +1288,30 @@ fn constructDepString(
12681288 }
12691289}
12701290
1291fn getGeneratedFilePath(self: *Compile, comptime tag_name: []const u8, asking_step: ?*Step) []const u8 {
1292 const maybe_path: ?*GeneratedFile = @field(self, tag_name);
1293
1294 const generated_file = maybe_path orelse {
1295 std.debug.getStderrMutex().lock();
1296 const stderr = std.io.getStdErr();
1297
1298 build_util.dumpBadGetPathHelp(&self.step, stderr, self.step.owner, asking_step) catch {};
1299
1300 @panic("missing emit option for " ++ tag_name);
1301 };
1302
1303 const path = generated_file.path orelse {
1304 std.debug.getStderrMutex().lock();
1305 const stderr = std.io.getStdErr();
1306
1307 build_util.dumpBadGetPathHelp(&self.step, stderr, self.step.owner, asking_step) catch {};
1308
1309 @panic(tag_name ++ " is null. Is there a missing step dependency?");
1310 };
1311
1312 return path;
1313}
1314
12711315fn make(step: *Step, prog_node: *std.Progress.Node) !void {
12721316 const b = step.owner;
12731317 const self = @fieldParentPtr(Compile, "step", step);
......@@ -1353,7 +1397,11 @@ fn make(step: *Step, prog_node: *std.Progress.Node) !void {
13531397 break :l;
13541398 }
13551399
1356 const full_path_lib = other.getEmittedImplib().getPath(b);
1400 // TODO(xq): Is that the right way?
1401 const full_path_lib = if (other.isDynamicLibrary() and other.target.isWindows())
1402 other.getGeneratedFilePath("generated_implib", &self.step)
1403 else
1404 other.getGeneratedFilePath("generated_bin", &self.step);
13571405 try zig_args.append(full_path_lib);
13581406
13591407 if (other.linkage == Linkage.dynamic and !self.target.isWindows()) {
......@@ -1496,14 +1544,36 @@ fn make(step: *Step, prog_node: *std.Progress.Node) !void {
14961544 if (b.verbose_cc or self.verbose_cc) try zig_args.append("--verbose-cc");
14971545 if (b.verbose_llvm_cpu_features) try zig_args.append("--verbose-llvm-cpu-features");
14981546
1499 if (self.emit_asm.getArg(b, "emit-asm")) |arg| try zig_args.append(arg);
1500 if (self.emit_bin.getArg(b, "emit-bin")) |arg| try zig_args.append(arg);
1501 if (self.generated_docs != null) try zig_args.append("-femit-docs");
1502 if (self.emit_implib.getArg(b, "emit-implib")) |arg| try zig_args.append(arg);
1503 if (self.emit_llvm_bc.getArg(b, "emit-llvm-bc")) |arg| try zig_args.append(arg);
1504 if (self.emit_llvm_ir.getArg(b, "emit-llvm-ir")) |arg| try zig_args.append(arg);
1547 const Emitter = struct {
1548 ptr: ?*GeneratedFile,
1549 emit_suffix: []const u8,
1550 };
15051551
1506 if (self.emit_h) try zig_args.append("-femit-h");
1552 const generated_files = [_]Emitter{
1553 .{ .ptr = self.generated_asm, .emit_suffix = "asm" },
1554 .{ .ptr = self.generated_bin, .emit_suffix = "bin" },
1555 .{ .ptr = self.generated_docs, .emit_suffix = "docs" },
1556 .{ .ptr = self.generated_implib, .emit_suffix = "implib" },
1557 .{ .ptr = self.generated_llvm_bc, .emit_suffix = "llvm-bc" },
1558 .{ .ptr = self.generated_llvm_ir, .emit_suffix = "llvm-ir" },
1559 .{ .ptr = self.generated_h, .emit_suffix = "h" },
1560 };
1561 var any_emitted_file = false;
1562 for (generated_files) |file| {
1563 try zig_args.append(if (file.ptr != null)
1564 b.fmt("-femit-{s}", .{file.emit_suffix})
1565 else
1566 b.fmt("-fno-emit-{s}", .{file.emit_suffix}));
1567
1568 if (file.ptr != null) any_emitted_file = true;
1569 }
1570
1571 if (!any_emitted_file) {
1572 std.debug.getStderrMutex().lock();
1573 const stderr = std.io.getStdErr();
1574 build_util.dumpBadGetPathHelp(&self.step, stderr, self.step.owner, null) catch {};
1575 std.debug.panic("Artifact '{s}' has no emit options set, but it is made. Did you forget to call `getEmitted*()`?.", .{self.name});
1576 }
15071577
15081578 try addFlag(&zig_args, "strip", self.strip);
15091579 try addFlag(&zig_args, "unwind-tables", self.unwind_tables);
......@@ -1755,10 +1825,9 @@ fn make(step: *Step, prog_node: *std.Progress.Node) !void {
17551825 try zig_args.append(common_include_path);
17561826 },
17571827 .other_step => |other| {
1758 if (other.emit_h) {
1759 const h_path = other.getEmittedH().getPath(b);
1828 if (other.generated_h) |header| {
17601829 try zig_args.append("-isystem");
1761 try zig_args.append(fs.path.dirname(h_path).?);
1830 try zig_args.append(fs.path.dirname(header.path.?).?);
17621831 }
17631832 if (other.installed_headers.items.len > 0) {
17641833 try zig_args.append("-I");
......@@ -1989,33 +2058,64 @@ fn make(step: *Step, prog_node: *std.Progress.Node) !void {
19892058 if (maybe_output_bin_path) |output_bin_path| {
19902059 const output_dir = fs.path.dirname(output_bin_path).?;
19912060
1992 self.output_dirname_source.path = output_dir;
1993
1994 self.output_path_source.path = b.pathJoin(
1995 &.{ output_dir, self.out_filename },
1996 );
2061 self.emit_directory.path = output_dir;
19972062
1998 if (self.kind == .lib) {
1999 self.output_lib_path_source.path = b.pathJoin(
2000 &.{ output_dir, self.out_lib_filename },
2063 // -femit-bin[=path] (default) Output machine code
2064 if (self.generated_bin) |bin| {
2065 bin.path = b.pathJoin(
2066 &.{ output_dir, self.out_filename },
20012067 );
20022068 }
20032069
2004 if (self.emit_h) {
2005 self.output_h_path_source.path = b.pathJoin(
2006 &.{ output_dir, self.out_h_filename },
2070 // output PDB if someone requested it
2071 if (self.generated_pdb) |pdb| {
2072 std.debug.assert(self.producesPdbFile());
2073 pdb.path = b.pathJoin(
2074 &.{ output_dir, self.out_pdb_filename },
20072075 );
20082076 }
20092077
2010 if (self.target.isWindows() or self.target.isUefi()) {
2011 self.output_pdb_path_source.path = b.pathJoin(
2012 &.{ output_dir, self.out_pdb_filename },
2078 // -femit-implib[=path] (default) Produce an import .lib when building a Windows DLL
2079 if (self.kind == .lib) {
2080 if (self.generated_implib) |lib| {
2081 lib.path = b.pathJoin(
2082 &.{ output_dir, self.out_lib_filename },
2083 );
2084 }
2085 }
2086
2087 // -femit-h[=path] Generate a C header file (.h)
2088 if (self.generated_h) |lazy_path| {
2089 lazy_path.path = b.pathJoin(
2090 &.{ output_dir, self.out_h_filename },
20132091 );
20142092 }
20152093
2094 // -femit-docs[=path] Create a docs/ dir with html documentation
20162095 if (self.generated_docs) |generated_docs| {
20172096 generated_docs.path = b.pathJoin(&.{ output_dir, "docs" });
20182097 }
2098
2099 // -femit-asm[=path] Output .s (assembly code)
2100 if (self.generated_asm) |lazy_path| {
2101 lazy_path.path = b.pathJoin(
2102 &.{ output_dir, self.out_asm_filename },
2103 );
2104 }
2105
2106 // -femit-llvm-ir[=path] Produce a .ll file with optimized LLVM IR (requires LLVM extensions)
2107 if (self.generated_llvm_ir) |lazy_path| {
2108 lazy_path.path = b.pathJoin(
2109 &.{ output_dir, self.out_ll_filename },
2110 );
2111 }
2112
2113 // -femit-llvm-bc[=path] Produce an optimized LLVM module as a .bc file (requires LLVM extensions)
2114 if (self.generated_llvm_bc) |lazy_path| {
2115 lazy_path.path = b.pathJoin(
2116 &.{ output_dir, self.out_bc_filename },
2117 );
2118 }
20192119 }
20202120
20212121 if (self.kind == .lib and self.linkage != null and self.linkage.? == .dynamic and
lib/std/Build/Step/InstallArtifact.zig+9-7
......@@ -37,11 +37,12 @@ pub fn create(owner: *std.Build, artifact: *Step.Compile) *InstallArtifact {
3737 break :blk InstallDir{ .lib = {} };
3838 }
3939 } else null,
40 .h_dir = if (artifact.kind == .lib and artifact.emit_h) .header else null,
40 .h_dir = if (artifact.kind == .lib and artifact.generated_h != null) .header else null,
4141 .dest_sub_path = null,
4242 };
4343 self.step.dependOn(&artifact.step);
4444
45 _ = artifact.getEmittedBin(); // force creation
4546 owner.pushInstalledFile(self.dest_dir, artifact.out_filename);
4647 if (self.artifact.isDynamicLibrary()) {
4748 if (artifact.major_only_filename) |name| {
......@@ -55,9 +56,11 @@ pub fn create(owner: *std.Build, artifact: *Step.Compile) *InstallArtifact {
5556 }
5657 }
5758 if (self.pdb_dir) |pdb_dir| {
59 _ = artifact.getEmittedPdb(); // force creation
5860 owner.pushInstalledFile(pdb_dir, artifact.out_pdb_filename);
5961 }
6062 if (self.h_dir) |h_dir| {
63 _ = artifact.getEmittedH(); // force creation
6164 owner.pushInstalledFile(h_dir, artifact.out_h_filename);
6265 }
6366 return self;
......@@ -66,7 +69,6 @@ pub fn create(owner: *std.Build, artifact: *Step.Compile) *InstallArtifact {
6669fn make(step: *Step, prog_node: *std.Progress.Node) !void {
6770 _ = prog_node;
6871 const self = @fieldParentPtr(InstallArtifact, "step", step);
69 const src_builder = self.artifact.step.owner;
7072 const dest_builder = step.owner;
7173
7274 const dest_sub_path = if (self.dest_sub_path) |sub_path| sub_path else self.artifact.out_filename;
......@@ -76,7 +78,7 @@ fn make(step: *Step, prog_node: *std.Progress.Node) !void {
7678 var all_cached = true;
7779
7880 {
79 const full_src_path = self.artifact.getEmittedBin().getPath(src_builder);
81 const full_src_path = self.artifact.generated_bin.?.path.?;
8082 const p = fs.Dir.updateFile(cwd, full_src_path, cwd, full_dest_path, .{}) catch |err| {
8183 return step.fail("unable to update file from '{s}' to '{s}': {s}", .{
8284 full_src_path, full_dest_path, @errorName(err),
......@@ -93,9 +95,9 @@ fn make(step: *Step, prog_node: *std.Progress.Node) !void {
9395 }
9496 if (self.artifact.isDynamicLibrary() and
9597 self.artifact.target.isWindows() and
96 self.artifact.emit_implib != .no_emit)
98 self.artifact.generated_implib != null)
9799 {
98 const full_src_path = self.artifact.getEmittedImplib().getPath(src_builder);
100 const full_src_path = self.artifact.generated_implib.?.path.?;
99101 const full_implib_path = dest_builder.getInstallPath(self.dest_dir, self.artifact.out_lib_filename);
100102 const p = fs.Dir.updateFile(cwd, full_src_path, cwd, full_implib_path, .{}) catch |err| {
101103 return step.fail("unable to update file from '{s}' to '{s}': {s}", .{
......@@ -105,7 +107,7 @@ fn make(step: *Step, prog_node: *std.Progress.Node) !void {
105107 all_cached = all_cached and p == .fresh;
106108 }
107109 if (self.pdb_dir) |pdb_dir| {
108 const full_src_path = self.artifact.getEmittedPdb().getPath(src_builder);
110 const full_src_path = self.artifact.generated_pdb.?.path.?;
109111 const full_pdb_path = dest_builder.getInstallPath(pdb_dir, self.artifact.out_pdb_filename);
110112 const p = fs.Dir.updateFile(cwd, full_src_path, cwd, full_pdb_path, .{}) catch |err| {
111113 return step.fail("unable to update file from '{s}' to '{s}': {s}", .{
......@@ -115,7 +117,7 @@ fn make(step: *Step, prog_node: *std.Progress.Node) !void {
115117 all_cached = all_cached and p == .fresh;
116118 }
117119 if (self.h_dir) |h_dir| {
118 const full_src_path = self.artifact.getEmittedH().getPath(src_builder);
120 const full_src_path = self.artifact.generated_h.?.path.?;
119121 const full_h_path = dest_builder.getInstallPath(h_dir, self.artifact.out_h_filename);
120122 const p = fs.Dir.updateFile(cwd, full_src_path, cwd, full_h_path, .{}) catch |err| {
121123 return step.fail("unable to update file from '{s}' to '{s}': {s}", .{
lib/std/Build/Step/Run.zig+5-2
......@@ -164,6 +164,10 @@ pub fn enableTestRunnerMode(self: *Run) void {
164164}
165165
166166pub fn addArtifactArg(self: *Run, artifact: *Step.Compile) void {
167 // enforce creation of the binary file by invoking getEmittedBin
168 const bin_file = artifact.getEmittedBin();
169 bin_file.addStepDependencies(&self.step);
170
167171 self.argv.append(Arg{ .artifact = artifact }) catch @panic("OOM");
168172 self.step.dependOn(&artifact.step);
169173}
......@@ -456,8 +460,7 @@ fn make(step: *Step, prog_node: *std.Progress.Node) !void {
456460 // On Windows we don't have rpaths so we have to add .dll search paths to PATH
457461 self.addPathForDynLibs(artifact);
458462 }
459 const file_path = artifact.installed_path orelse
460 artifact.getEmittedBin().getPath(b);
463 const file_path = artifact.installed_path orelse artifact.generated_bin.?.path.?; // the path is guaranteed to be set
461464
462465 try argv_list.append(file_path);
463466
lib/std/Build/util.zig created+53
......@@ -0,0 +1,53 @@
1const std = @import("std");
2const fs = std.fs;
3
4const Build = std.Build;
5const Step = std.Build.Step;
6
7/// In this function the stderr mutex has already been locked.
8pub fn dumpBadGetPathHelp(
9 s: *Step,
10 stderr: fs.File,
11 src_builder: *Build,
12 asking_step: ?*Step,
13) anyerror!void {
14 const w = stderr.writer();
15 try w.print(
16 \\getPath() was called on a GeneratedFile that wasn't built yet.
17 \\ source package path: {s}
18 \\ Is there a missing Step dependency on step '{s}'?
19 \\
20 , .{
21 src_builder.build_root.path orelse ".",
22 s.name,
23 });
24
25 const tty_config = std.io.tty.detectConfig(stderr);
26 tty_config.setColor(w, .red) catch {};
27 try stderr.writeAll(" The step was created by this stack trace:\n");
28 tty_config.setColor(w, .reset) catch {};
29
30 const debug_info = std.debug.getSelfDebugInfo() catch |err| {
31 try w.print("Unable to dump stack trace: Unable to open debug info: {s}\n", .{@errorName(err)});
32 return;
33 };
34 const ally = debug_info.allocator;
35 std.debug.writeStackTrace(s.getStackTrace(), w, ally, debug_info, tty_config) catch |err| {
36 try stderr.writer().print("Unable to dump stack trace: {s}\n", .{@errorName(err)});
37 return;
38 };
39 if (asking_step) |as| {
40 tty_config.setColor(w, .red) catch {};
41 try stderr.writeAll(" The step that is missing a dependency on the above step was created by this stack trace:\n");
42 tty_config.setColor(w, .reset) catch {};
43
44 std.debug.writeStackTrace(as.getStackTrace(), w, ally, debug_info, tty_config) catch |err| {
45 try stderr.writer().print("Unable to dump stack trace: {s}\n", .{@errorName(err)});
46 return;
47 };
48 }
49
50 tty_config.setColor(w, .red) catch {};
51 try stderr.writeAll(" Hope that helps. Proceeding to panic.\n");
52 tty_config.setColor(w, .reset) catch {};
53}
test/link/glibc_compat/build.zig+1
......@@ -13,6 +13,7 @@ pub fn build(b: *std.Build) void {
1313 ) catch unreachable,
1414 });
1515 exe.linkLibC();
16 _ = exe.getEmittedBin(); // force emission
1617 test_step.dependOn(&exe.step);
1718 }
1819}
test/link/macho/needed_library/build.zig+1
......@@ -23,6 +23,7 @@ fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.Optimize
2323 });
2424 dylib.addCSourceFile(.{ .file = .{ .path = "a.c" }, .flags = &.{} });
2525 dylib.linkLibC();
26 _ = dylib.getEmittedBin(); // enforce emission
2627
2728 // -dead_strip_dylibs
2829 // -needed-la
test/link/macho/search_strategy/build.zig+2
......@@ -60,6 +60,7 @@ fn createScenario(
6060 static.override_dest_dir = std.Build.InstallDir{
6161 .custom = "static",
6262 };
63 _ = static.getEmittedBin(); // enforce emission
6364
6465 const dylib = b.addSharedLibrary(.{
6566 .name = name,
......@@ -72,6 +73,7 @@ fn createScenario(
7273 dylib.override_dest_dir = std.Build.InstallDir{
7374 .custom = "dynamic",
7475 };
76 _ = dylib.getEmittedBin(); // enforce emission
7577
7678 const exe = b.addExecutable(.{
7779 .name = name,
test/src/Cases.zig+4-1
......@@ -551,7 +551,10 @@ pub fn lowerToBuildSteps(
551551 }),
552552 };
553553
554 artifact.emit_bin = if (case.emit_bin) .default else .no_emit;
554 if (case.emit_bin)
555 _ = artifact.getEmittedBin();
556
557 _ = artifact.getEmittedBin(); // TODO(xq): The test cases break if we set all to -fno-emit-X
555558
556559 if (case.link_libc) artifact.linkLibC();
557560
test/standalone.zig+8-10
......@@ -140,16 +140,14 @@ pub const build_cases = [_]BuildCase{
140140 .build_root = "test/standalone/install_raw_hex",
141141 .import = @import("standalone/install_raw_hex/build.zig"),
142142 },
143 // TODO take away EmitOption.emit_to option and make it give a FileSource
144 //.{
145 // .build_root = "test/standalone/emit_asm_and_bin",
146 // .import = @import("standalone/emit_asm_and_bin/build.zig"),
147 //},
148 // TODO take away EmitOption.emit_to option and make it give a FileSource
149 //.{
150 // .build_root = "test/standalone/issue_12588",
151 // .import = @import("standalone/issue_12588/build.zig"),
152 //},
143 .{
144 .build_root = "test/standalone/emit_asm_and_bin",
145 .import = @import("standalone/emit_asm_and_bin/build.zig"),
146 },
147 .{
148 .build_root = "test/standalone/issue_12588",
149 .import = @import("standalone/issue_12588/build.zig"),
150 },
153151 .{
154152 .build_root = "test/standalone/child_process",
155153 .import = @import("standalone/child_process/build.zig"),
test/standalone/embed_generated_file/build.zig+2
......@@ -22,5 +22,7 @@ pub fn build(b: *std.Build) void {
2222 .source_file = bootloader.getEmittedBin(),
2323 });
2424
25 _ = exe.getEmittedBin(); // enforce emission
26
2527 test_step.dependOn(&exe.step);
2628}
test/standalone/emit_asm_and_bin/build.zig+2-2
......@@ -8,8 +8,8 @@ pub fn build(b: *std.Build) void {
88 .root_source_file = .{ .path = "main.zig" },
99 .optimize = b.standardOptimizeOption(.{}),
1010 });
11 main.emit_asm = .{ .emit_to = b.pathFromRoot("main.s") };
12 main.emit_bin = .{ .emit_to = b.pathFromRoot("main") };
11 _ = main.getEmittedBin(); // main.emit_asm = .{ .emit_to = b.pathFromRoot("main.s") };
12 _ = main.getEmittedAsm(); // main.emit_bin = .{ .emit_to = b.pathFromRoot("main") };
1313
1414 test_step.dependOn(&b.addRunArtifact(main).step);
1515}
test/standalone/issue_12588/build.zig+2-3
......@@ -13,9 +13,8 @@ pub fn build(b: *std.Build) void {
1313 .optimize = optimize,
1414 .target = target,
1515 });
16 obj.emit_llvm_ir = .{ .emit_to = b.pathFromRoot("main.ll") };
17 obj.emit_llvm_bc = .{ .emit_to = b.pathFromRoot("main.bc") };
18 obj.emit_bin = .no_emit;
16 _ = obj.getEmittedLlvmIr();
17 _ = obj.getEmittedLlvmBc();
1918 b.default_step.dependOn(&obj.step);
2019
2120 test_step.dependOn(&obj.step);
test/standalone/issue_339/build.zig+2
......@@ -14,5 +14,7 @@ pub fn build(b: *std.Build) void {
1414 .optimize = optimize,
1515 });
1616
17 _ = obj.getEmittedBin(); // enforce emission
18
1719 test_step.dependOn(&obj.step);
1820}
test/standalone/issue_5825/build.zig+2
......@@ -27,5 +27,7 @@ pub fn build(b: *std.Build) void {
2727 exe.linkSystemLibrary("ntdll");
2828 exe.addObject(obj);
2929
30 _ = exe.getEmittedBin(); // enforce emission
31
3032 test_step.dependOn(&exe.step);
3133}
test/standalone/issue_794/build.zig+1
......@@ -8,6 +8,7 @@ pub fn build(b: *std.Build) void {
88 .root_source_file = .{ .path = "main.zig" },
99 });
1010 test_artifact.addIncludePath(.{ .path = "a_directory" });
11 _ = test_artifact.getEmittedBin(); // enforce emission
1112
1213 test_step.dependOn(&test_artifact.step);
1314}
test/standalone/main_pkg_path/build.zig+1-1
......@@ -7,7 +7,7 @@ pub fn build(b: *std.Build) void {
77 const test_exe = b.addTest(.{
88 .root_source_file = .{ .path = "a/test.zig" },
99 });
10 test_exe.setMainPkgPath(.{.path="."});
10 test_exe.setMainPkgPath(.{ .path = "." });
1111
1212 test_step.dependOn(&b.addRunArtifact(test_exe).step);
1313}
test/standalone/strip_empty_loop/build.zig+3
......@@ -14,5 +14,8 @@ pub fn build(b: *std.Build) void {
1414 .target = target,
1515 });
1616 main.strip = true;
17
18 _ = main.getEmittedBin(); // enforce emission
19
1720 test_step.dependOn(&main.step);
1821}
test/standalone/use_alias/build.zig+1-1
......@@ -10,7 +10,7 @@ pub fn build(b: *std.Build) void {
1010 .root_source_file = .{ .path = "main.zig" },
1111 .optimize = optimize,
1212 });
13 main.addIncludePath(.{.path="."});
13 main.addIncludePath(.{ .path = "." });
1414
1515 test_step.dependOn(&b.addRunArtifact(main).step);
1616}
test/tests.zig+2
......@@ -588,6 +588,8 @@ pub fn addStandaloneTests(
588588 });
589589 if (case.link_libc) exe.linkLibC();
590590
591 _ = exe.getEmittedBin(); // Force emission
592
591593 step.dependOn(&exe.step);
592594 }
593595