guix-commits
[Top][All Lists]
Advanced

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

53/73: gnu: Add zig-0.12.


From: guix-commits
Subject: 53/73: gnu: Add zig-0.12.
Date: Fri, 29 Nov 2024 06:39:43 -0500 (EST)

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

commit e79d1f36e319d35b466083a06f3b75819309c41c
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-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-glibc-abi-tool,zig-0.12): New variables.
    
    Change-Id: I700d0afa2b373bf24a4f3527548e86dbed1aff17
---
 gnu/local.mk                                       |   3 +
 gnu/packages/patches/zig-0.12-fix-runpath.patch    | 121 +++++++++++++++++++
 .../zig-0.12-use-baseline-cpu-by-default.patch     |  36 ++++++
 .../patches/zig-0.12-use-system-paths.patch        | 133 +++++++++++++++++++++
 gnu/packages/zig.scm                               |  48 ++++++++
 5 files changed, 341 insertions(+)

diff --git a/gnu/local.mk b/gnu/local.mk
index 244022af8a..8ddc6cb1dd 100644
--- a/gnu/local.mk
+++ b/gnu/local.mk
@@ -2375,6 +2375,9 @@ 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-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-fix-runpath.patch 
b/gnu/packages/patches/zig-0.12-fix-runpath.patch
new file mode 100644
index 0000000000..e945e8fd1e
--- /dev/null
+++ b/gnu/packages/patches/zig-0.12-fix-runpath.patch
@@ -0,0 +1,121 @@
+From 3cfc11396f1ceb5e28d5401c795fa9a02b898cc9 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                   | 37 +++++++++++++++++++++++++++++++++-
+ 3 files changed, 53 insertions(+), 1 deletion(-)
+
+diff --git a/lib/std/Build/Step/Compile.zig b/lib/std/Build/Step/Compile.zig
+index 5bf0805e79..836733a96e 100644
+--- a/lib/std/Build/Step/Compile.zig
++++ b/lib/std/Build/Step/Compile.zig
+@@ -743,6 +743,9 @@ fn runPkgConfig(self: *Compile, lib_name: []const u8) 
!PkgConfigResult {
+             try zig_cflags.appendSlice(&[_][]const u8{ "-D", macro });
+         } else if (mem.startsWith(u8, tok, "-D")) {
+             try zig_cflags.append(tok);
++        } else if (mem.startsWith(u8, tok, "-Wl,-rpath=")) {
++            const dir = tok["-Wl,-rpath=".len..];
++            try zig_libs.appendSlice(&[_][]const u8{ "-L", dir });
+         } else if (b.debug_pkg_config) {
+             return self.step.fail("unknown pkg-config flag '{s}'", .{tok});
+         }
+diff --git a/src/link/Elf.zig b/src/link/Elf.zig
+index 83a3d91346..6fd79fd1cd 100644
+--- a/src/link/Elf.zig
++++ b/src/link/Elf.zig
+@@ -1505,6 +1505,13 @@ fn dumpArgv(self: *Elf, comp: *Compilation) !void {
+             try argv.append(rpath);
+         }
+ 
++        if (comp.config.link_libc and link_mode == .dynamic and 
std.zig.system.NativePaths.isGuix(arena)) {
++            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 +2543,13 @@ fn linkWithLLD(self: *Elf, arena: Allocator, prog_node: 
*std.Progress.Node) !voi
+             }
+         }
+ 
++        if (comp.config.link_libc and link_mode == .dynamic and 
std.zig.system.NativePaths.isGuix(arena)) {
++            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 d27b63d5a3..5b06739b4e 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);
+         }
+ 
+@@ -3921,6 +3921,24 @@ fn createModule(
+         if (create_module.resolved_system_libs.len != 0)
+             create_module.opts.any_dyn_libs = true;
+ 
++        if (std.zig.system.NativePaths.isGuix(arena)) {
++            for (create_module.resolved_system_libs.items(.name)) |lib_name| {
++                for (create_module.lib_dirs.items) |lib_dir_path| {
++                    if (try libPathExists(arena, lib_dir_path, lib_name, 
target)) {
++                        try create_module.rpath_list.append(arena, 
lib_dir_path);
++                        break;
++                    }
++                }
++            }
++            for (create_module.link_objects.items) |obj| {
++                if (Compilation.classifyFileExt(obj.path) == .shared_library) 
{
++                    const lib_dir_path = fs.path.dirname(obj.path) orelse 
continue;
++                    if (obj.loption) continue;
++                    try create_module.rpath_list.append(arena, lib_dir_path);
++                }
++            }
++        }
++
+         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", .{}),
+@@ -7348,3 +7366,20 @@ fn handleModArg(
+     c_source_files_owner_index.* = create_module.c_source_files.items.len;
+     rc_source_files_owner_index.* = create_module.rc_source_files.items.len;
+ }
++
++fn libPathExists(
++    arena: Allocator,
++    lib_dir_path: []const u8,
++    lib_name: []const u8,
++    target: std.Target,
++) !bool {
++    const sep = fs.path.sep_str;
++    const lib_path = try std.fmt.allocPrint(arena, "{s}" ++ sep ++ 
"{s}{s}{s}", .{
++        lib_dir_path,
++        target.libPrefix(),
++        lib_name,
++        target.dynamicLibSuffix(),
++    });
++    fs.cwd().access(lib_path, .{}) catch return false;
++    return true;
++}
+-- 
+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..bcf0fd5ab1
--- /dev/null
+++ b/gnu/packages/patches/zig-0.12-use-system-paths.patch
@@ -0,0 +1,133 @@
+From ef7d97a722260e0a4fbd110625355b556c7ab8e3 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 83c798c342..138d43f2e1 100644
+--- a/lib/std/zig/system.zig
++++ b/lib/std/zig/system.zig
+@@ -1123,10 +1123,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.tag, abi)
+-        else
+-            query.dynamic_linker,
++        .dynamic_linker = if (query.dynamic_linker.get() == null) blk: {
++            var standard_linker = Target.DynamicLinker.standard(cpu, os.tag, 
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 2a50e27b0c..f4cc0e8b3a 100644
+--- a/lib/std/zig/system/NativePaths.zig
++++ b/lib/std/zig/system/NativePaths.zig
+@@ -15,6 +15,51 @@ warnings: std.ArrayListUnmanaged([]const u8) = .{},
+ 
+ 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 5b0e211647..d27b63d5a3 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 (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 4639848095..2a14062771 100644
--- a/gnu/packages/zig.scm
+++ b/gnu/packages/zig.scm
@@ -1441,4 +1441,52 @@ toolchain.  Among other features it provides
        (modify-inputs (package-native-inputs base)
          (replace "zig" `(,base "out")))))))
 
+(define zig-0.12-glibc-abi-tool
+  (origin
+    (method git-fetch)
+    (uri (git-reference
+          (url "https://github.com/ziglang/glibc-abi-tool";)
+          (commit "fc5d0a7046b76795e4219f8f168e118ec29fbc53")))
+    (file-name "zig-0.12-glibc-abi-tool-checkout")
+    (sha256
+     (base32 "1q9plbqkkk3jzrvsgcjmj5jjdncz4ym9p0snglz4kkjwwm65gqs1"))))
+
+(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-use-baseline-cpu-by-default.patch"
+         "zig-0.12-use-system-paths.patch"
+         "zig-0.12-fix-runpath.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 "glibc-abi-tool" zig-0.12-glibc-abi-tool)
+       (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]