Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

std.zig: search include dir and lib dir from environment variables #13145

Merged
merged 4 commits into from Oct 16, 2022
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
41 changes: 41 additions & 0 deletions lib/std/zig/system/NativePaths.zig
Expand Up @@ -135,6 +135,47 @@ pub fn detect(allocator: Allocator, native_info: NativeTargetInfo) !NativePaths
// zlib.h is in /usr/include (added above)
// libz.so.1 is in /lib/x86_64-linux-gnu (added here)
try self.addLibDirFmt("/lib/{s}", .{triple});

// NOTE: distro like guix doesn't use FHS, so it relies on envorinment
// variables (C_INCLUDE_PATH, CPLUS_INCLUDE_PATH and LIBRARY_PATH) to
// search for headers and libraries
if (process.getEnvVarOwned(allocator, "C_INCLUDE_PATH")) |c_include_path| {
defer allocator.free(c_include_path);
var it = mem.tokenize(u8, c_include_path, ":");
while (it.next()) |dir| {
try self.addIncludeDir(dir);
}
} else |err| switch (err) {
// NOTE: since the above code won't be executed on windows, this
// InvalidUtf8 error is generally unreachable
error.InvalidUtf8 => unreachable,
error.EnvironmentVariableNotFound => {},
error.OutOfMemory => |e| return e,
}

if (process.getEnvVarOwned(allocator, "CPLUS_INCLUDE_PATH")) |cplus_include_path| {
defer allocator.free(cplus_include_path);
var it = mem.tokenize(u8, cplus_include_path, ":");
while (it.next()) |dir| {
try self.addIncludeDir(dir);
}
} else |err| switch (err) {
error.InvalidUtf8 => unreachable,
error.EnvironmentVariableNotFound => {},
error.OutOfMemory => |e| return e,
}

if (process.getEnvVarOwned(allocator, "LIBRARY_PATH")) |library_path| {
defer allocator.free(library_path);
var it = mem.tokenize(u8, library_path, ":");
while (it.next()) |dir| {
try self.addLibDir(dir);
}
} else |err| switch (err) {
error.InvalidUtf8 => unreachable,
error.EnvironmentVariableNotFound => {},
error.OutOfMemory => |e| return e,
}
}

return self;
Expand Down