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,...@@ -207,6 +207,8 @@ use_lld: ?bool,
207/// otherwise.207/// otherwise.
208expect_errors: []const []const u8 = &.{},208expect_errors: []const []const u8 = &.{},
209209
210force_build: bool,
211
210emit_directory: GeneratedFile,212emit_directory: GeneratedFile,
211213
212generated_docs: ?*GeneratedFile,214generated_docs: ?*GeneratedFile,
...@@ -374,6 +376,17 @@ pub const Kind = enum {...@@ -374,6 +376,17 @@ pub const Kind = enum {
374376
375pub const Linkage = enum { dynamic, static };377pub 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
377pub fn create(owner: *std.Build, options: Options) *Compile {390pub fn create(owner: *std.Build, options: Options) *Compile {
378 const name = owner.dupe(options.name);391 const name = owner.dupe(options.name);
379 const root_src: ?LazyPath = if (options.root_source_file) |rsrc| rsrc.dupe(owner) else null;392 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 {...@@ -468,6 +481,8 @@ pub fn create(owner: *std.Build, options: Options) *Compile {
468 .installed_path = null,481 .installed_path = null,
469 .force_undefined_symbols = StringHashMap(void).init(owner.allocator),482 .force_undefined_symbols = StringHashMap(void).init(owner.allocator),
470483
484 .force_build = false,
485
471 .emit_directory = GeneratedFile{ .step = &self.step },486 .emit_directory = GeneratedFile{ .step = &self.step },
472487
473 .generated_docs = null,488 .generated_docs = null,
...@@ -972,6 +987,26 @@ fn getEmittedFileGeneric(self: *Compile, output_file: *?*GeneratedFile) LazyPath...@@ -972,6 +987,26 @@ fn getEmittedFileGeneric(self: *Compile, output_file: *?*GeneratedFile) LazyPath
972 return .{ .generated = generated_file };987 return .{ .generated = generated_file };
973}988}
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
975pub const getOutputDirectorySource = getEmitDirectory; // DEPRECATED, use getEmitDirectory1010pub const getOutputDirectorySource = getEmitDirectory; // DEPRECATED, use getEmitDirectory
9761011
977/// Returns the path to the output directory.1012/// Returns the path to the output directory.
...@@ -1163,7 +1198,7 @@ pub fn setExecCmd(self: *Compile, args: []const ?[]const u8) void {...@@ -1163,7 +1198,7 @@ pub fn setExecCmd(self: *Compile, args: []const ?[]const u8) void {
1163}1198}
11641199
1165fn linkLibraryOrObject(self: *Compile, other: *Compile) void {1200fn linkLibraryOrObject(self: *Compile, other: *Compile) void {
1166 _ = other.getEmittedBin(); // Force emission of the binary1201 other.forceEmit(.bin);
11671202
1168 if (other.kind == .lib and other.target.isWindows()) { // TODO(xq): Is this the correct logic here?1203 if (other.kind == .lib and other.target.isWindows()) { // TODO(xq): Is this the correct logic here?
1169 _ = other.getEmittedImplib(); // Force emission of the binary1204 _ = other.getEmittedImplib(); // Force emission of the binary
...@@ -1568,11 +1603,11 @@ fn make(step: *Step, prog_node: *std.Progress.Node) !void {...@@ -1568,11 +1603,11 @@ fn make(step: *Step, prog_node: *std.Progress.Node) !void {
1568 if (file.ptr != null) any_emitted_file = true;1603 if (file.ptr != null) any_emitted_file = true;
1569 }1604 }
15701605
1571 if (!any_emitted_file) {1606 if (!any_emitted_file and !self.force_build) {
1572 std.debug.getStderrMutex().lock();1607 std.debug.getStderrMutex().lock();
1573 const stderr = std.io.getStdErr();1608 const stderr = std.io.getStdErr();
1574 build_util.dumpBadGetPathHelp(&self.step, stderr, self.step.owner, null) catch {};1609 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});
1576 }1611 }
15771612
1578 try addFlag(&zig_args, "strip", self.strip);1613 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 {...@@ -42,7 +42,8 @@ pub fn create(owner: *std.Build, artifact: *Step.Compile) *InstallArtifact {
42 };42 };
43 self.step.dependOn(&artifact.step);43 self.step.dependOn(&artifact.step);
4444
45 _ = artifact.getEmittedBin(); // force creation45 artifact.forceEmit(.bin);
46
46 owner.pushInstalledFile(self.dest_dir, artifact.out_filename);47 owner.pushInstalledFile(self.dest_dir, artifact.out_filename);
47 if (self.artifact.isDynamicLibrary()) {48 if (self.artifact.isDynamicLibrary()) {
48 if (artifact.major_only_filename) |name| {49 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 {...@@ -13,7 +13,7 @@ pub fn build(b: *std.Build) void {
13 ) catch unreachable,13 ) catch unreachable,
14 });14 });
15 exe.linkLibC();15 exe.linkLibC();
16 _ = exe.getEmittedBin(); // force emission16 exe.forceBuild();
17 test_step.dependOn(&exe.step);17 test_step.dependOn(&exe.step);
18 }18 }
19}19}
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...@@ -23,7 +23,7 @@ fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.Optimize
23 });23 });
24 dylib.addCSourceFile(.{ .file = .{ .path = "a.c" }, .flags = &.{} });24 dylib.addCSourceFile(.{ .file = .{ .path = "a.c" }, .flags = &.{} });
25 dylib.linkLibC();25 dylib.linkLibC();
26 _ = dylib.getEmittedBin(); // enforce emission26 dylib.forceEmit(.bin); // enforce library creation, we import it below
2727
28 // -dead_strip_dylibs28 // -dead_strip_dylibs
29 // -needed-la29 // -needed-la
test/link/macho/search_strategy/build.zig+2-2
...@@ -60,7 +60,7 @@ fn createScenario(...@@ -60,7 +60,7 @@ fn createScenario(
60 static.override_dest_dir = std.Build.InstallDir{60 static.override_dest_dir = std.Build.InstallDir{
61 .custom = "static",61 .custom = "static",
62 };62 };
63 _ = static.getEmittedBin(); // enforce emission63 static.forceEmit(.bin);
6464
65 const dylib = b.addSharedLibrary(.{65 const dylib = b.addSharedLibrary(.{
66 .name = name,66 .name = name,
...@@ -73,7 +73,7 @@ fn createScenario(...@@ -73,7 +73,7 @@ fn createScenario(
73 dylib.override_dest_dir = std.Build.InstallDir{73 dylib.override_dest_dir = std.Build.InstallDir{
74 .custom = "dynamic",74 .custom = "dynamic",
75 };75 };
76 _ = dylib.getEmittedBin(); // enforce emission76 dylib.forceEmit(.bin); // we want the binary to be built as we use it further below
7777
78 const exe = b.addExecutable(.{78 const exe = b.addExecutable(.{
79 .name = name,79 .name = name,
test/src/Cases.zig+5-4
...@@ -551,10 +551,11 @@ pub fn lowerToBuildSteps(...@@ -551,10 +551,11 @@ pub fn lowerToBuildSteps(
551 }),551 }),
552 };552 };
553553
554 if (case.emit_bin)554 if (case.emit_bin) {
555 _ = artifact.getEmittedBin();555 artifact.forceEmit(.bin);
556556 } else {
557 _ = artifact.getEmittedBin(); // TODO(xq): The test cases break if we set all to -fno-emit-X557 artifact.forceBuild();
558 }
558559
559 if (case.link_libc) artifact.linkLibC();560 if (case.link_libc) artifact.linkLibC();
560561
test/standalone.zig+10-8
...@@ -140,14 +140,16 @@ pub const build_cases = [_]BuildCase{...@@ -140,14 +140,16 @@ pub const build_cases = [_]BuildCase{
140 .build_root = "test/standalone/install_raw_hex",140 .build_root = "test/standalone/install_raw_hex",
141 .import = @import("standalone/install_raw_hex/build.zig"),141 .import = @import("standalone/install_raw_hex/build.zig"),
142 },142 },
143 .{143 // TODO take away EmitOption.emit_to option and make it give a FileSource
144 .build_root = "test/standalone/emit_asm_and_bin",144 // .{
145 .import = @import("standalone/emit_asm_and_bin/build.zig"),145 // .build_root = "test/standalone/emit_asm_and_bin",
146 },146 // .import = @import("standalone/emit_asm_and_bin/build.zig"),
147 .{147 // },
148 .build_root = "test/standalone/issue_12588",148 // TODO take away EmitOption.emit_to option and make it give a FileSource
149 .import = @import("standalone/issue_12588/build.zig"),149 // .{
150 },150 // .build_root = "test/standalone/issue_12588",
151 // .import = @import("standalone/issue_12588/build.zig"),
152 // },
151 .{153 .{
152 .build_root = "test/standalone/child_process",154 .build_root = "test/standalone/child_process",
153 .import = @import("standalone/child_process/build.zig"),155 .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 {...@@ -23,7 +23,7 @@ pub fn build(b: *std.Build) void {
23 .optimize = optimize,23 .optimize = optimize,
24 .target = target,24 .target = target,
25 });25 });
26 lib.addCSourceFile("shared_lib.c", &.{"-gdwarf"});26 lib.addCSourceFile(.{ .file = "shared_lib.c", .flags = &.{"-gdwarf"} });
27 lib.linkLibC();27 lib.linkLibC();
28 exe.linkLibrary(lib);28 exe.linkLibrary(lib);
2929
test/standalone/embed_generated_file/build.zig+1-1
...@@ -22,7 +22,7 @@ pub fn build(b: *std.Build) void {...@@ -22,7 +22,7 @@ pub fn build(b: *std.Build) void {
22 .source_file = bootloader.getEmittedBin(),22 .source_file = bootloader.getEmittedBin(),
23 });23 });
2424
25 _ = exe.getEmittedBin(); // enforce emission25 exe.forceBuild();
2626
27 test_step.dependOn(&exe.step);27 test_step.dependOn(&exe.step);
28}28}
test/standalone/emit_asm_and_bin/build.zig+2-2
...@@ -8,8 +8,8 @@ pub fn build(b: *std.Build) void {...@@ -8,8 +8,8 @@ pub fn build(b: *std.Build) void {
8 .root_source_file = .{ .path = "main.zig" },8 .root_source_file = .{ .path = "main.zig" },
9 .optimize = b.standardOptimizeOption(.{}),9 .optimize = b.standardOptimizeOption(.{}),
10 });10 });
11 _ = main.getEmittedBin(); // main.emit_asm = .{ .emit_to = b.pathFromRoot("main.s") };11 main.forceEmit(.bin);
12 _ = main.getEmittedAsm(); // main.emit_bin = .{ .emit_to = b.pathFromRoot("main") };12 main.forceEmit(.@"asm");
1313
14 test_step.dependOn(&b.addRunArtifact(main).step);14 test_step.dependOn(&b.addRunArtifact(main).step);
15}15}
test/standalone/issue_339/build.zig+1-1
...@@ -14,7 +14,7 @@ pub fn build(b: *std.Build) void {...@@ -14,7 +14,7 @@ pub fn build(b: *std.Build) void {
14 .optimize = optimize,14 .optimize = optimize,
15 });15 });
1616
17 _ = obj.getEmittedBin(); // enforce emission17 obj.forceBuild();
1818
19 test_step.dependOn(&obj.step);19 test_step.dependOn(&obj.step);
20}20}
test/standalone/issue_5825/build.zig+1-1
...@@ -27,7 +27,7 @@ pub fn build(b: *std.Build) void {...@@ -27,7 +27,7 @@ pub fn build(b: *std.Build) void {
27 exe.linkSystemLibrary("ntdll");27 exe.linkSystemLibrary("ntdll");
28 exe.addObject(obj);28 exe.addObject(obj);
2929
30 _ = exe.getEmittedBin(); // enforce emission30 exe.forceBuild();
3131
32 test_step.dependOn(&exe.step);32 test_step.dependOn(&exe.step);
33}33}
test/standalone/issue_794/build.zig+2-1
...@@ -8,7 +8,8 @@ pub fn build(b: *std.Build) void {...@@ -8,7 +8,8 @@ pub fn build(b: *std.Build) void {
8 .root_source_file = .{ .path = "main.zig" },8 .root_source_file = .{ .path = "main.zig" },
9 });9 });
10 test_artifact.addIncludePath(.{ .path = "a_directory" });10 test_artifact.addIncludePath(.{ .path = "a_directory" });
11 _ = test_artifact.getEmittedBin(); // enforce emission11
12 test_artifact.forceBuild();
1213
13 test_step.dependOn(&test_artifact.step);14 test_step.dependOn(&test_artifact.step);
14}15}
test/standalone/strip_empty_loop/build.zig+1-1
...@@ -15,7 +15,7 @@ pub fn build(b: *std.Build) void {...@@ -15,7 +15,7 @@ pub fn build(b: *std.Build) void {
15 });15 });
16 main.strip = true;16 main.strip = true;
1717
18 _ = main.getEmittedBin(); // enforce emission18 main.forceBuild();
1919
20 test_step.dependOn(&main.step);20 test_step.dependOn(&main.step);
21}21}
test/tests.zig+1-1
...@@ -588,7 +588,7 @@ pub fn addStandaloneTests(...@@ -588,7 +588,7 @@ pub fn addStandaloneTests(
588 });588 });
589 if (case.link_libc) exe.linkLibC();589 if (case.link_libc) exe.linkLibC();
590590
591 _ = exe.getEmittedBin(); // Force emission591 exe.forceBuild();
592592
593 step.dependOn(&exe.step);593 step.dependOn(&exe.step);
594 }594 }