~ruther/guix-local

ab8979321881e59777da31ad11e77d5e482ff843 — Hilton Chain 1 year, 7 months ago 654e058
gnu: Add zig-0.11.

* gnu/packages/patches/zig-0.11-fix-runpath.patch: New file.
* gnu/packages/patches/zig-0.11-use-system-paths.patch: New file.
* gnu/local.mk (dist_patch_DATA): Register them.
* gnu/packages/zig.scm (zig-0.11-glibc-abi-tool,zig-0.11): New variables.

Change-Id: I2507af62918f3989967d55dec942b84655d6d8bd
M gnu/local.mk => gnu/local.mk +2 -0
@@ 2423,6 2423,8 @@ dist_patch_DATA =						\
  %D%/packages/patches/zig-0.10.0-675-TypeOf-hack.patch		\
  %D%/packages/patches/zig-0.10.0-747-CallOptions.patch		\
  %D%/packages/patches/zig-0.10.0-1638-re-add-qualCast.patch	\
  %D%/packages/patches/zig-0.11-fix-runpath.patch		\
  %D%/packages/patches/zig-0.11-use-system-paths.patch		\
  %D%/packages/patches/zsh-egrep-failing-test.patch		\
  %D%/packages/patches/zuo-bin-sh.patch


A gnu/packages/patches/zig-0.11-fix-runpath.patch => gnu/packages/patches/zig-0.11-fix-runpath.patch +45 -0
@@ 0,0 1,45 @@
From 4a5397b856564d0fbc0b89186e682e6694a909b0 Mon Sep 17 00:00:00 2001
From: Hilton Chain <hako@ultrarare.space>
Date: Wed, 27 Nov 2024 11:55:44 +0800
Subject: [PATCH] Fix RUNPATH issue.

Add needed libraries and libc to RUNPATH when CROSS_LIBRARY_PATH or LIBRARY_PATH
is set.
---
 src/Compilation.zig | 2 +-
 src/link/Elf.zig    | 6 ++++++
 2 files changed, 7 insertions(+), 1 deletion(-)

diff --git a/src/Compilation.zig b/src/Compilation.zig
index a08c3e09f4..add374d0ba 100644
--- a/src/Compilation.zig
+++ b/src/Compilation.zig
@@ -1542,7 +1542,7 @@ pub fn create(gpa: Allocator, options: InitOptions) !*Compilation {
             .llvm_cpu_features = llvm_cpu_features,
             .skip_linker_dependencies = options.skip_linker_dependencies,
             .parent_compilation_link_libc = options.parent_compilation_link_libc,
-            .each_lib_rpath = options.each_lib_rpath orelse false,
+            .each_lib_rpath = std.zig.system.NativePaths.isGuix(arena) or options.each_lib_rpath orelse false,
             .build_id = build_id,
             .cache_mode = cache_mode,
             .disable_lld_caching = options.disable_lld_caching or cache_mode == .whole,
diff --git a/src/link/Elf.zig b/src/link/Elf.zig
index dd88d47fab..b6e3944725 100644
--- a/src/link/Elf.zig
+++ b/src/link/Elf.zig
@@ -1730,6 +1730,12 @@ fn linkWithLLD(self: *Elf, comp: *Compilation, prog_node: *std.Progress.Node) !v
                     }
                 }
             }
+            if (self.base.options.link_libc and self.base.options.link_mode == .Dynamic) {
+                if (self.base.options.libc_installation) |libc_installation| {
+                    try argv.append("-rpath");
+                    try argv.append(libc_installation.crt_dir.?);
+                }
+            }
         }
 
         for (self.base.options.lib_dirs) |lib_dir| {
-- 
2.46.0


A gnu/packages/patches/zig-0.11-use-system-paths.patch => gnu/packages/patches/zig-0.11-use-system-paths.patch +133 -0
@@ 0,0 1,133 @@
From 8b76c88e577fcd6a054ecfee333b2989ec94a3cc Mon Sep 17 00:00:00 2001
From: Hilton Chain <hako@ultrarare.space>
Date: Wed, 27 Nov 2024 11:54:51 +0800
Subject: [PATCH] Use system paths.

Prefer Guix search paths and support Guix cross builds.
---
 lib/std/zig/system/NativePaths.zig      | 53 +++++++++++++++++++++++++
 lib/std/zig/system/NativeTargetInfo.zig | 25 ++++++++++--
 src/main.zig                            |  3 +-
 3 files changed, 76 insertions(+), 5 deletions(-)

diff --git a/lib/std/zig/system/NativePaths.zig b/lib/std/zig/system/NativePaths.zig
index 4c8f1286b8..5b4805f6c6 100644
--- a/lib/std/zig/system/NativePaths.zig
+++ b/lib/std/zig/system/NativePaths.zig
@@ -17,6 +17,51 @@ warnings: std.ArrayListUnmanaged([]const u8) = .{},
 pub fn detect(arena: Allocator, native_info: NativeTargetInfo) !NativePaths {
     const native_target = native_info.target;
     var self: NativePaths = .{ .arena = arena };
+    if (isGuix(arena)) {
+        inline for ([_][]const u8{ "CROSS_C_INCLUDE_PATH", "CROSS_CPLUS_INCLUDE_PATH" }) |env_var| {
+            if (process.getEnvVarOwned(arena, env_var)) |include_path| {
+                var it = mem.tokenizeScalar(u8, include_path, ':');
+                while (it.next()) |dir|
+                    try self.addIncludeDir(dir);
+            } else |err| switch (err) {
+                error.InvalidUtf8 => {},
+                error.EnvironmentVariableNotFound => {},
+                error.OutOfMemory => |e| return e,
+            }
+        }
+        if (process.getEnvVarOwned(arena, "CROSS_LIBRARY_PATH")) |library_path| {
+            var it = mem.tokenizeScalar(u8, library_path, ':');
+            while (it.next()) |dir|
+                try self.addLibDir(dir);
+        } else |err| switch (err) {
+            error.InvalidUtf8 => {},
+            error.EnvironmentVariableNotFound => {},
+            error.OutOfMemory => |e| return e,
+        }
+        if (!isCrossGuix(arena)) {
+            inline for ([_][]const u8{ "C_INCLUDE_PATH", "CPLUS_INCLUDE_PATH" }) |env_var| {
+                if (process.getEnvVarOwned(arena, env_var)) |include_path| {
+                    var it = mem.tokenizeScalar(u8, include_path, ':');
+                    while (it.next()) |dir|
+                        try self.addIncludeDir(dir);
+                } else |err| switch (err) {
+                    error.InvalidUtf8 => {},
+                    error.EnvironmentVariableNotFound => {},
+                    error.OutOfMemory => |e| return e,
+                }
+            }
+            if (process.getEnvVarOwned(arena, "LIBRARY_PATH")) |library_path| {
+                var it = mem.tokenizeScalar(u8, library_path, ':');
+                while (it.next()) |dir|
+                    try self.addLibDir(dir);
+            } else |err| switch (err) {
+                error.InvalidUtf8 => {},
+                error.EnvironmentVariableNotFound => {},
+                error.OutOfMemory => |e| return e,
+            }
+        }
+        return self;
+    }
     var is_nix = false;
     if (process.getEnvVarOwned(arena, "NIX_CFLAGS_COMPILE")) |nix_cflags_compile| {
         is_nix = true;
@@ -196,3 +241,11 @@ pub fn addWarningFmt(self: *NativePaths, comptime fmt: []const u8, args: anytype
 pub fn addRPath(self: *NativePaths, s: []const u8) !void {
     try self.rpaths.append(self.arena, s);
 }
+
+pub fn isCrossGuix(arena: Allocator) bool {
+    return process.hasEnvVar(arena, "CROSS_LIBRARY_PATH") catch false;
+}
+
+pub fn isGuix(arena: Allocator) bool {
+    return isCrossGuix(arena) or process.hasEnvVar(arena, "LIBRARY_PATH") catch false;
+}
diff --git a/lib/std/zig/system/NativeTargetInfo.zig b/lib/std/zig/system/NativeTargetInfo.zig
index 99a1a8f2ef..50d0d9949c 100644
--- a/lib/std/zig/system/NativeTargetInfo.zig
+++ b/lib/std/zig/system/NativeTargetInfo.zig
@@ -923,10 +923,27 @@ fn defaultAbiAndDynamicLinker(cpu: Target.Cpu, os: Target.Os, cross_target: Cros
     };
     return NativeTargetInfo{
         .target = target,
-        .dynamic_linker = if (cross_target.dynamic_linker.get() == null)
-            target.standardDynamicLinkerPath()
-        else
-            cross_target.dynamic_linker,
+        .dynamic_linker = if (cross_target.dynamic_linker.get() == null) blk: {
+            var standard_linker = target.standardDynamicLinkerPath();
+            if (standard_linker.get()) |standard_linker_path| {
+                if (builtin.os.tag != .windows and builtin.os.tag != .wasi) {
+                    if (std.os.getenv("CROSS_LIBRARY_PATH") orelse std.os.getenv("LIBRARY_PATH")) |library_path| {
+                        const linker_basename = fs.path.basename(standard_linker_path);
+                        var buffer: [255]u8 = undefined;
+                        var it = mem.tokenizeScalar(u8, library_path, ':');
+                        while (it.next()) |dir| {
+                            const linker_fullpath = std.fmt.bufPrint(&buffer, "{s}{s}{s}", .{ dir, fs.path.sep_str, linker_basename }) catch "";
+                            const guix_linker_path = fs.cwd().realpath(linker_fullpath, &buffer) catch "";
+                            if (guix_linker_path.len != 0) {
+                                standard_linker.set(guix_linker_path);
+                                break;
+                            }
+                        }
+                    }
+                }
+            }
+            break :blk standard_linker;
+        } else cross_target.dynamic_linker,
     };
 }
 
diff --git a/src/main.zig b/src/main.zig
index 456886c915..e2a97bf244 100644
--- a/src/main.zig
+++ b/src/main.zig
@@ -2674,7 +2674,8 @@ fn buildOutputType(
     // After this point, external_system_libs is used instead of system_libs.
 
     // Trigger native system library path detection if necessary.
-    if (sysroot == null and cross_target.isNativeOs() and cross_target.isNativeAbi() and
+    if (std.zig.system.NativePaths.isCrossGuix(arena) or
+        sysroot == null and cross_target.isNativeOs() and cross_target.isNativeAbi() and
         (external_system_libs.len != 0 or want_native_include_dirs))
     {
         const paths = std.zig.system.NativePaths.detect(arena, target_info) catch |err| {
-- 
2.46.0


M gnu/packages/zig.scm => gnu/packages/zig.scm +74 -0
@@ 1084,4 1084,78 @@ toolchain.  Among other features it provides
       (modify-inputs (package-native-inputs base)
         (replace "zig" `(,base "out")))))))

(define zig-0.11-glibc-abi-tool
  (origin
    (method git-fetch)
    (uri (git-reference
          (url "https://github.com/ziglang/glibc-abi-tool")
          (commit "13576b1ea957882be7ff2c99f4cdc27454930219")))
    (file-name "glibc-abi-tool")
    (sha256
     (base32 "09m0ipixxw0dnal0zsgk6kvcz29y9s256b9y00s4hkhj95n630il"))
    (modules '((guix build utils)))
    (snippet
     #~(begin
         (substitute* "consolidate.zig"
           ((".*minor = 3[5678].*") "")
           (("(w\\.writeIntLittle.u16, )(@intCast.*);" _ prefix suffix)
            (string-append prefix "@as(u16, " suffix ");")))
         (with-directory-excursion "glibc"
           (for-each delete-file-recursively
                     '("2.35" "2.36" "2.37" "2.38")))))))

(define-public zig-0.11
  (package
    (inherit zig-0.10)
    (name "zig")
    (version "0.11.0")
    (source
     (origin
       (inherit (zig-source
                 version version
                 "0qh7c27cd4wcdjj0mbpkarvwypfk1js8hkyxs0z149qv75zkbrca"))
       (patches
        (search-patches
         "zig-0.9-use-baseline-cpu-by-default.patch"
         "zig-0.11-use-system-paths.patch"
         "zig-0.11-fix-runpath.patch"))))
    (arguments
     (substitute-keyword-arguments (package-arguments zig-0.10)
       ((#:phases phases '%standard-phases)
        #~(modify-phases #$phases
            (add-after 'unpack 'prepare-source
              (lambda* (#:key native-inputs inputs #:allow-other-keys)
                (install-file (search-input-file
                               (or native-inputs inputs) "bin/zig1.wasm")
                              "stage1")
                (make-file-writable "stage1/zig1.wasm")))
            (add-after 'install 'build-zig1
              (lambda _
                (invoke (string-append #$output "/bin/zig")
                        "build" "update-zig1" "--verbose")))
            (add-after 'build-zig1 'install-zig1
              (lambda _
                (install-file "stage1/zig1.wasm"
                              (string-append #$output:zig1 "/bin"))))
            ;; TODO: Disable tests for macOS target and run full test.
            ;; Issue with glibc in CPLUS_INCLUDE_PATH:
            ;; <https://github.com/ziglang/zig/issues/18063>.
            (replace 'check
              (lambda* (#:key tests? #:allow-other-keys)
                (when tests?
                  (invoke (string-append #$output "/bin/zig")
                          "test" "-I" "test" "test/behavior.zig"))))))))
    (inputs
     (modify-inputs (package-inputs zig-0.10)
       (replace "clang" clang-16)
       (replace "lld" lld-16)))
    (native-inputs
     (modify-inputs (package-native-inputs zig-0.10)
       (prepend binaryen `(,zig-0.10.0-3985 "zig1"))
       (replace "glibc-abi-tool" zig-0.11-glibc-abi-tool)
       (replace "llvm" llvm-16)))
    (outputs '("out" "zig1"))
    (properties `((max-silent-time . 9600)
                  ,@(clang-compiler-cpu-architectures "16")))))

(define-public zig zig-0.10)