fix build logic due to state mutations and break the API accordingly
* remove setName, setFilter, and setTestRunner. Please set these options directly when creating the CompileStep. * removed unused field * remove computeOutFileNames and inline the logic, making clear the goal of avoiding state mutations after the build step is created.
This commit is contained in:
@@ -97,8 +97,6 @@ out_lib_filename: []const u8,
|
||||
out_pdb_filename: []const u8,
|
||||
modules: std.StringArrayHashMap(*Module),
|
||||
|
||||
object_src: []const u8,
|
||||
|
||||
link_objects: ArrayList(LinkObject),
|
||||
include_dirs: ArrayList(IncludeDir),
|
||||
c_macros: ArrayList([]const u8),
|
||||
@@ -287,6 +285,8 @@ pub const Options = struct {
|
||||
linkage: ?Linkage = null,
|
||||
version: ?std.builtin.Version = null,
|
||||
max_rss: usize = 0,
|
||||
filter: ?[]const u8 = null,
|
||||
test_runner: ?[]const u8 = null,
|
||||
};
|
||||
|
||||
pub const Kind = enum {
|
||||
@@ -339,6 +339,23 @@ pub fn create(owner: *std.Build, options: Options) *CompileStep {
|
||||
options.target.zigTriple(owner.allocator) catch @panic("OOM"),
|
||||
});
|
||||
|
||||
const target_info = NativeTargetInfo.detect(options.target) catch @panic("unhandled error");
|
||||
|
||||
const out_filename = std.zig.binNameAlloc(owner.allocator, .{
|
||||
.root_name = name,
|
||||
.target = target_info.target,
|
||||
.output_mode = switch (options.kind) {
|
||||
.lib => .Lib,
|
||||
.obj => .Obj,
|
||||
.exe, .@"test" => .Exe,
|
||||
},
|
||||
.link_mode = if (options.linkage) |some| @as(std.builtin.LinkMode, switch (some) {
|
||||
.dynamic => .Dynamic,
|
||||
.static => .Static,
|
||||
}) else null,
|
||||
.version = options.version,
|
||||
}) catch @panic("OOM");
|
||||
|
||||
const self = owner.allocator.create(CompileStep) catch @panic("OOM");
|
||||
self.* = CompileStep{
|
||||
.strip = null,
|
||||
@@ -360,7 +377,7 @@ pub fn create(owner: *std.Build, options: Options) *CompileStep {
|
||||
.max_rss = options.max_rss,
|
||||
}),
|
||||
.version = options.version,
|
||||
.out_filename = undefined,
|
||||
.out_filename = out_filename,
|
||||
.out_h_filename = owner.fmt("{s}.h", .{name}),
|
||||
.out_lib_filename = undefined,
|
||||
.out_pdb_filename = owner.fmt("{s}.pdb", .{name}),
|
||||
@@ -374,13 +391,12 @@ pub fn create(owner: *std.Build, options: Options) *CompileStep {
|
||||
.rpaths = ArrayList(FileSource).init(owner.allocator),
|
||||
.framework_dirs = ArrayList(FileSource).init(owner.allocator),
|
||||
.installed_headers = ArrayList(*Step).init(owner.allocator),
|
||||
.object_src = undefined,
|
||||
.c_std = std.Build.CStd.C99,
|
||||
.zig_lib_dir = null,
|
||||
.main_pkg_path = null,
|
||||
.exec_cmd_args = null,
|
||||
.filter = null,
|
||||
.test_runner = null,
|
||||
.filter = options.filter,
|
||||
.test_runner = options.test_runner,
|
||||
.disable_stack_probing = false,
|
||||
.disable_sanitize_c = false,
|
||||
.sanitize_thread = false,
|
||||
@@ -395,60 +411,41 @@ pub fn create(owner: *std.Build, options: Options) *CompileStep {
|
||||
.output_pdb_path_source = GeneratedFile{ .step = &self.step },
|
||||
.output_dirname_source = GeneratedFile{ .step = &self.step },
|
||||
|
||||
.target_info = NativeTargetInfo.detect(self.target) catch @panic("unhandled error"),
|
||||
.target_info = target_info,
|
||||
};
|
||||
self.computeOutFileNames();
|
||||
if (root_src) |rs| rs.addStepDependencies(&self.step);
|
||||
return self;
|
||||
}
|
||||
|
||||
fn computeOutFileNames(self: *CompileStep) void {
|
||||
const b = self.step.owner;
|
||||
const target = self.target_info.target;
|
||||
|
||||
self.out_filename = std.zig.binNameAlloc(b.allocator, .{
|
||||
.root_name = self.name,
|
||||
.target = target,
|
||||
.output_mode = switch (self.kind) {
|
||||
.lib => .Lib,
|
||||
.obj => .Obj,
|
||||
.exe, .@"test" => .Exe,
|
||||
},
|
||||
.link_mode = if (self.linkage) |some| @as(std.builtin.LinkMode, switch (some) {
|
||||
.dynamic => .Dynamic,
|
||||
.static => .Static,
|
||||
}) else null,
|
||||
.version = self.version,
|
||||
}) catch @panic("OOM");
|
||||
|
||||
if (self.kind == .lib) {
|
||||
if (self.linkage != null and self.linkage.? == .static) {
|
||||
self.out_lib_filename = self.out_filename;
|
||||
} else if (self.version) |version| {
|
||||
if (target.isDarwin()) {
|
||||
self.major_only_filename = b.fmt("lib{s}.{d}.dylib", .{
|
||||
if (target_info.target.isDarwin()) {
|
||||
self.major_only_filename = owner.fmt("lib{s}.{d}.dylib", .{
|
||||
self.name,
|
||||
version.major,
|
||||
});
|
||||
self.name_only_filename = b.fmt("lib{s}.dylib", .{self.name});
|
||||
self.name_only_filename = owner.fmt("lib{s}.dylib", .{self.name});
|
||||
self.out_lib_filename = self.out_filename;
|
||||
} else if (target.os.tag == .windows) {
|
||||
self.out_lib_filename = b.fmt("{s}.lib", .{self.name});
|
||||
} else if (target_info.target.os.tag == .windows) {
|
||||
self.out_lib_filename = owner.fmt("{s}.lib", .{self.name});
|
||||
} else {
|
||||
self.major_only_filename = b.fmt("lib{s}.so.{d}", .{ self.name, version.major });
|
||||
self.name_only_filename = b.fmt("lib{s}.so", .{self.name});
|
||||
self.major_only_filename = owner.fmt("lib{s}.so.{d}", .{ self.name, version.major });
|
||||
self.name_only_filename = owner.fmt("lib{s}.so", .{self.name});
|
||||
self.out_lib_filename = self.out_filename;
|
||||
}
|
||||
} else {
|
||||
if (target.isDarwin()) {
|
||||
if (target_info.target.isDarwin()) {
|
||||
self.out_lib_filename = self.out_filename;
|
||||
} else if (target.os.tag == .windows) {
|
||||
self.out_lib_filename = b.fmt("{s}.lib", .{self.name});
|
||||
} else if (target_info.target.os.tag == .windows) {
|
||||
self.out_lib_filename = owner.fmt("{s}.lib", .{self.name});
|
||||
} else {
|
||||
self.out_lib_filename = self.out_filename;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (root_src) |rs| rs.addStepDependencies(&self.step);
|
||||
|
||||
return self;
|
||||
}
|
||||
|
||||
pub fn installHeader(cs: *CompileStep, src_path: []const u8, dest_rel_path: []const u8) void {
|
||||
@@ -841,24 +838,6 @@ fn linkSystemLibraryInner(self: *CompileStep, name: []const u8, opts: struct {
|
||||
}) catch @panic("OOM");
|
||||
}
|
||||
|
||||
pub fn setName(self: *CompileStep, text: []const u8) void {
|
||||
const b = self.step.owner;
|
||||
assert(self.kind == .@"test");
|
||||
self.name = b.dupe(text);
|
||||
}
|
||||
|
||||
pub fn setFilter(self: *CompileStep, text: ?[]const u8) void {
|
||||
const b = self.step.owner;
|
||||
assert(self.kind == .@"test");
|
||||
self.filter = if (text) |t| b.dupe(t) else null;
|
||||
}
|
||||
|
||||
pub fn setTestRunner(self: *CompileStep, path: ?[]const u8) void {
|
||||
const b = self.step.owner;
|
||||
assert(self.kind == .@"test");
|
||||
self.test_runner = if (path) |p| b.dupePath(p) else null;
|
||||
}
|
||||
|
||||
/// Handy when you have many C/C++ source files and want them all to have the same flags.
|
||||
pub fn addCSourceFiles(self: *CompileStep, files: []const []const u8, flags: []const []const u8) void {
|
||||
const b = self.step.owner;
|
||||
|
||||
Reference in New Issue
Block a user