From d91e75f5cacebd80f574993160839f8af21a5984 Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Thu, 15 Oct 2020 16:44:16 -0700 Subject: [PATCH] getExternalExecutor fixups regarding dynamic linker * std.Target.standardDynamicLinkerPath: macOS has a dynamic linker * no need to override the default dynamic linker in the macos CrossTarget initialization in the tests * in getExternalExecutor, when validating the dynamic linker path, take into account the standard dynamic linker path. --- lib/std/target.zig | 34 ++++++++++++++++++++-------------- lib/std/zig/cross_target.zig | 11 +++++++---- test/stage2/test.zig | 5 ++--- 3 files changed, 29 insertions(+), 21 deletions(-) diff --git a/lib/std/target.zig b/lib/std/target.zig index 86aabf4282..1b93c82a76 100644 --- a/lib/std/target.zig +++ b/lib/std/target.zig @@ -483,6 +483,16 @@ pub const Target = struct { else => false, }; } + + pub fn floatAbi(abi: Abi) FloatAbi { + return switch (abi) { + .gnueabihf, + .eabihf, + .musleabihf, + => .hard, + else => .soft, + }; + } }; pub const ObjectFormat = enum { @@ -1259,13 +1269,7 @@ pub const Target = struct { }; pub fn getFloatAbi(self: Target) FloatAbi { - return switch (self.abi) { - .gnueabihf, - .eabihf, - .musleabihf, - => .hard, - else => .soft, - }; + return self.abi.floatAbi(); } pub fn hasDynamicLinker(self: Target) bool { @@ -1336,12 +1340,12 @@ pub const Target = struct { const print = S.print; const copy = S.copy; - if (self.isAndroid()) { + if (self.abi == .android) { const suffix = if (self.cpu.arch.ptrBitWidth() == 64) "64" else ""; return print(&result, "/system/bin/linker{}", .{suffix}); } - if (self.isMusl()) { + if (self.abi.isMusl()) { const is_arm = switch (self.cpu.arch) { .arm, .armeb, .thumb, .thumbeb => true, else => false, @@ -1351,7 +1355,7 @@ pub const Target = struct { .armeb, .thumbeb => "armeb", else => |arch| @tagName(arch), }; - const arch_suffix = if (is_arm and self.getFloatAbi() == .hard) "hf" else ""; + const arch_suffix = if (is_arm and self.abi.floatAbi() == .hard) "hf" else ""; return print(&result, "/lib/ld-musl-{}{}.so.1", .{ arch_part, arch_suffix }); } @@ -1373,7 +1377,7 @@ pub const Target = struct { .armeb, .thumb, .thumbeb, - => return copy(&result, switch (self.getFloatAbi()) { + => return copy(&result, switch (self.abi.floatAbi()) { .hard => "/lib/ld-linux-armhf.so.3", else => "/lib/ld-linux.so.3", }), @@ -1444,13 +1448,15 @@ pub const Target = struct { => return result, }, - // Operating systems in this list have been verified as not having a standard - // dynamic linker path. - .freestanding, .ios, .tvos, .watchos, .macos, + => return copy(&result, "/usr/lib/dyld"), + + // Operating systems in this list have been verified as not having a standard + // dynamic linker path. + .freestanding, .uefi, .windows, .emscripten, diff --git a/lib/std/zig/cross_target.zig b/lib/std/zig/cross_target.zig index 77c7f12728..2f9446982d 100644 --- a/lib/std/zig/cross_target.zig +++ b/lib/std/zig/cross_target.zig @@ -606,15 +606,18 @@ pub const CrossTarget = struct { const os_match = os_tag == Target.current.os.tag; // If the OS and CPU arch match, the binary can be considered native. + // TODO additionally match the CPU features. This `getExternalExecutor` function should + // be moved to std.Target and match any chosen target against the native target. if (os_match and cpu_arch == Target.current.cpu.arch) { // However, we also need to verify that the dynamic linker path is valid. if (self.os_tag == null) { return .native; } - if (self.dynamic_linker.max_byte) |len| blk: { - std.fs.cwd().access(self.dynamic_linker.buffer[0..len + 1], .{}) catch { - break :blk; - }; + // TODO here we call toTarget, a deprecated function, because of the above TODO about moving + // this code to std.Target. + const opt_dl = self.dynamic_linker.get() orelse self.toTarget().standardDynamicLinkerPath().get(); + if (opt_dl) |dl| blk: { + std.fs.cwd().access(dl, .{}) catch break :blk; return .native; } } diff --git a/test/stage2/test.zig b/test/stage2/test.zig index 2090c9493e..7e1de126b6 100644 --- a/test/stage2/test.zig +++ b/test/stage2/test.zig @@ -14,7 +14,6 @@ const linux_x64 = std.zig.CrossTarget{ const macosx_x64 = std.zig.CrossTarget{ .cpu_arch = .x86_64, .os_tag = .macos, - .dynamic_linker = std.zig.CrossTarget.DynamicLinker.init("/usr/lib/dyld"), }; const linux_riscv64 = std.zig.CrossTarget{ @@ -184,7 +183,7 @@ pub fn addCases(ctx: *TestContext) !void { \\ ); \\ unreachable; \\} - , + , "Hello, World!\n", ); // Now change the message only @@ -994,7 +993,7 @@ pub fn addCases(ctx: *TestContext) !void { \\ ); \\ unreachable; \\} - , + , "Hello, World!\n", ); try case.files.append(.{