guix-commits
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

53/60: gnu: Add zig-0.12.


From: guix-commits
Subject: 53/60: gnu: Add zig-0.12.
Date: Mon, 18 Nov 2024 07:16:55 -0500 (EST)

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

commit d43a34d25f18c5e951b318814fce10c31e4cca63
Author: Hilton Chain <hako@ultrarare.space>
AuthorDate: Wed Nov 13 23:44:33 2024 +0800

    gnu: Add zig-0.12.
    
    * gnu/packages/patches/zig-0.12-add-GUIX_ZIG_GLIBC_LINKER.patch: New file.
    * gnu/packages/patches/zig-0.12-fix-runpath.patch: New file.
    * gnu/packages/patches/zig-0.12-use-baseline-cpu-by-default.patch: New file.
    * gnu/packages/patches/zig-0.12-use-system-paths.patch: New file.
    * gnu/local.mk (dist_patch_DATA): Regisiter them.
    * gnu/packages/zig.scm (zig-0.12): New variable.
    
    Change-Id: I700d0afa2b373bf24a4f3527548e86dbed1aff17
---
 gnu/local.mk                                       |   4 +
 .../zig-0.12-add-GUIX_ZIG_GLIBC_LINKER.patch       |  38 ++++++
 gnu/packages/patches/zig-0.12-fix-runpath.patch    | 144 +++++++++++++++++++++
 .../zig-0.12-use-baseline-cpu-by-default.patch     |  36 ++++++
 .../patches/zig-0.12-use-system-paths.patch        |  92 +++++++++++++
 gnu/packages/zig.scm                               |  38 ++++++
 6 files changed, 352 insertions(+)

diff --git a/gnu/local.mk b/gnu/local.mk
index f1e1923318..5f6e40b3ec 100644
--- a/gnu/local.mk
+++ b/gnu/local.mk
@@ -2380,6 +2380,10 @@ dist_patch_DATA =                                        
        \
   %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/zig-0.12-add-GUIX_ZIG_GLIBC_LINKER.patch        \
+  %D%/packages/patches/zig-0.12-fix-runpath.patch              \
+  %D%/packages/patches/zig-0.12-use-baseline-cpu-by-default.patch      \
+  %D%/packages/patches/zig-0.12-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.12-add-GUIX_ZIG_GLIBC_LINKER.patch 
b/gnu/packages/patches/zig-0.12-add-GUIX_ZIG_GLIBC_LINKER.patch
new file mode 100644
index 0000000000..216e5e5c77
--- /dev/null
+++ b/gnu/packages/patches/zig-0.12-add-GUIX_ZIG_GLIBC_LINKER.patch
@@ -0,0 +1,38 @@
+From 4a853a1b024b1c510342a194fde779147202057e Mon Sep 17 00:00:00 2001
+From: Hilton Chain <hako@ultrarare.space>
+Date: Sun, 17 Nov 2024 10:04:53 +0800
+Subject: [PATCH 2/5] Add GUIX_ZIG_GLIBC_LINKER.
+
+---
+ lib/std/zig/system.zig | 9 ++++++++-
+ 1 file changed, 8 insertions(+), 1 deletion(-)
+
+diff --git a/lib/std/zig/system.zig b/lib/std/zig/system.zig
+index 83c798c342..1ef750847a 100644
+--- a/lib/std/zig/system.zig
++++ b/lib/std/zig/system.zig
+@@ -1118,13 +1118,20 @@ fn detectAbiAndDynamicLinker(
+ 
+ fn defaultAbiAndDynamicLinker(cpu: Target.Cpu, os: Target.Os, query: 
Target.Query) Target {
+     const abi = query.abi orelse Target.Abi.default(cpu.arch, os);
++    var standard_linker = Target.DynamicLinker.standard(cpu, os.tag, abi);
++    const getenv_available = !(builtin.os.tag == .windows or (builtin.os.tag 
== .wasi and !builtin.link_libc));
++    if (getenv_available) {
++        const guix_zig_glibc_linker =  posix.getenv("GUIX_ZIG_GLIBC_LINKER");
++        if (guix_zig_glibc_linker != null)
++            standard_linker.set(guix_zig_glibc_linker);
++    }
+     return .{
+         .cpu = cpu,
+         .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.tag, abi)
++            standard_linker
+         else
+             query.dynamic_linker,
+     };
+-- 
+2.46.0
+
diff --git a/gnu/packages/patches/zig-0.12-fix-runpath.patch 
b/gnu/packages/patches/zig-0.12-fix-runpath.patch
new file mode 100644
index 0000000000..7caf09348e
--- /dev/null
+++ b/gnu/packages/patches/zig-0.12-fix-runpath.patch
@@ -0,0 +1,144 @@
+From 1dc1d47c6ac684eda7240ab32bcfcd8360117aee Mon Sep 17 00:00:00 2001
+From: Hilton Chain <hako@ultrarare.space>
+Date: Fri, 15 Nov 2024 18:39:32 +0800
+Subject: [PATCH 3/5] Fix RUNPATH issue.
+
+Add needed libraries and libc to RUNPATH when CROSS_LIBRARY_PATH or 
LIBRARY_PATH
+is set.
+---
+ src/link/Elf.zig | 84 ++++++++++++++++++++++++++++++++++++++++++++++++
+ src/main.zig     |  2 +-
+ 2 files changed, 85 insertions(+), 1 deletion(-)
+
+diff --git a/src/link/Elf.zig b/src/link/Elf.zig
+index 83a3d91346..957aad348f 100644
+--- a/src/link/Elf.zig
++++ b/src/link/Elf.zig
+@@ -244,6 +244,13 @@ pub const HashStyle = enum { sysv, gnu, both };
+ pub const CompressDebugSections = enum { none, zlib, zstd };
+ pub const SortSection = enum { name, alignment };
+ 
++fn isGuix() bool {
++    const getenv_available = !(builtin.os.tag == .windows or (builtin.os.tag 
== .wasi and !builtin.link_libc));
++    return getenv_available and
++        std.process.hasEnvVarConstant("CROSS_LIBRARY_PATH") or
++        std.process.hasEnvVarConstant("LIBRARY_PATH");
++}
++
+ pub fn createEmpty(
+     arena: Allocator,
+     comp: *Compilation,
+@@ -1144,6 +1151,30 @@ pub fn flushModule(self: *Elf, arena: Allocator, 
prog_node: *std.Progress.Node)
+         _ = try rpath_table.put(rpath, {});
+     }
+ 
++    if (isGuix()) {
++        var test_path = std.ArrayList(u8).init(gpa);
++        defer test_path.deinit();
++        for (self.lib_dirs) |lib_dir_path| {
++            for (comp.system_libs.keys()) |link_lib| {
++                if (!(try self.accessLibPath(arena, &test_path, null, 
lib_dir_path, link_lib, .dynamic)))
++                    continue;
++                _ = try rpath_table.put(lib_dir_path, {});
++            }
++        }
++        for (comp.objects) |obj| {
++            if (Compilation.classifyFileExt(obj.path) == .shared_library) {
++                const lib_dir_path = std.fs.path.dirname(obj.path) orelse 
continue;
++                if (obj.loption) continue;
++                _ = try rpath_table.put(lib_dir_path, {});
++            }
++        }
++        if (comp.config.link_libc and link_mode == .dynamic) {
++            if (self.base.comp.libc_installation) |libc_installation| {
++                _ = try rpath_table.put(libc_installation.crt_dir.?, {});
++            }
++        }
++    }
++
+     // TSAN
+     if (comp.config.any_sanitize_thread) {
+         try positionals.append(.{ .path = 
comp.tsan_static_lib.?.full_object_path });
+@@ -1505,6 +1536,28 @@ fn dumpArgv(self: *Elf, comp: *Compilation) !void {
+             try argv.append(rpath);
+         }
+ 
++        if (isGuix()) {
++            for (self.lib_dirs) |lib_dir_path| {
++                try argv.append("-rpath");
++                try argv.append(lib_dir_path);
++            }
++            for (comp.objects) |obj| {
++                if (Compilation.classifyFileExt(obj.path) == .shared_library) 
{
++                    const lib_dir_path = std.fs.path.dirname(obj.path) orelse 
continue;
++                    if (obj.loption) continue;
++
++                    try argv.append("-rpath");
++                    try argv.append(lib_dir_path);
++                }
++            }
++            if (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.?);
++                }
++            }
++        }
++
+         try argv.appendSlice(&.{
+             "-z",
+             try std.fmt.allocPrint(arena, "stack-size={d}", 
.{self.base.stack_size}),
+@@ -2536,6 +2589,37 @@ fn linkWithLLD(self: *Elf, arena: Allocator, prog_node: 
*std.Progress.Node) !voi
+             }
+         }
+ 
++        if (isGuix()) {
++            var test_path = std.ArrayList(u8).init(arena);
++            for (self.lib_dirs) |lib_dir_path| {
++                for (comp.system_libs.keys()) |link_lib| {
++                    if (!(try self.accessLibPath(arena, &test_path, null, 
lib_dir_path, link_lib, .dynamic)))
++                        continue;
++                    if ((try rpath_table.fetchPut(lib_dir_path, {})) == null) 
{
++                        try argv.append("-rpath");
++                        try argv.append(lib_dir_path);
++                    }
++                }
++            }
++            for (comp.objects) |obj| {
++                if (Compilation.classifyFileExt(obj.path) == .shared_library) 
{
++                    const lib_dir_path = std.fs.path.dirname(obj.path) orelse 
continue;
++                    if (obj.loption) continue;
++
++                    if ((try rpath_table.fetchPut(lib_dir_path, {})) == null) 
{
++                        try argv.append("-rpath");
++                        try argv.append(lib_dir_path);
++                    }
++                }
++            }
++            if (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 5b0e211647..7568a1c3a6 100644
+--- a/src/main.zig
++++ b/src/main.zig
+@@ -3701,7 +3701,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.appendSlice(arena, 
create_module.lib_dirs.items);
+         }
+ 
+-- 
+2.46.0
+
diff --git a/gnu/packages/patches/zig-0.12-use-baseline-cpu-by-default.patch 
b/gnu/packages/patches/zig-0.12-use-baseline-cpu-by-default.patch
new file mode 100644
index 0000000000..a219fa7c4b
--- /dev/null
+++ b/gnu/packages/patches/zig-0.12-use-baseline-cpu-by-default.patch
@@ -0,0 +1,36 @@
+From 3e9875c721f6e5b48b8c63560f049dd1bc398293 Mon Sep 17 00:00:00 2001
+From: Ekaitz Zarraga <ekaitz@elenq.tech>
+Date: Sat, 18 Nov 2023 15:04:16 +0100
+Subject: [PATCH 4/5] 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 8871e360a5..797d6a74db 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 = CpuModel.determined_by_cpu_arch,
++cpu_model: CpuModel = CpuModel.baseline,
+ 
+ /// Sparse set of CPU features to add to the set from `cpu_model`.
+ cpu_features_add: Target.Cpu.Feature.Set = Target.Cpu.Feature.Set.empty,
+-- 
+2.46.0
+
diff --git a/gnu/packages/patches/zig-0.12-use-system-paths.patch 
b/gnu/packages/patches/zig-0.12-use-system-paths.patch
new file mode 100644
index 0000000000..8c5374359c
--- /dev/null
+++ b/gnu/packages/patches/zig-0.12-use-system-paths.patch
@@ -0,0 +1,92 @@
+From 4b5df095d7a1c2bc492bad3cf0b79e8c204b8a7d Mon Sep 17 00:00:00 2001
+From: Hilton Chain <hako@ultrarare.space>
+Date: Sun, 17 Nov 2024 10:04:49 +0800
+Subject: [PATCH 5/5] Use system paths.
+
+Prefer Guix search paths and support guix cross builds.
+---
+ lib/std/zig/system/NativePaths.zig | 53 ++++++++++++++++++++++++++++++
+ src/main.zig                       |  3 +-
+ 2 files changed, 55 insertions(+), 1 deletion(-)
+
+diff --git a/lib/std/zig/system/NativePaths.zig 
b/lib/std/zig/system/NativePaths.zig
+index 2a50e27b0c..04d08ec658 100644
+--- a/lib/std/zig/system/NativePaths.zig
++++ b/lib/std/zig/system/NativePaths.zig
+@@ -15,6 +15,59 @@ warnings: std.ArrayListUnmanaged([]const u8) = .{},
+ 
+ pub fn detect(arena: Allocator, native_target: std.Target) !NativePaths {
+     var self: NativePaths = .{ .arena = arena };
++    const getenv_available = !(builtin.os.tag == .windows or (builtin.os.tag 
== .wasi and !builtin.link_libc));
++    var is_guix = false;
++    var is_cross_guix = false;
++    if (getenv_available) {
++        if (std.posix.getenv("CROSS_C_INCLUDE_PATH")) |c_include_path| {
++            is_cross_guix = true;
++            var it = mem.tokenizeScalar(u8, c_include_path, ':');
++            while (it.next()) |dir| {
++                try self.addIncludeDir(dir);
++            }
++        }
++        if (std.posix.getenv("CROSS_CPLUS_INCLUDE_PATH")) 
|cplus_include_path| {
++            is_cross_guix = true;
++            var it = mem.tokenizeScalar(u8, cplus_include_path, ':');
++            while (it.next()) |dir| {
++                try self.addIncludeDir(dir);
++            }
++        }
++        if (std.posix.getenv("CROSS_LIBRARY_PATH")) |library_path| {
++            is_cross_guix = true;
++            var it = mem.tokenizeScalar(u8, library_path, ':');
++            while (it.next()) |dir| {
++                try self.addLibDir(dir);
++            }
++        }
++        if (!is_cross_guix) {
++            if (std.posix.getenv("C_INCLUDE_PATH")) |c_include_path| {
++                is_guix = true;
++                var it = mem.tokenizeScalar(u8, c_include_path, ':');
++                while (it.next()) |dir| {
++                    try self.addIncludeDir(dir);
++                }
++            }
++            if (std.posix.getenv("CPLUS_INCLUDE_PATH")) |cplus_include_path| {
++                is_guix = true;
++                var it = mem.tokenizeScalar(u8, cplus_include_path, ':');
++                while (it.next()) |dir| {
++                    try self.addIncludeDir(dir);
++                }
++            }
++            if (std.posix.getenv("LIBRARY_PATH")) |library_path| {
++                is_guix = true;
++                var it = mem.tokenizeScalar(u8, library_path, ':');
++                while (it.next()) |dir| {
++                    try self.addLibDir(dir);
++                }
++            }
++        }
++    }
++    if (is_guix or is_cross_guix) {
++        return self;
++    }
++
+     var is_nix = false;
+     if (process.getEnvVarOwned(arena, "NIX_CFLAGS_COMPILE")) 
|nix_cflags_compile| {
+         is_nix = true;
+diff --git a/src/main.zig b/src/main.zig
+index 7568a1c3a6..9156f909e3 100644
+--- a/src/main.zig
++++ b/src/main.zig
+@@ -3706,7 +3706,8 @@ fn createModule(
+         }
+ 
+         // Trigger native system library path detection if necessary.
+-        if (create_module.sysroot == null and
++        if (process.hasEnvVar(arena, "CROSS_LIBRARY_PATH") catch false 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 fc3cea98de..eb7e8bab99 100644
--- a/gnu/packages/zig.scm
+++ b/gnu/packages/zig.scm
@@ -1370,4 +1370,42 @@ toolchain.  Among other features it provides
        (modify-inputs (package-native-inputs base)
          (replace "zig" `(,base "out")))))))
 
+(define-public zig-0.12
+  (package
+    (inherit zig-0.11)
+    (name "zig")
+    (version "0.12.1")
+    (source
+     (origin
+       (inherit (zig-source
+                 version version
+                 "0ssgfrsk116p16rwjwq1z2pvvcdij6s30s19bhzjms7maz4s77hb"))
+       (patches
+        (search-patches
+         "zig-0.12-add-GUIX_ZIG_GLIBC_LINKER.patch"
+         "zig-0.12-fix-runpath.patch"
+         "zig-0.12-use-baseline-cpu-by-default.patch"
+         "zig-0.12-use-system-paths.patch"))))
+    (arguments
+     (substitute-keyword-arguments (package-arguments zig-0.11)
+       ((#:phases phases '%standard-phases)
+        #~(modify-phases #$phases
+            (replace 'patch-more-shebangs
+              (lambda* (#:key inputs #:allow-other-keys)
+                ;; Zig uses information about an ELF file to determine the
+                ;; version of glibc and other data for native builds.
+                (substitute* "lib/std/zig/system.zig"
+                  (("/usr/bin/env")
+                   (search-input-file inputs "bin/clang++")))))))))
+    (inputs
+     (modify-inputs (package-inputs zig-0.11)
+       (replace "clang" clang-17)
+       (replace "lld" lld-17)))
+    (native-inputs
+     (modify-inputs (package-native-inputs zig-0.11)
+       (replace "llvm" llvm-17)
+       (replace "zig" `(,zig-0.11.0-3604 "zig1"))))
+    (properties `((max-silent-time . 9600)
+                  ,@(clang-compiler-cpu-architectures "17")))))
+
 (define-public zig zig-0.10)



reply via email to

[Prev in Thread] Current Thread [Next in Thread]