diff options
Diffstat (limited to 'pkgs/development/compilers/zig/0.9')
5 files changed, 317 insertions, 0 deletions
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 <contact@leroycepearson.dev> +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 <kubkon@jakubkonka.com> +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..1d3439b1af620 --- /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 = "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 |