authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-12-05 16:09:07-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2024-01-01 17:51:18-07:00
logb92e30ff0bd2b77a486451b21d17666a311407f3
treecd2504c6815b27486e554c2541d4496d7e8f91ab
parentf5613a0e3589fcca51411ce379f3c90eace99fa6

std.Build.ResolvedTarget: rename target field to result

This change is seemingly insignificant but I actually agonized over this for three days. Some other things I considered: * (status quo in master branch) make Compile step creation functions accept a Target.Query and delete the ResolvedTarget struct. - downside: redundantly resolve target queries many times * same as before but additionally add a hash map to cache target query resolutions. - downside: now there is a hash map that doesn't actually need to exist, just to make the API more ergonomic. * add is_native_os and is_native_abi fields to std.Target and use it directly as the result of resolving a target query. - downside: they really don't belong there. They would be available as comptime booleans via `@import("builtin")` but they should not be exposed that way. With this change the downsides are: * the option name of addExecutable and friends is `target` instead of `resolved_target` matching the type name. - upside: this does not break compatibility with existing build scripts * you likely end up seeing `target.result.cpu.arch` rather than `target.cpu.arch`. - upside: this is an improvement over `target.target.cpu.arch` which it was before this commit. - downside: `b.host.target` is now `b.host.result`.

16 files changed, 43 insertions(+), 41 deletions(-)

build.zig+4-4
......@@ -221,7 +221,7 @@ pub fn build(b: *std.Build) !void {
221221
222222 test_step.dependOn(&exe.step);
223223
224 if (target.target.os.tag == .windows and target.target.abi == .gnu) {
224 if (target.result.os.tag == .windows and target.result.abi == .gnu) {
225225 // LTO is currently broken on mingw, this can be removed when it's fixed.
226226 exe.want_lto = false;
227227 check_case_exe.want_lto = false;
......@@ -347,7 +347,7 @@ pub fn build(b: *std.Build) !void {
347347 try addStaticLlvmOptionsToExe(exe);
348348 try addStaticLlvmOptionsToExe(check_case_exe);
349349 }
350 if (target.target.os.tag == .windows) {
350 if (target.result.os.tag == .windows) {
351351 inline for (.{ exe, check_case_exe }) |artifact| {
352352 artifact.linkSystemLibrary("version");
353353 artifact.linkSystemLibrary("uuid");
......@@ -371,7 +371,7 @@ pub fn build(b: *std.Build) !void {
371371 );
372372
373373 // On mingw, we need to opt into windows 7+ to get some features required by tracy.
374 const tracy_c_flags: []const []const u8 = if (target.target.os.tag == .windows and target.target.abi == .gnu)
374 const tracy_c_flags: []const []const u8 = if (target.result.os.tag == .windows and target.result.abi == .gnu)
375375 &[_][]const u8{ "-DTRACY_ENABLE=1", "-fno-sanitize=undefined", "-D_WIN32_WINNT=0x601" }
376376 else
377377 &[_][]const u8{ "-DTRACY_ENABLE=1", "-fno-sanitize=undefined" };
......@@ -383,7 +383,7 @@ pub fn build(b: *std.Build) !void {
383383 }
384384 exe.linkLibC();
385385
386 if (target.target.os.tag == .windows) {
386 if (target.result.os.tag == .windows) {
387387 exe.linkSystemLibrary("dbghelp");
388388 exe.linkSystemLibrary("ws2_32");
389389 }
lib/build_runner.zig+1-1
......@@ -48,7 +48,7 @@ pub fn main() !void {
4848
4949 const host: std.Build.ResolvedTarget = .{
5050 .query = .{},
51 .target = try std.zig.system.resolveTargetQuery(.{}),
51 .result = try std.zig.system.resolveTargetQuery(.{}),
5252 };
5353
5454 const build_root_directory: std.Build.Cache.Directory = .{
lib/std/Build.zig+2-2
......@@ -2044,7 +2044,7 @@ pub fn hex64(x: u64) [16]u8 {
20442044/// of the target are "native". This can apply to the CPU, the OS, or even the ABI.
20452045pub const ResolvedTarget = struct {
20462046 query: Target.Query,
2047 target: Target,
2047 result: Target,
20482048};
20492049
20502050/// Converts a target query into a fully resolved target that can be passed to
......@@ -2056,7 +2056,7 @@ pub fn resolveTargetQuery(b: *Build, query: Target.Query) ResolvedTarget {
20562056
20572057 return .{
20582058 .query = query,
2059 .target = std.zig.system.resolveTargetQuery(query) catch
2059 .result = std.zig.system.resolveTargetQuery(query) catch
20602060 @panic("unable to resolve target query"),
20612061 };
20622062}
lib/std/Build/Module.zig+5-5
......@@ -10,7 +10,7 @@ root_source_file: ?LazyPath,
1010/// maintain step dependency edges.
1111import_table: std.StringArrayHashMapUnmanaged(*Module),
1212
13target: ?std.Build.ResolvedTarget = null,
13resolved_target: ?std.Build.ResolvedTarget = null,
1414optimize: ?std.builtin.OptimizeMode = null,
1515dwarf_format: ?std.dwarf.Format,
1616
......@@ -192,7 +192,7 @@ pub fn init(m: *Module, owner: *std.Build, options: CreateOptions, compile: ?*St
192192 .depending_steps = .{},
193193 .root_source_file = if (options.root_source_file) |lp| lp.dupe(owner) else null,
194194 .import_table = .{},
195 .target = options.target,
195 .resolved_target = options.target,
196196 .optimize = options.optimize,
197197 .link_libc = options.link_libc,
198198 .link_libcpp = options.link_libcpp,
......@@ -627,7 +627,7 @@ pub fn appendZigProcessFlags(
627627 try zig_args.append(@tagName(m.code_model));
628628 }
629629
630 if (m.target) |*target| {
630 if (m.resolved_target) |*target| {
631631 // Communicate the query via CLI since it's more compact.
632632 if (!target.query.isNative()) {
633633 try zig_args.appendSlice(&.{
......@@ -737,9 +737,9 @@ fn linkLibraryOrObject(m: *Module, other: *Step.Compile) void {
737737}
738738
739739fn requireKnownTarget(m: *Module) std.Target {
740 const resolved_target = m.target orelse
740 const resolved_target = m.resolved_target orelse
741741 @panic("this API requires the Module to be created with a known 'target' field");
742 return resolved_target.target;
742 return resolved_target.result;
743743}
744744
745745const Module = @This();
lib/std/Build/Step/Compile.zig+3-3
......@@ -251,7 +251,7 @@ pub fn create(owner: *std.Build, options: Options) *Compile {
251251 else
252252 owner.fmt("{s} ", .{name});
253253
254 const target = options.root_module.target.?.target;
254 const target = options.root_module.target.?.result;
255255
256256 const step_name = owner.fmt("{s} {s}{s} {s}", .{
257257 switch (options.kind) {
......@@ -954,7 +954,7 @@ fn make(step: *Step, prog_node: *std.Progress.Node) !void {
954954 try addFlag(&zig_args, "llvm", self.use_llvm);
955955 try addFlag(&zig_args, "lld", self.use_lld);
956956
957 if (self.root_module.target.?.query.ofmt) |ofmt| {
957 if (self.root_module.resolved_target.?.query.ofmt) |ofmt| {
958958 try zig_args.append(try std.fmt.allocPrint(b.allocator, "-ofmt={s}", .{@tagName(ofmt)}));
959959 }
960960
......@@ -1845,5 +1845,5 @@ fn matchCompileError(actual: []const u8, expected: []const u8) bool {
18451845
18461846pub fn rootModuleTarget(c: *Compile) std.Target {
18471847 // The root module is always given a target, so we know this to be non-null.
1848 return c.root_module.target.?.target;
1848 return c.root_module.resolved_target.?.result;
18491849}
lib/std/Build/Step/Run.zig+8-6
......@@ -678,8 +678,8 @@ fn runCommand(
678678
679679 const need_cross_glibc = exe.rootModuleTarget().isGnuLibC() and
680680 exe.is_linking_libc;
681 const other_target = exe.root_module.target.?.target;
682 switch (std.zig.system.getExternalExecutor(b.host.target, &other_target, .{
681 const other_target = exe.root_module.resolved_target.?.result;
682 switch (std.zig.system.getExternalExecutor(b.host.result, &other_target, .{
683683 .qemu_fixes_dl = need_cross_glibc and b.glibc_runtimes_dir != null,
684684 .link_libc = exe.is_linking_libc,
685685 })) {
......@@ -752,7 +752,7 @@ fn runCommand(
752752 .bad_dl => |foreign_dl| {
753753 if (allow_skip) return error.MakeSkipped;
754754
755 const host_dl = b.host.target.dynamic_linker.get() orelse "(none)";
755 const host_dl = b.host.result.dynamic_linker.get() orelse "(none)";
756756
757757 return step.fail(
758758 \\the host system is unable to execute binaries from the target
......@@ -764,7 +764,7 @@ fn runCommand(
764764 .bad_os_or_cpu => {
765765 if (allow_skip) return error.MakeSkipped;
766766
767 const host_name = try b.host.target.zigTriple(b.allocator);
767 const host_name = try b.host.result.zigTriple(b.allocator);
768768 const foreign_name = try exe.rootModuleTarget().zigTriple(b.allocator);
769769
770770 return step.fail("the host system ({s}) is unable to execute binaries from the target ({s})", .{
......@@ -1295,7 +1295,9 @@ fn addPathForDynLibs(self: *Run, artifact: *Step.Compile) void {
12951295 while (it.next()) |item| {
12961296 const other = item.compile.?;
12971297 if (item.module == &other.root_module) {
1298 if (item.module.target.?.target.os.tag == .windows and other.isDynamicLibrary()) {
1298 if (item.module.resolved_target.?.result.os.tag == .windows and
1299 other.isDynamicLibrary())
1300 {
12991301 addPathDir(self, fs.path.dirname(other.getEmittedBin().getPath(b)).?);
13001302 }
13011303 }
......@@ -1314,7 +1316,7 @@ fn failForeign(
13141316 return error.MakeSkipped;
13151317
13161318 const b = self.step.owner;
1317 const host_name = try b.host.target.zigTriple(b.allocator);
1319 const host_name = try b.host.result.zigTriple(b.allocator);
13181320 const foreign_name = try exe.rootModuleTarget().zigTriple(b.allocator);
13191321
13201322 return self.step.fail(
test/link/elf.zig+1-1
......@@ -1763,7 +1763,7 @@ fn testInitArrayOrder(b: *Build, opts: Options) *Step {
17631763 exe.addObject(g_o);
17641764 exe.addObject(h_o);
17651765
1766 if (opts.target.target.isGnuLibC()) {
1766 if (opts.target.result.isGnuLibC()) {
17671767 // TODO I think we need to clarify our use of `-fPIC -fPIE` flags for different targets
17681768 exe.pie = true;
17691769 }
test/link/link.zig+1-1
......@@ -14,7 +14,7 @@ pub const Options = struct {
1414};
1515
1616pub fn addTestStep(b: *Build, prefix: []const u8, opts: Options) *Step {
17 const target = opts.target.target.zigTriple(b.allocator) catch @panic("OOM");
17 const target = opts.target.result.zigTriple(b.allocator) catch @panic("OOM");
1818 const optimize = @tagName(opts.optimize);
1919 const use_llvm = if (opts.use_llvm) "llvm" else "no-llvm";
2020 const name = std.fmt.allocPrint(b.allocator, "test-{s}-{s}-{s}-{s}", .{
test/link/macho/bugs/13056/build.zig+1-1
......@@ -15,7 +15,7 @@ pub fn build(b: *std.Build) void {
1515
1616fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.OptimizeMode) void {
1717 const target = b.resolveTargetQuery(.{ .os_tag = .macos });
18 const sdk = std.zig.system.darwin.getSdk(b.allocator, target.target) orelse
18 const sdk = std.zig.system.darwin.getSdk(b.allocator, target.result) orelse
1919 @panic("macOS SDK is required to run the test");
2020
2121 const exe = b.addExecutable(.{
test/src/Cases.zig+5-5
......@@ -467,7 +467,7 @@ fn addFromDirInner(
467467 // Cross-product to get all possible test combinations
468468 for (targets) |target_query| {
469469 const resolved_target = b.resolveTargetQuery(target_query);
470 const target = resolved_target.target;
470 const target = resolved_target.result;
471471 for (backends) |backend| {
472472 if (backend == .stage2 and
473473 target.cpu.arch != .wasm32 and target.cpu.arch != .x86_64)
......@@ -647,8 +647,8 @@ pub fn lowerToBuildSteps(
647647 parent_step.dependOn(&artifact.step);
648648 },
649649 .Execution => |expected_stdout| no_exec: {
650 const run = if (case.target.target.ofmt == .c) run_step: {
651 if (getExternalExecutor(host, &case.target.target, .{ .link_libc = true }) != .native) {
650 const run = if (case.target.result.ofmt == .c) run_step: {
651 if (getExternalExecutor(host, &case.target.result, .{ .link_libc = true }) != .native) {
652652 // We wouldn't be able to run the compiled C code.
653653 break :no_exec;
654654 }
......@@ -667,7 +667,7 @@ pub fn lowerToBuildSteps(
667667 "--",
668668 "-lc",
669669 "-target",
670 case.target.target.zigTriple(b.allocator) catch @panic("OOM"),
670 case.target.result.zigTriple(b.allocator) catch @panic("OOM"),
671671 });
672672 run_c.addArtifactArg(artifact);
673673 break :run_step run_c;
......@@ -693,7 +693,7 @@ pub fn lowerToBuildSteps(
693693 continue; // Pass test.
694694 }
695695
696 if (getExternalExecutor(host, &case.target.target, .{ .link_libc = true }) != .native) {
696 if (getExternalExecutor(host, &case.target.result, .{ .link_libc = true }) != .native) {
697697 // We wouldn't be able to run the compiled C code.
698698 continue; // Pass test.
699699 }
test/standalone/c_compiler/build.zig+1-1
......@@ -42,7 +42,7 @@ fn add(
4242 exe_cpp.addCSourceFile(.{ .file = .{ .path = "test.cpp" }, .flags = &[0][]const u8{} });
4343 exe_cpp.linkLibCpp();
4444
45 switch (target.target.os.tag) {
45 switch (target.result.os.tag) {
4646 .windows => {
4747 // https://github.com/ziglang/zig/issues/8531
4848 exe_cpp.want_lto = false;
test/standalone/compiler_rt_panic/build.zig+4-4
......@@ -4,16 +4,16 @@ pub fn build(b: *std.Build) void {
44 const test_step = b.step("test", "Test it");
55 b.default_step = test_step;
66
7 const resolved_target = b.standardTargetOptions(.{});
8 const target = resolved_target.target;
7 const target = b.standardTargetOptions(.{});
98 const optimize = b.standardOptimizeOption(.{});
109
11 if (target.ofmt != .elf or !(target.abi.isMusl() or target.abi.isGnu())) return;
10 if (target.result.ofmt != .elf or !(target.result.abi.isMusl() or target.result.abi.isGnu()))
11 return;
1212
1313 const exe = b.addExecutable(.{
1414 .name = "main",
1515 .optimize = optimize,
16 .target = resolved_target,
16 .target = target,
1717 });
1818 exe.linkLibC();
1919 exe.addCSourceFile(.{
test/standalone/ios/build.zig+1-1
......@@ -12,7 +12,7 @@ pub fn build(b: *std.Build) void {
1212 .cpu_arch = .aarch64,
1313 .os_tag = .ios,
1414 });
15 const sdk = std.zig.system.darwin.getSdk(b.allocator, target.target) orelse
15 const sdk = std.zig.system.darwin.getSdk(b.allocator, target.result) orelse
1616 @panic("no iOS SDK found");
1717 b.sysroot = sdk;
1818
test/standalone/self_exe_symlink/build.zig+1-1
......@@ -11,7 +11,7 @@ pub fn build(b: *std.Build) void {
1111
1212 // The test requires getFdPath in order to to get the path of the
1313 // File returned by openSelfExe
14 if (!std.os.isGetFdPathSupportedOnTarget(target.target.os)) return;
14 if (!std.os.isGetFdPathSupportedOnTarget(target.result.os)) return;
1515
1616 const main = b.addExecutable(.{
1717 .name = "main",
test/standalone/stack_iterator/build.zig+3-3
......@@ -22,7 +22,7 @@ pub fn build(b: *std.Build) void {
2222 .root_source_file = .{ .path = "unwind.zig" },
2323 .target = target,
2424 .optimize = optimize,
25 .unwind_tables = target.target.isDarwin(),
25 .unwind_tables = target.result.isDarwin(),
2626 .omit_frame_pointer = false,
2727 });
2828
......@@ -70,7 +70,7 @@ pub fn build(b: *std.Build) void {
7070 .strip = false,
7171 });
7272
73 if (target.target.os.tag == .windows)
73 if (target.result.os.tag == .windows)
7474 c_shared_lib.defineCMacro("LIB_API", "__declspec(dllexport)");
7575
7676 c_shared_lib.addCSourceFile(.{
......@@ -84,7 +84,7 @@ pub fn build(b: *std.Build) void {
8484 .root_source_file = .{ .path = "shared_lib_unwind.zig" },
8585 .target = target,
8686 .optimize = optimize,
87 .unwind_tables = target.target.isDarwin(),
87 .unwind_tables = target.result.isDarwin(),
8888 .omit_frame_pointer = true,
8989 });
9090
test/tests.zig+2-2
......@@ -1043,7 +1043,7 @@ pub fn addModuleTests(b: *std.Build, options: ModuleTestOptions) *Step {
10431043 continue;
10441044
10451045 const resolved_target = b.resolveTargetQuery(test_target.target);
1046 const target = resolved_target.target;
1046 const target = resolved_target.result;
10471047
10481048 if (options.skip_cross_glibc and !test_target.target.isNative() and
10491049 target.isGnuLibC() and test_target.link_libc == true)
......@@ -1229,7 +1229,7 @@ pub fn addCAbiTests(b: *std.Build, skip_non_native: bool, skip_release: bool) *S
12291229 if (skip_non_native and !c_abi_target.target.isNative()) continue;
12301230
12311231 const resolved_target = b.resolveTargetQuery(c_abi_target.target);
1232 const target = resolved_target.target;
1232 const target = resolved_target.result;
12331233
12341234 if (target.os.tag == .windows and target.cpu.arch == .aarch64) {
12351235 // https://github.com/ziglang/zig/issues/14908