authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-08-02 18:35:18-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-08-03 09:52:15-07:00
logc94bbebb9150f68ce179caa4f6beeab0622696a6
tree37402477650ec1d7933f201b5f63cca783e8b568
parentea0e6e737bb658bb6353ad6d3ab3c8cff61e051a

std.zig.system.NativePaths: simplify and integrate with Darwin SDK


3 files changed, 40 insertions(+), 99 deletions(-)

lib/std/zig/system/NativePaths.zig+34-74
...@@ -1,6 +1,5 @@...@@ -1,6 +1,5 @@
1const std = @import("../../std.zig");1const std = @import("../../std.zig");
2const builtin = @import("builtin");2const builtin = @import("builtin");
3const ArrayList = std.ArrayList;
4const Allocator = std.mem.Allocator;3const Allocator = std.mem.Allocator;
5const process = std.process;4const process = std.process;
6const mem = std.mem;5const mem = std.mem;
...@@ -8,28 +7,18 @@ const mem = std.mem;...@@ -8,28 +7,18 @@ const mem = std.mem;
8const NativePaths = @This();7const NativePaths = @This();
9const NativeTargetInfo = std.zig.system.NativeTargetInfo;8const NativeTargetInfo = std.zig.system.NativeTargetInfo;
109
11include_dirs: ArrayList([:0]u8),10arena: Allocator,
12lib_dirs: ArrayList([:0]u8),11include_dirs: std.ArrayListUnmanaged([]const u8) = .{},
13framework_dirs: ArrayList([:0]u8),12lib_dirs: std.ArrayListUnmanaged([]const u8) = .{},
14rpaths: ArrayList([:0]u8),13framework_dirs: std.ArrayListUnmanaged([]const u8) = .{},
15warnings: ArrayList([:0]u8),14rpaths: std.ArrayListUnmanaged([]const u8) = .{},
15warnings: std.ArrayListUnmanaged([]const u8) = .{},
1616
17pub fn detect(allocator: Allocator, native_info: NativeTargetInfo) !NativePaths {17pub fn detect(arena: Allocator, native_info: NativeTargetInfo) !NativePaths {
18 const native_target = native_info.target;18 const native_target = native_info.target;
1919 var self: NativePaths = .{ .arena = arena };
20 var self: NativePaths = .{
21 .include_dirs = ArrayList([:0]u8).init(allocator),
22 .lib_dirs = ArrayList([:0]u8).init(allocator),
23 .framework_dirs = ArrayList([:0]u8).init(allocator),
24 .rpaths = ArrayList([:0]u8).init(allocator),
25 .warnings = ArrayList([:0]u8).init(allocator),
26 };
27 errdefer self.deinit();
28
29 var is_nix = false;20 var is_nix = false;
30 if (process.getEnvVarOwned(allocator, "NIX_CFLAGS_COMPILE")) |nix_cflags_compile| {21 if (process.getEnvVarOwned(arena, "NIX_CFLAGS_COMPILE")) |nix_cflags_compile| {
31 defer allocator.free(nix_cflags_compile);
32
33 is_nix = true;22 is_nix = true;
34 var it = mem.tokenizeScalar(u8, nix_cflags_compile, ' ');23 var it = mem.tokenizeScalar(u8, nix_cflags_compile, ' ');
35 while (true) {24 while (true) {
...@@ -58,9 +47,7 @@ pub fn detect(allocator: Allocator, native_info: NativeTargetInfo) !NativePaths...@@ -58,9 +47,7 @@ pub fn detect(allocator: Allocator, native_info: NativeTargetInfo) !NativePaths
58 error.EnvironmentVariableNotFound => {},47 error.EnvironmentVariableNotFound => {},
59 error.OutOfMemory => |e| return e,48 error.OutOfMemory => |e| return e,
60 }49 }
61 if (process.getEnvVarOwned(allocator, "NIX_LDFLAGS")) |nix_ldflags| {50 if (process.getEnvVarOwned(arena, "NIX_LDFLAGS")) |nix_ldflags| {
62 defer allocator.free(nix_ldflags);
63
64 is_nix = true;51 is_nix = true;
65 var it = mem.tokenizeScalar(u8, nix_ldflags, ' ');52 var it = mem.tokenizeScalar(u8, nix_ldflags, ' ');
66 while (true) {53 while (true) {
...@@ -89,17 +76,18 @@ pub fn detect(allocator: Allocator, native_info: NativeTargetInfo) !NativePaths...@@ -89,17 +76,18 @@ pub fn detect(allocator: Allocator, native_info: NativeTargetInfo) !NativePaths
89 return self;76 return self;
90 }77 }
9178
79 // TODO: consider also adding homebrew paths
80 // TODO: consider also adding macports paths
92 if (comptime builtin.target.isDarwin()) {81 if (comptime builtin.target.isDarwin()) {
93 try self.addIncludeDir("/usr/include");82 if (std.zig.system.darwin.isDarwinSDKInstalled(arena)) sdk: {
94 try self.addLibDir("/usr/lib");83 const sdk = std.zig.system.darwin.getDarwinSDK(arena, native_target) orelse break :sdk;
95 try self.addFrameworkDir("/System/Library/Frameworks");84 try self.addLibDir(try std.fs.path.join(arena, &.{ sdk.path, "usr/lib" }));
9685 try self.addFrameworkDir(try std.fs.path.join(arena, &.{ sdk.path, "System/Library/Frameworks" }));
97 if (builtin.target.os.version_range.semver.min.major < 11) {86 try self.addIncludeDir(try std.fs.path.join(arena, &.{ sdk.path, "usr/include" }));
98 try self.addIncludeDir("/usr/local/include");87 return self;
99 try self.addLibDir("/usr/local/lib");
100 try self.addFrameworkDir("/Library/Frameworks");
101 }88 }
10289 // These do not include headers, so the ones that come with the SDK are preferred.
90 try self.addFrameworkDir("/System/Library/Frameworks");
103 return self;91 return self;
104 }92 }
10593
...@@ -115,8 +103,7 @@ pub fn detect(allocator: Allocator, native_info: NativeTargetInfo) !NativePaths...@@ -115,8 +103,7 @@ pub fn detect(allocator: Allocator, native_info: NativeTargetInfo) !NativePaths
115 }103 }
116104
117 if (builtin.os.tag != .windows) {105 if (builtin.os.tag != .windows) {
118 const triple = try native_target.linuxTriple(allocator);106 const triple = try native_target.linuxTriple(arena);
119 defer allocator.free(triple);
120107
121 const qual = native_target.ptrBitWidth();108 const qual = native_target.ptrBitWidth();
122109
...@@ -172,69 +159,42 @@ pub fn detect(allocator: Allocator, native_info: NativeTargetInfo) !NativePaths...@@ -172,69 +159,42 @@ pub fn detect(allocator: Allocator, native_info: NativeTargetInfo) !NativePaths
172 return self;159 return self;
173}160}
174161
175pub fn deinit(self: *NativePaths) void {
176 deinitArray(&self.include_dirs);
177 deinitArray(&self.lib_dirs);
178 deinitArray(&self.framework_dirs);
179 deinitArray(&self.rpaths);
180 deinitArray(&self.warnings);
181 self.* = undefined;
182}
183
184fn deinitArray(array: *ArrayList([:0]u8)) void {
185 for (array.items) |item| {
186 array.allocator.free(item);
187 }
188 array.deinit();
189}
190
191pub fn addIncludeDir(self: *NativePaths, s: []const u8) !void {162pub fn addIncludeDir(self: *NativePaths, s: []const u8) !void {
192 return self.appendArray(&self.include_dirs, s);163 return self.include_dirs.append(self.arena, s);
193}164}
194165
195pub fn addIncludeDirFmt(self: *NativePaths, comptime fmt: []const u8, args: anytype) !void {166pub fn addIncludeDirFmt(self: *NativePaths, comptime fmt: []const u8, args: anytype) !void {
196 const item = try std.fmt.allocPrintZ(self.include_dirs.allocator, fmt, args);167 const item = try std.fmt.allocPrint(self.arena, fmt, args);
197 errdefer self.include_dirs.allocator.free(item);168 try self.include_dirs.append(self.arena, item);
198 try self.include_dirs.append(item);
199}169}
200170
201pub fn addLibDir(self: *NativePaths, s: []const u8) !void {171pub fn addLibDir(self: *NativePaths, s: []const u8) !void {
202 return self.appendArray(&self.lib_dirs, s);172 try self.lib_dirs.append(self.arena, s);
203}173}
204174
205pub fn addLibDirFmt(self: *NativePaths, comptime fmt: []const u8, args: anytype) !void {175pub fn addLibDirFmt(self: *NativePaths, comptime fmt: []const u8, args: anytype) !void {
206 const item = try std.fmt.allocPrintZ(self.lib_dirs.allocator, fmt, args);176 const item = try std.fmt.allocPrint(self.arena, fmt, args);
207 errdefer self.lib_dirs.allocator.free(item);177 try self.lib_dirs.append(self.arena, item);
208 try self.lib_dirs.append(item);
209}178}
210179
211pub fn addWarning(self: *NativePaths, s: []const u8) !void {180pub fn addWarning(self: *NativePaths, s: []const u8) !void {
212 return self.appendArray(&self.warnings, s);181 return self.warnings.append(self.arena, s);
213}182}
214183
215pub fn addFrameworkDir(self: *NativePaths, s: []const u8) !void {184pub fn addFrameworkDir(self: *NativePaths, s: []const u8) !void {
216 return self.appendArray(&self.framework_dirs, s);185 return self.framework_dirs.append(self.arena, s);
217}186}
218187
219pub fn addFrameworkDirFmt(self: *NativePaths, comptime fmt: []const u8, args: anytype) !void {188pub fn addFrameworkDirFmt(self: *NativePaths, comptime fmt: []const u8, args: anytype) !void {
220 const item = try std.fmt.allocPrintZ(self.framework_dirs.allocator, fmt, args);189 const item = try std.fmt.allocPrint(self.arena, fmt, args);
221 errdefer self.framework_dirs.allocator.free(item);190 try self.framework_dirs.append(self.arena, item);
222 try self.framework_dirs.append(item);
223}191}
224192
225pub fn addWarningFmt(self: *NativePaths, comptime fmt: []const u8, args: anytype) !void {193pub fn addWarningFmt(self: *NativePaths, comptime fmt: []const u8, args: anytype) !void {
226 const item = try std.fmt.allocPrintZ(self.warnings.allocator, fmt, args);194 const item = try std.fmt.allocPrint(self.arena, fmt, args);
227 errdefer self.warnings.allocator.free(item);195 try self.warnings.append(self.arena, item);
228 try self.warnings.append(item);
229}196}
230197
231pub fn addRPath(self: *NativePaths, s: []const u8) !void {198pub fn addRPath(self: *NativePaths, s: []const u8) !void {
232 return self.appendArray(&self.rpaths, s);199 try self.rpaths.append(self.arena, s);
233}
234
235fn appendArray(self: *NativePaths, array: *ArrayList([:0]u8), s: []const u8) !void {
236 _ = self;
237 const item = try array.allocator.dupeZ(u8, s);
238 errdefer array.allocator.free(item);
239 try array.append(item);
240}200}
src/Compilation.zig+4-2
...@@ -857,8 +857,6 @@ pub fn create(gpa: Allocator, options: InitOptions) !*Compilation {...@@ -857,8 +857,6 @@ pub fn create(gpa: Allocator, options: InitOptions) !*Compilation {
857 const sysroot = blk: {857 const sysroot = blk: {
858 if (options.sysroot) |sysroot| {858 if (options.sysroot) |sysroot| {
859 break :blk sysroot;859 break :blk sysroot;
860 } else if (options.native_darwin_sdk) |sdk| {
861 break :blk sdk.path;
862 } else {860 } else {
863 break :blk null;861 break :blk null;
864 }862 }
...@@ -4341,6 +4339,10 @@ pub fn addCCArgs(...@@ -4341,6 +4339,10 @@ pub fn addCCArgs(
4341 try argv.append("-ObjC++");4339 try argv.append("-ObjC++");
4342 }4340 }
43434341
4342 for (comp.bin_file.options.framework_dirs) |framework_dir| {
4343 try argv.appendSlice(&.{ "-isystem", framework_dir });
4344 }
4345
4344 // According to Rich Felker libc headers are supposed to go before C language headers.4346 // According to Rich Felker libc headers are supposed to go before C language headers.
4345 // However as noted by @dimenus, appending libc headers before c_headers breaks intrinsics4347 // However as noted by @dimenus, appending libc headers before c_headers breaks intrinsics
4346 // and other compiler specific items.4348 // and other compiler specific items.
src/main.zig+2-23
...@@ -2685,34 +2685,13 @@ fn buildOutputType(...@@ -2685,34 +2685,13 @@ fn buildOutputType(
2685 warn("{s}", .{warning});2685 warn("{s}", .{warning});
2686 }2686 }
26872687
2688 const has_sysroot = if (comptime builtin.target.isDarwin()) outer: {
2689 if (std.zig.system.darwin.isDarwinSDKInstalled(arena)) {
2690 const sdk = std.zig.system.darwin.getDarwinSDK(arena, target_info.target) orelse
2691 break :outer false;
2692 native_darwin_sdk = sdk;
2693 try clang_argv.ensureUnusedCapacity(2);
2694 clang_argv.appendAssumeCapacity("-isysroot");
2695 clang_argv.appendAssumeCapacity(sdk.path);
2696 break :outer true;
2697 } else break :outer false;
2698 } else false;
2699
2700 try clang_argv.ensureUnusedCapacity(paths.include_dirs.items.len * 2);2688 try clang_argv.ensureUnusedCapacity(paths.include_dirs.items.len * 2);
2701 const isystem_flag = if (has_sysroot) "-iwithsysroot" else "-isystem";
2702 for (paths.include_dirs.items) |include_dir| {2689 for (paths.include_dirs.items) |include_dir| {
2703 clang_argv.appendAssumeCapacity(isystem_flag);2690 clang_argv.appendAssumeCapacity("-isystem");
2704 clang_argv.appendAssumeCapacity(include_dir);2691 clang_argv.appendAssumeCapacity(include_dir);
2705 }2692 }
27062693
2707 try clang_argv.ensureUnusedCapacity(paths.framework_dirs.items.len * 2);2694 try framework_dirs.appendSlice(paths.framework_dirs.items);
2708 try framework_dirs.ensureUnusedCapacity(paths.framework_dirs.items.len);
2709 const iframework_flag = if (has_sysroot) "-iframeworkwithsysroot" else "-iframework";
2710 for (paths.framework_dirs.items) |framework_dir| {
2711 clang_argv.appendAssumeCapacity(iframework_flag);
2712 clang_argv.appendAssumeCapacity(framework_dir);
2713 framework_dirs.appendAssumeCapacity(framework_dir);
2714 }
2715
2716 try lib_dirs.appendSlice(paths.lib_dirs.items);2695 try lib_dirs.appendSlice(paths.lib_dirs.items);
2717 try rpath_list.appendSlice(paths.rpaths.items);2696 try rpath_list.appendSlice(paths.rpaths.items);
2718 }2697 }