Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 12 additions & 11 deletions lib/std/process.zig
Original file line number Diff line number Diff line change
Expand Up @@ -351,13 +351,7 @@ test "getEnvMap" {
defer env.deinit();
}

pub const GetEnvVarOwnedError = error{
OutOfMemory,
EnvironmentVariableNotFound,

/// See https://github.com/ziglang/zig/issues/1774
InvalidUtf8,
};
pub const GetEnvVarOwnedError = error{EnvironmentVariableNotFound} || HasEnvVarError;

/// Caller must free returned memory.
pub fn getEnvVarOwned(allocator: Allocator, key: []const u8) GetEnvVarOwnedError![]u8 {
Expand Down Expand Up @@ -396,11 +390,18 @@ pub fn hasEnvVarConstant(comptime key: []const u8) bool {
}
}

pub fn hasEnvVar(allocator: Allocator, key: []const u8) error{OutOfMemory}!bool {
pub const HasEnvVarError = error{
OutOfMemory,
/// See https://github.com/ziglang/zig/issues/1774
InvalidUtf8,
};

pub fn hasEnvVar(allocator: Allocator, key: []const u8) HasEnvVarError!bool {
if (builtin.os.tag == .windows) {
var stack_alloc = std.heap.stackFallback(256 * @sizeOf(u16), allocator);
const key_w = try std.unicode.utf8ToUtf16LeWithNull(stack_alloc.get(), key);
defer stack_alloc.allocator.free(key_w);
var stack_allocator = std.heap.stackFallback(256 * @sizeOf(u16), allocator);
const stack_alloc = stack_allocator.get();
const key_w = try std.unicode.utf8ToUtf16LeWithNull(stack_alloc, key);
defer stack_alloc.free(key_w);
return std.os.getenvW(key_w) != null;
} else if (builtin.os.tag == .wasi and !builtin.link_libc) {
var envmap = getEnvMap(allocator) catch return error.OutOfMemory;
Expand Down
25 changes: 22 additions & 3 deletions lib/std/zig/system/NativePaths.zig
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,20 @@ const process = std.process;
const mem = std.mem;

const NativePaths = @This();
const NativeTargetInfo = std.zig.system.NativeTargetInfo;

include_dirs: ArrayList([:0]u8),
lib_dirs: ArrayList([:0]u8),
framework_dirs: ArrayList([:0]u8),
rpaths: ArrayList([:0]u8),
warnings: ArrayList([:0]u8),

pub fn detect(allocator: Allocator, native_info: NativeTargetInfo) !NativePaths {
const native_target = native_info.target;
pub fn isNix(allocator: Allocator) bool {
const cflags = process.hasEnvVar(allocator, "NIX_CFLAGS_COMPILE") catch return false;
const ldflags = process.hasEnvVar(allocator, "NIX_LDFLAGS") catch return false;
return cflags and ldflags;
}

pub fn detect(allocator: Allocator, native_target: std.Target) !NativePaths {
var self: NativePaths = .{
.include_dirs = ArrayList([:0]u8).init(allocator),
.lib_dirs = ArrayList([:0]u8).init(allocator),
Expand Down Expand Up @@ -188,6 +191,22 @@ fn deinitArray(array: *ArrayList([:0]u8)) void {
array.deinit();
}

pub inline fn getIncludeDirs(self: *const NativePaths) []const [:0]const u8 {
return self.include_dirs.items;
}

pub inline fn getLibDirs(self: *const NativePaths) []const [:0]const u8 {
return self.lib_dirs.items;
}

pub inline fn getFrameworkDirs(self: *const NativePaths) []const [:0]const u8 {
return self.framework_dirs.items;
}

pub inline fn getRpaths(self: *const NativePaths) []const [:0]const u8 {
return self.rpaths.items;
}

Comment on lines +194 to +209
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why inline?

why do these getters even exist?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It made sense to me instead of typing paths.lib_dirs.items everywhere but it's a matter of preference so I am happy to remove it.

pub fn addIncludeDir(self: *NativePaths, s: []const u8) !void {
return self.appendArray(&self.include_dirs, s);
}
Expand Down
Loading