From 48d0fe6e1ff9740171f944c022719bdafad16ea6 Mon Sep 17 00:00:00 2001 From: dan Date: Thu, 13 Oct 2022 01:13:22 +0800 Subject: [PATCH] search include dir and lib dir from environment variables --- lib/std/zig/system/NativePaths.zig | 42 ++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/lib/std/zig/system/NativePaths.zig b/lib/std/zig/system/NativePaths.zig index 5f52b04ce423..fc406ed0de9e 100644 --- a/lib/std/zig/system/NativePaths.zig +++ b/lib/std/zig/system/NativePaths.zig @@ -135,6 +135,48 @@ 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 (true) { + const dir = it.next() orelse break; + try self.addIncludeDir(dir); + } + } else |err| switch (err) { + error.InvalidUtf8 => {}, + 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 (true) { + const dir = it.next() orelse break; + try self.addIncludeDir(dir); + } + } else |err| switch (err) { + error.InvalidUtf8 => {}, + 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 (true) { + const dir = it.next() orelse break; + try self.addLibDir(dir); + } + } else |err| switch (err) { + error.InvalidUtf8 => {}, + error.EnvironmentVariableNotFound => {}, + error.OutOfMemory => |e| return e, + } } return self;