Skip to content

Commit d7aeadb

Browse files
author
Felix "xq" Queißner
committed
Introduces Compile.forceBuild() and Compile.forceEmit(…)
1 parent 59fc4b6 commit d7aeadb

File tree

15 files changed

+69
-29
lines changed

15 files changed

+69
-29
lines changed

lib/std/Build/Step/Compile.zig

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,8 @@ use_lld: ?bool,
207207
/// otherwise.
208208
expect_errors: []const []const u8 = &.{},
209209

210+
force_build: bool,
211+
210212
emit_directory: GeneratedFile,
211213

212214
generated_docs: ?*GeneratedFile,
@@ -374,6 +376,17 @@ pub const Kind = enum {
374376

375377
pub const Linkage = enum { dynamic, static };
376378

379+
pub const EmitOption = enum {
380+
docs,
381+
@"asm",
382+
bin,
383+
pdb,
384+
implib,
385+
llvm_bc,
386+
llvm_ir,
387+
h,
388+
};
389+
377390
pub 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.
993+
pub fn forceBuild(self: *Compile) void {
994+
self.force_build = true;
995+
}
996+
997+
pub 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+
9751010
pub 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

11651200
fn 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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}

0 commit comments

Comments
 (0)