| ... | @@ -1,6 +1,5 @@ | ... | @@ -1,6 +1,5 @@ |
| 1 | const std = @import("../../std.zig"); | 1 | const std = @import("../../std.zig"); |
| 2 | const builtin = @import("builtin"); | 2 | const builtin = @import("builtin"); |
| 3 | const ArrayList = std.ArrayList; | | |
| 4 | const Allocator = std.mem.Allocator; | 3 | const Allocator = std.mem.Allocator; |
| 5 | const process = std.process; | 4 | const process = std.process; |
| 6 | const mem = std.mem; | 5 | const mem = std.mem; |
| ... | @@ -8,28 +7,18 @@ const mem = std.mem; | ... | @@ -8,28 +7,18 @@ const mem = std.mem; |
| 8 | const NativePaths = @This(); | 7 | const NativePaths = @This(); |
| 9 | const NativeTargetInfo = std.zig.system.NativeTargetInfo; | 8 | const NativeTargetInfo = std.zig.system.NativeTargetInfo; |
| 10 | | 9 | |
| 11 | include_dirs: ArrayList([:0]u8), | 10 | arena: Allocator, |
| 12 | lib_dirs: ArrayList([:0]u8), | 11 | include_dirs: std.ArrayListUnmanaged([]const u8) = .{}, |
| 13 | framework_dirs: ArrayList([:0]u8), | 12 | lib_dirs: std.ArrayListUnmanaged([]const u8) = .{}, |
| 14 | rpaths: ArrayList([:0]u8), | 13 | framework_dirs: std.ArrayListUnmanaged([]const u8) = .{}, |
| 15 | warnings: ArrayList([:0]u8), | 14 | rpaths: std.ArrayListUnmanaged([]const u8) = .{}, |
| | 15 | warnings: std.ArrayListUnmanaged([]const u8) = .{}, |
| 16 | | 16 | |
| 17 | pub fn detect(allocator: Allocator, native_info: NativeTargetInfo) !NativePaths { | 17 | pub fn detect(arena: Allocator, native_info: NativeTargetInfo) !NativePaths { |
| 18 | const native_target = native_info.target; | 18 | const native_target = native_info.target; |
| 19 | | 19 | 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 | } |
| 91 | | 78 | |
| | 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" })); |
| 96 | | 85 | 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 | } |
| 102 | | 89 | // 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 | } |
| 105 | | 93 | |
| ... | @@ -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 | } |
| 116 | | 104 | |
| 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); | | |
| 120 | | 107 | |
| 121 | const qual = native_target.ptrBitWidth(); | 108 | const qual = native_target.ptrBitWidth(); |
| 122 | | 109 | |
| ... | @@ -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 | } |
| 174 | | 161 | |
| 175 | pub 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 | | | |
| 184 | fn deinitArray(array: *ArrayList([:0]u8)) void { | | |
| 185 | for (array.items) |item| { | | |
| 186 | array.allocator.free(item); | | |
| 187 | } | | |
| 188 | array.deinit(); | | |
| 189 | } | | |
| 190 | | | |
| 191 | pub fn addIncludeDir(self: *NativePaths, s: []const u8) !void { | 162 | pub 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 | } |
| 194 | | 165 | |
| 195 | pub fn addIncludeDirFmt(self: *NativePaths, comptime fmt: []const u8, args: anytype) !void { | 166 | pub 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 | } |
| 200 | | 170 | |
| 201 | pub fn addLibDir(self: *NativePaths, s: []const u8) !void { | 171 | pub 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 | } |
| 204 | | 174 | |
| 205 | pub fn addLibDirFmt(self: *NativePaths, comptime fmt: []const u8, args: anytype) !void { | 175 | pub 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 | } |
| 210 | | 179 | |
| 211 | pub fn addWarning(self: *NativePaths, s: []const u8) !void { | 180 | pub 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 | } |
| 214 | | 183 | |
| 215 | pub fn addFrameworkDir(self: *NativePaths, s: []const u8) !void { | 184 | pub 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 | } |
| 218 | | 187 | |
| 219 | pub fn addFrameworkDirFmt(self: *NativePaths, comptime fmt: []const u8, args: anytype) !void { | 188 | pub 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 | } |
| 224 | | 192 | |
| 225 | pub fn addWarningFmt(self: *NativePaths, comptime fmt: []const u8, args: anytype) !void { | 193 | pub 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 | } |
| 230 | | 197 | |
| 231 | pub fn addRPath(self: *NativePaths, s: []const u8) !void { | 198 | pub 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 | | | |
| 235 | fn 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 | } |