zig/src-self-hosted/introspect.zig

69 lines
2.0 KiB
Zig
Raw Normal View History

// Introspection and determination of system libraries needed by zig.
const std = @import("std");
const mem = std.mem;
2019-05-26 10:17:34 -07:00
const fs = std.fs;
const warn = std.debug.warn;
2020-02-17 12:23:59 -08:00
pub fn detectDynamicLinker(allocator: *mem.Allocator, target: std.Target) ![:0]u8 {
if (target == .Native) {
return @import("libc_installation.zig").detectNativeDynamicLinker(allocator);
} else {
return target.getStandardDynamicLinkerPath(allocator);
}
}
/// Caller must free result
pub fn testZigInstallPrefix(allocator: *mem.Allocator, test_path: []const u8) ![]u8 {
2019-11-29 18:49:08 -08:00
const test_zig_dir = try fs.path.join(allocator, &[_][]const u8{ test_path, "lib", "zig" });
errdefer allocator.free(test_zig_dir);
2019-11-29 18:49:08 -08:00
const test_index_file = try fs.path.join(allocator, &[_][]const u8{ test_zig_dir, "std", "std.zig" });
defer allocator.free(test_index_file);
var file = try fs.cwd().openRead(test_index_file);
file.close();
return test_zig_dir;
}
/// Caller must free result
pub fn findZigLibDir(allocator: *mem.Allocator) ![]u8 {
2019-05-26 10:17:34 -07:00
const self_exe_path = try fs.selfExeDirPathAlloc(allocator);
defer allocator.free(self_exe_path);
var cur_path: []const u8 = self_exe_path;
while (true) {
2019-05-26 10:17:34 -07:00
const test_dir = fs.path.dirname(cur_path) orelse ".";
if (mem.eql(u8, test_dir, cur_path)) {
break;
}
return testZigInstallPrefix(allocator, test_dir) catch |err| {
cur_path = test_dir;
continue;
};
}
return error.FileNotFound;
}
pub fn resolveZigLibDir(allocator: *mem.Allocator) ![]u8 {
return findZigLibDir(allocator) catch |err| {
warn(
\\Unable to find zig lib directory: {}.
\\Reinstall Zig or use --zig-install-prefix.
\\
, .{@errorName(err)});
return error.ZigLibDirNotFound;
};
}
/// Caller must free result
pub fn resolveZigCacheDir(allocator: *mem.Allocator) ![]u8 {
return std.mem.dupe(allocator, u8, "zig-cache");
}