authorgravatar for xq@random-projects.netFelix "xq" Queißner <xq@random-projects.net> 2023-07-26 16:32:50+02:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-07-30 11:18:50-07:00
log35d0a49db911e9c459902a021a5bfa05c97ca107
tree4c3d3c526fe4bcca50ca30fb1ec0ff1b785360f4
parent5c0181841081170a118d8e50af2a09f5006f59e1

Introduces Compile.forceBuild() and Compile.forceEmit(…)


15 files changed, 69 insertions(+), 29 deletions(-)

lib/std/Build/Step/Compile.zig+38-3
......@@ -207,6 +207,8 @@ use_lld: ?bool,
207207/// otherwise.
208208expect_errors: []const []const u8 = &.{},
209209
210force_build: bool,
211
210212emit_directory: GeneratedFile,
211213
212214generated_docs: ?*GeneratedFile,
......@@ -374,6 +376,17 @@ pub const Kind = enum {
374376
375377pub const Linkage = enum { dynamic, static };
376378
379pub const EmitOption = enum {
380 docs,
381 @"asm",
382 bin,
383 pdb,
384 implib,
385 llvm_bc,
386 llvm_ir,
387 h,
388};
389
377390pub fn create(owner: *std.Build, options: Options) *Compile {
378391 const name = owner.dupe(options.name);
379392 const root_src: ?LazyPath = if (options.root_source_file) |rsrc| rsrc.dupe(owner) else null;
......@@ -468,6 +481,8 @@ pub fn create(owner: *std.Build, options: Options) *Compile {
468481 .installed_path = null,
469482 .force_undefined_symbols = StringHashMap(void).init(owner.allocator),
470483
484 .force_build = false,
485
471486 .emit_directory = GeneratedFile{ .step = &self.step },
472487
473488 .generated_docs = null,
......@@ -972,6 +987,26 @@ fn getEmittedFileGeneric(self: *Compile, output_file: *?*GeneratedFile) LazyPath
972987 return .{ .generated = generated_file };
973988}
974989
990/// Disables the panic in the build evaluation if nothing is emitted.
991///
992/// Unless for compilation tests this is a code smell.
993pub fn forceBuild(self: *Compile) void {
994 self.force_build = true;
995}
996
997pub fn forceEmit(self: *Compile, emit: EmitOption) void {
998 switch (emit) {
999 .docs => _ = self.getEmittedDocs(),
1000 .@"asm" => _ = self.getEmittedAsm(),
1001 .bin => _ = self.getEmittedBin(),
1002 .pdb => _ = self.getEmittedPdb(),
1003 .implib => _ = self.getEmittedImplib(),
1004 .llvm_bc => _ = self.getEmittedLlvmBc(),
1005 .llvm_ir => _ = self.getEmittedLlvmIr(),
1006 .h => _ = self.getEmittedH(),
1007 }
1008}
1009
9751010pub const getOutputDirectorySource = getEmitDirectory; // DEPRECATED, use getEmitDirectory
9761011
9771012/// Returns the path to the output directory.
......@@ -1163,7 +1198,7 @@ pub fn setExecCmd(self: *Compile, args: []const ?[]const u8) void {
11631198}
11641199
11651200fn linkLibraryOrObject(self: *Compile, other: *Compile) void {
1166 _ = other.getEmittedBin(); // Force emission of the binary
1201 other.forceEmit(.bin);
11671202
11681203 if (other.kind == .lib and other.target.isWindows()) { // TODO(xq): Is this the correct logic here?
11691204 _ = other.getEmittedImplib(); // Force emission of the binary
......@@ -1568,11 +1603,11 @@ fn make(step: *Step, prog_node: *std.Progress.Node) !void {
15681603 if (file.ptr != null) any_emitted_file = true;
15691604 }
15701605
1571 if (!any_emitted_file) {
1606 if (!any_emitted_file and !self.force_build) {
15721607 std.debug.getStderrMutex().lock();
15731608 const stderr = std.io.getStdErr();
15741609 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});
1610 std.debug.panic("Artifact '{s}' has no emit options set, but it is made. Did you forget to call `.getEmitted*()`? If not, use `.forceBuild()` or `.forceEmit(…)` to make sure it builds anyways.", .{self.name});
15761611 }
15771612
15781613 try addFlag(&zig_args, "strip", self.strip);
lib/std/Build/Step/InstallArtifact.zig+2-1
......@@ -42,7 +42,8 @@ pub fn create(owner: *std.Build, artifact: *Step.Compile) *InstallArtifact {
4242 };
4343 self.step.dependOn(&artifact.step);
4444
45 _ = artifact.getEmittedBin(); // force creation
45 artifact.forceEmit(.bin);
46
4647 owner.pushInstalledFile(self.dest_dir, artifact.out_filename);
4748 if (self.artifact.isDynamicLibrary()) {
4849 if (artifact.major_only_filename) |name| {
test/link/glibc_compat/build.zig+1-1
......@@ -13,7 +13,7 @@ pub fn build(b: *std.Build) void {
1313 ) catch unreachable,
1414 });
1515 exe.linkLibC();
16 _ = exe.getEmittedBin(); // force emission
16 exe.forceBuild();
1717 test_step.dependOn(&exe.step);
1818 }
1919}
test/link/macho/needed_library/build.zig+1-1
......@@ -23,7 +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
26 dylib.forceEmit(.bin); // enforce library creation, we import it below
2727
2828 // -dead_strip_dylibs
2929 // -needed-la
test/link/macho/search_strategy/build.zig+2-2
......@@ -60,7 +60,7 @@ fn createScenario(
6060 static.override_dest_dir = std.Build.InstallDir{
6161 .custom = "static",
6262 };
63 _ = static.getEmittedBin(); // enforce emission
63 static.forceEmit(.bin);
6464
6565 const dylib = b.addSharedLibrary(.{
6666 .name = name,
......@@ -73,7 +73,7 @@ fn createScenario(
7373 dylib.override_dest_dir = std.Build.InstallDir{
7474 .custom = "dynamic",
7575 };
76 _ = dylib.getEmittedBin(); // enforce emission
76 dylib.forceEmit(.bin); // we want the binary to be built as we use it further below
7777
7878 const exe = b.addExecutable(.{
7979 .name = name,
test/src/Cases.zig+5-4
......@@ -551,10 +551,11 @@ pub fn lowerToBuildSteps(
551551 }),
552552 };
553553
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
554 if (case.emit_bin) {
555 artifact.forceEmit(.bin);
556 } else {
557 artifact.forceBuild();
558 }
558559
559560 if (case.link_libc) artifact.linkLibC();
560561
test/standalone.zig+10-8
......@@ -140,14 +140,16 @@ pub const build_cases = [_]BuildCase{
140140 .build_root = "test/standalone/install_raw_hex",
141141 .import = @import("standalone/install_raw_hex/build.zig"),
142142 },
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 },
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 // },
151153 .{
152154 .build_root = "test/standalone/child_process",
153155 .import = @import("standalone/child_process/build.zig"),
test/standalone/coff_dwarf/build.zig+1-1
......@@ -23,7 +23,7 @@ pub fn build(b: *std.Build) void {
2323 .optimize = optimize,
2424 .target = target,
2525 });
26 lib.addCSourceFile("shared_lib.c", &.{"-gdwarf"});
26 lib.addCSourceFile(.{ .file = "shared_lib.c", .flags = &.{"-gdwarf"} });
2727 lib.linkLibC();
2828 exe.linkLibrary(lib);
2929
test/standalone/embed_generated_file/build.zig+1-1
......@@ -22,7 +22,7 @@ pub fn build(b: *std.Build) void {
2222 .source_file = bootloader.getEmittedBin(),
2323 });
2424
25 _ = exe.getEmittedBin(); // enforce emission
25 exe.forceBuild();
2626
2727 test_step.dependOn(&exe.step);
2828}
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.getEmittedBin(); // main.emit_asm = .{ .emit_to = b.pathFromRoot("main.s") };
12 _ = main.getEmittedAsm(); // main.emit_bin = .{ .emit_to = b.pathFromRoot("main") };
11 main.forceEmit(.bin);
12 main.forceEmit(.@"asm");
1313
1414 test_step.dependOn(&b.addRunArtifact(main).step);
1515}
test/standalone/issue_339/build.zig+1-1
......@@ -14,7 +14,7 @@ pub fn build(b: *std.Build) void {
1414 .optimize = optimize,
1515 });
1616
17 _ = obj.getEmittedBin(); // enforce emission
17 obj.forceBuild();
1818
1919 test_step.dependOn(&obj.step);
2020}
test/standalone/issue_5825/build.zig+1-1
......@@ -27,7 +27,7 @@ pub fn build(b: *std.Build) void {
2727 exe.linkSystemLibrary("ntdll");
2828 exe.addObject(obj);
2929
30 _ = exe.getEmittedBin(); // enforce emission
30 exe.forceBuild();
3131
3232 test_step.dependOn(&exe.step);
3333}
test/standalone/issue_794/build.zig+2-1
......@@ -8,7 +8,8 @@ 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
11
12 test_artifact.forceBuild();
1213
1314 test_step.dependOn(&test_artifact.step);
1415}
test/standalone/strip_empty_loop/build.zig+1-1
......@@ -15,7 +15,7 @@ pub fn build(b: *std.Build) void {
1515 });
1616 main.strip = true;
1717
18 _ = main.getEmittedBin(); // enforce emission
18 main.forceBuild();
1919
2020 test_step.dependOn(&main.step);
2121}
test/tests.zig+1-1
......@@ -588,7 +588,7 @@ pub fn addStandaloneTests(
588588 });
589589 if (case.link_libc) exe.linkLibC();
590590
591 _ = exe.getEmittedBin(); // Force emission
591 exe.forceBuild();
592592
593593 step.dependOn(&exe.step);
594594 }