From 6734271eb0b8b7eafd098986b30a5bc34e180b72 Mon Sep 17 00:00:00 2001 From: Andrew Gutekanst Date: Fri, 10 Sep 2021 23:52:46 -0400 Subject: [PATCH 1/3] link: fix invalid file path used when cross-compiling for Windows -> Mac While investigating hexops/mach#8 with @slimsag, we found that zld is forming invalid file paths (absolute paths concatenated together), which hits the unreachable `OBJECT_NAME_INVALID` case in `openDirAccessMaskW`: https://github.com/ziglang/zig/blob/0c091feb5ae52caf1ebf885c0de55b3159207001/lib/std/fs.zig#L1522 This is caused by appending `dir` (which is guaranteed to be absolute) to `root`, an obviously incorrect operation: https://github.com/ziglang/zig/blob/0c091feb5ae52caf1ebf885c0de55b3159207001/src/link/MachO.zig#L494-L499 Fixes hexops/mach#8 Co-authored-by: Stephen Gutekanst Signed-off-by: Stephen Gutekanst Signed-off-by: Andrew Gutekanst --- src/link/MachO.zig | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/link/MachO.zig b/src/link/MachO.zig index 12556b53d6..2b7fa280ba 100644 --- a/src/link/MachO.zig +++ b/src/link/MachO.zig @@ -491,7 +491,7 @@ fn resolveSearchDir( ) !?[]const u8 { var candidates = std.ArrayList([]const u8).init(arena); - if (fs.path.isAbsolute(dir)) { + if (!fs.path.isAbsolute(dir)) { if (syslibroot) |root| { const full_path = try fs.path.join(arena, &[_][]const u8{ root, dir }); try candidates.append(full_path); From 62a6d83da11553daadb7d463330571f361c771d8 Mon Sep 17 00:00:00 2001 From: Andrew Gutekanst Date: Sun, 12 Sep 2021 19:34:06 -0400 Subject: [PATCH 2/3] Address feedback --- src/link/MachO.zig | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/src/link/MachO.zig b/src/link/MachO.zig index 2b7fa280ba..de5e789b8c 100644 --- a/src/link/MachO.zig +++ b/src/link/MachO.zig @@ -491,9 +491,21 @@ fn resolveSearchDir( ) !?[]const u8 { var candidates = std.ArrayList([]const u8).init(arena); - if (!fs.path.isAbsolute(dir)) { + if (fs.path.isAbsolute(dir)) { if (syslibroot) |root| { - const full_path = try fs.path.join(arena, &[_][]const u8{ root, dir }); + const common_dir = if (std.Target.current.os.tag == .windows) blk: { + // We need to check for disk designator and strip it out from dir path so + // that we can concat dir with syslibroot. + // TODO we should backport this mechanism to 'MachO.Dylib.parseDependentLibs()' + const disk_designator = fs.path.diskDesignatorWindows(dir); + + if (mem.indexOf(u8, dir, disk_designator)) |where| { + break :blk dir[where + disk_designator.len ..]; + } + + break :blk dir; + } else dir; + const full_path = try fs.path.join(arena, &[_][]const u8{ root, common_dir }); try candidates.append(full_path); } } From ffb989169594cbfcfefbbcc00dd7c3d07f1a9949 Mon Sep 17 00:00:00 2001 From: Andrew Gutekanst Date: Sun, 12 Sep 2021 19:35:03 -0400 Subject: [PATCH 3/3] Fix same issue with dir/sysroot dir concatenation with includes on Windows --- lib/std/build.zig | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/lib/std/build.zig b/lib/std/build.zig index 7ac41d1bb0..7b976405dc 100644 --- a/lib/std/build.zig +++ b/lib/std/build.zig @@ -2549,7 +2549,22 @@ pub const LibExeObjStep = struct { } else { try zig_args.append("-isystem"); } - try zig_args.append(self.builder.pathFromRoot(include_path)); + + const resolved_include_path = self.builder.pathFromRoot(include_path); + + const common_include_path = if (std.Target.current.os.tag == .windows and builder.sysroot != null and fs.path.isAbsolute(resolved_include_path)) blk: { + // We need to check for disk designator and strip it out from dir path so + // that zig/clang can concat resolved_include_path with sysroot. + const disk_designator = fs.path.diskDesignatorWindows(resolved_include_path); + + if (mem.indexOf(u8, resolved_include_path, disk_designator)) |where| { + break :blk resolved_include_path[where + disk_designator.len ..]; + } + + break :blk resolved_include_path; + } else resolved_include_path; + + try zig_args.append(common_include_path); }, .other_step => |other| if (other.emit_h) { const h_path = other.getOutputHSource().getPath(self.builder);