From 42a351e099dd41fe96dab4f6dadd277884188f3f Mon Sep 17 00:00:00 2001 From: Jakub Konka Date: Wed, 24 Nov 2021 16:14:52 +0100 Subject: infer and match macos sdk version for libc headers --- src/Compilation.zig | 127 +++++++++++++++++++++++++++++++++------------------- 1 file changed, 81 insertions(+), 46 deletions(-) (limited to 'src/Compilation.zig') diff --git a/src/Compilation.zig b/src/Compilation.zig index 15b8f7c3f2..7520e7cb4b 100644 --- a/src/Compilation.zig +++ b/src/Compilation.zig @@ -3798,53 +3798,88 @@ fn detectLibCIncludeDirs( // If not linking system libraries, build and provide our own libc by // default if possible. if (target_util.canBuildLibC(target)) { - const generic_name = target_util.libCGenericName(target); - // Some architectures are handled by the same set of headers. - const arch_name = if (target.abi.isMusl()) - musl.archName(target.cpu.arch) - else if (target.cpu.arch.isThumb()) - // ARM headers are valid for Thumb too. - switch (target.cpu.arch) { - .thumb => "arm", - .thumbeb => "armeb", - else => unreachable, - } - else - @tagName(target.cpu.arch); - const os_name = @tagName(target.os.tag); - // Musl's headers are ABI-agnostic and so they all have the "musl" ABI name. - const abi_name = if (target.abi.isMusl()) "musl" else @tagName(target.abi); - const s = std.fs.path.sep_str; - const arch_include_dir = try std.fmt.allocPrint( - arena, - "{s}" ++ s ++ "libc" ++ s ++ "include" ++ s ++ "{s}-{s}-{s}", - .{ zig_lib_dir, arch_name, os_name, abi_name }, - ); - const generic_include_dir = try std.fmt.allocPrint( - arena, - "{s}" ++ s ++ "libc" ++ s ++ "include" ++ s ++ "generic-{s}", - .{ zig_lib_dir, generic_name }, - ); - const arch_os_include_dir = try std.fmt.allocPrint( - arena, - "{s}" ++ s ++ "libc" ++ s ++ "include" ++ s ++ "{s}-{s}-any", - .{ zig_lib_dir, @tagName(target.cpu.arch), os_name }, - ); - const generic_os_include_dir = try std.fmt.allocPrint( - arena, - "{s}" ++ s ++ "libc" ++ s ++ "include" ++ s ++ "any-{s}-any", - .{ zig_lib_dir, os_name }, - ); + switch (target.os.tag) { + .macos => { + const arch_name = @tagName(target.cpu.arch); + const os_name = try std.fmt.allocPrint(arena, "{s}.{d}", .{ + @tagName(target.os.tag), + target.os.version_range.semver.min.major, + }); + const s = std.fs.path.sep_str; + const list = try arena.alloc([]const u8, 3); - const list = try arena.alloc([]const u8, 4); - list[0] = arch_include_dir; - list[1] = generic_include_dir; - list[2] = arch_os_include_dir; - list[3] = generic_os_include_dir; - return LibCDirs{ - .libc_include_dir_list = list, - .libc_installation = null, - }; + list[0] = try std.fmt.allocPrint( + arena, + "{s}" ++ s ++ "libc" ++ s ++ "include" ++ s ++ "{s}-{s}-gnu", + .{ zig_lib_dir, arch_name, os_name }, + ); + list[1] = try std.fmt.allocPrint( + arena, + "{s}" ++ s ++ "libc" ++ s ++ "include" ++ s ++ "any-{s}-any", + .{ zig_lib_dir, os_name }, + ); + list[2] = try std.fmt.allocPrint( + arena, + "{s}" ++ s ++ "libc" ++ s ++ "include" ++ s ++ "any-macos-any", + .{zig_lib_dir}, + ); + + return LibCDirs{ + .libc_include_dir_list = list, + .libc_installation = null, + }; + }, + else => { + const generic_name = target_util.libCGenericName(target); + // Some architectures are handled by the same set of headers. + const arch_name = if (target.abi.isMusl()) + musl.archName(target.cpu.arch) + else if (target.cpu.arch.isThumb()) + // ARM headers are valid for Thumb too. + switch (target.cpu.arch) { + .thumb => "arm", + .thumbeb => "armeb", + else => unreachable, + } + else + @tagName(target.cpu.arch); + const os_name = @tagName(target.os.tag); + // Musl's headers are ABI-agnostic and so they all have the "musl" ABI name. + const abi_name = if (target.abi.isMusl()) "musl" else @tagName(target.abi); + const s = std.fs.path.sep_str; + const arch_include_dir = try std.fmt.allocPrint( + arena, + "{s}" ++ s ++ "libc" ++ s ++ "include" ++ s ++ "{s}-{s}-{s}", + .{ zig_lib_dir, arch_name, os_name, abi_name }, + ); + const generic_include_dir = try std.fmt.allocPrint( + arena, + "{s}" ++ s ++ "libc" ++ s ++ "include" ++ s ++ "generic-{s}", + .{ zig_lib_dir, generic_name }, + ); + const arch_os_include_dir = try std.fmt.allocPrint( + arena, + "{s}" ++ s ++ "libc" ++ s ++ "include" ++ s ++ "{s}-{s}-any", + .{ zig_lib_dir, @tagName(target.cpu.arch), os_name }, + ); + const generic_os_include_dir = try std.fmt.allocPrint( + arena, + "{s}" ++ s ++ "libc" ++ s ++ "include" ++ s ++ "any-{s}-any", + .{ zig_lib_dir, os_name }, + ); + + const list = try arena.alloc([]const u8, 4); + list[0] = arch_include_dir; + list[1] = generic_include_dir; + list[2] = arch_os_include_dir; + list[3] = generic_os_include_dir; + + return LibCDirs{ + .libc_include_dir_list = list, + .libc_installation = null, + }; + }, + } } // If zig can't build the libc for the target and we are targeting the -- cgit v1.2.3 From 1954cdc1066f2d59961e4cdd24a9d80fd6eb86c3 Mon Sep 17 00:00:00 2001 From: Jakub Konka Date: Wed, 24 Nov 2021 23:41:09 +0100 Subject: Autofetch macOS SDK if native target on macOS only This means that I am purposefully regressing linking iOS and related which will require manual specification of the sysroot path, etc. --- src/Compilation.zig | 8 +++++--- src/main.zig | 12 +----------- 2 files changed, 6 insertions(+), 14 deletions(-) (limited to 'src/Compilation.zig') diff --git a/src/Compilation.zig b/src/Compilation.zig index 7520e7cb4b..20d67e7c77 100644 --- a/src/Compilation.zig +++ b/src/Compilation.zig @@ -962,15 +962,17 @@ pub fn create(gpa: *Allocator, options: InitOptions) !*Compilation { break :blk false; }; - const darwin_can_use_system_sdk = blk: { + const darwin_use_system_sdk = blk: { if (comptime !builtin.target.isDarwin()) break :blk false; - break :blk builtin.os.tag == .macos and options.target.isDarwin(); + if (!options.is_native_os) break :blk false; + if (builtin.os.tag != .macos or !options.target.isDarwin()) break :blk false; + break :blk options.frameworks.len > 0 or options.framework_dirs.len > 0; }; const sysroot = blk: { if (options.sysroot) |sysroot| { break :blk sysroot; - } else if (darwin_can_use_system_sdk) { + } else if (darwin_use_system_sdk) { break :blk try std.zig.system.darwin.getSDKPath(arena, options.target); } else { break :blk null; diff --git a/src/main.zig b/src/main.zig index 87086bf874..20ea5caded 100644 --- a/src/main.zig +++ b/src/main.zig @@ -1848,11 +1848,7 @@ fn buildOutputType( want_native_include_dirs = true; } - const is_darwin_on_darwin = (comptime builtin.target.isDarwin()) and cross_target.isDarwin(); - - if (sysroot == null and (cross_target.isNativeOs() or is_darwin_on_darwin) and - (system_libs.count() != 0 or want_native_include_dirs)) - { + if (sysroot == null and cross_target.isNativeOs() and (system_libs.count() != 0 or want_native_include_dirs)) { const paths = std.zig.system.NativePaths.detect(arena, target_info) catch |err| { fatal("unable to detect native system paths: {s}", .{@errorName(err)}); }; @@ -1861,12 +1857,6 @@ fn buildOutputType( } const has_sysroot = if (comptime builtin.target.isDarwin()) outer: { - const should_get_sdk_path = if (cross_target.isNativeOs() and target_info.target.os.tag == .macos) inner: { - const min = target_info.target.os.getVersionRange().semver.min; - const at_least_mojave = min.major >= 11 or (min.major >= 10 and min.minor >= 14); - break :inner at_least_mojave; - } else true; - if (!should_get_sdk_path) break :outer false; if (try std.zig.system.darwin.getSDKPath(arena, target_info.target)) |sdk_path| { try clang_argv.ensureUnusedCapacity(2); clang_argv.appendAssumeCapacity("-isysroot"); -- cgit v1.2.3 From 4270f234db02d40da1507fe5cb20029aa320ee42 Mon Sep 17 00:00:00 2001 From: Jakub Konka Date: Thu, 25 Nov 2021 12:08:16 +0100 Subject: Skip detecting native libc dirs on darwin This is handled before by detecting and adding SDK path which is a centralised point for the native libc installation on darwin. --- src/Compilation.zig | 7 +++++++ src/libc_installation.zig | 4 +++- 2 files changed, 10 insertions(+), 1 deletion(-) (limited to 'src/Compilation.zig') diff --git a/src/Compilation.zig b/src/Compilation.zig index 20d67e7c77..9da855789f 100644 --- a/src/Compilation.zig +++ b/src/Compilation.zig @@ -3792,6 +3792,13 @@ fn detectLibCIncludeDirs( // If linking system libraries and targeting the native abi, default to // using the system libc installation. if (link_system_libs and is_native_abi and !target.isMinGW()) { + if (target.isDarwin()) { + // For Darwin/macOS, we are all set with getSDKPath found earlier. + return LibCDirs{ + .libc_include_dir_list = &[0][]u8{}, + .libc_installation = null, + }; + } const libc = try arena.create(LibCInstallation); libc.* = try LibCInstallation.findNative(.{ .allocator = arena, .verbose = true }); return detectLibCFromLibCInstallation(arena, target, libc); diff --git a/src/libc_installation.zig b/src/libc_installation.zig index 3567f2e22f..615f9436e6 100644 --- a/src/libc_installation.zig +++ b/src/libc_installation.zig @@ -185,7 +185,9 @@ pub const LibCInstallation = struct { pub fn findNative(args: FindNativeOptions) FindError!LibCInstallation { var self: LibCInstallation = .{}; - if (is_windows) { + if (is_darwin) { + @panic("Darwin is handled separately via std.zig.system.darwin module"); + } else if (is_windows) { if (!build_options.have_llvm) return error.WindowsSdkNotFound; var sdk: *ZigWindowsSDK = undefined; -- cgit v1.2.3