| 1 | From ef7d97a722260e0a4fbd110625355b556c7ab8e3 Mon Sep 17 00:00:00 2001
|
| 2 | From: Hilton Chain <[email protected]>
|
| 3 | Date: Fri, 29 Nov 2024 14:13:30 +0800
|
| 4 | Subject: [PATCH] Use system paths.
|
| 5 |
|
| 6 | Prefer Guix search paths and support Guix cross builds.
|
| 7 | ---
|
| 8 | lib/std/zig/system.zig | 25 +++++++++++---
|
| 9 | lib/std/zig/system/NativePaths.zig | 53 ++++++++++++++++++++++++++++++
|
| 10 | src/main.zig | 3 +-
|
| 11 | 3 files changed, 76 insertions(+), 5 deletions(-)
|
| 12 |
|
| 13 | diff --git a/lib/std/zig/system.zig b/lib/std/zig/system.zig
|
| 14 | index 83c798c342..138d43f2e1 100644
|
| 15 | |
| 16 | |
| 17 | @@ -1123,10 +1123,27 @@ fn defaultAbiAndDynamicLinker(cpu: Target.Cpu, os: Target.Os, query: Target.Quer
|
| 18 | .os = os,
|
| 19 | .abi = abi,
|
| 20 | .ofmt = query.ofmt orelse Target.ObjectFormat.default(os.tag, cpu.arch),
|
| 21 | - .dynamic_linker = if (query.dynamic_linker.get() == null)
|
| 22 | - Target.DynamicLinker.standard(cpu, os.tag, abi)
|
| 23 | - else
|
| 24 | - query.dynamic_linker,
|
| 25 | + .dynamic_linker = if (query.dynamic_linker.get() == null) blk: {
|
| 26 | + var standard_linker = Target.DynamicLinker.standard(cpu, os.tag, abi);
|
| 27 | + if (standard_linker.get()) |standard_linker_path| {
|
| 28 | + if (builtin.os.tag != .windows and builtin.os.tag != .wasi) {
|
| 29 | + if (posix.getenv("CROSS_LIBRARY_PATH") orelse posix.getenv("LIBRARY_PATH")) |library_path| {
|
| 30 | + const linker_basename = fs.path.basename(standard_linker_path);
|
| 31 | + var buffer: [255]u8 = undefined;
|
| 32 | + var it = mem.tokenizeScalar(u8, library_path, ':');
|
| 33 | + while (it.next()) |dir| {
|
| 34 | + const linker_fullpath = std.fmt.bufPrint(&buffer, "{s}{s}{s}", .{ dir, fs.path.sep_str, linker_basename }) catch "";
|
| 35 | + const guix_linker_path = fs.cwd().realpath(linker_fullpath, &buffer) catch "";
|
| 36 | + if (guix_linker_path.len != 0) {
|
| 37 | + standard_linker.set(guix_linker_path);
|
| 38 | + break;
|
| 39 | + }
|
| 40 | + }
|
| 41 | + }
|
| 42 | + }
|
| 43 | + }
|
| 44 | + break :blk standard_linker;
|
| 45 | + } else query.dynamic_linker,
|
| 46 | };
|
| 47 | }
|
| 48 |
|
| 49 | diff --git a/lib/std/zig/system/NativePaths.zig b/lib/std/zig/system/NativePaths.zig
|
| 50 | index 2a50e27b0c..f4cc0e8b3a 100644
|
| 51 | |
| 52 | |
| 53 | @@ -15,6 +15,51 @@ warnings: std.ArrayListUnmanaged([]const u8) = .{},
|
| 54 |
|
| 55 | pub fn detect(arena: Allocator, native_target: std.Target) !NativePaths {
|
| 56 | var self: NativePaths = .{ .arena = arena };
|
| 57 | + if (isGuix(arena)) {
|
| 58 | + inline for ([_][]const u8{ "CROSS_C_INCLUDE_PATH", "CROSS_CPLUS_INCLUDE_PATH" }) |env_var| {
|
| 59 | + if (process.getEnvVarOwned(arena, env_var)) |include_path| {
|
| 60 | + var it = mem.tokenizeScalar(u8, include_path, ':');
|
| 61 | + while (it.next()) |dir|
|
| 62 | + try self.addIncludeDir(dir);
|
| 63 | + } else |err| switch (err) {
|
| 64 | + error.InvalidWtf8 => unreachable,
|
| 65 | + error.EnvironmentVariableNotFound => {},
|
| 66 | + error.OutOfMemory => |e| return e,
|
| 67 | + }
|
| 68 | + }
|
| 69 | + if (process.getEnvVarOwned(arena, "CROSS_LIBRARY_PATH")) |library_path| {
|
| 70 | + var it = mem.tokenizeScalar(u8, library_path, ':');
|
| 71 | + while (it.next()) |dir|
|
| 72 | + try self.addLibDir(dir);
|
| 73 | + } else |err| switch (err) {
|
| 74 | + error.InvalidWtf8 => unreachable,
|
| 75 | + error.EnvironmentVariableNotFound => {},
|
| 76 | + error.OutOfMemory => |e| return e,
|
| 77 | + }
|
| 78 | + if (!isCrossGuix(arena)) {
|
| 79 | + inline for ([_][]const u8{ "C_INCLUDE_PATH", "CPLUS_INCLUDE_PATH" }) |env_var| {
|
| 80 | + if (process.getEnvVarOwned(arena, env_var)) |include_path| {
|
| 81 | + var it = mem.tokenizeScalar(u8, include_path, ':');
|
| 82 | + while (it.next()) |dir|
|
| 83 | + try self.addIncludeDir(dir);
|
| 84 | + } else |err| switch (err) {
|
| 85 | + error.InvalidWtf8 => unreachable,
|
| 86 | + error.EnvironmentVariableNotFound => {},
|
| 87 | + error.OutOfMemory => |e| return e,
|
| 88 | + }
|
| 89 | + }
|
| 90 | + if (process.getEnvVarOwned(arena, "LIBRARY_PATH")) |library_path| {
|
| 91 | + var it = mem.tokenizeScalar(u8, library_path, ':');
|
| 92 | + while (it.next()) |dir|
|
| 93 | + try self.addLibDir(dir);
|
| 94 | + } else |err| switch (err) {
|
| 95 | + error.InvalidWtf8 => unreachable,
|
| 96 | + error.EnvironmentVariableNotFound => {},
|
| 97 | + error.OutOfMemory => |e| return e,
|
| 98 | + }
|
| 99 | + }
|
| 100 | + return self;
|
| 101 | + }
|
| 102 | var is_nix = false;
|
| 103 | if (process.getEnvVarOwned(arena, "NIX_CFLAGS_COMPILE")) |nix_cflags_compile| {
|
| 104 | is_nix = true;
|
| 105 | @@ -208,3 +253,11 @@ pub fn addWarningFmt(self: *NativePaths, comptime fmt: []const u8, args: anytype
|
| 106 | pub fn addRPath(self: *NativePaths, s: []const u8) !void {
|
| 107 | try self.rpaths.append(self.arena, s);
|
| 108 | }
|
| 109 | +
|
| 110 | +pub fn isCrossGuix(arena: Allocator) bool {
|
| 111 | + return process.hasEnvVar(arena, "CROSS_LIBRARY_PATH") catch false;
|
| 112 | +}
|
| 113 | +
|
| 114 | +pub fn isGuix(arena: Allocator) bool {
|
| 115 | + return isCrossGuix(arena) or process.hasEnvVar(arena, "LIBRARY_PATH") catch false;
|
| 116 | +}
|
| 117 | diff --git a/src/main.zig b/src/main.zig
|
| 118 | index 5b0e211647..d27b63d5a3 100644
|
| 119 | |
| 120 | |
| 121 | @@ -3706,7 +3706,8 @@ fn createModule(
|
| 122 | }
|
| 123 |
|
| 124 | // Trigger native system library path detection if necessary.
|
| 125 | - if (create_module.sysroot == null and
|
| 126 | + if (std.zig.system.NativePaths.isCrossGuix(arena) or
|
| 127 | + create_module.sysroot == null and
|
| 128 | resolved_target.is_native_os and resolved_target.is_native_abi and
|
| 129 | create_module.want_native_include_dirs)
|
| 130 | {
|
| 131 | --
|
| 132 | 2.46.0
|
| 133 |
|