authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-08-02 19:17:07-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-08-03 09:52:15-07:00
logda91ef5c28bd11823bd84b6f54df9ca601f03e21
treeec6e830b6062bb2ce8e88a9e9b7e8d06f79979eb
parentc94bbebb9150f68ce179caa4f6beeab0622696a6

zig libc: restore functionality on macOS

Regressed in 2006add8496c47804ee3b6c562f420871cb4ea0a. References to native_darwin_sdk are no longer kept in the frontend. Instead the darwin SDK is detected as part of NativePaths and as part of LibCInstallation.

5 files changed, 77 insertions(+), 90 deletions(-)

src/Compilation.zig+55-79
...@@ -637,8 +637,6 @@ pub const InitOptions = struct {...@@ -637,8 +637,6 @@ pub const InitOptions = struct {
637 wasi_exec_model: ?std.builtin.WasiExecModel = null,637 wasi_exec_model: ?std.builtin.WasiExecModel = null,
638 /// (Zig compiler development) Enable dumping linker's state as JSON.638 /// (Zig compiler development) Enable dumping linker's state as JSON.
639 enable_link_snapshots: bool = false,639 enable_link_snapshots: bool = false,
640 /// (Darwin) Path and version of the native SDK if detected.
641 native_darwin_sdk: ?std.zig.system.darwin.DarwinSDK = null,
642 /// (Darwin) Install name of the dylib640 /// (Darwin) Install name of the dylib
643 install_name: ?[]const u8 = null,641 install_name: ?[]const u8 = null,
644 /// (Darwin) Path to entitlements file642 /// (Darwin) Path to entitlements file
...@@ -945,7 +943,6 @@ pub fn create(gpa: Allocator, options: InitOptions) !*Compilation {...@@ -945,7 +943,6 @@ pub fn create(gpa: Allocator, options: InitOptions) !*Compilation {
945 options.is_native_abi,943 options.is_native_abi,
946 link_libc,944 link_libc,
947 options.libc_installation,945 options.libc_installation,
948 options.native_darwin_sdk != null,
949 );946 );
950947
951 const must_pie = target_util.requiresPIE(options.target);948 const must_pie = target_util.requiresPIE(options.target);
...@@ -1560,7 +1557,6 @@ pub fn create(gpa: Allocator, options: InitOptions) !*Compilation {...@@ -1560,7 +1557,6 @@ pub fn create(gpa: Allocator, options: InitOptions) !*Compilation {
1560 .wasi_exec_model = wasi_exec_model,1557 .wasi_exec_model = wasi_exec_model,
1561 .hash_style = options.hash_style,1558 .hash_style = options.hash_style,
1562 .enable_link_snapshots = options.enable_link_snapshots,1559 .enable_link_snapshots = options.enable_link_snapshots,
1563 .native_darwin_sdk = options.native_darwin_sdk,
1564 .install_name = options.install_name,1560 .install_name = options.install_name,
1565 .entitlements = options.entitlements,1561 .entitlements = options.entitlements,
1566 .pagezero_size = options.pagezero_size,1562 .pagezero_size = options.pagezero_size,
...@@ -4865,7 +4861,6 @@ fn detectLibCIncludeDirs(...@@ -4865,7 +4861,6 @@ fn detectLibCIncludeDirs(
4865 is_native_abi: bool,4861 is_native_abi: bool,
4866 link_libc: bool,4862 link_libc: bool,
4867 libc_installation: ?*const LibCInstallation,4863 libc_installation: ?*const LibCInstallation,
4868 has_macos_sdk: bool,
4869) !LibCDirs {4864) !LibCDirs {
4870 if (!link_libc) {4865 if (!link_libc) {
4871 return LibCDirs{4866 return LibCDirs{
...@@ -4881,28 +4876,19 @@ fn detectLibCIncludeDirs(...@@ -4881,28 +4876,19 @@ fn detectLibCIncludeDirs(
4881 // If linking system libraries and targeting the native abi, default to4876 // If linking system libraries and targeting the native abi, default to
4882 // using the system libc installation.4877 // using the system libc installation.
4883 if (is_native_abi and !target.isMinGW()) {4878 if (is_native_abi and !target.isMinGW()) {
4884 if (target.isDarwin()) {
4885 return if (has_macos_sdk)
4886 // For Darwin/macOS, we are all set with getDarwinSDK found earlier.
4887 LibCDirs{
4888 .libc_include_dir_list = &[0][]u8{},
4889 .libc_installation = null,
4890 }
4891 else
4892 getZigShippedLibCIncludeDirsDarwin(arena, zig_lib_dir, target);
4893 }
4894 const libc = try arena.create(LibCInstallation);4879 const libc = try arena.create(LibCInstallation);
4895 libc.* = LibCInstallation.findNative(.{ .allocator = arena }) catch |err| switch (err) {4880 libc.* = LibCInstallation.findNative(.{ .allocator = arena, .target = target }) catch |err| switch (err) {
4896 error.CCompilerExitCode,4881 error.CCompilerExitCode,
4897 error.CCompilerCrashed,4882 error.CCompilerCrashed,
4898 error.CCompilerCannotFindHeaders,4883 error.CCompilerCannotFindHeaders,
4899 error.UnableToSpawnCCompiler,4884 error.UnableToSpawnCCompiler,
4885 error.DarwinSdkNotFound,
4900 => |e| {4886 => |e| {
4901 // We tried to integrate with the native system C compiler,4887 // We tried to integrate with the native system C compiler,
4902 // however, it is not installed. So we must rely on our bundled4888 // however, it is not installed. So we must rely on our bundled
4903 // libc files.4889 // libc files.
4904 if (target_util.canBuildLibC(target)) {4890 if (target_util.canBuildLibC(target)) {
4905 return detectLibCFromBuilding(arena, zig_lib_dir, target, has_macos_sdk);4891 return detectLibCFromBuilding(arena, zig_lib_dir, target);
4906 }4892 }
4907 return e;4893 return e;
4908 },4894 },
...@@ -4914,7 +4900,7 @@ fn detectLibCIncludeDirs(...@@ -4914,7 +4900,7 @@ fn detectLibCIncludeDirs(
4914 // If not linking system libraries, build and provide our own libc by4900 // If not linking system libraries, build and provide our own libc by
4915 // default if possible.4901 // default if possible.
4916 if (target_util.canBuildLibC(target)) {4902 if (target_util.canBuildLibC(target)) {
4917 return detectLibCFromBuilding(arena, zig_lib_dir, target, has_macos_sdk);4903 return detectLibCFromBuilding(arena, zig_lib_dir, target);
4918 }4904 }
49194905
4920 // If zig can't build the libc for the target and we are targeting the4906 // If zig can't build the libc for the target and we are targeting the
...@@ -4928,7 +4914,7 @@ fn detectLibCIncludeDirs(...@@ -4928,7 +4914,7 @@ fn detectLibCIncludeDirs(
49284914
4929 if (use_system_abi) {4915 if (use_system_abi) {
4930 const libc = try arena.create(LibCInstallation);4916 const libc = try arena.create(LibCInstallation);
4931 libc.* = try LibCInstallation.findNative(.{ .allocator = arena, .verbose = true });4917 libc.* = try LibCInstallation.findNative(.{ .allocator = arena, .verbose = true, .target = target });
4932 return detectLibCFromLibCInstallation(arena, target, libc);4918 return detectLibCFromLibCInstallation(arena, target, libc);
4933 }4919 }
49344920
...@@ -4977,69 +4963,59 @@ fn detectLibCFromBuilding(...@@ -4977,69 +4963,59 @@ fn detectLibCFromBuilding(
4977 arena: Allocator,4963 arena: Allocator,
4978 zig_lib_dir: []const u8,4964 zig_lib_dir: []const u8,
4979 target: std.Target,4965 target: std.Target,
4980 has_macos_sdk: bool,
4981) !LibCDirs {4966) !LibCDirs {
4982 switch (target.os.tag) {4967 if (target.isDarwin())
4983 .macos => return if (has_macos_sdk)4968 return getZigShippedLibCIncludeDirsDarwin(arena, zig_lib_dir, target);
4984 // For Darwin/macOS, we are all set with getDarwinSDK found earlier.4969
4985 LibCDirs{4970 const generic_name = target_util.libCGenericName(target);
4986 .libc_include_dir_list = &[0][]u8{},4971 // Some architectures are handled by the same set of headers.
4987 .libc_installation = null,4972 const arch_name = if (target.abi.isMusl())
4988 }4973 musl.archNameHeaders(target.cpu.arch)
4989 else4974 else if (target.cpu.arch.isThumb())
4990 getZigShippedLibCIncludeDirsDarwin(arena, zig_lib_dir, target),4975 // ARM headers are valid for Thumb too.
4991 else => {4976 switch (target.cpu.arch) {
4992 const generic_name = target_util.libCGenericName(target);4977 .thumb => "arm",
4993 // Some architectures are handled by the same set of headers.4978 .thumbeb => "armeb",
4994 const arch_name = if (target.abi.isMusl())4979 else => unreachable,
4995 musl.archNameHeaders(target.cpu.arch)4980 }
4996 else if (target.cpu.arch.isThumb())4981 else
4997 // ARM headers are valid for Thumb too.4982 @tagName(target.cpu.arch);
4998 switch (target.cpu.arch) {4983 const os_name = @tagName(target.os.tag);
4999 .thumb => "arm",4984 // Musl's headers are ABI-agnostic and so they all have the "musl" ABI name.
5000 .thumbeb => "armeb",4985 const abi_name = if (target.abi.isMusl()) "musl" else @tagName(target.abi);
5001 else => unreachable,4986 const s = std.fs.path.sep_str;
5002 }4987 const arch_include_dir = try std.fmt.allocPrint(
5003 else4988 arena,
5004 @tagName(target.cpu.arch);4989 "{s}" ++ s ++ "libc" ++ s ++ "include" ++ s ++ "{s}-{s}-{s}",
5005 const os_name = @tagName(target.os.tag);4990 .{ zig_lib_dir, arch_name, os_name, abi_name },
5006 // Musl's headers are ABI-agnostic and so they all have the "musl" ABI name.4991 );
5007 const abi_name = if (target.abi.isMusl()) "musl" else @tagName(target.abi);4992 const generic_include_dir = try std.fmt.allocPrint(
5008 const s = std.fs.path.sep_str;4993 arena,
5009 const arch_include_dir = try std.fmt.allocPrint(4994 "{s}" ++ s ++ "libc" ++ s ++ "include" ++ s ++ "generic-{s}",
5010 arena,4995 .{ zig_lib_dir, generic_name },
5011 "{s}" ++ s ++ "libc" ++ s ++ "include" ++ s ++ "{s}-{s}-{s}",4996 );
5012 .{ zig_lib_dir, arch_name, os_name, abi_name },4997 const generic_arch_name = target_util.osArchName(target);
5013 );4998 const arch_os_include_dir = try std.fmt.allocPrint(
5014 const generic_include_dir = try std.fmt.allocPrint(4999 arena,
5015 arena,5000 "{s}" ++ s ++ "libc" ++ s ++ "include" ++ s ++ "{s}-{s}-any",
5016 "{s}" ++ s ++ "libc" ++ s ++ "include" ++ s ++ "generic-{s}",5001 .{ zig_lib_dir, generic_arch_name, os_name },
5017 .{ zig_lib_dir, generic_name },5002 );
5018 );5003 const generic_os_include_dir = try std.fmt.allocPrint(
5019 const generic_arch_name = target_util.osArchName(target);5004 arena,
5020 const arch_os_include_dir = try std.fmt.allocPrint(5005 "{s}" ++ s ++ "libc" ++ s ++ "include" ++ s ++ "any-{s}-any",
5021 arena,5006 .{ zig_lib_dir, os_name },
5022 "{s}" ++ s ++ "libc" ++ s ++ "include" ++ s ++ "{s}-{s}-any",5007 );
5023 .{ zig_lib_dir, generic_arch_name, os_name },
5024 );
5025 const generic_os_include_dir = try std.fmt.allocPrint(
5026 arena,
5027 "{s}" ++ s ++ "libc" ++ s ++ "include" ++ s ++ "any-{s}-any",
5028 .{ zig_lib_dir, os_name },
5029 );
50305008
5031 const list = try arena.alloc([]const u8, 4);5009 const list = try arena.alloc([]const u8, 4);
5032 list[0] = arch_include_dir;5010 list[0] = arch_include_dir;
5033 list[1] = generic_include_dir;5011 list[1] = generic_include_dir;
5034 list[2] = arch_os_include_dir;5012 list[2] = arch_os_include_dir;
5035 list[3] = generic_os_include_dir;5013 list[3] = generic_os_include_dir;
50365014
5037 return LibCDirs{5015 return LibCDirs{
5038 .libc_include_dir_list = list,5016 .libc_include_dir_list = list,
5039 .libc_installation = null,5017 .libc_installation = null,
5040 };5018 };
5041 },
5042 }
5043}5019}
50445020
5045pub fn get_libc_crt_file(comp: *Compilation, arena: Allocator, basename: []const u8) ![]const u8 {5021pub fn get_libc_crt_file(comp: *Compilation, arena: Allocator, basename: []const u8) ![]const u8 {
src/libc_installation.zig+15-1
...@@ -33,6 +33,7 @@ pub const LibCInstallation = struct {...@@ -33,6 +33,7 @@ pub const LibCInstallation = struct {
33 LibCKernel32LibNotFound,33 LibCKernel32LibNotFound,
34 UnsupportedArchitecture,34 UnsupportedArchitecture,
35 WindowsSdkNotFound,35 WindowsSdkNotFound,
36 DarwinSdkNotFound,
36 ZigIsTheCCompiler,37 ZigIsTheCCompiler,
37 };38 };
3839
...@@ -171,6 +172,7 @@ pub const LibCInstallation = struct {...@@ -171,6 +172,7 @@ pub const LibCInstallation = struct {
171172
172 pub const FindNativeOptions = struct {173 pub const FindNativeOptions = struct {
173 allocator: Allocator,174 allocator: Allocator,
175 target: std.Target,
174176
175 /// If enabled, will print human-friendly errors to stderr.177 /// If enabled, will print human-friendly errors to stderr.
176 verbose: bool = false,178 verbose: bool = false,
...@@ -181,7 +183,19 @@ pub const LibCInstallation = struct {...@@ -181,7 +183,19 @@ pub const LibCInstallation = struct {
181 var self: LibCInstallation = .{};183 var self: LibCInstallation = .{};
182184
183 if (is_darwin) {185 if (is_darwin) {
184 @panic("Darwin is handled separately via std.zig.system.darwin module");186 if (!std.zig.system.darwin.isDarwinSDKInstalled(args.allocator))
187 return error.DarwinSdkNotFound;
188 const sdk = std.zig.system.darwin.getDarwinSDK(args.allocator, args.target) orelse
189 return error.DarwinSdkNotFound;
190 defer args.allocator.free(sdk.path);
191
192 self.include_dir = try fs.path.join(args.allocator, &.{
193 sdk.path, "usr/include",
194 });
195 self.sys_include_dir = try fs.path.join(args.allocator, &.{
196 sdk.path, "usr/include",
197 });
198 return self;
185 } else if (is_windows) {199 } else if (is_windows) {
186 var sdk: ZigWindowsSDK = ZigWindowsSDK.find(args.allocator) catch |err| switch (err) {200 var sdk: ZigWindowsSDK = ZigWindowsSDK.find(args.allocator) catch |err| switch (err) {
187 error.NotFound => return error.WindowsSdkNotFound,201 error.NotFound => return error.WindowsSdkNotFound,
src/link.zig+1-3
...@@ -231,6 +231,7 @@ pub const Options = struct {...@@ -231,6 +231,7 @@ pub const Options = struct {
231231
232 version: ?std.SemanticVersion,232 version: ?std.SemanticVersion,
233 compatibility_version: ?std.SemanticVersion,233 compatibility_version: ?std.SemanticVersion,
234 darwin_sdk_version: ?std.SemanticVersion = null,
234 libc_installation: ?*const LibCInstallation,235 libc_installation: ?*const LibCInstallation,
235236
236 dwarf_format: ?std.dwarf.Format,237 dwarf_format: ?std.dwarf.Format,
...@@ -241,9 +242,6 @@ pub const Options = struct {...@@ -241,9 +242,6 @@ pub const Options = struct {
241 /// (Zig compiler development) Enable dumping of linker's state as JSON.242 /// (Zig compiler development) Enable dumping of linker's state as JSON.
242 enable_link_snapshots: bool = false,243 enable_link_snapshots: bool = false,
243244
244 /// (Darwin) Path and version of the native SDK if detected.
245 native_darwin_sdk: ?std.zig.system.darwin.DarwinSDK = null,
246
247 /// (Darwin) Install name for the dylib245 /// (Darwin) Install name for the dylib
248 install_name: ?[]const u8 = null,246 install_name: ?[]const u8 = null,
249247
src/link/MachO/load_commands.zig+4-5
...@@ -278,11 +278,10 @@ pub fn writeBuildVersionLC(options: *const link.Options, lc_writer: anytype) !vo...@@ -278,11 +278,10 @@ pub fn writeBuildVersionLC(options: *const link.Options, lc_writer: anytype) !vo
278 const platform_version = @as(u32, @intCast(ver.major << 16 | ver.minor << 8));278 const platform_version = @as(u32, @intCast(ver.major << 16 | ver.minor << 8));
279 break :blk platform_version;279 break :blk platform_version;
280 };280 };
281 const sdk_version = if (options.native_darwin_sdk) |sdk| blk: {281 const sdk_version: u32 = if (options.darwin_sdk_version) |ver|
282 const ver = sdk.version;282 @intCast(ver.major << 16 | ver.minor << 8)
283 const sdk_version = @as(u32, @intCast(ver.major << 16 | ver.minor << 8));283 else
284 break :blk sdk_version;284 platform_version;
285 } else platform_version;
286 const is_simulator_abi = options.target.abi == .simulator;285 const is_simulator_abi = options.target.abi == .simulator;
287 try lc_writer.writeStruct(macho.build_version_command{286 try lc_writer.writeStruct(macho.build_version_command{
288 .cmdsize = cmdsize,287 .cmdsize = cmdsize,
src/main.zig+2-2
...@@ -891,7 +891,6 @@ fn buildOutputType(...@@ -891,7 +891,6 @@ fn buildOutputType(
891 var minor_subsystem_version: ?u32 = null;891 var minor_subsystem_version: ?u32 = null;
892 var wasi_exec_model: ?std.builtin.WasiExecModel = null;892 var wasi_exec_model: ?std.builtin.WasiExecModel = null;
893 var enable_link_snapshots: bool = false;893 var enable_link_snapshots: bool = false;
894 var native_darwin_sdk: ?std.zig.system.darwin.DarwinSDK = null;
895 var install_name: ?[]const u8 = null;894 var install_name: ?[]const u8 = null;
896 var hash_style: link.HashStyle = .both;895 var hash_style: link.HashStyle = .both;
897 var entitlements: ?[]const u8 = null;896 var entitlements: ?[]const u8 = null;
...@@ -3367,7 +3366,6 @@ fn buildOutputType(...@@ -3367,7 +3366,6 @@ fn buildOutputType(
3367 .wasi_exec_model = wasi_exec_model,3366 .wasi_exec_model = wasi_exec_model,
3368 .debug_compile_errors = debug_compile_errors,3367 .debug_compile_errors = debug_compile_errors,
3369 .enable_link_snapshots = enable_link_snapshots,3368 .enable_link_snapshots = enable_link_snapshots,
3370 .native_darwin_sdk = native_darwin_sdk,
3371 .install_name = install_name,3369 .install_name = install_name,
3372 .entitlements = entitlements,3370 .entitlements = entitlements,
3373 .pagezero_size = pagezero_size,3371 .pagezero_size = pagezero_size,
...@@ -4243,10 +4241,12 @@ pub fn cmdLibC(gpa: Allocator, args: []const []const u8) !void {...@@ -4243,10 +4241,12 @@ pub fn cmdLibC(gpa: Allocator, args: []const []const u8) !void {
4243 if (!cross_target.isNative()) {4241 if (!cross_target.isNative()) {
4244 fatal("unable to detect libc for non-native target", .{});4242 fatal("unable to detect libc for non-native target", .{});
4245 }4243 }
4244 const target_info = try detectNativeTargetInfo(cross_target);
42464245
4247 var libc = LibCInstallation.findNative(.{4246 var libc = LibCInstallation.findNative(.{
4248 .allocator = gpa,4247 .allocator = gpa,
4249 .verbose = true,4248 .verbose = true,
4249 .target = target_info.target,
4250 }) catch |err| {4250 }) catch |err| {
4251 fatal("unable to detect native libc: {s}", .{@errorName(err)});4251 fatal("unable to detect native libc: {s}", .{@errorName(err)});
4252 };4252 };