字段
arena: Allocator
include_dirs: std.ArrayList([]const u8) = .empty
lib_dirs: std.ArrayList([]const u8) = .empty
framework_dirs: std.ArrayList([]const u8) = .empty
rpaths: std.ArrayList([]const u8) = .empty
warnings: std.ArrayList([]const u8) = .empty
std.zig.system.NativePaths.addFrameworkDir
函数
pub fn addFrameworkDir(self: *NativePaths, s: []const u8) !void
参数
self: *NativePaths
s: []const u8
std.zig.system.NativePaths.addFrameworkDirFmt
函数
pub fn addFrameworkDirFmt(self: *NativePaths, comptime fmt: []const u8, args: anytype) !void
参数
self: *NativePaths
fmt: []const u8
std.zig.system.NativePaths.addIncludeDir
函数
pub fn addIncludeDir(self: *NativePaths, s: []const u8) !void
参数
self: *NativePaths
s: []const u8
std.zig.system.NativePaths.addIncludeDirFmt
函数
pub fn addIncludeDirFmt(self: *NativePaths, comptime fmt: []const u8, args: anytype) !void
参数
self: *NativePaths
fmt: []const u8
std.zig.system.NativePaths.addLibDir
函数
pub fn addLibDir(self: *NativePaths, s: []const u8) !void
参数
self: *NativePaths
s: []const u8
std.zig.system.NativePaths.addLibDirFmt
函数
pub fn addLibDirFmt(self: *NativePaths, comptime fmt: []const u8, args: anytype) !void
参数
self: *NativePaths
fmt: []const u8
std.zig.system.NativePaths.addRPath
函数
pub fn addRPath(self: *NativePaths, s: []const u8) !void
参数
self: *NativePaths
s: []const u8
std.zig.system.NativePaths.addWarning
函数
pub fn addWarning(self: *NativePaths, s: []const u8) !void
参数
self: *NativePaths
s: []const u8
std.zig.system.NativePaths.addWarningFmt
函数
pub fn addWarningFmt(self: *NativePaths, comptime fmt: []const u8, args: anytype) !void
参数
self: *NativePaths
fmt: []const u8
std.zig.system.NativePaths.Allocator
别名
别名目标 std.mem.Allocator
std.zig.system.NativePaths.builtin
常量
std.zig.system.NativePaths.detect
函数
pub fn detect( arena: Allocator, io: Io, native_target: *const std.Target, environ_map: *const process.Environ.Map, ) !NativePaths
参数
std.zig.system.NativePaths.Io
别名
别名目标 std.Io
std.zig.system.NativePaths.mem
别名
别名目标 std.mem
std.zig.system.NativePaths.NativePaths
别名
std.zig.system.NativePaths.process
别名
别名目标 std.process
std.zig.system.NativePaths.std
别名
别名目标 std
源码
std/zig/system/NativePaths.zig
const NativePaths = @This();
const builtin = @import("builtin");
const std = @import("../../std.zig");
const Io = std.Io;
const Allocator = std.mem.Allocator;
const process = std.process;
const mem = std.mem;
arena: Allocator,
include_dirs: std.ArrayList([]const u8) = .empty,
lib_dirs: std.ArrayList([]const u8) = .empty,
framework_dirs: std.ArrayList([]const u8) = .empty,
rpaths: std.ArrayList([]const u8) = .empty,
warnings: std.ArrayList([]const u8) = .empty,
pub fn detect(
arena: Allocator,
io: Io,
native_target: *const std.Target,
environ_map: *const process.Environ.Map,
) !NativePaths {
var self: NativePaths = .{ .arena = arena };
var is_nix = false;
if (std.zig.EnvVar.NIX_CFLAGS_COMPILE.get(environ_map)) |nix_cflags_compile| {
is_nix = true;
var it = mem.tokenizeScalar(u8, nix_cflags_compile, ' ');
while (true) {
const word = it.next() orelse break;
if (mem.eql(u8, word, "-isystem") or mem.eql(u8, word, "-idirafter")) {
const include_path = it.next() orelse {
try self.addWarningFmt("Expected argument after {s} in NIX_CFLAGS_COMPILE", .{word});
break;
};
try self.addIncludeDir(include_path);
} else if (mem.eql(u8, word, "-iframework")) {
const framework_path = it.next() orelse {
try self.addWarning("Expected argument after -iframework in NIX_CFLAGS_COMPILE");
break;
};
try self.addFrameworkDir(framework_path);
} else if (mem.startsWith(u8, word, "-frandom-seed=") or
mem.startsWith(u8, word, "-fmacro-prefix-map="))
{
// 忽略此参数。
} else {
try self.addWarningFmt("Unrecognized C flag from NIX_CFLAGS_COMPILE: {s}", .{word});
}
}
}
if (std.zig.EnvVar.NIX_LDFLAGS.get(environ_map)) |nix_ldflags| {
is_nix = true;
var it = mem.tokenizeScalar(u8, nix_ldflags, ' ');
while (true) {
const word = it.next() orelse break;
if (mem.eql(u8, word, "-rpath")) {
const rpath = it.next() orelse {
try self.addWarning("Expected argument after -rpath in NIX_LDFLAGS");
break;
};
try self.addRPath(rpath);
} else if (mem.eql(u8, word, "-L") or mem.eql(u8, word, "-l")) {
_ = it.next() orelse {
try self.addWarning("Expected argument after -L or -l in NIX_LDFLAGS");
break;
};
} else if (mem.startsWith(u8, word, "-L")) {
const lib_path = word[2..];
try self.addLibDir(lib_path);
try self.addRPath(lib_path);
} else if (mem.startsWith(u8, word, "-l") or mem.startsWith(u8, word, "-static")) {
// 忽略此参数。
} else {
try self.addWarningFmt("Unrecognized C flag from NIX_LDFLAGS: {s}", .{word});
break;
}
}
}
if (std.zig.EnvVar.NIX_CFLAGS_LINK.get(environ_map)) |nix_cflags_link| {
is_nix = true;
var it = mem.tokenizeScalar(u8, nix_cflags_link, ' ');
while (true) {
const word = it.next() orelse break;
if (mem.eql(u8, word, "-rpath")) {
const rpath = it.next() orelse {
try self.addWarning("Expected argument after -rpath in NIX_CFLAGS_LINK");
break;
};
try self.addRPath(rpath);
} else if (mem.eql(u8, word, "-L") or mem.eql(u8, word, "-l")) {
_ = it.next() orelse {
try self.addWarning("Expected argument after -L or -l in NIX_CFLAGS_LINK");
break;
};
} else if (mem.startsWith(u8, word, "-L")) {
const lib_path = word[2..];
try self.addLibDir(lib_path);
try self.addRPath(lib_path);
} else if (mem.startsWith(u8, word, "-l") or mem.startsWith(u8, word, "-static")) {
// 忽略此参数。
} else {
try self.addWarningFmt("Unrecognized C flag from NIX_CFLAGS_LINK: {s}", .{word});
break;
}
}
}
if (is_nix) {
return self;
}
// TODO: 考虑同时添加 macports 路径
if (builtin.target.os.tag.isDarwin()) {
if (std.zig.system.darwin.isSdkInstalled(arena, io)) sdk: {
const sdk = std.zig.system.darwin.getSdk(arena, io, native_target) orelse break :sdk;
try self.addLibDir(try std.fs.path.join(arena, &.{ sdk, "usr/lib" }));
try self.addFrameworkDir(try std.fs.path.join(arena, &.{ sdk, "System/Library/Frameworks" }));
try self.addIncludeDir(try std.fs.path.join(arena, &.{ sdk, "usr/include" }));
}
// 检查 Homebrew 路径
if (std.zig.EnvVar.HOMEBREW_PREFIX.get(environ_map)) |prefix| {
try self.addLibDir(try std.fs.path.join(arena, &.{ prefix, "/lib" }));
try self.addIncludeDir(try std.fs.path.join(arena, &.{ prefix, "/include" }));
}
return self;
}
if (builtin.os.tag == .illumos) {
try self.addLibDir("/usr/lib/64");
try self.addLibDir("/usr/local/lib/64");
try self.addLibDir("/lib/64");
try self.addIncludeDir("/usr/include");
try self.addIncludeDir("/usr/local/include");
return self;
}
if (builtin.os.tag == .haiku) {
try self.addLibDir("/system/non-packaged/lib");
try self.addLibDir("/system/develop/lib");
try self.addLibDir("/system/lib");
return self;
}
if (builtin.os.tag != .windows and builtin.os.tag != .wasi) {
const triple = try std.zig.target.linuxTriple(arena, native_target);
const qual = native_target.ptrBitWidth();
// TODO: $ ld --verbose | grep SEARCH_DIR
// 输出包含一些以 lib64 结尾的路径,或许也应加入?
// TODO: 最佳顺序是什么?
// TODO: 其中一些路径可疑,应该仅在某些系统上添加,需要审核。
try self.addIncludeDir("/usr/local/include");
try self.addLibDirFmt("/usr/local/lib{d}", .{qual});
try self.addLibDir("/usr/local/lib");
try self.addIncludeDirFmt("/usr/include/{s}", .{triple});
try self.addLibDirFmt("/usr/lib/{s}", .{triple});
try self.addIncludeDir("/usr/include");
try self.addLibDirFmt("/lib{d}", .{qual});
try self.addLibDir("/lib");
try self.addLibDirFmt("/usr/lib{d}", .{qual});
try self.addLibDir("/usr/lib");
// 例如:基于 Debian 的 64 位 Linux 发行版,通过 apt 安装 zlib 时:
// zlib.h 位于 /usr/include(已在上文添加)
// libz.so.1 位于 /lib/x86_64-linux-gnu(在此添加)
try self.addLibDirFmt("/lib/{s}", .{triple});
// guix 等发行版不使用 FHS,因此依赖环境
// 变量查找头文件和库。
if (std.zig.EnvVar.C_INCLUDE_PATH.get(environ_map)) |c_include_path| {
var it = mem.tokenizeScalar(u8, c_include_path, ':');
while (it.next()) |dir| {
try self.addIncludeDir(dir);
}
}
if (std.zig.EnvVar.CPLUS_INCLUDE_PATH.get(environ_map)) |cplus_include_path| {
var it = mem.tokenizeScalar(u8, cplus_include_path, ':');
while (it.next()) |dir| {
try self.addIncludeDir(dir);
}
}
if (std.zig.EnvVar.LIBRARY_PATH.get(environ_map)) |library_path| {
var it = mem.tokenizeScalar(u8, library_path, ':');
while (it.next()) |dir| {
try self.addLibDir(dir);
}
}
}
return self;
}
pub fn addIncludeDir(self: *NativePaths, s: []const u8) !void {
return self.include_dirs.append(self.arena, s);
}
pub fn addIncludeDirFmt(self: *NativePaths, comptime fmt: []const u8, args: anytype) !void {
const item = try std.fmt.allocPrint(self.arena, fmt, args);
try self.include_dirs.append(self.arena, item);
}
pub fn addLibDir(self: *NativePaths, s: []const u8) !void {
try self.lib_dirs.append(self.arena, s);
}
pub fn addLibDirFmt(self: *NativePaths, comptime fmt: []const u8, args: anytype) !void {
const item = try std.fmt.allocPrint(self.arena, fmt, args);
try self.lib_dirs.append(self.arena, item);
}
pub fn addWarning(self: *NativePaths, s: []const u8) !void {
return self.warnings.append(self.arena, s);
}
pub fn addFrameworkDir(self: *NativePaths, s: []const u8) !void {
return self.framework_dirs.append(self.arena, s);
}
pub fn addFrameworkDirFmt(self: *NativePaths, comptime fmt: []const u8, args: anytype) !void {
const item = try std.fmt.allocPrint(self.arena, fmt, args);
try self.framework_dirs.append(self.arena, item);
}
pub fn addWarningFmt(self: *NativePaths, comptime fmt: []const u8, args: anytype) !void {
const item = try std.fmt.allocPrint(self.arena, fmt, args);
try self.warnings.append(self.arena, item);
}
pub fn addRPath(self: *NativePaths, s: []const u8) !void {
try self.rpaths.append(self.arena, s);
}