From 048a15057a5c652e553ed55cb1ebb8a744d6e1e0 Mon Sep 17 00:00:00 2001 From: Anderson Torres Date: Sun, 26 May 2024 20:09:12 -0300 Subject: zig-shell-completions: migrate to by-name --- .../compilers/zig/shell-completions.nix | 42 ---------------------- 1 file changed, 42 deletions(-) delete mode 100644 pkgs/development/compilers/zig/shell-completions.nix (limited to 'pkgs/development/compilers') diff --git a/pkgs/development/compilers/zig/shell-completions.nix b/pkgs/development/compilers/zig/shell-completions.nix deleted file mode 100644 index 44773cf9a553e..0000000000000 --- a/pkgs/development/compilers/zig/shell-completions.nix +++ /dev/null @@ -1,42 +0,0 @@ -{ lib -, stdenv -, fetchFromGitHub -, installShellFiles -, unstableGitUpdater -}: - -stdenv.mkDerivation (finalAttrs: { - pname = "zig-shell-completions"; - version = "0-unstable-2023-11-18"; - - src = fetchFromGitHub { - owner = "ziglang"; - repo = "shell-completions"; - rev = "31d3ad12890371bf467ef7143f5c2f31cfa7b7c1"; - hash = "sha256-ID/K0vdg7BTKGgozISk/X4RBxCVfhSkVD6GSZUoP9Ls="; - }; - - nativeBuildInputs = [ installShellFiles ]; - - dontConfigure = true; - dontBuild = true; - - installPhase = '' - runHook preInstall - - installShellCompletion --bash --name zig.bash _zig.bash - installShellCompletion --zsh --name _zig _zig - - runHook postInstall - ''; - - passthru.updateScript = unstableGitUpdater { }; - - meta = { - homepage = "https://github.com/ziglang/shell-completions"; - description = "Shell completions for the Zig compiler"; - license = lib.licenses.mit; - maintainers = with lib.maintainers; [ aaronjheng ]; - platforms = lib.platforms.all; - }; -}) -- cgit 1.4.1 From cad194ac57ba4ace6dcbae7f3ef171cbf872d4ad Mon Sep 17 00:00:00 2001 From: Anderson Torres Date: Sun, 26 May 2024 20:42:11 -0300 Subject: zig_0_9: huge refactor - migrate it to a dedicated directory, zig/0.9 - can't migrate to by-name - because of undecipherable darwin.apple_sdk_11_0.callPackage - nixfmt - add a simple version test --- pkgs/development/compilers/zig/0.9.nix | 47 --------- .../0.9/000-0.9-read-dynstr-at-rpath-offset.patch | 39 ++++++++ .../0.9/001-0.9-bump-macos-supported-version.patch | 50 ++++++++++ pkgs/development/compilers/zig/0.9/default.nix | 111 +++++++++++++++++++++ pkgs/development/compilers/zig/0.9/hook.nix | 47 +++++++++ pkgs/development/compilers/zig/0.9/setup-hook.sh | 70 +++++++++++++ .../zig/000-0.9-read-dynstr-at-rpath-offset.patch | 39 -------- .../zig/001-0.9-bump-macos-supported-version.patch | 50 ---------- pkgs/top-level/all-packages.nix | 2 +- 9 files changed, 318 insertions(+), 137 deletions(-) delete mode 100644 pkgs/development/compilers/zig/0.9.nix create mode 100644 pkgs/development/compilers/zig/0.9/000-0.9-read-dynstr-at-rpath-offset.patch create mode 100644 pkgs/development/compilers/zig/0.9/001-0.9-bump-macos-supported-version.patch create mode 100644 pkgs/development/compilers/zig/0.9/default.nix create mode 100644 pkgs/development/compilers/zig/0.9/hook.nix create mode 100644 pkgs/development/compilers/zig/0.9/setup-hook.sh delete mode 100644 pkgs/development/compilers/zig/000-0.9-read-dynstr-at-rpath-offset.patch delete mode 100644 pkgs/development/compilers/zig/001-0.9-bump-macos-supported-version.patch (limited to 'pkgs/development/compilers') diff --git a/pkgs/development/compilers/zig/0.9.nix b/pkgs/development/compilers/zig/0.9.nix deleted file mode 100644 index d648c9c2eb642..0000000000000 --- a/pkgs/development/compilers/zig/0.9.nix +++ /dev/null @@ -1,47 +0,0 @@ -{ lib -, stdenv -, fetchFromGitHub -, cmake -, llvmPackages -, libxml2 -, zlib -, coreutils -, callPackage -}@args: - -import ./generic.nix args { - version = "0.9.1"; - - hash = "sha256-x2c4c9RSrNWGqEngio4ArW7dJjW0gg+8nqBwPcR721k="; - - patches = [ - # Fix index out of bounds reading RPATH (cherry-picked from 0.10-dev) - ./000-0.9-read-dynstr-at-rpath-offset.patch - # Fix build on macOS 13 (cherry-picked from 0.10-dev) - ./001-0.9-bump-macos-supported-version.patch - ]; - - prePatch = - let - zig_0_10_0 = fetchFromGitHub { - owner = "ziglang"; - repo = "zig"; - rev = "0.10.0"; - hash = "sha256-DNs937N7PLQimuM2anya4npYXcj6cyH+dRS7AiOX7tw="; - }; - in - '' - cp -R ${zig_0_10_0}/lib/libc/include/any-macos.13-any lib/libc/include/any-macos.13-any - cp -R ${zig_0_10_0}/lib/libc/include/aarch64-macos.13-none lib/libc/include/aarch64-macos.13-gnu - cp -R ${zig_0_10_0}/lib/libc/include/x86_64-macos.13-none lib/libc/include/x86_64-macos.13-gnu - cp ${zig_0_10_0}/lib/libc/darwin/libSystem.13.tbd lib/libc/darwin/ - ''; - - cmakeFlags = [ - # file RPATH_CHANGE could not write new RPATH - "-DCMAKE_SKIP_BUILD_RPATH=ON" - - # ensure determinism in the compiler build - "-DZIG_TARGET_MCPU=baseline" - ]; -} diff --git a/pkgs/development/compilers/zig/0.9/000-0.9-read-dynstr-at-rpath-offset.patch b/pkgs/development/compilers/zig/0.9/000-0.9-read-dynstr-at-rpath-offset.patch new file mode 100644 index 0000000000000..70633193f944d --- /dev/null +++ b/pkgs/development/compilers/zig/0.9/000-0.9-read-dynstr-at-rpath-offset.patch @@ -0,0 +1,39 @@ +commit ebcdbd9b3c9d437780aee4d6af76bbd2ab32ea06 +Author: LeRoyce Pearson +Date: 2022-07-17 16:01:22 -0600 + + Read dynstr starting at rpath offset + + Since we know the offset, we may as well read starting there. Still expects + rpath to fit in 4096 bytes; that might be worth fixing in the future. + + Fixes issue #12112 + +diff --git a/lib/std/zig/system/NativeTargetInfo.zig b/lib/std/zig/system/NativeTargetInfo.zig +index af41fc790579..ad0b6d5ce1e1 100644 +--- a/lib/std/zig/system/NativeTargetInfo.zig ++++ b/lib/std/zig/system/NativeTargetInfo.zig +@@ -652,14 +652,19 @@ pub fn abiAndDynamicLinkerFromFile( + } else null; + + if (dynstr) |ds| { +- const strtab_len = std.math.min(ds.size, strtab_buf.len); +- const strtab_read_len = try preadMin(file, &strtab_buf, ds.offset, strtab_len); +- const strtab = strtab_buf[0..strtab_read_len]; + // TODO this pointer cast should not be necessary + const rpoff_usize = std.math.cast(usize, rpoff) catch |err| switch (err) { + error.Overflow => return error.InvalidElfFile, + }; +- const rpath_list = mem.sliceTo(std.meta.assumeSentinel(strtab[rpoff_usize..].ptr, 0), 0); ++ if (rpoff_usize > ds.size) return error.InvalidElfFile; ++ const rpoff_file = ds.offset + rpoff_usize; ++ const rp_max_size = ds.size - rpoff_usize; ++ ++ const strtab_len = std.math.min(rp_max_size, strtab_buf.len); ++ const strtab_read_len = try preadMin(file, &strtab_buf, rpoff_file, strtab_len); ++ const strtab = strtab_buf[0..strtab_read_len]; ++ ++ const rpath_list = mem.sliceTo(std.meta.assumeSentinel(strtab.ptr, 0), 0); + var it = mem.tokenize(u8, rpath_list, ":"); + while (it.next()) |rpath| { + var dir = fs.cwd().openDir(rpath, .{}) catch |err| switch (err) { diff --git a/pkgs/development/compilers/zig/0.9/001-0.9-bump-macos-supported-version.patch b/pkgs/development/compilers/zig/0.9/001-0.9-bump-macos-supported-version.patch new file mode 100644 index 0000000000000..07b90e223553b --- /dev/null +++ b/pkgs/development/compilers/zig/0.9/001-0.9-bump-macos-supported-version.patch @@ -0,0 +1,50 @@ +From 98285b17b3887de37b630da66f09a44f42ddbe01 Mon Sep 17 00:00:00 2001 +From: Jakub Konka +Date: Tue, 25 Oct 2022 11:46:42 +0200 +Subject: [PATCH] darwin: bump max macOS version to 13.0 + +--- + lib/std/target.zig | 4 ++-- + src/target.zig | 2 ++ + 2 files changed, 4 insertions(+), 2 deletions(-) + +diff --git a/lib/std/target.zig b/lib/std/target.zig +index d791e3b0350..7fbad5baa3c 100644 +--- a/lib/std/target.zig ++++ b/lib/std/target.zig +@@ -277,13 +277,13 @@ pub const Target = struct { + .aarch64 => VersionRange{ + .semver = .{ + .min = .{ .major = 11, .minor = 6 }, +- .max = .{ .major = 12, .minor = 0 }, ++ .max = .{ .major = 13, .minor = 0 }, + }, + }, + .x86_64 => VersionRange{ + .semver = .{ + .min = .{ .major = 10, .minor = 13 }, +- .max = .{ .major = 12, .minor = 0 }, ++ .max = .{ .major = 13, .minor = 0 }, + }, + }, + else => unreachable, +diff --git a/src/target.zig b/src/target.zig +index 9e2d26dac65..fc585912c45 100644 +--- a/src/target.zig ++++ b/src/target.zig +@@ -18,6 +18,7 @@ pub const available_libcs = [_]ArchOsAbi{ + .{ .arch = .aarch64, .os = .windows, .abi = .gnu }, + .{ .arch = .aarch64, .os = .macos, .abi = .gnu, .os_ver = .{ .major = 11, .minor = 0 } }, + .{ .arch = .aarch64, .os = .macos, .abi = .gnu, .os_ver = .{ .major = 12, .minor = 0 } }, ++ .{ .arch = .aarch64, .os = .macos, .abi = .gnu, .os_ver = .{ .major = 13, .minor = 0 } }, + .{ .arch = .armeb, .os = .linux, .abi = .gnueabi }, + .{ .arch = .armeb, .os = .linux, .abi = .gnueabihf }, + .{ .arch = .armeb, .os = .linux, .abi = .musleabi }, +@@ -73,6 +74,7 @@ pub const available_libcs = [_]ArchOsAbi{ + .{ .arch = .x86_64, .os = .macos, .abi = .gnu, .os_ver = .{ .major = 10, .minor = 0 } }, + .{ .arch = .x86_64, .os = .macos, .abi = .gnu, .os_ver = .{ .major = 11, .minor = 0 } }, + .{ .arch = .x86_64, .os = .macos, .abi = .gnu, .os_ver = .{ .major = 12, .minor = 0 } }, ++ .{ .arch = .x86_64, .os = .macos, .abi = .gnu, .os_ver = .{ .major = 13, .minor = 0 } }, + }; + + pub fn libCGenericName(target: std.Target) [:0]const u8 { diff --git a/pkgs/development/compilers/zig/0.9/default.nix b/pkgs/development/compilers/zig/0.9/default.nix new file mode 100644 index 0000000000000..079773f00e88c --- /dev/null +++ b/pkgs/development/compilers/zig/0.9/default.nix @@ -0,0 +1,111 @@ +{ + lib, + callPackage, + cmake, + coreutils, + fetchFromGitHub, + libxml2, + llvmPackages, + stdenv, + testers, + zlib, +}: + +stdenv.mkDerivation (finalAttrs: { + pname = "zig"; + version = "0.9.1"; + + src = fetchFromGitHub { + owner = "ziglang"; + repo = "zig"; + rev = finalAttrs.version; + hash = "sha256-x2c4c9RSrNWGqEngio4ArW7dJjW0gg+8nqBwPcR721k="; + }; + + patches = [ + # Fix index out of bounds reading RPATH (cherry-picked from 0.10-dev) + ./000-0.9-read-dynstr-at-rpath-offset.patch + # Fix build on macOS 13 (cherry-picked from 0.10-dev) + ./001-0.9-bump-macos-supported-version.patch + ]; + + nativeBuildInputs = [ + cmake + (lib.getDev llvmPackages.llvm) + ]; + + buildInputs = + [ + libxml2 + zlib + ] + ++ (with llvmPackages; [ + libclang + lld + llvm + ]); + + cmakeFlags = [ + # file RPATH_CHANGE could not write new RPATH + (lib.cmakeBool "CMAKE_SKIP_BUILD_RPATH" true) + # ensure determinism in the compiler build + (lib.cmakeFeature "ZIG_TARGET_MCPU" "baseline") + ]; + + env.ZIG_GLOBAL_CACHE_DIR = "$TMPDIR/zig-cache"; + + doInstallCheck = true; + + strictDeps = true; + + prePatch = + let + zig_0_10_0 = fetchFromGitHub { + owner = "ziglang"; + repo = "zig"; + rev = "0.10.0"; + hash = "sha256-DNs937N7PLQimuM2anya4npYXcj6cyH+dRS7AiOX7tw="; + }; + in + '' + cp -R ${zig_0_10_0}/lib/libc/include/any-macos.13-any lib/libc/include/any-macos.13-any + cp -R ${zig_0_10_0}/lib/libc/include/aarch64-macos.13-none lib/libc/include/aarch64-macos.13-gnu + cp -R ${zig_0_10_0}/lib/libc/include/x86_64-macos.13-none lib/libc/include/x86_64-macos.13-gnu + cp ${zig_0_10_0}/lib/libc/darwin/libSystem.13.tbd lib/libc/darwin/ + ''; + + # Zig's build looks at /usr/bin/env to find dynamic linking info. This doesn't + # work in Nix's sandbox. Use env from our coreutils instead. + postPatch = '' + substituteInPlace lib/std/zig/system/NativeTargetInfo.zig \ + --replace "/usr/bin/env" "${lib.getExe' coreutils "env"}" + ''; + + installCheckPhase = '' + runHook preInstallCheck + + $out/bin/zig test --cache-dir "$TMPDIR/zig-test-cache" -I $src/test $src/test/behavior.zig + + runHook postInstallCheck + ''; + + passthru = { + hook = callPackage ./hook.nix { zig = finalAttrs.finalPackage; }; + tests = { + version = testers.testVersion { + package = finalAttrs.finalPackage; + command = "zig version"; + }; + }; + }; + + meta = { + description = "General-purpose programming language and toolchain for maintaining robust, optimal, and reusable software"; + homepage = "https://ziglang.org/"; + changelog = "https://ziglang.org/download/${finalAttrs.version}/release-notes.html"; + license = lib.licenses.mit; + mainProgram = "zig"; + maintainers = with lib.maintainers; [ andrewrk ] ++ lib.teams.zig.members; + platforms = lib.platforms.unix; + }; +}) diff --git a/pkgs/development/compilers/zig/0.9/hook.nix b/pkgs/development/compilers/zig/0.9/hook.nix new file mode 100644 index 0000000000000..298d50d9e5f9d --- /dev/null +++ b/pkgs/development/compilers/zig/0.9/hook.nix @@ -0,0 +1,47 @@ +{ + lib, + makeSetupHook, + zig, +}: + +makeSetupHook { + name = "zig-hook"; + + propagatedBuildInputs = [ zig ]; + + substitutions = { + # This zig_default_flags below is meant to avoid CPU feature impurity in + # Nixpkgs. However, this flagset is "unstable": it is specifically meant to + # be controlled by the upstream development team - being up to that team + # exposing or not that flags to the outside (especially the package manager + # teams). + + # Because of this hurdle, @andrewrk from Zig Software Foundation proposed + # some solutions for this issue. Hopefully they will be implemented in + # future releases of Zig. When this happens, this flagset should be + # revisited accordingly. + + # Below are some useful links describing the discovery process of this 'bug' + # in Nixpkgs: + + # https://github.com/NixOS/nixpkgs/issues/169461 + # https://github.com/NixOS/nixpkgs/issues/185644 + # https://github.com/NixOS/nixpkgs/pull/197046 + # https://github.com/NixOS/nixpkgs/pull/241741#issuecomment-1624227485 + # https://github.com/ziglang/zig/issues/14281#issuecomment-1624220653 + + zig_default_flags = [ + "-Dcpu=baseline" + "-Drelease-safe=true" + ]; + }; + + passthru = { + inherit zig; + }; + + meta = { + description = "A setup hook for using the Zig compiler in Nixpkgs"; + inherit (zig.meta) maintainers platforms broken; + }; +} ./setup-hook.sh diff --git a/pkgs/development/compilers/zig/0.9/setup-hook.sh b/pkgs/development/compilers/zig/0.9/setup-hook.sh new file mode 100644 index 0000000000000..689ebec8a3077 --- /dev/null +++ b/pkgs/development/compilers/zig/0.9/setup-hook.sh @@ -0,0 +1,70 @@ +# shellcheck shell=bash disable=SC2154,SC2086 + +readonly zigDefaultFlagsArray=(@zig_default_flags@) + +function zigSetGlobalCacheDir { + ZIG_GLOBAL_CACHE_DIR=$(mktemp -d) + export ZIG_GLOBAL_CACHE_DIR +} + +function zigBuildPhase { + runHook preBuild + + local flagsArray=( + "${zigDefaultFlagsArray[@]}" + $zigBuildFlags "${zigBuildFlagsArray[@]}" + ) + + echoCmd 'zig build flags' "${flagsArray[@]}" + zig build "${flagsArray[@]}" + + runHook postBuild +} + +function zigCheckPhase { + runHook preCheck + + local flagsArray=( + "${zigDefaultFlagsArray[@]}" + $zigCheckFlags "${zigCheckFlagsArray[@]}" + ) + + echoCmd 'zig check flags' "${flagsArray[@]}" + zig build test "${flagsArray[@]}" + + runHook postCheck +} + +function zigInstallPhase { + runHook preInstall + + local flagsArray=( + "${zigDefaultFlagsArray[@]}" + $zigBuildFlags "${zigBuildFlagsArray[@]}" + $zigInstallFlags "${zigInstallFlagsArray[@]}" + ) + + if [ -z "${dontAddPrefix-}" ]; then + # Zig does not recognize `--prefix=/dir/`, only `--prefix /dir/` + flagsArray+=("${prefixKey:---prefix}" "$prefix") + fi + + echoCmd 'zig install flags' "${flagsArray[@]}" + zig build install "${flagsArray[@]}" + + runHook postInstall +} + +addEnvHooks "$targetOffset" zigSetGlobalCacheDir + +if [ -z "${dontUseZigBuild-}" ] && [ -z "${buildPhase-}" ]; then + buildPhase=zigBuildPhase +fi + +if [ -z "${dontUseZigCheck-}" ] && [ -z "${checkPhase-}" ]; then + checkPhase=zigCheckPhase +fi + +if [ -z "${dontUseZigInstall-}" ] && [ -z "${installPhase-}" ]; then + installPhase=zigInstallPhase +fi diff --git a/pkgs/development/compilers/zig/000-0.9-read-dynstr-at-rpath-offset.patch b/pkgs/development/compilers/zig/000-0.9-read-dynstr-at-rpath-offset.patch deleted file mode 100644 index 70633193f944d..0000000000000 --- a/pkgs/development/compilers/zig/000-0.9-read-dynstr-at-rpath-offset.patch +++ /dev/null @@ -1,39 +0,0 @@ -commit ebcdbd9b3c9d437780aee4d6af76bbd2ab32ea06 -Author: LeRoyce Pearson -Date: 2022-07-17 16:01:22 -0600 - - Read dynstr starting at rpath offset - - Since we know the offset, we may as well read starting there. Still expects - rpath to fit in 4096 bytes; that might be worth fixing in the future. - - Fixes issue #12112 - -diff --git a/lib/std/zig/system/NativeTargetInfo.zig b/lib/std/zig/system/NativeTargetInfo.zig -index af41fc790579..ad0b6d5ce1e1 100644 ---- a/lib/std/zig/system/NativeTargetInfo.zig -+++ b/lib/std/zig/system/NativeTargetInfo.zig -@@ -652,14 +652,19 @@ pub fn abiAndDynamicLinkerFromFile( - } else null; - - if (dynstr) |ds| { -- const strtab_len = std.math.min(ds.size, strtab_buf.len); -- const strtab_read_len = try preadMin(file, &strtab_buf, ds.offset, strtab_len); -- const strtab = strtab_buf[0..strtab_read_len]; - // TODO this pointer cast should not be necessary - const rpoff_usize = std.math.cast(usize, rpoff) catch |err| switch (err) { - error.Overflow => return error.InvalidElfFile, - }; -- const rpath_list = mem.sliceTo(std.meta.assumeSentinel(strtab[rpoff_usize..].ptr, 0), 0); -+ if (rpoff_usize > ds.size) return error.InvalidElfFile; -+ const rpoff_file = ds.offset + rpoff_usize; -+ const rp_max_size = ds.size - rpoff_usize; -+ -+ const strtab_len = std.math.min(rp_max_size, strtab_buf.len); -+ const strtab_read_len = try preadMin(file, &strtab_buf, rpoff_file, strtab_len); -+ const strtab = strtab_buf[0..strtab_read_len]; -+ -+ const rpath_list = mem.sliceTo(std.meta.assumeSentinel(strtab.ptr, 0), 0); - var it = mem.tokenize(u8, rpath_list, ":"); - while (it.next()) |rpath| { - var dir = fs.cwd().openDir(rpath, .{}) catch |err| switch (err) { diff --git a/pkgs/development/compilers/zig/001-0.9-bump-macos-supported-version.patch b/pkgs/development/compilers/zig/001-0.9-bump-macos-supported-version.patch deleted file mode 100644 index 07b90e223553b..0000000000000 --- a/pkgs/development/compilers/zig/001-0.9-bump-macos-supported-version.patch +++ /dev/null @@ -1,50 +0,0 @@ -From 98285b17b3887de37b630da66f09a44f42ddbe01 Mon Sep 17 00:00:00 2001 -From: Jakub Konka -Date: Tue, 25 Oct 2022 11:46:42 +0200 -Subject: [PATCH] darwin: bump max macOS version to 13.0 - ---- - lib/std/target.zig | 4 ++-- - src/target.zig | 2 ++ - 2 files changed, 4 insertions(+), 2 deletions(-) - -diff --git a/lib/std/target.zig b/lib/std/target.zig -index d791e3b0350..7fbad5baa3c 100644 ---- a/lib/std/target.zig -+++ b/lib/std/target.zig -@@ -277,13 +277,13 @@ pub const Target = struct { - .aarch64 => VersionRange{ - .semver = .{ - .min = .{ .major = 11, .minor = 6 }, -- .max = .{ .major = 12, .minor = 0 }, -+ .max = .{ .major = 13, .minor = 0 }, - }, - }, - .x86_64 => VersionRange{ - .semver = .{ - .min = .{ .major = 10, .minor = 13 }, -- .max = .{ .major = 12, .minor = 0 }, -+ .max = .{ .major = 13, .minor = 0 }, - }, - }, - else => unreachable, -diff --git a/src/target.zig b/src/target.zig -index 9e2d26dac65..fc585912c45 100644 ---- a/src/target.zig -+++ b/src/target.zig -@@ -18,6 +18,7 @@ pub const available_libcs = [_]ArchOsAbi{ - .{ .arch = .aarch64, .os = .windows, .abi = .gnu }, - .{ .arch = .aarch64, .os = .macos, .abi = .gnu, .os_ver = .{ .major = 11, .minor = 0 } }, - .{ .arch = .aarch64, .os = .macos, .abi = .gnu, .os_ver = .{ .major = 12, .minor = 0 } }, -+ .{ .arch = .aarch64, .os = .macos, .abi = .gnu, .os_ver = .{ .major = 13, .minor = 0 } }, - .{ .arch = .armeb, .os = .linux, .abi = .gnueabi }, - .{ .arch = .armeb, .os = .linux, .abi = .gnueabihf }, - .{ .arch = .armeb, .os = .linux, .abi = .musleabi }, -@@ -73,6 +74,7 @@ pub const available_libcs = [_]ArchOsAbi{ - .{ .arch = .x86_64, .os = .macos, .abi = .gnu, .os_ver = .{ .major = 10, .minor = 0 } }, - .{ .arch = .x86_64, .os = .macos, .abi = .gnu, .os_ver = .{ .major = 11, .minor = 0 } }, - .{ .arch = .x86_64, .os = .macos, .abi = .gnu, .os_ver = .{ .major = 12, .minor = 0 } }, -+ .{ .arch = .x86_64, .os = .macos, .abi = .gnu, .os_ver = .{ .major = 13, .minor = 0 } }, - }; - - pub fn libCGenericName(target: std.Target) [:0]const u8 { diff --git a/pkgs/top-level/all-packages.nix b/pkgs/top-level/all-packages.nix index 7f00b48a32b09..06d640bcf704d 100644 --- a/pkgs/top-level/all-packages.nix +++ b/pkgs/top-level/all-packages.nix @@ -25054,7 +25054,7 @@ with pkgs; libzra = callPackage ../development/libraries/libzra { }; # requires a newer Apple SDK - zig_0_9 = darwin.apple_sdk_11_0.callPackage ../development/compilers/zig/0.9.nix { + zig_0_9 = darwin.apple_sdk_11_0.callPackage ../development/compilers/zig/0.9 { llvmPackages = llvmPackages_13; }; # requires a newer Apple SDK -- cgit 1.4.1 From 5427d85cc7c158c3d64d64cadfa5e5afc96e2323 Mon Sep 17 00:00:00 2001 From: Anderson Torres Date: Sun, 26 May 2024 22:55:55 -0300 Subject: zig_0_10: huge refactor - migrate it to a dedicated directory, zig/0.10 - can't migrate to by-name - because of undecipherable darwin.apple_sdk_11_0.callPackage - nixfmt - add a simple version test --- pkgs/development/compilers/zig/0.10.nix | 43 --- .../compilers/zig/0.10/001-0.10-macho-fixes.patch | 367 +++++++++++++++++++++ pkgs/development/compilers/zig/0.10/default.nix | 109 ++++++ pkgs/development/compilers/zig/0.10/hook.nix | 47 +++ pkgs/development/compilers/zig/0.10/setup-hook.sh | 70 ++++ .../compilers/zig/002-0.10-macho-fixes.patch | 367 --------------------- pkgs/top-level/all-packages.nix | 2 +- 7 files changed, 594 insertions(+), 411 deletions(-) delete mode 100644 pkgs/development/compilers/zig/0.10.nix create mode 100644 pkgs/development/compilers/zig/0.10/001-0.10-macho-fixes.patch create mode 100644 pkgs/development/compilers/zig/0.10/default.nix create mode 100644 pkgs/development/compilers/zig/0.10/hook.nix create mode 100644 pkgs/development/compilers/zig/0.10/setup-hook.sh delete mode 100644 pkgs/development/compilers/zig/002-0.10-macho-fixes.patch (limited to 'pkgs/development/compilers') diff --git a/pkgs/development/compilers/zig/0.10.nix b/pkgs/development/compilers/zig/0.10.nix deleted file mode 100644 index d02b9ff1c976b..0000000000000 --- a/pkgs/development/compilers/zig/0.10.nix +++ /dev/null @@ -1,43 +0,0 @@ -{ lib -, stdenv -, fetchFromGitHub -, cmake -, llvmPackages -, libxml2 -, zlib -, coreutils -, callPackage -}@args: - -import ./generic.nix args { - version = "0.10.1"; - - hash = "sha256-69QIkkKzApOGfrBdgtmxFMDytRkSh+0YiaJQPbXsBeo="; - - outputs = [ "out" "doc" ]; - - patches = [ - # Backport alignment related panics from zig-master to 0.10. - # Upstream issue: https://github.com/ziglang/zig/issues/14559 - ./002-0.10-macho-fixes.patch - ]; - - cmakeFlags = [ - # file RPATH_CHANGE could not write new RPATH - "-DCMAKE_SKIP_BUILD_RPATH=ON" - - # always link against static build of LLVM - "-DZIG_STATIC_LLVM=ON" - - # ensure determinism in the compiler build - "-DZIG_TARGET_MCPU=baseline" - ]; - - postBuild = '' - ./zig2 run ../doc/docgen.zig -- ./zig2 ../doc/langref.html.in langref.html - ''; - - postInstall = '' - install -Dm644 -t $doc/share/doc/zig-$version/html ./langref.html - ''; -} diff --git a/pkgs/development/compilers/zig/0.10/001-0.10-macho-fixes.patch b/pkgs/development/compilers/zig/0.10/001-0.10-macho-fixes.patch new file mode 100644 index 0000000000000..95f0bdf701e29 --- /dev/null +++ b/pkgs/development/compilers/zig/0.10/001-0.10-macho-fixes.patch @@ -0,0 +1,367 @@ +From 405801d8a8be734425eca4f3eebc56287804ac93 Mon Sep 17 00:00:00 2001 +From: Jakub Konka +Date: Sun, 5 Feb 2023 10:04:34 +0100 +Subject: [PATCH] macho: temp fix alignment and enable some logs + +--- + src/link/MachO/Object.zig | 80 ++++++++++++++++++++++++++------------ + src/link/MachO/ZldAtom.zig | 29 +++++++------- + src/link/MachO/zld.zig | 22 +++++------ + 3 files changed, 79 insertions(+), 52 deletions(-) + +diff --git a/src/link/MachO/Object.zig b/src/link/MachO/Object.zig +index 401184da515..05638c1f858 100644 +--- a/src/link/MachO/Object.zig ++++ b/src/link/MachO/Object.zig +@@ -54,12 +54,18 @@ atom_by_index_table: []AtomIndex = undefined, + /// Can be undefined as set together with in_symtab. + globals_lookup: []i64 = undefined, + ++/// All relocs sorted and flattened. ++relocs: std.ArrayListUnmanaged(macho.relocation_info) = .{}, ++sect_relocs_lookup: std.ArrayListUnmanaged(u32) = .{}, ++ + atoms: std.ArrayListUnmanaged(AtomIndex) = .{}, + + pub fn deinit(self: *Object, gpa: Allocator) void { + self.atoms.deinit(gpa); + gpa.free(self.name); + gpa.free(self.contents); ++ self.relocs.deinit(gpa); ++ self.sect_relocs_lookup.deinit(gpa); + if (self.in_symtab) |_| { + gpa.free(self.source_symtab_lookup); + gpa.free(self.source_address_lookup); +@@ -101,6 +107,10 @@ pub fn parse(self: *Object, allocator: Allocator, cpu_arch: std.Target.Cpu.Arch) + return error.MismatchedCpuArchitecture; + } + ++ const nsects = self.getSourceSections().len; ++ try self.sect_relocs_lookup.resize(allocator, nsects); ++ mem.set(u32, self.sect_relocs_lookup.items, 0); ++ + var it = LoadCommandIterator{ + .ncmds = self.header.ncmds, + .buffer = self.contents[@sizeOf(macho.mach_header_64)..][0..self.header.sizeofcmds], +@@ -110,13 +120,11 @@ pub fn parse(self: *Object, allocator: Allocator, cpu_arch: std.Target.Cpu.Arch) + .SYMTAB => { + const symtab = cmd.cast(macho.symtab_command).?; + self.in_symtab = @ptrCast( +- [*]const macho.nlist_64, +- @alignCast(@alignOf(macho.nlist_64), &self.contents[symtab.symoff]), ++ [*]align(1) const macho.nlist_64, ++ self.contents.ptr + symtab.symoff, + )[0..symtab.nsyms]; + self.in_strtab = self.contents[symtab.stroff..][0..symtab.strsize]; + +- const nsects = self.getSourceSections().len; +- + self.symtab = try allocator.alloc(macho.nlist_64, self.in_symtab.?.len + nsects); + self.source_symtab_lookup = try allocator.alloc(u32, self.in_symtab.?.len); + self.strtab_lookup = try allocator.alloc(u32, self.in_symtab.?.len); +@@ -192,6 +200,17 @@ const SymbolAtIndex = struct { + return mem.sliceTo(@ptrCast([*:0]const u8, ctx.in_strtab.?.ptr + off), 0); + } + ++ fn getSymbolSeniority(self: SymbolAtIndex, ctx: Context) u2 { ++ const sym = self.getSymbol(ctx); ++ if (!sym.ext()) { ++ const sym_name = self.getSymbolName(ctx); ++ if (mem.startsWith(u8, sym_name, "l") or mem.startsWith(u8, sym_name, "L")) return 0; ++ return 1; ++ } ++ if (sym.weakDef() or sym.pext()) return 2; ++ return 3; ++ } ++ + /// Performs lexicographic-like check. + /// * lhs and rhs defined + /// * if lhs == rhs +@@ -206,23 +225,15 @@ const SymbolAtIndex = struct { + if (lhs.sect() and rhs.sect()) { + if (lhs.n_value == rhs.n_value) { + if (lhs.n_sect == rhs.n_sect) { +- if (lhs.ext() and rhs.ext()) { +- if ((lhs.pext() or lhs.weakDef()) and (rhs.pext() or rhs.weakDef())) { +- return false; +- } else return rhs.pext() or rhs.weakDef(); +- } else { +- const lhs_name = lhs_index.getSymbolName(ctx); +- const lhs_temp = mem.startsWith(u8, lhs_name, "l") or mem.startsWith(u8, lhs_name, "L"); +- const rhs_name = rhs_index.getSymbolName(ctx); +- const rhs_temp = mem.startsWith(u8, rhs_name, "l") or mem.startsWith(u8, rhs_name, "L"); +- if (lhs_temp and rhs_temp) { +- return false; +- } else return rhs_temp; +- } ++ const lhs_senior = lhs_index.getSymbolSeniority(ctx); ++ const rhs_senior = rhs_index.getSymbolSeniority(ctx); ++ if (lhs_senior == rhs_senior) { ++ return lessThanByNStrx(ctx, lhs_index, rhs_index); ++ } else return lhs_senior < rhs_senior; + } else return lhs.n_sect < rhs.n_sect; + } else return lhs.n_value < rhs.n_value; + } else if (lhs.undf() and rhs.undf()) { +- return false; ++ return lessThanByNStrx(ctx, lhs_index, rhs_index); + } else return rhs.undf(); + } + +@@ -393,6 +404,16 @@ pub fn splitIntoAtoms(self: *Object, zld: *Zld, object_id: u31) !void { + zld.sections.items(.header)[out_sect_id].sectName(), + }); + ++ // Parse all relocs for the input section, and sort in descending order. ++ // Previously, I have wrongly assumed the compilers output relocations for each ++ // section in a sorted manner which is simply not true. ++ const start = @intCast(u32, self.relocs.items.len); ++ if (self.getSourceRelocs(section.header)) |relocs| { ++ try self.relocs.appendUnalignedSlice(gpa, relocs); ++ std.sort.sort(macho.relocation_info, self.relocs.items[start..], {}, relocGreaterThan); ++ } ++ self.sect_relocs_lookup.items[section.id] = start; ++ + const cpu_arch = zld.options.target.cpu.arch; + const sect_loc = filterSymbolsBySection(symtab[sect_sym_index..], sect_id + 1); + const sect_start_index = sect_sym_index + sect_loc.index; +@@ -559,7 +580,7 @@ pub fn getSourceSections(self: Object) []const macho.section_64 { + } else unreachable; + } + +-pub fn parseDataInCode(self: Object) ?[]const macho.data_in_code_entry { ++pub fn parseDataInCode(self: Object) ?[]align(1) const macho.data_in_code_entry { + var it = LoadCommandIterator{ + .ncmds = self.header.ncmds, + .buffer = self.contents[@sizeOf(macho.mach_header_64)..][0..self.header.sizeofcmds], +@@ -569,10 +590,7 @@ pub fn parseDataInCode(self: Object) ?[]const macho.data_in_code_entry { + .DATA_IN_CODE => { + const dice = cmd.cast(macho.linkedit_data_command).?; + const ndice = @divExact(dice.datasize, @sizeOf(macho.data_in_code_entry)); +- return @ptrCast( +- [*]const macho.data_in_code_entry, +- @alignCast(@alignOf(macho.data_in_code_entry), &self.contents[dice.dataoff]), +- )[0..ndice]; ++ return @ptrCast([*]align(1) const macho.data_in_code_entry, self.contents.ptr + dice.dataoff)[0..ndice]; + }, + else => {}, + } +@@ -632,11 +650,23 @@ pub fn getSectionAliasSymbolPtr(self: *Object, sect_id: u8) *macho.nlist_64 { + return &self.symtab[self.getSectionAliasSymbolIndex(sect_id)]; + } + +-pub fn getRelocs(self: Object, sect: macho.section_64) []align(1) const macho.relocation_info { +- if (sect.nreloc == 0) return &[0]macho.relocation_info{}; ++fn getSourceRelocs(self: Object, sect: macho.section_64) ?[]align(1) const macho.relocation_info { ++ if (sect.nreloc == 0) return null; + return @ptrCast([*]align(1) const macho.relocation_info, self.contents.ptr + sect.reloff)[0..sect.nreloc]; + } + ++pub fn getRelocs(self: Object, sect_id: u16) []const macho.relocation_info { ++ const sect = self.getSourceSection(sect_id); ++ const start = self.sect_relocs_lookup.items[sect_id]; ++ const len = sect.nreloc; ++ return self.relocs.items[start..][0..len]; ++} ++ ++fn relocGreaterThan(ctx: void, lhs: macho.relocation_info, rhs: macho.relocation_info) bool { ++ _ = ctx; ++ return lhs.r_address > rhs.r_address; ++} ++ + pub fn getSymbolName(self: Object, index: u32) []const u8 { + const strtab = self.in_strtab.?; + const sym = self.symtab[index]; +diff --git a/src/link/MachO/ZldAtom.zig b/src/link/MachO/ZldAtom.zig +index 817aa816625..b42309598d7 100644 +--- a/src/link/MachO/ZldAtom.zig ++++ b/src/link/MachO/ZldAtom.zig +@@ -465,7 +465,7 @@ pub fn resolveRelocs( + zld: *Zld, + atom_index: AtomIndex, + atom_code: []u8, +- atom_relocs: []align(1) const macho.relocation_info, ++ atom_relocs: []const macho.relocation_info, + reverse_lookup: []u32, + ) !void { + const arch = zld.options.target.cpu.arch; +@@ -540,7 +540,7 @@ fn resolveRelocsArm64( + zld: *Zld, + atom_index: AtomIndex, + atom_code: []u8, +- atom_relocs: []align(1) const macho.relocation_info, ++ atom_relocs: []const macho.relocation_info, + reverse_lookup: []u32, + context: RelocContext, + ) !void { +@@ -579,7 +579,6 @@ fn resolveRelocsArm64( + } + + const target = parseRelocTarget(zld, atom_index, rel, reverse_lookup); +- const rel_offset = @intCast(u32, rel.r_address - context.base_offset); + + log.debug(" RELA({s}) @ {x} => %{d} ('{s}') in object({?})", .{ + @tagName(rel_type), +@@ -589,6 +588,7 @@ fn resolveRelocsArm64( + target.file, + }); + ++ const rel_offset = @intCast(u32, rel.r_address - context.base_offset); + const source_addr = blk: { + const source_sym = zld.getSymbol(atom.getSymbolWithLoc()); + break :blk source_sym.n_value + rel_offset; +@@ -596,7 +596,7 @@ fn resolveRelocsArm64( + const is_tlv = is_tlv: { + const source_sym = zld.getSymbol(atom.getSymbolWithLoc()); + const header = zld.sections.items(.header)[source_sym.n_sect - 1]; +- break :is_tlv header.@"type"() == macho.S_THREAD_LOCAL_VARIABLES; ++ break :is_tlv header.type() == macho.S_THREAD_LOCAL_VARIABLES; + }; + const target_addr = try getRelocTargetAddress(zld, rel, target, is_tlv); + +@@ -831,7 +831,7 @@ fn resolveRelocsX86( + zld: *Zld, + atom_index: AtomIndex, + atom_code: []u8, +- atom_relocs: []align(1) const macho.relocation_info, ++ atom_relocs: []const macho.relocation_info, + reverse_lookup: []u32, + context: RelocContext, + ) !void { +@@ -877,7 +877,7 @@ fn resolveRelocsX86( + const is_tlv = is_tlv: { + const source_sym = zld.getSymbol(atom.getSymbolWithLoc()); + const header = zld.sections.items(.header)[source_sym.n_sect - 1]; +- break :is_tlv header.@"type"() == macho.S_THREAD_LOCAL_VARIABLES; ++ break :is_tlv header.type() == macho.S_THREAD_LOCAL_VARIABLES; + }; + + log.debug(" | source_addr = 0x{x}", .{source_addr}); +@@ -1015,27 +1015,24 @@ pub fn getAtomCode(zld: *Zld, atom_index: AtomIndex) []const u8 { + return code[offset..][0..code_len]; + } + +-pub fn getAtomRelocs(zld: *Zld, atom_index: AtomIndex) []align(1) const macho.relocation_info { ++pub fn getAtomRelocs(zld: *Zld, atom_index: AtomIndex) []const macho.relocation_info { + const atom = zld.getAtomPtr(atom_index); + assert(atom.getFile() != null); // Synthetic atom shouldn't need to unique for relocs. + const object = zld.objects.items[atom.getFile().?]; + +- const source_sect = if (object.getSourceSymbol(atom.sym_index)) |source_sym| blk: { +- const source_sect = object.getSourceSection(source_sym.n_sect - 1); +- assert(!source_sect.isZerofill()); +- break :blk source_sect; ++ const source_sect_id = if (object.getSourceSymbol(atom.sym_index)) |source_sym| blk: { ++ break :blk source_sym.n_sect - 1; + } else blk: { + // If there was no matching symbol present in the source symtab, this means + // we are dealing with either an entire section, or part of it, but also + // starting at the beginning. + const nbase = @intCast(u32, object.in_symtab.?.len); + const sect_id = @intCast(u16, atom.sym_index - nbase); +- const source_sect = object.getSourceSection(sect_id); +- assert(!source_sect.isZerofill()); +- break :blk source_sect; ++ break :blk sect_id; + }; +- +- const relocs = object.getRelocs(source_sect); ++ const source_sect = object.getSourceSection(source_sect_id); ++ assert(!source_sect.isZerofill()); ++ const relocs = object.getRelocs(source_sect_id); + + if (atom.cached_relocs_start == -1) { + const indexes = if (object.getSourceSymbol(atom.sym_index)) |source_sym| blk: { +diff --git a/src/link/MachO/zld.zig b/src/link/MachO/zld.zig +index 3a2ea79c6ec..cee3f302c08 100644 +--- a/src/link/MachO/zld.zig ++++ b/src/link/MachO/zld.zig +@@ -396,7 +396,7 @@ pub const Zld = struct { + break :blk null; + } + +- switch (sect.@"type"()) { ++ switch (sect.type()) { + macho.S_4BYTE_LITERALS, + macho.S_8BYTE_LITERALS, + macho.S_16BYTE_LITERALS, +@@ -1701,7 +1701,7 @@ pub const Zld = struct { + break :outer; + } + } +- switch (header.@"type"()) { ++ switch (header.type()) { + macho.S_NON_LAZY_SYMBOL_POINTERS => { + try self.writeGotPointer(count, buffer.writer()); + }, +@@ -1718,7 +1718,7 @@ pub const Zld = struct { + break :outer; + } + } +- if (header.@"type"() == macho.S_SYMBOL_STUBS) { ++ if (header.type() == macho.S_SYMBOL_STUBS) { + try self.writeStubCode(atom_index, count, buffer.writer()); + } else if (mem.eql(u8, header.sectName(), "__stub_helper")) { + try self.writeStubHelperCode(atom_index, buffer.writer()); +@@ -1802,7 +1802,7 @@ pub const Zld = struct { + for (slice.items(.header)) |*header, sect_id| { + if (header.size == 0) continue; + if (self.requiresThunks()) { +- if (header.isCode() and !(header.@"type"() == macho.S_SYMBOL_STUBS) and !mem.eql(u8, header.sectName(), "__stub_helper")) continue; ++ if (header.isCode() and !(header.type() == macho.S_SYMBOL_STUBS) and !mem.eql(u8, header.sectName(), "__stub_helper")) continue; + } + + var atom_index = slice.items(.first_atom_index)[sect_id]; +@@ -1830,7 +1830,7 @@ pub const Zld = struct { + if (self.requiresThunks()) { + for (slice.items(.header)) |header, sect_id| { + if (!header.isCode()) continue; +- if (header.@"type"() == macho.S_SYMBOL_STUBS) continue; ++ if (header.type() == macho.S_SYMBOL_STUBS) continue; + if (mem.eql(u8, header.sectName(), "__stub_helper")) continue; + + // Create jump/branch range extenders if needed. +@@ -1994,10 +1994,10 @@ pub const Zld = struct { + const section_precedence: u4 = blk: { + if (header.isCode()) { + if (mem.eql(u8, "__text", header.sectName())) break :blk 0x0; +- if (header.@"type"() == macho.S_SYMBOL_STUBS) break :blk 0x1; ++ if (header.type() == macho.S_SYMBOL_STUBS) break :blk 0x1; + break :blk 0x2; + } +- switch (header.@"type"()) { ++ switch (header.type()) { + macho.S_NON_LAZY_SYMBOL_POINTERS, + macho.S_LAZY_SYMBOL_POINTERS, + => break :blk 0x0, +@@ -2121,7 +2121,7 @@ pub const Zld = struct { + + // Finally, unpack the rest. + for (slice.items(.header)) |header, sect_id| { +- switch (header.@"type"()) { ++ switch (header.type()) { + macho.S_LITERAL_POINTERS, + macho.S_REGULAR, + macho.S_MOD_INIT_FUNC_POINTERS, +@@ -2252,7 +2252,7 @@ pub const Zld = struct { + // Finally, unpack the rest. + const slice = self.sections.slice(); + for (slice.items(.header)) |header, sect_id| { +- switch (header.@"type"()) { ++ switch (header.type()) { + macho.S_LITERAL_POINTERS, + macho.S_REGULAR, + macho.S_MOD_INIT_FUNC_POINTERS, +@@ -2707,10 +2707,10 @@ pub const Zld = struct { + } + + fn filterDataInCode( +- dices: []const macho.data_in_code_entry, ++ dices: []align(1) const macho.data_in_code_entry, + start_addr: u64, + end_addr: u64, +- ) []const macho.data_in_code_entry { ++ ) []align(1) const macho.data_in_code_entry { + const Predicate = struct { + addr: u64, + + diff --git a/pkgs/development/compilers/zig/0.10/default.nix b/pkgs/development/compilers/zig/0.10/default.nix new file mode 100644 index 0000000000000..170edafa819cf --- /dev/null +++ b/pkgs/development/compilers/zig/0.10/default.nix @@ -0,0 +1,109 @@ +{ + lib, + callPackage, + cmake, + coreutils, + fetchFromGitHub, + libxml2, + llvmPackages, + stdenv, + testers, + zlib, +}: + +stdenv.mkDerivation (finalAttrs: { + pname = "zig"; + version = "0.10.1"; + + src = fetchFromGitHub { + owner = "ziglang"; + repo = "zig"; + rev = finalAttrs.version; + hash = "sha256-69QIkkKzApOGfrBdgtmxFMDytRkSh+0YiaJQPbXsBeo="; + }; + + patches = [ + # Backport alignment related panics from zig-master to 0.10. + # Upstream issue: https://github.com/ziglang/zig/issues/14559 + ./001-0.10-macho-fixes.patch + ]; + + nativeBuildInputs = [ + cmake + (lib.getDev llvmPackages.llvm) + ]; + + buildInputs = + [ + libxml2 + zlib + ] + ++ (with llvmPackages; [ + libclang + lld + llvm + ]); + + outputs = [ + "out" + "doc" + ]; + + cmakeFlags = [ + # file RPATH_CHANGE could not write new RPATH + (lib.cmakeBool "CMAKE_SKIP_BUILD_RPATH" true) + # always link against static build of LLVM + (lib.cmakeBool "ZIG_STATIC_LLVM" true) + # ensure determinism in the compiler build + (lib.cmakeFeature "ZIG_TARGET_MCPU" "baseline") + ]; + + env.ZIG_GLOBAL_CACHE_DIR = "$TMPDIR/zig-cache"; + + doInstallCheck = true; + + strictDeps = true; + + # Zig's build looks at /usr/bin/env to find dynamic linking info. This doesn't + # work in Nix's sandbox. Use env from our coreutils instead. + postPatch = '' + substituteInPlace lib/std/zig/system/NativeTargetInfo.zig \ + --replace "/usr/bin/env" "${lib.getExe' coreutils "env"}" + ''; + + postBuild = '' + ./zig2 run ../doc/docgen.zig -- ./zig2 ../doc/langref.html.in langref.html + ''; + + postInstall = '' + install -Dm644 -t $doc/share/doc/zig-$version/html ./langref.html + ''; + + installCheckPhase = '' + runHook preInstallCheck + + $out/bin/zig test --cache-dir "$TMPDIR/zig-test-cache" -I $src/test $src/test/behavior.zig + + runHook postInstallCheck + ''; + + passthru = { + hook = callPackage ./hook.nix { zig = finalAttrs.finalPackage; }; + tests = { + version = testers.testVersion { + package = finalAttrs.finalPackage; + command = "zig version"; + }; + }; + }; + + meta = { + description = "General-purpose programming language and toolchain for maintaining robust, optimal, and reusable software"; + homepage = "https://ziglang.org/"; + changelog = "https://ziglang.org/download/${finalAttrs.version}/release-notes.html"; + license = lib.licenses.mit; + mainProgram = "zig"; + maintainers = with lib.maintainers; [ andrewrk ] ++ lib.teams.zig.members; + platforms = lib.platforms.unix; + }; +}) diff --git a/pkgs/development/compilers/zig/0.10/hook.nix b/pkgs/development/compilers/zig/0.10/hook.nix new file mode 100644 index 0000000000000..298d50d9e5f9d --- /dev/null +++ b/pkgs/development/compilers/zig/0.10/hook.nix @@ -0,0 +1,47 @@ +{ + lib, + makeSetupHook, + zig, +}: + +makeSetupHook { + name = "zig-hook"; + + propagatedBuildInputs = [ zig ]; + + substitutions = { + # This zig_default_flags below is meant to avoid CPU feature impurity in + # Nixpkgs. However, this flagset is "unstable": it is specifically meant to + # be controlled by the upstream development team - being up to that team + # exposing or not that flags to the outside (especially the package manager + # teams). + + # Because of this hurdle, @andrewrk from Zig Software Foundation proposed + # some solutions for this issue. Hopefully they will be implemented in + # future releases of Zig. When this happens, this flagset should be + # revisited accordingly. + + # Below are some useful links describing the discovery process of this 'bug' + # in Nixpkgs: + + # https://github.com/NixOS/nixpkgs/issues/169461 + # https://github.com/NixOS/nixpkgs/issues/185644 + # https://github.com/NixOS/nixpkgs/pull/197046 + # https://github.com/NixOS/nixpkgs/pull/241741#issuecomment-1624227485 + # https://github.com/ziglang/zig/issues/14281#issuecomment-1624220653 + + zig_default_flags = [ + "-Dcpu=baseline" + "-Drelease-safe=true" + ]; + }; + + passthru = { + inherit zig; + }; + + meta = { + description = "A setup hook for using the Zig compiler in Nixpkgs"; + inherit (zig.meta) maintainers platforms broken; + }; +} ./setup-hook.sh diff --git a/pkgs/development/compilers/zig/0.10/setup-hook.sh b/pkgs/development/compilers/zig/0.10/setup-hook.sh new file mode 100644 index 0000000000000..689ebec8a3077 --- /dev/null +++ b/pkgs/development/compilers/zig/0.10/setup-hook.sh @@ -0,0 +1,70 @@ +# shellcheck shell=bash disable=SC2154,SC2086 + +readonly zigDefaultFlagsArray=(@zig_default_flags@) + +function zigSetGlobalCacheDir { + ZIG_GLOBAL_CACHE_DIR=$(mktemp -d) + export ZIG_GLOBAL_CACHE_DIR +} + +function zigBuildPhase { + runHook preBuild + + local flagsArray=( + "${zigDefaultFlagsArray[@]}" + $zigBuildFlags "${zigBuildFlagsArray[@]}" + ) + + echoCmd 'zig build flags' "${flagsArray[@]}" + zig build "${flagsArray[@]}" + + runHook postBuild +} + +function zigCheckPhase { + runHook preCheck + + local flagsArray=( + "${zigDefaultFlagsArray[@]}" + $zigCheckFlags "${zigCheckFlagsArray[@]}" + ) + + echoCmd 'zig check flags' "${flagsArray[@]}" + zig build test "${flagsArray[@]}" + + runHook postCheck +} + +function zigInstallPhase { + runHook preInstall + + local flagsArray=( + "${zigDefaultFlagsArray[@]}" + $zigBuildFlags "${zigBuildFlagsArray[@]}" + $zigInstallFlags "${zigInstallFlagsArray[@]}" + ) + + if [ -z "${dontAddPrefix-}" ]; then + # Zig does not recognize `--prefix=/dir/`, only `--prefix /dir/` + flagsArray+=("${prefixKey:---prefix}" "$prefix") + fi + + echoCmd 'zig install flags' "${flagsArray[@]}" + zig build install "${flagsArray[@]}" + + runHook postInstall +} + +addEnvHooks "$targetOffset" zigSetGlobalCacheDir + +if [ -z "${dontUseZigBuild-}" ] && [ -z "${buildPhase-}" ]; then + buildPhase=zigBuildPhase +fi + +if [ -z "${dontUseZigCheck-}" ] && [ -z "${checkPhase-}" ]; then + checkPhase=zigCheckPhase +fi + +if [ -z "${dontUseZigInstall-}" ] && [ -z "${installPhase-}" ]; then + installPhase=zigInstallPhase +fi diff --git a/pkgs/development/compilers/zig/002-0.10-macho-fixes.patch b/pkgs/development/compilers/zig/002-0.10-macho-fixes.patch deleted file mode 100644 index 95f0bdf701e29..0000000000000 --- a/pkgs/development/compilers/zig/002-0.10-macho-fixes.patch +++ /dev/null @@ -1,367 +0,0 @@ -From 405801d8a8be734425eca4f3eebc56287804ac93 Mon Sep 17 00:00:00 2001 -From: Jakub Konka -Date: Sun, 5 Feb 2023 10:04:34 +0100 -Subject: [PATCH] macho: temp fix alignment and enable some logs - ---- - src/link/MachO/Object.zig | 80 ++++++++++++++++++++++++++------------ - src/link/MachO/ZldAtom.zig | 29 +++++++------- - src/link/MachO/zld.zig | 22 +++++------ - 3 files changed, 79 insertions(+), 52 deletions(-) - -diff --git a/src/link/MachO/Object.zig b/src/link/MachO/Object.zig -index 401184da515..05638c1f858 100644 ---- a/src/link/MachO/Object.zig -+++ b/src/link/MachO/Object.zig -@@ -54,12 +54,18 @@ atom_by_index_table: []AtomIndex = undefined, - /// Can be undefined as set together with in_symtab. - globals_lookup: []i64 = undefined, - -+/// All relocs sorted and flattened. -+relocs: std.ArrayListUnmanaged(macho.relocation_info) = .{}, -+sect_relocs_lookup: std.ArrayListUnmanaged(u32) = .{}, -+ - atoms: std.ArrayListUnmanaged(AtomIndex) = .{}, - - pub fn deinit(self: *Object, gpa: Allocator) void { - self.atoms.deinit(gpa); - gpa.free(self.name); - gpa.free(self.contents); -+ self.relocs.deinit(gpa); -+ self.sect_relocs_lookup.deinit(gpa); - if (self.in_symtab) |_| { - gpa.free(self.source_symtab_lookup); - gpa.free(self.source_address_lookup); -@@ -101,6 +107,10 @@ pub fn parse(self: *Object, allocator: Allocator, cpu_arch: std.Target.Cpu.Arch) - return error.MismatchedCpuArchitecture; - } - -+ const nsects = self.getSourceSections().len; -+ try self.sect_relocs_lookup.resize(allocator, nsects); -+ mem.set(u32, self.sect_relocs_lookup.items, 0); -+ - var it = LoadCommandIterator{ - .ncmds = self.header.ncmds, - .buffer = self.contents[@sizeOf(macho.mach_header_64)..][0..self.header.sizeofcmds], -@@ -110,13 +120,11 @@ pub fn parse(self: *Object, allocator: Allocator, cpu_arch: std.Target.Cpu.Arch) - .SYMTAB => { - const symtab = cmd.cast(macho.symtab_command).?; - self.in_symtab = @ptrCast( -- [*]const macho.nlist_64, -- @alignCast(@alignOf(macho.nlist_64), &self.contents[symtab.symoff]), -+ [*]align(1) const macho.nlist_64, -+ self.contents.ptr + symtab.symoff, - )[0..symtab.nsyms]; - self.in_strtab = self.contents[symtab.stroff..][0..symtab.strsize]; - -- const nsects = self.getSourceSections().len; -- - self.symtab = try allocator.alloc(macho.nlist_64, self.in_symtab.?.len + nsects); - self.source_symtab_lookup = try allocator.alloc(u32, self.in_symtab.?.len); - self.strtab_lookup = try allocator.alloc(u32, self.in_symtab.?.len); -@@ -192,6 +200,17 @@ const SymbolAtIndex = struct { - return mem.sliceTo(@ptrCast([*:0]const u8, ctx.in_strtab.?.ptr + off), 0); - } - -+ fn getSymbolSeniority(self: SymbolAtIndex, ctx: Context) u2 { -+ const sym = self.getSymbol(ctx); -+ if (!sym.ext()) { -+ const sym_name = self.getSymbolName(ctx); -+ if (mem.startsWith(u8, sym_name, "l") or mem.startsWith(u8, sym_name, "L")) return 0; -+ return 1; -+ } -+ if (sym.weakDef() or sym.pext()) return 2; -+ return 3; -+ } -+ - /// Performs lexicographic-like check. - /// * lhs and rhs defined - /// * if lhs == rhs -@@ -206,23 +225,15 @@ const SymbolAtIndex = struct { - if (lhs.sect() and rhs.sect()) { - if (lhs.n_value == rhs.n_value) { - if (lhs.n_sect == rhs.n_sect) { -- if (lhs.ext() and rhs.ext()) { -- if ((lhs.pext() or lhs.weakDef()) and (rhs.pext() or rhs.weakDef())) { -- return false; -- } else return rhs.pext() or rhs.weakDef(); -- } else { -- const lhs_name = lhs_index.getSymbolName(ctx); -- const lhs_temp = mem.startsWith(u8, lhs_name, "l") or mem.startsWith(u8, lhs_name, "L"); -- const rhs_name = rhs_index.getSymbolName(ctx); -- const rhs_temp = mem.startsWith(u8, rhs_name, "l") or mem.startsWith(u8, rhs_name, "L"); -- if (lhs_temp and rhs_temp) { -- return false; -- } else return rhs_temp; -- } -+ const lhs_senior = lhs_index.getSymbolSeniority(ctx); -+ const rhs_senior = rhs_index.getSymbolSeniority(ctx); -+ if (lhs_senior == rhs_senior) { -+ return lessThanByNStrx(ctx, lhs_index, rhs_index); -+ } else return lhs_senior < rhs_senior; - } else return lhs.n_sect < rhs.n_sect; - } else return lhs.n_value < rhs.n_value; - } else if (lhs.undf() and rhs.undf()) { -- return false; -+ return lessThanByNStrx(ctx, lhs_index, rhs_index); - } else return rhs.undf(); - } - -@@ -393,6 +404,16 @@ pub fn splitIntoAtoms(self: *Object, zld: *Zld, object_id: u31) !void { - zld.sections.items(.header)[out_sect_id].sectName(), - }); - -+ // Parse all relocs for the input section, and sort in descending order. -+ // Previously, I have wrongly assumed the compilers output relocations for each -+ // section in a sorted manner which is simply not true. -+ const start = @intCast(u32, self.relocs.items.len); -+ if (self.getSourceRelocs(section.header)) |relocs| { -+ try self.relocs.appendUnalignedSlice(gpa, relocs); -+ std.sort.sort(macho.relocation_info, self.relocs.items[start..], {}, relocGreaterThan); -+ } -+ self.sect_relocs_lookup.items[section.id] = start; -+ - const cpu_arch = zld.options.target.cpu.arch; - const sect_loc = filterSymbolsBySection(symtab[sect_sym_index..], sect_id + 1); - const sect_start_index = sect_sym_index + sect_loc.index; -@@ -559,7 +580,7 @@ pub fn getSourceSections(self: Object) []const macho.section_64 { - } else unreachable; - } - --pub fn parseDataInCode(self: Object) ?[]const macho.data_in_code_entry { -+pub fn parseDataInCode(self: Object) ?[]align(1) const macho.data_in_code_entry { - var it = LoadCommandIterator{ - .ncmds = self.header.ncmds, - .buffer = self.contents[@sizeOf(macho.mach_header_64)..][0..self.header.sizeofcmds], -@@ -569,10 +590,7 @@ pub fn parseDataInCode(self: Object) ?[]const macho.data_in_code_entry { - .DATA_IN_CODE => { - const dice = cmd.cast(macho.linkedit_data_command).?; - const ndice = @divExact(dice.datasize, @sizeOf(macho.data_in_code_entry)); -- return @ptrCast( -- [*]const macho.data_in_code_entry, -- @alignCast(@alignOf(macho.data_in_code_entry), &self.contents[dice.dataoff]), -- )[0..ndice]; -+ return @ptrCast([*]align(1) const macho.data_in_code_entry, self.contents.ptr + dice.dataoff)[0..ndice]; - }, - else => {}, - } -@@ -632,11 +650,23 @@ pub fn getSectionAliasSymbolPtr(self: *Object, sect_id: u8) *macho.nlist_64 { - return &self.symtab[self.getSectionAliasSymbolIndex(sect_id)]; - } - --pub fn getRelocs(self: Object, sect: macho.section_64) []align(1) const macho.relocation_info { -- if (sect.nreloc == 0) return &[0]macho.relocation_info{}; -+fn getSourceRelocs(self: Object, sect: macho.section_64) ?[]align(1) const macho.relocation_info { -+ if (sect.nreloc == 0) return null; - return @ptrCast([*]align(1) const macho.relocation_info, self.contents.ptr + sect.reloff)[0..sect.nreloc]; - } - -+pub fn getRelocs(self: Object, sect_id: u16) []const macho.relocation_info { -+ const sect = self.getSourceSection(sect_id); -+ const start = self.sect_relocs_lookup.items[sect_id]; -+ const len = sect.nreloc; -+ return self.relocs.items[start..][0..len]; -+} -+ -+fn relocGreaterThan(ctx: void, lhs: macho.relocation_info, rhs: macho.relocation_info) bool { -+ _ = ctx; -+ return lhs.r_address > rhs.r_address; -+} -+ - pub fn getSymbolName(self: Object, index: u32) []const u8 { - const strtab = self.in_strtab.?; - const sym = self.symtab[index]; -diff --git a/src/link/MachO/ZldAtom.zig b/src/link/MachO/ZldAtom.zig -index 817aa816625..b42309598d7 100644 ---- a/src/link/MachO/ZldAtom.zig -+++ b/src/link/MachO/ZldAtom.zig -@@ -465,7 +465,7 @@ pub fn resolveRelocs( - zld: *Zld, - atom_index: AtomIndex, - atom_code: []u8, -- atom_relocs: []align(1) const macho.relocation_info, -+ atom_relocs: []const macho.relocation_info, - reverse_lookup: []u32, - ) !void { - const arch = zld.options.target.cpu.arch; -@@ -540,7 +540,7 @@ fn resolveRelocsArm64( - zld: *Zld, - atom_index: AtomIndex, - atom_code: []u8, -- atom_relocs: []align(1) const macho.relocation_info, -+ atom_relocs: []const macho.relocation_info, - reverse_lookup: []u32, - context: RelocContext, - ) !void { -@@ -579,7 +579,6 @@ fn resolveRelocsArm64( - } - - const target = parseRelocTarget(zld, atom_index, rel, reverse_lookup); -- const rel_offset = @intCast(u32, rel.r_address - context.base_offset); - - log.debug(" RELA({s}) @ {x} => %{d} ('{s}') in object({?})", .{ - @tagName(rel_type), -@@ -589,6 +588,7 @@ fn resolveRelocsArm64( - target.file, - }); - -+ const rel_offset = @intCast(u32, rel.r_address - context.base_offset); - const source_addr = blk: { - const source_sym = zld.getSymbol(atom.getSymbolWithLoc()); - break :blk source_sym.n_value + rel_offset; -@@ -596,7 +596,7 @@ fn resolveRelocsArm64( - const is_tlv = is_tlv: { - const source_sym = zld.getSymbol(atom.getSymbolWithLoc()); - const header = zld.sections.items(.header)[source_sym.n_sect - 1]; -- break :is_tlv header.@"type"() == macho.S_THREAD_LOCAL_VARIABLES; -+ break :is_tlv header.type() == macho.S_THREAD_LOCAL_VARIABLES; - }; - const target_addr = try getRelocTargetAddress(zld, rel, target, is_tlv); - -@@ -831,7 +831,7 @@ fn resolveRelocsX86( - zld: *Zld, - atom_index: AtomIndex, - atom_code: []u8, -- atom_relocs: []align(1) const macho.relocation_info, -+ atom_relocs: []const macho.relocation_info, - reverse_lookup: []u32, - context: RelocContext, - ) !void { -@@ -877,7 +877,7 @@ fn resolveRelocsX86( - const is_tlv = is_tlv: { - const source_sym = zld.getSymbol(atom.getSymbolWithLoc()); - const header = zld.sections.items(.header)[source_sym.n_sect - 1]; -- break :is_tlv header.@"type"() == macho.S_THREAD_LOCAL_VARIABLES; -+ break :is_tlv header.type() == macho.S_THREAD_LOCAL_VARIABLES; - }; - - log.debug(" | source_addr = 0x{x}", .{source_addr}); -@@ -1015,27 +1015,24 @@ pub fn getAtomCode(zld: *Zld, atom_index: AtomIndex) []const u8 { - return code[offset..][0..code_len]; - } - --pub fn getAtomRelocs(zld: *Zld, atom_index: AtomIndex) []align(1) const macho.relocation_info { -+pub fn getAtomRelocs(zld: *Zld, atom_index: AtomIndex) []const macho.relocation_info { - const atom = zld.getAtomPtr(atom_index); - assert(atom.getFile() != null); // Synthetic atom shouldn't need to unique for relocs. - const object = zld.objects.items[atom.getFile().?]; - -- const source_sect = if (object.getSourceSymbol(atom.sym_index)) |source_sym| blk: { -- const source_sect = object.getSourceSection(source_sym.n_sect - 1); -- assert(!source_sect.isZerofill()); -- break :blk source_sect; -+ const source_sect_id = if (object.getSourceSymbol(atom.sym_index)) |source_sym| blk: { -+ break :blk source_sym.n_sect - 1; - } else blk: { - // If there was no matching symbol present in the source symtab, this means - // we are dealing with either an entire section, or part of it, but also - // starting at the beginning. - const nbase = @intCast(u32, object.in_symtab.?.len); - const sect_id = @intCast(u16, atom.sym_index - nbase); -- const source_sect = object.getSourceSection(sect_id); -- assert(!source_sect.isZerofill()); -- break :blk source_sect; -+ break :blk sect_id; - }; -- -- const relocs = object.getRelocs(source_sect); -+ const source_sect = object.getSourceSection(source_sect_id); -+ assert(!source_sect.isZerofill()); -+ const relocs = object.getRelocs(source_sect_id); - - if (atom.cached_relocs_start == -1) { - const indexes = if (object.getSourceSymbol(atom.sym_index)) |source_sym| blk: { -diff --git a/src/link/MachO/zld.zig b/src/link/MachO/zld.zig -index 3a2ea79c6ec..cee3f302c08 100644 ---- a/src/link/MachO/zld.zig -+++ b/src/link/MachO/zld.zig -@@ -396,7 +396,7 @@ pub const Zld = struct { - break :blk null; - } - -- switch (sect.@"type"()) { -+ switch (sect.type()) { - macho.S_4BYTE_LITERALS, - macho.S_8BYTE_LITERALS, - macho.S_16BYTE_LITERALS, -@@ -1701,7 +1701,7 @@ pub const Zld = struct { - break :outer; - } - } -- switch (header.@"type"()) { -+ switch (header.type()) { - macho.S_NON_LAZY_SYMBOL_POINTERS => { - try self.writeGotPointer(count, buffer.writer()); - }, -@@ -1718,7 +1718,7 @@ pub const Zld = struct { - break :outer; - } - } -- if (header.@"type"() == macho.S_SYMBOL_STUBS) { -+ if (header.type() == macho.S_SYMBOL_STUBS) { - try self.writeStubCode(atom_index, count, buffer.writer()); - } else if (mem.eql(u8, header.sectName(), "__stub_helper")) { - try self.writeStubHelperCode(atom_index, buffer.writer()); -@@ -1802,7 +1802,7 @@ pub const Zld = struct { - for (slice.items(.header)) |*header, sect_id| { - if (header.size == 0) continue; - if (self.requiresThunks()) { -- if (header.isCode() and !(header.@"type"() == macho.S_SYMBOL_STUBS) and !mem.eql(u8, header.sectName(), "__stub_helper")) continue; -+ if (header.isCode() and !(header.type() == macho.S_SYMBOL_STUBS) and !mem.eql(u8, header.sectName(), "__stub_helper")) continue; - } - - var atom_index = slice.items(.first_atom_index)[sect_id]; -@@ -1830,7 +1830,7 @@ pub const Zld = struct { - if (self.requiresThunks()) { - for (slice.items(.header)) |header, sect_id| { - if (!header.isCode()) continue; -- if (header.@"type"() == macho.S_SYMBOL_STUBS) continue; -+ if (header.type() == macho.S_SYMBOL_STUBS) continue; - if (mem.eql(u8, header.sectName(), "__stub_helper")) continue; - - // Create jump/branch range extenders if needed. -@@ -1994,10 +1994,10 @@ pub const Zld = struct { - const section_precedence: u4 = blk: { - if (header.isCode()) { - if (mem.eql(u8, "__text", header.sectName())) break :blk 0x0; -- if (header.@"type"() == macho.S_SYMBOL_STUBS) break :blk 0x1; -+ if (header.type() == macho.S_SYMBOL_STUBS) break :blk 0x1; - break :blk 0x2; - } -- switch (header.@"type"()) { -+ switch (header.type()) { - macho.S_NON_LAZY_SYMBOL_POINTERS, - macho.S_LAZY_SYMBOL_POINTERS, - => break :blk 0x0, -@@ -2121,7 +2121,7 @@ pub const Zld = struct { - - // Finally, unpack the rest. - for (slice.items(.header)) |header, sect_id| { -- switch (header.@"type"()) { -+ switch (header.type()) { - macho.S_LITERAL_POINTERS, - macho.S_REGULAR, - macho.S_MOD_INIT_FUNC_POINTERS, -@@ -2252,7 +2252,7 @@ pub const Zld = struct { - // Finally, unpack the rest. - const slice = self.sections.slice(); - for (slice.items(.header)) |header, sect_id| { -- switch (header.@"type"()) { -+ switch (header.type()) { - macho.S_LITERAL_POINTERS, - macho.S_REGULAR, - macho.S_MOD_INIT_FUNC_POINTERS, -@@ -2707,10 +2707,10 @@ pub const Zld = struct { - } - - fn filterDataInCode( -- dices: []const macho.data_in_code_entry, -+ dices: []align(1) const macho.data_in_code_entry, - start_addr: u64, - end_addr: u64, -- ) []const macho.data_in_code_entry { -+ ) []align(1) const macho.data_in_code_entry { - const Predicate = struct { - addr: u64, - - diff --git a/pkgs/top-level/all-packages.nix b/pkgs/top-level/all-packages.nix index 06d640bcf704d..3d2ac15f2d526 100644 --- a/pkgs/top-level/all-packages.nix +++ b/pkgs/top-level/all-packages.nix @@ -25058,7 +25058,7 @@ with pkgs; llvmPackages = llvmPackages_13; }; # requires a newer Apple SDK - zig_0_10 = darwin.apple_sdk_11_0.callPackage ../development/compilers/zig/0.10.nix { + zig_0_10 = darwin.apple_sdk_11_0.callPackage ../development/compilers/zig/0.10 { llvmPackages = llvmPackages_15; }; # requires a newer Apple SDK -- cgit 1.4.1 From e1f9afc289abbe11ac67f0e3ccd11d1f775dbbc6 Mon Sep 17 00:00:00 2001 From: Anderson Torres Date: Sun, 26 May 2024 23:23:32 -0300 Subject: zig_0_11: huge refactor - migrate it to a dedicated directory, zig/0.11 - can't migrate to by-name - because of undecipherable darwin.apple_sdk_11_0.callPackage - nixfmt - add a simple version test --- pkgs/development/compilers/zig/0.11.nix | 37 -------- pkgs/development/compilers/zig/0.11/default.nix | 103 ++++++++++++++++++++++ pkgs/development/compilers/zig/0.11/hook.nix | 47 ++++++++++ pkgs/development/compilers/zig/0.11/setup-hook.sh | 70 +++++++++++++++ pkgs/top-level/all-packages.nix | 2 +- 5 files changed, 221 insertions(+), 38 deletions(-) delete mode 100644 pkgs/development/compilers/zig/0.11.nix create mode 100644 pkgs/development/compilers/zig/0.11/default.nix create mode 100644 pkgs/development/compilers/zig/0.11/hook.nix create mode 100644 pkgs/development/compilers/zig/0.11/setup-hook.sh (limited to 'pkgs/development/compilers') diff --git a/pkgs/development/compilers/zig/0.11.nix b/pkgs/development/compilers/zig/0.11.nix deleted file mode 100644 index b393f42c65136..0000000000000 --- a/pkgs/development/compilers/zig/0.11.nix +++ /dev/null @@ -1,37 +0,0 @@ -{ lib -, stdenv -, fetchFromGitHub -, cmake -, llvmPackages -, libxml2 -, zlib -, coreutils -, callPackage -}@args: - -import ./generic.nix args { - version = "0.11.0"; - - hash = "sha256-iuU1fzkbJxI+0N1PiLQM013Pd1bzrgqkbIyTxo5gB2I="; - - outputs = [ "out" "doc" ]; - - cmakeFlags = [ - # file RPATH_CHANGE could not write new RPATH - "-DCMAKE_SKIP_BUILD_RPATH=ON" - - # always link against static build of LLVM - "-DZIG_STATIC_LLVM=ON" - - # ensure determinism in the compiler build - "-DZIG_TARGET_MCPU=baseline" - ]; - - postBuild = '' - stage3/bin/zig run ../tools/docgen.zig -- ../doc/langref.html.in langref.html --zig $PWD/stage3/bin/zig - ''; - - postInstall = '' - install -Dm444 -t $doc/share/doc/zig-$version/html langref.html - ''; -} diff --git a/pkgs/development/compilers/zig/0.11/default.nix b/pkgs/development/compilers/zig/0.11/default.nix new file mode 100644 index 0000000000000..0f19f455cbb0f --- /dev/null +++ b/pkgs/development/compilers/zig/0.11/default.nix @@ -0,0 +1,103 @@ +{ + lib, + callPackage, + cmake, + coreutils, + fetchFromGitHub, + libxml2, + llvmPackages, + stdenv, + testers, + zlib, +}: + +stdenv.mkDerivation (finalAttrs: { + pname = "zig"; + version = "0.11.0"; + + src = fetchFromGitHub { + owner = "ziglang"; + repo = "zig"; + rev = finalAttrs.version; + hash = "sha256-iuU1fzkbJxI+0N1PiLQM013Pd1bzrgqkbIyTxo5gB2I="; + }; + + nativeBuildInputs = [ + cmake + (lib.getDev llvmPackages.llvm) + ]; + + buildInputs = + [ + libxml2 + zlib + ] + ++ (with llvmPackages; [ + libclang + lld + llvm + ]); + + outputs = [ + "out" + "doc" + ]; + + cmakeFlags = [ + # file RPATH_CHANGE could not write new RPATH + (lib.cmakeBool "CMAKE_SKIP_BUILD_RPATH" true) + # ensure determinism in the compiler build + (lib.cmakeFeature "ZIG_TARGET_MCPU" "baseline") + # always link against static build of LLVM + (lib.cmakeBool "ZIG_STATIC_LLVM" true) + ]; + + env.ZIG_GLOBAL_CACHE_DIR = "$TMPDIR/zig-cache"; + + doInstallCheck = true; + + strictDeps = true; + + # Zig's build looks at /usr/bin/env to find dynamic linking info. This doesn't + # work in Nix's sandbox. Use env from our coreutils instead. + postPatch = '' + substituteInPlace lib/std/zig/system/NativeTargetInfo.zig \ + --replace "/usr/bin/env" "${lib.getExe' coreutils "env"}" + ''; + + postBuild = '' + stage3/bin/zig run ../tools/docgen.zig -- ../doc/langref.html.in langref.html --zig $PWD/stage3/bin/zig + ''; + + postInstall = '' + install -Dm444 -t $doc/share/doc/zig-$version/html langref.html + ''; + + installCheckPhase = '' + runHook preInstallCheck + + $out/bin/zig test --cache-dir "$TMPDIR/zig-test-cache" -I $src/test $src/test/behavior.zig + + runHook postInstallCheck + ''; + + passthru = { + hook = callPackage ./hook.nix { zig = finalAttrs.finalPackage; }; + tests = { + version = testers.testVersion { + package = finalAttrs.finalPackage; + command = "zig version"; + }; + }; + }; + + meta = { + description = "General-purpose programming language and toolchain for maintaining robust, optimal, and reusable software"; + homepage = "https://ziglang.org/"; + changelog = "https://ziglang.org/download/${finalAttrs.version}/release-notes.html"; + license = lib.licenses.mit; + mainProgram = "zig"; + maintainers = with lib.maintainers; [ andrewrk ] ++ lib.teams.zig.members; + platforms = lib.platforms.unix; + }; +}) diff --git a/pkgs/development/compilers/zig/0.11/hook.nix b/pkgs/development/compilers/zig/0.11/hook.nix new file mode 100644 index 0000000000000..391140d4a7ff3 --- /dev/null +++ b/pkgs/development/compilers/zig/0.11/hook.nix @@ -0,0 +1,47 @@ +{ + lib, + makeSetupHook, + zig, +}: + +makeSetupHook { + name = "zig-hook"; + + propagatedBuildInputs = [ zig ]; + + substitutions = { + # This zig_default_flags below is meant to avoid CPU feature impurity in + # Nixpkgs. However, this flagset is "unstable": it is specifically meant to + # be controlled by the upstream development team - being up to that team + # exposing or not that flags to the outside (especially the package manager + # teams). + + # Because of this hurdle, @andrewrk from Zig Software Foundation proposed + # some solutions for this issue. Hopefully they will be implemented in + # future releases of Zig. When this happens, this flagset should be + # revisited accordingly. + + # Below are some useful links describing the discovery process of this 'bug' + # in Nixpkgs: + + # https://github.com/NixOS/nixpkgs/issues/169461 + # https://github.com/NixOS/nixpkgs/issues/185644 + # https://github.com/NixOS/nixpkgs/pull/197046 + # https://github.com/NixOS/nixpkgs/pull/241741#issuecomment-1624227485 + # https://github.com/ziglang/zig/issues/14281#issuecomment-1624220653 + + zig_default_flags = [ + "-Dcpu=baseline" + "-Doptimize=ReleaseSafe" + ]; + }; + + passthru = { + inherit zig; + }; + + meta = { + description = "A setup hook for using the Zig compiler in Nixpkgs"; + inherit (zig.meta) maintainers platforms broken; + }; +} ./setup-hook.sh diff --git a/pkgs/development/compilers/zig/0.11/setup-hook.sh b/pkgs/development/compilers/zig/0.11/setup-hook.sh new file mode 100644 index 0000000000000..689ebec8a3077 --- /dev/null +++ b/pkgs/development/compilers/zig/0.11/setup-hook.sh @@ -0,0 +1,70 @@ +# shellcheck shell=bash disable=SC2154,SC2086 + +readonly zigDefaultFlagsArray=(@zig_default_flags@) + +function zigSetGlobalCacheDir { + ZIG_GLOBAL_CACHE_DIR=$(mktemp -d) + export ZIG_GLOBAL_CACHE_DIR +} + +function zigBuildPhase { + runHook preBuild + + local flagsArray=( + "${zigDefaultFlagsArray[@]}" + $zigBuildFlags "${zigBuildFlagsArray[@]}" + ) + + echoCmd 'zig build flags' "${flagsArray[@]}" + zig build "${flagsArray[@]}" + + runHook postBuild +} + +function zigCheckPhase { + runHook preCheck + + local flagsArray=( + "${zigDefaultFlagsArray[@]}" + $zigCheckFlags "${zigCheckFlagsArray[@]}" + ) + + echoCmd 'zig check flags' "${flagsArray[@]}" + zig build test "${flagsArray[@]}" + + runHook postCheck +} + +function zigInstallPhase { + runHook preInstall + + local flagsArray=( + "${zigDefaultFlagsArray[@]}" + $zigBuildFlags "${zigBuildFlagsArray[@]}" + $zigInstallFlags "${zigInstallFlagsArray[@]}" + ) + + if [ -z "${dontAddPrefix-}" ]; then + # Zig does not recognize `--prefix=/dir/`, only `--prefix /dir/` + flagsArray+=("${prefixKey:---prefix}" "$prefix") + fi + + echoCmd 'zig install flags' "${flagsArray[@]}" + zig build install "${flagsArray[@]}" + + runHook postInstall +} + +addEnvHooks "$targetOffset" zigSetGlobalCacheDir + +if [ -z "${dontUseZigBuild-}" ] && [ -z "${buildPhase-}" ]; then + buildPhase=zigBuildPhase +fi + +if [ -z "${dontUseZigCheck-}" ] && [ -z "${checkPhase-}" ]; then + checkPhase=zigCheckPhase +fi + +if [ -z "${dontUseZigInstall-}" ] && [ -z "${installPhase-}" ]; then + installPhase=zigInstallPhase +fi diff --git a/pkgs/top-level/all-packages.nix b/pkgs/top-level/all-packages.nix index 3d2ac15f2d526..0d517efa12269 100644 --- a/pkgs/top-level/all-packages.nix +++ b/pkgs/top-level/all-packages.nix @@ -25062,7 +25062,7 @@ with pkgs; llvmPackages = llvmPackages_15; }; # requires a newer Apple SDK - zig_0_11 = darwin.apple_sdk_11_0.callPackage ../development/compilers/zig/0.11.nix { + zig_0_11 = darwin.apple_sdk_11_0.callPackage ../development/compilers/zig/0.11 { llvmPackages = llvmPackages_16; }; # requires a newer Apple SDK -- cgit 1.4.1 From 0b575c975923f0995b30f700e0933aea25aee522 Mon Sep 17 00:00:00 2001 From: Anderson Torres Date: Sun, 26 May 2024 23:59:32 -0300 Subject: zig_0_12: huge refactor - migrate it to a dedicated directory, zig/0.12 - can't migrate to by-name - because of undecipherable darwin.apple_sdk_11_0.callPackage - nixfmt - add a simple version test --- pkgs/development/compilers/zig/0.12.nix | 37 -------- pkgs/development/compilers/zig/0.12/default.nix | 103 ++++++++++++++++++++++ pkgs/development/compilers/zig/0.12/hook.nix | 47 ++++++++++ pkgs/development/compilers/zig/0.12/setup-hook.sh | 70 +++++++++++++++ pkgs/top-level/all-packages.nix | 2 +- 5 files changed, 221 insertions(+), 38 deletions(-) delete mode 100644 pkgs/development/compilers/zig/0.12.nix create mode 100644 pkgs/development/compilers/zig/0.12/default.nix create mode 100644 pkgs/development/compilers/zig/0.12/hook.nix create mode 100644 pkgs/development/compilers/zig/0.12/setup-hook.sh (limited to 'pkgs/development/compilers') diff --git a/pkgs/development/compilers/zig/0.12.nix b/pkgs/development/compilers/zig/0.12.nix deleted file mode 100644 index e234b600bc227..0000000000000 --- a/pkgs/development/compilers/zig/0.12.nix +++ /dev/null @@ -1,37 +0,0 @@ -{ lib -, stdenv -, fetchFromGitHub -, cmake -, llvmPackages -, libxml2 -, zlib -, coreutils -, callPackage -}@args: - -import ./generic.nix args { - version = "0.12.0"; - - hash = "sha256-RNZiUZtaKXoab5kFrDij6YCAospeVvlLWheTc3FGMks="; - - outputs = [ "out" "doc" ]; - - cmakeFlags = [ - # file RPATH_CHANGE could not write new RPATH - (lib.cmakeBool "CMAKE_SKIP_BUILD_RPATH" true) - - # always link against static build of LLVM - (lib.cmakeBool "ZIG_STATIC_LLVM" true) - - # ensure determinism in the compiler build - (lib.cmakeFeature "ZIG_TARGET_MCPU" "baseline") - ]; - - postBuild = '' - stage3/bin/zig run ../tools/docgen.zig -- ../doc/langref.html.in langref.html --zig $PWD/stage3/bin/zig - ''; - - postInstall = '' - install -Dm444 langref.html -t $doc/share/doc/zig-$version/html - ''; -} diff --git a/pkgs/development/compilers/zig/0.12/default.nix b/pkgs/development/compilers/zig/0.12/default.nix new file mode 100644 index 0000000000000..831366419a934 --- /dev/null +++ b/pkgs/development/compilers/zig/0.12/default.nix @@ -0,0 +1,103 @@ +{ + lib, + callPackage, + cmake, + coreutils, + fetchFromGitHub, + libxml2, + llvmPackages, + stdenv, + testers, + zlib, +}: + +stdenv.mkDerivation (finalAttrs: { + pname = "zig"; + version = "0.12.0"; + + src = fetchFromGitHub { + owner = "ziglang"; + repo = "zig"; + rev = finalAttrs.version; + hash = "sha256-RNZiUZtaKXoab5kFrDij6YCAospeVvlLWheTc3FGMks="; + }; + + nativeBuildInputs = [ + cmake + (lib.getDev llvmPackages.llvm) + ]; + + buildInputs = + [ + libxml2 + zlib + ] + ++ (with llvmPackages; [ + libclang + lld + llvm + ]); + + outputs = [ + "out" + "doc" + ]; + + cmakeFlags = [ + # file RPATH_CHANGE could not write new RPATH + (lib.cmakeBool "CMAKE_SKIP_BUILD_RPATH" true) + # ensure determinism in the compiler build + (lib.cmakeFeature "ZIG_TARGET_MCPU" "baseline") + # always link against static build of LLVM + (lib.cmakeBool "ZIG_STATIC_LLVM" true) + ]; + + env.ZIG_GLOBAL_CACHE_DIR = "$TMPDIR/zig-cache"; + + doInstallCheck = true; + + strictDeps = true; + + # Zig's build looks at /usr/bin/env to find dynamic linking info. This doesn't + # work in Nix's sandbox. Use env from our coreutils instead. + postPatch = '' + substituteInPlace lib/std/zig/system.zig \ + --replace "/usr/bin/env" "${lib.getExe' coreutils "env"}" + ''; + + postBuild = '' + stage3/bin/zig run ../tools/docgen.zig -- ../doc/langref.html.in langref.html --zig $PWD/stage3/bin/zig + ''; + + postInstall = '' + install -Dm444 langref.html -t $doc/share/doc/zig-${finalAttrs.version}/html + ''; + + installCheckPhase = '' + runHook preInstallCheck + + $out/bin/zig test --cache-dir "$TMPDIR/zig-test-cache" -I $src/test $src/test/behavior.zig + + runHook postInstallCheck + ''; + + passthru = { + hook = callPackage ./hook.nix { zig = finalAttrs.finalPackage; }; + tests = { + version = testers.testVersion { + package = finalAttrs.finalPackage; + command = "zig version"; + }; + }; + }; + + meta = { + description = "General-purpose programming language and toolchain for maintaining robust, optimal, and reusable software"; + changelog = "https://ziglang.org/download/${finalAttrs.version}/release-notes.html"; + homepage = "https://ziglang.org/"; + license = lib.licenses.mit; + mainProgram = "zig"; + maintainers = with lib.maintainers; [ andrewrk ] ++ lib.teams.zig.members; + platforms = lib.platforms.unix; + }; +}) diff --git a/pkgs/development/compilers/zig/0.12/hook.nix b/pkgs/development/compilers/zig/0.12/hook.nix new file mode 100644 index 0000000000000..294aa851ce0d3 --- /dev/null +++ b/pkgs/development/compilers/zig/0.12/hook.nix @@ -0,0 +1,47 @@ +{ + lib, + makeSetupHook, + zig, +}: + +makeSetupHook { + name = "zig-hook"; + + propagatedBuildInputs = [ zig ]; + + substitutions = { + # This zig_default_flags below is meant to avoid CPU feature impurity in + # Nixpkgs. However, this flagset is "unstable": it is specifically meant to + # be controlled by the upstream development team - being up to that team + # exposing or not that flags to the outside (especially the package manager + # teams). + + # Because of this hurdle, @andrewrk from Zig Software Foundation proposed + # some solutions for this issue. Hopefully they will be implemented in + # future releases of Zig. When this happens, this flagset should be + # revisited accordingly. + + # Below are some useful links describing the discovery process of this 'bug' + # in Nixpkgs: + + # https://github.com/NixOS/nixpkgs/issues/169461 + # https://github.com/NixOS/nixpkgs/issues/185644 + # https://github.com/NixOS/nixpkgs/pull/197046 + # https://github.com/NixOS/nixpkgs/pull/241741#issuecomment-1624227485 + # https://github.com/ziglang/zig/issues/14281#issuecomment-1624220653 + + zig_default_flags = [ + "-Dcpu=baseline" + "--release=safe" + ]; + }; + + passthru = { + inherit zig; + }; + + meta = { + description = "A setup hook for using the Zig compiler in Nixpkgs"; + inherit (zig.meta) maintainers platforms broken; + }; +} ./setup-hook.sh diff --git a/pkgs/development/compilers/zig/0.12/setup-hook.sh b/pkgs/development/compilers/zig/0.12/setup-hook.sh new file mode 100644 index 0000000000000..689ebec8a3077 --- /dev/null +++ b/pkgs/development/compilers/zig/0.12/setup-hook.sh @@ -0,0 +1,70 @@ +# shellcheck shell=bash disable=SC2154,SC2086 + +readonly zigDefaultFlagsArray=(@zig_default_flags@) + +function zigSetGlobalCacheDir { + ZIG_GLOBAL_CACHE_DIR=$(mktemp -d) + export ZIG_GLOBAL_CACHE_DIR +} + +function zigBuildPhase { + runHook preBuild + + local flagsArray=( + "${zigDefaultFlagsArray[@]}" + $zigBuildFlags "${zigBuildFlagsArray[@]}" + ) + + echoCmd 'zig build flags' "${flagsArray[@]}" + zig build "${flagsArray[@]}" + + runHook postBuild +} + +function zigCheckPhase { + runHook preCheck + + local flagsArray=( + "${zigDefaultFlagsArray[@]}" + $zigCheckFlags "${zigCheckFlagsArray[@]}" + ) + + echoCmd 'zig check flags' "${flagsArray[@]}" + zig build test "${flagsArray[@]}" + + runHook postCheck +} + +function zigInstallPhase { + runHook preInstall + + local flagsArray=( + "${zigDefaultFlagsArray[@]}" + $zigBuildFlags "${zigBuildFlagsArray[@]}" + $zigInstallFlags "${zigInstallFlagsArray[@]}" + ) + + if [ -z "${dontAddPrefix-}" ]; then + # Zig does not recognize `--prefix=/dir/`, only `--prefix /dir/` + flagsArray+=("${prefixKey:---prefix}" "$prefix") + fi + + echoCmd 'zig install flags' "${flagsArray[@]}" + zig build install "${flagsArray[@]}" + + runHook postInstall +} + +addEnvHooks "$targetOffset" zigSetGlobalCacheDir + +if [ -z "${dontUseZigBuild-}" ] && [ -z "${buildPhase-}" ]; then + buildPhase=zigBuildPhase +fi + +if [ -z "${dontUseZigCheck-}" ] && [ -z "${checkPhase-}" ]; then + checkPhase=zigCheckPhase +fi + +if [ -z "${dontUseZigInstall-}" ] && [ -z "${installPhase-}" ]; then + installPhase=zigInstallPhase +fi diff --git a/pkgs/top-level/all-packages.nix b/pkgs/top-level/all-packages.nix index 0d517efa12269..d80f4e4522bb0 100644 --- a/pkgs/top-level/all-packages.nix +++ b/pkgs/top-level/all-packages.nix @@ -25066,7 +25066,7 @@ with pkgs; llvmPackages = llvmPackages_16; }; # requires a newer Apple SDK - zig_0_12 = darwin.apple_sdk_11_0.callPackage ../development/compilers/zig/0.12.nix { + zig_0_12 = darwin.apple_sdk_11_0.callPackage ../development/compilers/zig/0.12 { llvmPackages = llvmPackages_17; }; zig = zig_0_12; -- cgit 1.4.1