authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-10-15 16:44:16-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-10-15 16:44:16-07:00
logd91e75f5cacebd80f574993160839f8af21a5984
tree3ee85f0691a0f53e3be75b2ac05306bc198396f7
parent0e1afee732c1007280b8afdab51aef3ceb860432

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.

3 files changed, 29 insertions(+), 21 deletions(-)

lib/std/target.zig+20-14
......@@ -483,6 +483,16 @@ pub const Target = struct {
483483 else => false,
484484 };
485485 }
486
487 pub fn floatAbi(abi: Abi) FloatAbi {
488 return switch (abi) {
489 .gnueabihf,
490 .eabihf,
491 .musleabihf,
492 => .hard,
493 else => .soft,
494 };
495 }
486496 };
487497
488498 pub const ObjectFormat = enum {
......@@ -1259,13 +1269,7 @@ pub const Target = struct {
12591269 };
12601270
12611271 pub fn getFloatAbi(self: Target) FloatAbi {
1262 return switch (self.abi) {
1263 .gnueabihf,
1264 .eabihf,
1265 .musleabihf,
1266 => .hard,
1267 else => .soft,
1268 };
1272 return self.abi.floatAbi();
12691273 }
12701274
12711275 pub fn hasDynamicLinker(self: Target) bool {
......@@ -1336,12 +1340,12 @@ pub const Target = struct {
13361340 const print = S.print;
13371341 const copy = S.copy;
13381342
1339 if (self.isAndroid()) {
1343 if (self.abi == .android) {
13401344 const suffix = if (self.cpu.arch.ptrBitWidth() == 64) "64" else "";
13411345 return print(&result, "/system/bin/linker{}", .{suffix});
13421346 }
13431347
1344 if (self.isMusl()) {
1348 if (self.abi.isMusl()) {
13451349 const is_arm = switch (self.cpu.arch) {
13461350 .arm, .armeb, .thumb, .thumbeb => true,
13471351 else => false,
......@@ -1351,7 +1355,7 @@ pub const Target = struct {
13511355 .armeb, .thumbeb => "armeb",
13521356 else => |arch| @tagName(arch),
13531357 };
1354 const arch_suffix = if (is_arm and self.getFloatAbi() == .hard) "hf" else "";
1358 const arch_suffix = if (is_arm and self.abi.floatAbi() == .hard) "hf" else "";
13551359 return print(&result, "/lib/ld-musl-{}{}.so.1", .{ arch_part, arch_suffix });
13561360 }
13571361
......@@ -1373,7 +1377,7 @@ pub const Target = struct {
13731377 .armeb,
13741378 .thumb,
13751379 .thumbeb,
1376 => return copy(&result, switch (self.getFloatAbi()) {
1380 => return copy(&result, switch (self.abi.floatAbi()) {
13771381 .hard => "/lib/ld-linux-armhf.so.3",
13781382 else => "/lib/ld-linux.so.3",
13791383 }),
......@@ -1444,13 +1448,15 @@ pub const Target = struct {
14441448 => return result,
14451449 },
14461450
1447 // Operating systems in this list have been verified as not having a standard
1448 // dynamic linker path.
1449 .freestanding,
14501451 .ios,
14511452 .tvos,
14521453 .watchos,
14531454 .macos,
1455 => return copy(&result, "/usr/lib/dyld"),
1456
1457 // Operating systems in this list have been verified as not having a standard
1458 // dynamic linker path.
1459 .freestanding,
14541460 .uefi,
14551461 .windows,
14561462 .emscripten,
lib/std/zig/cross_target.zig+7-4
......@@ -606,15 +606,18 @@ pub const CrossTarget = struct {
606606 const os_match = os_tag == Target.current.os.tag;
607607
608608 // If the OS and CPU arch match, the binary can be considered native.
609 // TODO additionally match the CPU features. This `getExternalExecutor` function should
610 // be moved to std.Target and match any chosen target against the native target.
609611 if (os_match and cpu_arch == Target.current.cpu.arch) {
610612 // However, we also need to verify that the dynamic linker path is valid.
611613 if (self.os_tag == null) {
612614 return .native;
613615 }
614 if (self.dynamic_linker.max_byte) |len| blk: {
615 std.fs.cwd().access(self.dynamic_linker.buffer[0..len + 1], .{}) catch {
616 break :blk;
617 };
616 // TODO here we call toTarget, a deprecated function, because of the above TODO about moving
617 // this code to std.Target.
618 const opt_dl = self.dynamic_linker.get() orelse self.toTarget().standardDynamicLinkerPath().get();
619 if (opt_dl) |dl| blk: {
620 std.fs.cwd().access(dl, .{}) catch break :blk;
618621 return .native;
619622 }
620623 }
test/stage2/test.zig+2-3
......@@ -14,7 +14,6 @@ const linux_x64 = std.zig.CrossTarget{
1414const macosx_x64 = std.zig.CrossTarget{
1515 .cpu_arch = .x86_64,
1616 .os_tag = .macos,
17 .dynamic_linker = std.zig.CrossTarget.DynamicLinker.init("/usr/lib/dyld"),
1817};
1918
2019const linux_riscv64 = std.zig.CrossTarget{
......@@ -184,7 +183,7 @@ pub fn addCases(ctx: *TestContext) !void {
184183 \\ );
185184 \\ unreachable;
186185 \\}
187 ,
186 ,
188187 "Hello, World!\n",
189188 );
190189 // Now change the message only
......@@ -994,7 +993,7 @@ pub fn addCases(ctx: *TestContext) !void {
994993 \\ );
995994 \\ unreachable;
996995 \\}
997 ,
996 ,
998997 "Hello, World!\n",
999998 );
1000999 try case.files.append(.{