| 1 | From 95a42ae38fed8ef0b0dd07b2568b649c1d7a9aeb 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 | 56 ++++++++++++++++++++++++-
|
| 9 | lib/std/zig/system/NativeTargetInfo.zig | 25 +++++++++--
|
| 10 | src/main.zig | 3 +-
|
| 11 | 3 files changed, 78 insertions(+), 6 deletions(-)
|
| 12 |
|
| 13 | diff --git a/lib/std/zig/system/NativePaths.zig b/lib/std/zig/system/NativePaths.zig
|
| 14 | index 2c4db3ec85..6366e37cd9 100644
|
| 15 | |
| 16 | |
| 17 | @@ -25,7 +25,53 @@ pub fn detect(allocator: Allocator, native_info: NativeTargetInfo) !NativePaths
|
| 18 | .warnings = ArrayList([:0]u8).init(allocator),
|
| 19 | };
|
| 20 | errdefer self.deinit();
|
| 21 | -
|
| 22 | + if (isGuix(allocator)) {
|
| 23 | + const guix_cross_include_paths = [_][]const u8{ "CROSS_C_INCLUDE_PATH", "CROSS_CPLUS_INCLUDE_PATH" };
|
| 24 | + for (guix_cross_include_paths) |env_var| {
|
| 25 | + if (process.getEnvVarOwned(allocator, env_var)) |include_path| {
|
| 26 | + var it = mem.tokenize(u8, include_path, ":");
|
| 27 | + while (it.next()) |dir|
|
| 28 | + try self.addIncludeDir(dir);
|
| 29 | + } else |err| switch (err) {
|
| 30 | + error.InvalidUtf8 => {},
|
| 31 | + error.EnvironmentVariableNotFound => {},
|
| 32 | + error.OutOfMemory => |e| return e,
|
| 33 | + }
|
| 34 | + }
|
| 35 | + if (process.getEnvVarOwned(allocator, "CROSS_LIBRARY_PATH")) |library_path| {
|
| 36 | + var it = mem.tokenize(u8, library_path, ":");
|
| 37 | + while (it.next()) |dir|
|
| 38 | + try self.addLibDir(dir);
|
| 39 | + } else |err| switch (err) {
|
| 40 | + error.InvalidUtf8 => {},
|
| 41 | + error.EnvironmentVariableNotFound => {},
|
| 42 | + error.OutOfMemory => |e| return e,
|
| 43 | + }
|
| 44 | + if (!isCrossGuix(allocator)) {
|
| 45 | + const guix_include_paths = [_][]const u8{ "C_INCLUDE_PATH", "CPLUS_INCLUDE_PATH" };
|
| 46 | + for (guix_include_paths) |env_var| {
|
| 47 | + if (process.getEnvVarOwned(allocator, env_var)) |include_path| {
|
| 48 | + var it = mem.tokenize(u8, include_path, ":");
|
| 49 | + while (it.next()) |dir|
|
| 50 | + try self.addIncludeDir(dir);
|
| 51 | + } else |err| switch (err) {
|
| 52 | + error.InvalidUtf8 => {},
|
| 53 | + error.EnvironmentVariableNotFound => {},
|
| 54 | + error.OutOfMemory => |e| return e,
|
| 55 | + }
|
| 56 | + }
|
| 57 | + if (process.getEnvVarOwned(allocator, "LIBRARY_PATH")) |library_path| {
|
| 58 | + var it = mem.tokenize(u8, library_path, ":");
|
| 59 | + while (it.next()) |dir|
|
| 60 | + try self.addLibDir(dir);
|
| 61 | + } else |err| switch (err) {
|
| 62 | + error.InvalidUtf8 => {},
|
| 63 | + error.EnvironmentVariableNotFound => {},
|
| 64 | + error.OutOfMemory => |e| return e,
|
| 65 | + }
|
| 66 | + }
|
| 67 | + return self;
|
| 68 | + }
|
| 69 | var is_nix = false;
|
| 70 | if (process.getEnvVarOwned(allocator, "NIX_CFLAGS_COMPILE")) |nix_cflags_compile| {
|
| 71 | defer allocator.free(nix_cflags_compile);
|
| 72 | @@ -231,3 +277,11 @@ fn appendArray(self: *NativePaths, array: *ArrayList([:0]u8), s: []const u8) !vo
|
| 73 | errdefer array.allocator.free(item);
|
| 74 | try array.append(item);
|
| 75 | }
|
| 76 | +
|
| 77 | +pub fn isCrossGuix(arena: Allocator) bool {
|
| 78 | + return process.hasEnvVar(arena, "CROSS_LIBRARY_PATH") catch false;
|
| 79 | +}
|
| 80 | +
|
| 81 | +pub fn isGuix(arena: Allocator) bool {
|
| 82 | + return isCrossGuix(arena) or process.hasEnvVar(arena, "LIBRARY_PATH") catch false;
|
| 83 | +}
|
| 84 | diff --git a/lib/std/zig/system/NativeTargetInfo.zig b/lib/std/zig/system/NativeTargetInfo.zig
|
| 85 | index cae45af64b..0c87d49816 100644
|
| 86 | |
| 87 | |
| 88 | @@ -936,10 +936,27 @@ fn defaultAbiAndDynamicLinker(cpu: Target.Cpu, os: Target.Os, cross_target: Cros
|
| 89 | };
|
| 90 | return NativeTargetInfo{
|
| 91 | .target = target,
|
| 92 | - .dynamic_linker = if (cross_target.dynamic_linker.get() == null)
|
| 93 | - target.standardDynamicLinkerPath()
|
| 94 | - else
|
| 95 | - cross_target.dynamic_linker,
|
| 96 | + .dynamic_linker = if (cross_target.dynamic_linker.get() == null) blk: {
|
| 97 | + var standard_linker = target.standardDynamicLinkerPath();
|
| 98 | + if (standard_linker.get()) |standard_linker_path| {
|
| 99 | + if (builtin.os.tag != .windows and builtin.os.tag != .wasi) {
|
| 100 | + if (std.os.getenv("CROSS_LIBRARY_PATH") orelse std.os.getenv("LIBRARY_PATH")) |library_path| {
|
| 101 | + const linker_basename = fs.path.basename(standard_linker_path);
|
| 102 | + var buffer: [255]u8 = undefined;
|
| 103 | + var it = mem.tokenize(u8, library_path, ":");
|
| 104 | + while (it.next()) |dir| {
|
| 105 | + const linker_fullpath = std.fmt.bufPrint(&buffer, "{s}{s}{s}", .{ dir, fs.path.sep_str, linker_basename }) catch "";
|
| 106 | + const guix_linker_path = fs.cwd().realpath(linker_fullpath, &buffer) catch "";
|
| 107 | + if (guix_linker_path.len != 0) {
|
| 108 | + standard_linker.set(guix_linker_path);
|
| 109 | + break;
|
| 110 | + }
|
| 111 | + }
|
| 112 | + }
|
| 113 | + }
|
| 114 | + }
|
| 115 | + break :blk standard_linker;
|
| 116 | + } else cross_target.dynamic_linker,
|
| 117 | };
|
| 118 | }
|
| 119 |
|
| 120 | diff --git a/src/main.zig b/src/main.zig
|
| 121 | index b752c89308..4fb7310a4f 100644
|
| 122 | |
| 123 | |
| 124 | @@ -2375,7 +2375,8 @@ fn buildOutputType(
|
| 125 | want_native_include_dirs = true;
|
| 126 | }
|
| 127 |
|
| 128 | - if (sysroot == null and cross_target.isNativeOs() and
|
| 129 | + if (std.zig.system.NativePaths.isCrossGuix(arena) or
|
| 130 | + sysroot == null and cross_target.isNativeOs() and
|
| 131 | (system_libs.count() != 0 or want_native_include_dirs))
|
| 132 | {
|
| 133 | const paths = std.zig.system.NativePaths.detect(arena, target_info) catch |err| {
|
| 134 | --
|
| 135 | 2.46.0
|
| 136 |
|