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