mirror of
https://github.com/ziglang/zig.git
synced 2025-12-09 15:53:08 +00:00
* add target_util.zig which has ported code from src/target.cpp
* Module gains an arena that owns memory used during initialization
that has the same lifetime as the Module. Useful for constructing
file paths and lists of strings that have mixed lifetimes.
- The Module memory itself is allocated in this arena. init/deinit
are modified to be create/destroy.
- root_name moves to the arena and no longer needs manual free
* implement the ability to invoke `zig clang` as a subprocess
- there are lots of TODOs that should be solved before merging
* Module now requires a Random object and zig_lib_dir
* Module now requires a path to its own executable or any zig
executable that can do `zig clang`.
* Wire up more CLI options.
* Module creates "zig-cache" directory and "tmp" and "o" subdirectories
("h" is created by the cache_hash)
* stubbed out some of the things linker code needs to do with TODO
prints
* delete dead code for computing compiler id. the previous commit
eliminated the need for it.
* add `zig translate-c` CLI option but it's not fully hooked up yet.
It should be possible for this to be fully wired up before merging
this branch.
* `zig targets` now uses canonical data for available_libcs
96 lines
3.0 KiB
Zig
96 lines
3.0 KiB
Zig
//! Introspection and determination of system libraries needed by zig.
|
|
|
|
const std = @import("std");
|
|
const mem = std.mem;
|
|
const fs = std.fs;
|
|
const CacheHash = std.cache_hash.CacheHash;
|
|
|
|
/// Caller must free result
|
|
pub fn testZigInstallPrefix(allocator: *mem.Allocator, test_path: []const u8) ![]u8 {
|
|
{
|
|
const test_zig_dir = try fs.path.join(allocator, &[_][]const u8{ test_path, "lib", "zig" });
|
|
errdefer allocator.free(test_zig_dir);
|
|
|
|
const test_index_file = try fs.path.join(allocator, &[_][]const u8{ test_zig_dir, "std", "std.zig" });
|
|
defer allocator.free(test_index_file);
|
|
|
|
if (fs.cwd().openFile(test_index_file, .{})) |file| {
|
|
file.close();
|
|
return test_zig_dir;
|
|
} else |err| switch (err) {
|
|
error.FileNotFound => {
|
|
allocator.free(test_zig_dir);
|
|
},
|
|
else => |e| return e,
|
|
}
|
|
}
|
|
|
|
// Also try without "zig"
|
|
const test_zig_dir = try fs.path.join(allocator, &[_][]const u8{ test_path, "lib" });
|
|
errdefer allocator.free(test_zig_dir);
|
|
|
|
const test_index_file = try fs.path.join(allocator, &[_][]const u8{ test_zig_dir, "std", "std.zig" });
|
|
defer allocator.free(test_index_file);
|
|
|
|
const file = try fs.cwd().openFile(test_index_file, .{});
|
|
file.close();
|
|
|
|
return test_zig_dir;
|
|
}
|
|
|
|
/// Caller must free result
|
|
pub fn findZigLibDir(allocator: *mem.Allocator) ![]u8 {
|
|
const self_exe_path = try fs.selfExePathAlloc(allocator);
|
|
defer allocator.free(self_exe_path);
|
|
|
|
var cur_path: []const u8 = self_exe_path;
|
|
while (true) {
|
|
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| {
|
|
std.debug.print(
|
|
\\Unable to find zig lib directory: {}.
|
|
\\Reinstall Zig or use --zig-install-prefix.
|
|
\\
|
|
, .{@errorName(err)});
|
|
|
|
return error.ZigLibDirNotFound;
|
|
};
|
|
}
|
|
|
|
/// Caller owns returned memory.
|
|
pub fn resolveGlobalCacheDir(allocator: *mem.Allocator) ![]u8 {
|
|
const appname = "zig";
|
|
|
|
if (std.Target.current.os.tag != .windows) {
|
|
if (std.os.getenv("XDG_CACHE_HOME")) |cache_root| {
|
|
return fs.path.join(allocator, &[_][]const u8{ cache_root, appname });
|
|
} else if (std.os.getenv("HOME")) |home| {
|
|
return fs.path.join(allocator, &[_][]const u8{ home, ".cache", appname });
|
|
}
|
|
}
|
|
|
|
return fs.getAppDataDir(allocator, appname);
|
|
}
|
|
|
|
pub fn openGlobalCacheDir() !fs.Dir {
|
|
var buf: [fs.MAX_PATH_BYTES]u8 = undefined;
|
|
var fba = std.heap.FixedBufferAllocator.init(&buf);
|
|
const path_name = try resolveGlobalCacheDir(&fba.allocator);
|
|
return fs.cwd().makeOpenPath(path_name, .{});
|
|
}
|