hako pushed a commit to branch wip-zig-bootstrap
in repository guix.

commit bc247374b8d5b205ea5b85cce552157bbd936581
Author: Hilton Chain <[email protected]>
AuthorDate: Tue Nov 12 17:37:33 2024 +0800

    gnu: zig-0.10: Fix RUNPATH issue.
    
    * gnu/packages/patches/zig-0.10-fix-runpath.patch: New file.
    * gnu/packages/patches/zig-0.10-use-system-paths.patch: New file.
    * gnu/local.mk (dist_patch_DATA): Regisiter them.
    * gnu/packages/zig.scm (zig-0.10)[source]: Add patches.
    Use zig-source.
    [arguments]<#:validate-runpath?>: Unset.
    <#:phases>: Adjust 'patch-more-shebangs to use a file in inputs instead.
    
    Change-Id: Ic4fd22d8bba664e3d42f433875f9d099969b9df9
---
 gnu/local.mk                                       |   2 +
 gnu/packages/patches/zig-0.10-fix-runpath.patch    |  45 +++++++
 .../patches/zig-0.10-use-system-paths.patch        | 136 +++++++++++++++++++++
 gnu/packages/zig.scm                               |  24 ++--
 4 files changed, 194 insertions(+), 13 deletions(-)

diff --git a/gnu/local.mk b/gnu/local.mk
index 557566f69e..5c10ced827 100644
--- a/gnu/local.mk
+++ b/gnu/local.mk
@@ -2391,6 +2391,8 @@ dist_patch_DATA =                                         
\
   %D%/packages/patches/zig-0.9-riscv-support.patch             \
   %D%/packages/patches/zig-0.9-use-baseline-cpu-by-default.patch       \
   %D%/packages/patches/zig-0.9-use-system-paths.patch          \
+  %D%/packages/patches/zig-0.10-fix-runpath.patch              \
+  %D%/packages/patches/zig-0.10-use-system-paths.patch         \
   %D%/packages/patches/zsh-egrep-failing-test.patch            \
   %D%/packages/patches/zuo-bin-sh.patch
 
diff --git a/gnu/packages/patches/zig-0.10-fix-runpath.patch 
b/gnu/packages/patches/zig-0.10-fix-runpath.patch
new file mode 100644
index 0000000000..59b7975717
--- /dev/null
+++ b/gnu/packages/patches/zig-0.10-fix-runpath.patch
@@ -0,0 +1,45 @@
+From b8678a24d96c4a94d7309ea56bd35bae41fbbeae Mon Sep 17 00:00:00 2001
+From: Hilton Chain <[email protected]>
+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 3ddf936e57..138df1f913 100644
+--- a/src/Compilation.zig
++++ b/src/Compilation.zig
+@@ -1886,7 +1886,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 
options.is_native_os,
++            .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 2663f90aee..8813aea827 100644
+--- a/src/link/Elf.zig
++++ b/src/link/Elf.zig
+@@ -1620,6 +1620,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
+
diff --git a/gnu/packages/patches/zig-0.10-use-system-paths.patch 
b/gnu/packages/patches/zig-0.10-use-system-paths.patch
new file mode 100644
index 0000000000..af53209ad7
--- /dev/null
+++ b/gnu/packages/patches/zig-0.10-use-system-paths.patch
@@ -0,0 +1,136 @@
+From 95a42ae38fed8ef0b0dd07b2568b649c1d7a9aeb Mon Sep 17 00:00:00 2001
+From: Hilton Chain <[email protected]>
+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      | 56 ++++++++++++++++++++++++-
+ lib/std/zig/system/NativeTargetInfo.zig | 25 +++++++++--
+ src/main.zig                            |  3 +-
+ 3 files changed, 78 insertions(+), 6 deletions(-)
+
+diff --git a/lib/std/zig/system/NativePaths.zig 
b/lib/std/zig/system/NativePaths.zig
+index 2c4db3ec85..6366e37cd9 100644
+--- a/lib/std/zig/system/NativePaths.zig
++++ b/lib/std/zig/system/NativePaths.zig
+@@ -25,7 +25,53 @@ pub fn detect(allocator: Allocator, native_info: 
NativeTargetInfo) !NativePaths
+         .warnings = ArrayList([:0]u8).init(allocator),
+     };
+     errdefer self.deinit();
+-
++    if (isGuix(allocator)) {
++        const guix_cross_include_paths = [_][]const u8{ 
"CROSS_C_INCLUDE_PATH", "CROSS_CPLUS_INCLUDE_PATH" };
++        for (guix_cross_include_paths) |env_var| {
++            if (process.getEnvVarOwned(allocator, env_var)) |include_path| {
++                var it = mem.tokenize(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(allocator, "CROSS_LIBRARY_PATH")) 
|library_path| {
++            var it = mem.tokenize(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(allocator)) {
++            const guix_include_paths = [_][]const u8{ "C_INCLUDE_PATH", 
"CPLUS_INCLUDE_PATH" };
++            for (guix_include_paths) |env_var| {
++                if (process.getEnvVarOwned(allocator, env_var)) 
|include_path| {
++                    var it = mem.tokenize(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(allocator, "LIBRARY_PATH")) 
|library_path| {
++                var it = mem.tokenize(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(allocator, "NIX_CFLAGS_COMPILE")) 
|nix_cflags_compile| {
+         defer allocator.free(nix_cflags_compile);
+@@ -231,3 +277,11 @@ fn appendArray(self: *NativePaths, array: 
*ArrayList([:0]u8), s: []const u8) !vo
+     errdefer array.allocator.free(item);
+     try array.append(item);
+ }
++
++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 cae45af64b..0c87d49816 100644
+--- a/lib/std/zig/system/NativeTargetInfo.zig
++++ b/lib/std/zig/system/NativeTargetInfo.zig
+@@ -936,10 +936,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.tokenize(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 b752c89308..4fb7310a4f 100644
+--- a/src/main.zig
++++ b/src/main.zig
+@@ -2375,7 +2375,8 @@ fn buildOutputType(
+             want_native_include_dirs = true;
+     }
+ 
+-    if (sysroot == null and cross_target.isNativeOs() and
++    if (std.zig.system.NativePaths.isCrossGuix(arena) or
++        sysroot == null and cross_target.isNativeOs() and
+         (system_libs.count() != 0 or want_native_include_dirs))
+     {
+         const paths = std.zig.system.NativePaths.detect(arena, target_info) 
catch |err| {
+-- 
+2.46.0
+
diff --git a/gnu/packages/zig.scm b/gnu/packages/zig.scm
index 8818ffdf87..aff69f5aaa 100644
--- a/gnu/packages/zig.scm
+++ b/gnu/packages/zig.scm
@@ -207,14 +207,14 @@ toolchain.  Among other features it provides
     (version "0.10.1")
     (source
      (origin
-       (method git-fetch)
-       (uri (git-reference
-             (url "https://github.com/ziglang/zig.git";)
-             (commit version)))
-       (file-name (git-file-name name version))
-       (sha256
-        (base32 "1sh5xjsksl52i4cfv1qj36sz5h0ln7cq4pdhgs3960mk8a90im7b"))
-       (patches (search-patches "zig-0.9-use-baseline-cpu-by-default.patch"))))
+       (inherit (zig-source
+                 version version
+                 "1sh5xjsksl52i4cfv1qj36sz5h0ln7cq4pdhgs3960mk8a90im7b"))
+       (patches
+        (search-patches
+         "zig-0.9-use-baseline-cpu-by-default.patch"
+         "zig-0.10-use-system-paths.patch"
+         "zig-0.10-fix-runpath.patch"))))
     (arguments
      (substitute-keyword-arguments (package-arguments zig-0.9)
        ((#:configure-flags flags ''())
@@ -222,8 +222,6 @@ toolchain.  Among other features it provides
                  "-DZIG_SHARED_LLVM=ON"
                  (string-append "-DZIG_LIB_DIR=" #$output "/lib/zig")
                  #$flags))
-       ;; TODO: zig binary can't find ld-linux.
-       ((#:validate-runpath? _ #t) #f)
        ((#:tests? _ #t) #t)
        ((#:phases phases '%standard-phases)
         #~(modify-phases #$phases
@@ -237,10 +235,10 @@ toolchain.  Among other features it provides
                 (setenv "CC" #$(cc-for-target))))
             (add-after 'patch-source-shebangs 'patch-more-shebangs
               (lambda* (#:key inputs #:allow-other-keys)
-                ;; Zig uses information about /usr/bin/env to determine the
-                ;; version of glibc and other data.
+                ;; Zig uses information about an ELF file to determine the
+                ;; version of glibc and other data for native builds.
                 (substitute* "lib/std/zig/system/NativeTargetInfo.zig"
-                  (("/usr/bin/env") (search-input-file inputs "/bin/env")))))
+                  (("/usr/bin/env") (search-input-file inputs 
"bin/clang++")))))
             (replace 'check
               (lambda* (#:key tests? #:allow-other-keys)
                 (when tests?

Reply via email to