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

commit b675cfe17c0e421a7ecc35b227369c3ce1c6c035
Author: Hilton Chain <hako@ultrarare.space>
AuthorDate: Tue Dec 31 21:15:46 2024 +0800

    DRAFT gnu: Add zig-0.14 (fake).
    
    * gnu/packages/patches/zig-0.14-fix-runpath.patch: New file.
    * gnu/packages/patches/zig-0.14-use-baseline-cpu-by-default.patch: New file.
    * gnu/packages/patches/zig-0.14-use-system-paths.patch: New file.
    * gnu/local.mk (dist_patch_DATA): Regisiter them.
    * gnu/packages/zig.scm (zig-0.14-glibc-abi-tool,zig-0.14): New variables.
    
    Change-Id: Ibb9e49ee451ca3bac58bd33a50a9f53e0aa31402
---
 gnu/local.mk                                       |   3 +
 gnu/packages/patches/zig-0.14-fix-runpath.patch    | 118 ++++++++++++++++++
 .../zig-0.14-use-baseline-cpu-by-default.patch     |  36 ++++++
 .../patches/zig-0.14-use-system-paths.patch        | 133 +++++++++++++++++++++
 gnu/packages/zig.scm                               |  39 ++++++
 5 files changed, 329 insertions(+)

diff --git a/gnu/local.mk b/gnu/local.mk
index c421da85cb..f238348074 100644
--- a/gnu/local.mk
+++ b/gnu/local.mk
@@ -2453,6 +2453,9 @@ dist_patch_DATA =                                         
\
   %D%/packages/patches/zig-0.12-use-system-paths.patch         \
   %D%/packages/patches/zig-0.13-build-respect-PKG_CONFIG-env-var.patch \
   %D%/packages/patches/zig-0.13-fix-runpath.patch              \
+  %D%/packages/patches/zig-0.14-fix-runpath.patch              \
+  %D%/packages/patches/zig-0.14-use-baseline-cpu-by-default.patch      \
+  %D%/packages/patches/zig-0.14-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.14-fix-runpath.patch 
b/gnu/packages/patches/zig-0.14-fix-runpath.patch
new file mode 100644
index 0000000000..324544165e
--- /dev/null
+++ b/gnu/packages/patches/zig-0.14-fix-runpath.patch
@@ -0,0 +1,118 @@
+From d5c31f6d99a3d02c8b0efba13e1a1e82e0e36f96 Mon Sep 17 00:00:00 2001
+From: Hilton Chain <hako@ultrarare.space>
+Date: Fri, 29 Nov 2024 14:13:46 +0800
+Subject: [PATCH] Fix RUNPATH issue.
+
+Add needed libraries and libc to RUNPATH when CROSS_LIBRARY_PATH or 
LIBRARY_PATH
+is set.
+---
+ lib/std/Build/Step/Compile.zig |  3 +++
+ src/link/Elf.zig               | 14 ++++++++++++++
+ src/main.zig                   | 34 +++++++++++++++++++++++++++++++++-
+ 3 files changed, 50 insertions(+), 1 deletion(-)
+
+diff --git a/lib/std/Build/Step/Compile.zig b/lib/std/Build/Step/Compile.zig
+index 1dd444abf2..df7f4a56db 100644
+--- a/lib/std/Build/Step/Compile.zig
++++ b/lib/std/Build/Step/Compile.zig
+@@ -778,6 +778,9 @@ fn runPkgConfig(compile: *Compile, lib_name: []const u8) 
!PkgConfigResult {
+             try zig_cflags.appendSlice(&[_][]const u8{ "-D", macro });
+         } else if (mem.startsWith(u8, arg, "-D")) {
+             try zig_cflags.append(arg);
++        } else if (mem.startsWith(u8, arg, "-Wl,-rpath=")) {
++            const dir = arg["-Wl,-rpath=".len..];
++            try zig_libs.appendSlice(&[_][]const u8{ "-L", dir });
+         } else if (b.debug_pkg_config) {
+             return compile.step.fail("unknown pkg-config flag '{s}'", .{arg});
+         }
+diff --git a/src/link/Elf.zig b/src/link/Elf.zig
+index 608ff2fe3a..2b9fe803fd 100644
+--- a/src/link/Elf.zig
++++ b/src/link/Elf.zig
+@@ -1037,6 +1037,13 @@ fn dumpArgvInit(self: *Elf, arena: Allocator) !void {
+             try argv.appendSlice(gpa, &.{ "-rpath", rpath });
+         }
+ 
++        if (std.zig.system.NativePaths.isGuix(arena) and 
comp.config.link_libc and comp.config.link_mode == .dynamic) {
++            if (self.base.comp.libc_installation) |libc_installation| {
++                try argv.append(gpa, "-rpath");
++                try argv.append(gpa, libc_installation.crt_dir.?);
++            }
++        }
++
+         try argv.appendSlice(gpa, &.{
+             "-z",
+             try std.fmt.allocPrint(arena, "stack-size={d}", 
.{self.base.stack_size}),
+@@ -1857,6 +1864,13 @@ fn linkWithLLD(self: *Elf, arena: Allocator, tid: 
Zcu.PerThread.Id, prog_node: s
+             try argv.appendSlice(&.{ "-rpath", rpath });
+         }
+ 
++        if (std.zig.system.NativePaths.isGuix(arena) and 
comp.config.link_libc and link_mode == .dynamic) {
++            if (self.base.comp.libc_installation) |libc_installation| {
++                try argv.append("-rpath");
++                try argv.append(libc_installation.crt_dir.?);
++            }
++        }
++
+         for (self.symbol_wrap_set.keys()) |symbol_name| {
+             try argv.appendSlice(&.{ "-wrap", symbol_name });
+         }
+diff --git a/src/main.zig b/src/main.zig
+index 79493aa624..505bb7570a 100644
+--- a/src/main.zig
++++ b/src/main.zig
+@@ -3901,7 +3901,7 @@ fn createModule(
+                 create_module.want_native_include_dirs = true;
+         }
+ 
+-        if (create_module.each_lib_rpath orelse resolved_target.is_native_os) 
{
++        if (create_module.each_lib_rpath orelse false) {
+             try create_module.rpath_list.ensureUnusedCapacity(arena, 
create_module.lib_directories.items.len);
+             for (create_module.lib_directories.items) |lib_directory| {
+                 
create_module.rpath_list.appendAssumeCapacity(lib_directory.path.?);
+@@ -3976,6 +3976,28 @@ fn createModule(
+             else => {},
+         };
+ 
++        if (std.zig.system.NativePaths.isGuix(arena)) {
++            for (create_module.link_inputs.items) |link_input| {
++                if (link_input.path()) |lib| {
++                    const lib_name = lib.sub_path;
++                    if (Compilation.classifyFileExt(lib_name) == 
.shared_library) {
++                        if (fs.path.isAbsolute(lib_name)) {
++                            const lib_dir_path = fs.path.dirname(lib_name).?;
++                            try create_module.rpath_list.append(arena, 
lib_dir_path);
++                            continue;
++                        }
++                        for (create_module.lib_directories.items) |lib_dir| {
++                            const lib_dir_path = lib_dir.path.?;
++                            if (try libPathExists(arena, lib_dir_path, 
lib_name)) {
++                                try create_module.rpath_list.append(arena, 
lib_dir_path);
++                                break;
++                            }
++                        }
++                    }
++                }
++            }
++        }
++
+         create_module.resolved_options = 
Compilation.Config.resolve(create_module.opts) catch |err| switch (err) {
+             error.WasiExecModelRequiresWasi => fatal("only WASI OS targets 
support execution model", .{}),
+             error.SharedMemoryIsWasmOnly => fatal("only WebAssembly CPU 
targets support shared memory", .{}),
+@@ -7495,3 +7517,13 @@ fn addLibDirectoryWarn2(
+         .path = path,
+     });
+ }
++
++fn libPathExists(arena: Allocator, lib_dir_path: []const u8, lib_name: 
[]const u8) !bool {
++    const lib_path = try std.fmt.allocPrint(arena, "{s}{s}{s}", .{
++        lib_dir_path,
++        fs.path.sep_str,
++        lib_name,
++    });
++    fs.cwd().access(lib_path, .{}) catch return false;
++    return true;
++}
+-- 
+2.46.0
+
diff --git a/gnu/packages/patches/zig-0.14-use-baseline-cpu-by-default.patch 
b/gnu/packages/patches/zig-0.14-use-baseline-cpu-by-default.patch
new file mode 100644
index 0000000000..c4a6f64540
--- /dev/null
+++ b/gnu/packages/patches/zig-0.14-use-baseline-cpu-by-default.patch
@@ -0,0 +1,36 @@
+From cd20f7d3088d2befb80d7940b587be0197bdc07b Mon Sep 17 00:00:00 2001
+From: Ekaitz Zarraga <eka...@elenq.tech>
+Date: Sat, 18 Nov 2023 15:04:16 +0100
+Subject: [PATCH] Use `baseline` cpu by default.
+
+This helps Guix tune the package later. Tunning will only add
+`-Dcpu=whatever` which should override the standard behaviour.
+
+Zig by default uses `native`, which interferes with our build process.
+In our previous zig-build-system we chose to add `-Dcpu=baseline` flag
+in each `zig build` execution, but that doesn't allow us to tune the
+package later. Tunning is only designed to add extra flags in the
+command line call, and we already had one set for the baseline case.
+With this patch we set the standard behavior to `baseline` so we don't
+need to add the `-Dcpu=baseline` flag in the zig-build-system and we can
+tune with no issues.
+---
+ lib/std/Target/Query.zig | 2 +-
+ 1 file changed, 1 insertion(+), 1 deletion(-)
+
+diff --git a/lib/std/Target/Query.zig b/lib/std/Target/Query.zig
+index 56387c27b3..1c0f18f93d 100644
+--- a/lib/std/Target/Query.zig
++++ b/lib/std/Target/Query.zig
+@@ -6,7 +6,7 @@
+ /// `null` means native.
+ cpu_arch: ?Target.Cpu.Arch = null,
+ 
+-cpu_model: CpuModel = .determined_by_arch_os,
++cpu_model: CpuModel = .baseline,
+ 
+ /// Sparse set of CPU features to add to the set from `cpu_model`.
+ cpu_features_add: Target.Cpu.Feature.Set = .empty,
+-- 
+2.47.1
+
diff --git a/gnu/packages/patches/zig-0.14-use-system-paths.patch 
b/gnu/packages/patches/zig-0.14-use-system-paths.patch
new file mode 100644
index 0000000000..5ece0c0cdd
--- /dev/null
+++ b/gnu/packages/patches/zig-0.14-use-system-paths.patch
@@ -0,0 +1,133 @@
+From 813993ef425ba24e7a18d3cdf700c0589a333943 Mon Sep 17 00:00:00 2001
+From: Hilton Chain <hako@ultrarare.space>
+Date: Fri, 29 Nov 2024 14:13:30 +0800
+Subject: [PATCH] Use system paths.
+
+Prefer Guix search paths and support Guix cross builds.
+---
+ lib/std/zig/system.zig             | 25 +++++++++++---
+ lib/std/zig/system/NativePaths.zig | 53 ++++++++++++++++++++++++++++++
+ src/main.zig                       |  3 +-
+ 3 files changed, 76 insertions(+), 5 deletions(-)
+
+diff --git a/lib/std/zig/system.zig b/lib/std/zig/system.zig
+index 48b195dc3e..b636c46656 100644
+--- a/lib/std/zig/system.zig
++++ b/lib/std/zig/system.zig
+@@ -1176,10 +1176,27 @@ fn defaultAbiAndDynamicLinker(cpu: Target.Cpu, os: 
Target.Os, query: Target.Quer
+         .os = os,
+         .abi = abi,
+         .ofmt = query.ofmt orelse Target.ObjectFormat.default(os.tag, 
cpu.arch),
+-        .dynamic_linker = if (query.dynamic_linker.get() == null)
+-            Target.DynamicLinker.standard(cpu, os, abi)
+-        else
+-            query.dynamic_linker,
++        .dynamic_linker = if (query.dynamic_linker.get() == null) blk: {
++            var standard_linker = Target.DynamicLinker.standard(cpu, os, abi);
++            if (standard_linker.get()) |standard_linker_path| {
++                if (builtin.os.tag != .windows and builtin.os.tag != .wasi) {
++                    if (posix.getenv("CROSS_LIBRARY_PATH") orelse 
posix.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 query.dynamic_linker,
+     };
+ }
+ 
+diff --git a/lib/std/zig/system/NativePaths.zig 
b/lib/std/zig/system/NativePaths.zig
+index 3c96134556..90f31b17c1 100644
+--- a/lib/std/zig/system/NativePaths.zig
++++ b/lib/std/zig/system/NativePaths.zig
+@@ -15,6 +15,51 @@ warnings: std.ArrayListUnmanaged([]const u8) = .empty,
+ 
+ pub fn detect(arena: Allocator, native_target: std.Target) !NativePaths {
+     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.InvalidWtf8 => unreachable,
++                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.InvalidWtf8 => unreachable,
++            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.InvalidWtf8 => unreachable,
++                    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.InvalidWtf8 => unreachable,
++                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;
+@@ -208,3 +253,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/src/main.zig b/src/main.zig
+index 0d239f1f99..79493aa624 100644
+--- a/src/main.zig
++++ b/src/main.zig
+@@ -3909,7 +3909,8 @@ fn createModule(
+         }
+ 
+         // Trigger native system library path detection if necessary.
+-        if (create_module.sysroot == null and
++        if (std.zig.system.NativePaths.isCrossGuix(arena) or
++            create_module.sysroot == null and
+             resolved_target.is_native_os and resolved_target.is_native_abi and
+             create_module.want_native_include_dirs)
+         {
+-- 
+2.46.0
+
diff --git a/gnu/packages/zig.scm b/gnu/packages/zig.scm
index 476fe0e298..1d4fab2030 100644
--- a/gnu/packages/zig.scm
+++ b/gnu/packages/zig.scm
@@ -1847,4 +1847,43 @@ toolchain.  Among other features it provides
        (modify-inputs (package-native-inputs base)
          (replace "zig" `(,base "zig1")))))))
 
+(define zig-0.14-glibc-abi-tool
+  (origin
+    (method git-fetch)
+    (uri (git-reference
+          (url "https://github.com/ziglang/glibc-abi-tool";)
+          (commit "ed9d3bb356413e73b836955e75f399dfb3ec255e")))
+    (file-name "glibc-abi-tool")
+    (sha256
+     (base32 "0ckhwlszm0vkiqsyp2rzp1zcfljh1ng24c7pdvpyfj51x4s612z1"))))
+
+(define-public zig-0.14
+  (let ((commit "05d8b565ad1a1d6c0c1e93dc47f1d828043fcafc")
+        (revision "3286"))
+    (package
+      (inherit zig-0.13)
+      (name "zig")
+      (version (git-version "0.13.0" revision commit))
+      (source
+       (origin
+         (inherit (zig-source
+                   version commit
+                   "179k39851dnmy9aizy1mkdryq168qwvnrh4mi6mvms4w5drwd33b"))
+         (patches
+          (search-patches
+           "zig-0.14-use-baseline-cpu-by-default.patch"
+           "zig-0.14-use-system-paths.patch"
+           "zig-0.14-fix-runpath.patch"))))
+      (inputs
+       (modify-inputs (package-inputs zig-0.13)
+         (replace "clang" clang-19)
+         (replace "lld" lld-19)))
+      (native-inputs
+       (modify-inputs (package-native-inputs zig-0.13)
+         (replace "glibc-abi-tool" zig-0.14-glibc-abi-tool)
+         (replace "llvm" llvm-19)
+         (replace "zig" `(,zig-0.13.0-3252 "zig1"))))
+      (properties `((max-silent-time . 9600)
+                    ,@(clang-compiler-cpu-architectures "19"))))))
+
 (define-public zig zig-0.13)

Reply via email to