| ... | ... | @@ -1,6 +1,5 @@ |
| 1 | 1 | const std = @import("../../std.zig"); |
| 2 | 2 | const builtin = @import("builtin"); |
| 3 | | const ArrayList = std.ArrayList; |
| 4 | 3 | const Allocator = std.mem.Allocator; |
| 5 | 4 | const process = std.process; |
| 6 | 5 | const mem = std.mem; |
| ... | ... | @@ -8,28 +7,18 @@ const mem = std.mem; |
| 8 | 7 | const NativePaths = @This(); |
| 9 | 8 | const NativeTargetInfo = std.zig.system.NativeTargetInfo; |
| 10 | 9 | |
| 11 | | include_dirs: ArrayList([:0]u8), |
| 12 | | lib_dirs: ArrayList([:0]u8), |
| 13 | | framework_dirs: ArrayList([:0]u8), |
| 14 | | rpaths: ArrayList([:0]u8), |
| 15 | | warnings: ArrayList([:0]u8), |
| 10 | arena: Allocator, |
| 11 | include_dirs: std.ArrayListUnmanaged([]const u8) = .{}, |
| 12 | lib_dirs: std.ArrayListUnmanaged([]const u8) = .{}, |
| 13 | framework_dirs: std.ArrayListUnmanaged([]const 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 | 18 | const native_target = native_info.target; |
| 19 | | |
| 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 | | |
| 19 | var self: NativePaths = .{ .arena = arena }; |
| 29 | 20 | var is_nix = false; |
| 30 | | if (process.getEnvVarOwned(allocator, "NIX_CFLAGS_COMPILE")) |nix_cflags_compile| { |
| 31 | | defer allocator.free(nix_cflags_compile); |
| 32 | | |
| 21 | if (process.getEnvVarOwned(arena, "NIX_CFLAGS_COMPILE")) |nix_cflags_compile| { |
| 33 | 22 | is_nix = true; |
| 34 | 23 | var it = mem.tokenizeScalar(u8, nix_cflags_compile, ' '); |
| 35 | 24 | while (true) { |
| ... | ... | @@ -58,9 +47,7 @@ pub fn detect(allocator: Allocator, native_info: NativeTargetInfo) !NativePaths |
| 58 | 47 | error.EnvironmentVariableNotFound => {}, |
| 59 | 48 | error.OutOfMemory => |e| return e, |
| 60 | 49 | } |
| 61 | | if (process.getEnvVarOwned(allocator, "NIX_LDFLAGS")) |nix_ldflags| { |
| 62 | | defer allocator.free(nix_ldflags); |
| 63 | | |
| 50 | if (process.getEnvVarOwned(arena, "NIX_LDFLAGS")) |nix_ldflags| { |
| 64 | 51 | is_nix = true; |
| 65 | 52 | var it = mem.tokenizeScalar(u8, nix_ldflags, ' '); |
| 66 | 53 | while (true) { |
| ... | ... | @@ -89,17 +76,18 @@ pub fn detect(allocator: Allocator, native_info: NativeTargetInfo) !NativePaths |
| 89 | 76 | return self; |
| 90 | 77 | } |
| 91 | 78 | |
| 79 | // TODO: consider also adding homebrew paths |
| 80 | // TODO: consider also adding macports paths |
| 92 | 81 | if (comptime builtin.target.isDarwin()) { |
| 93 | | try self.addIncludeDir("/usr/include"); |
| 94 | | try self.addLibDir("/usr/lib"); |
| 95 | | try self.addFrameworkDir("/System/Library/Frameworks"); |
| 96 | | |
| 97 | | if (builtin.target.os.version_range.semver.min.major < 11) { |
| 98 | | try self.addIncludeDir("/usr/local/include"); |
| 99 | | try self.addLibDir("/usr/local/lib"); |
| 100 | | try self.addFrameworkDir("/Library/Frameworks"); |
| 82 | if (std.zig.system.darwin.isDarwinSDKInstalled(arena)) sdk: { |
| 83 | const sdk = std.zig.system.darwin.getDarwinSDK(arena, native_target) orelse break :sdk; |
| 84 | try self.addLibDir(try std.fs.path.join(arena, &.{ sdk.path, "usr/lib" })); |
| 85 | try self.addFrameworkDir(try std.fs.path.join(arena, &.{ sdk.path, "System/Library/Frameworks" })); |
| 86 | try self.addIncludeDir(try std.fs.path.join(arena, &.{ sdk.path, "usr/include" })); |
| 87 | return self; |
| 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 | 91 | return self; |
| 104 | 92 | } |
| 105 | 93 | |
| ... | ... | @@ -115,8 +103,7 @@ pub fn detect(allocator: Allocator, native_info: NativeTargetInfo) !NativePaths |
| 115 | 103 | } |
| 116 | 104 | |
| 117 | 105 | if (builtin.os.tag != .windows) { |
| 118 | | const triple = try native_target.linuxTriple(allocator); |
| 119 | | defer allocator.free(triple); |
| 106 | const triple = try native_target.linuxTriple(arena); |
| 120 | 107 | |
| 121 | 108 | const qual = native_target.ptrBitWidth(); |
| 122 | 109 | |
| ... | ... | @@ -172,69 +159,42 @@ pub fn detect(allocator: Allocator, native_info: NativeTargetInfo) !NativePaths |
| 172 | 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 | 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 | 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); |
| 197 | | errdefer self.include_dirs.allocator.free(item); |
| 198 | | try self.include_dirs.append(item); |
| 167 | const item = try std.fmt.allocPrint(self.arena, fmt, args); |
| 168 | try self.include_dirs.append(self.arena, item); |
| 199 | 169 | } |
| 200 | 170 | |
| 201 | 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 | 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); |
| 207 | | errdefer self.lib_dirs.allocator.free(item); |
| 208 | | try self.lib_dirs.append(item); |
| 176 | const item = try std.fmt.allocPrint(self.arena, fmt, args); |
| 177 | try self.lib_dirs.append(self.arena, item); |
| 209 | 178 | } |
| 210 | 179 | |
| 211 | 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 | 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 | 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); |
| 221 | | errdefer self.framework_dirs.allocator.free(item); |
| 222 | | try self.framework_dirs.append(item); |
| 189 | const item = try std.fmt.allocPrint(self.arena, fmt, args); |
| 190 | try self.framework_dirs.append(self.arena, item); |
| 223 | 191 | } |
| 224 | 192 | |
| 225 | 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); |
| 227 | | errdefer self.warnings.allocator.free(item); |
| 228 | | try self.warnings.append(item); |
| 194 | const item = try std.fmt.allocPrint(self.arena, fmt, args); |
| 195 | try self.warnings.append(self.arena, item); |
| 229 | 196 | } |
| 230 | 197 | |
| 231 | 198 | pub fn addRPath(self: *NativePaths, s: []const u8) !void { |
| 232 | | return self.appendArray(&self.rpaths, 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); |
| 199 | try self.rpaths.append(self.arena, s); |
| 240 | 200 | } |