From 55fcb63b2c3c245f5f9d1aafa68671c4d6304881 Mon Sep 17 00:00:00 2001 From: Tristan Ross Date: Thu, 6 Jun 2024 16:58:13 -0700 Subject: [PATCH 1/2] zig_0_13: init --- .../compilers/zig/0.13/default.nix | 105 ++++++++++++++++++ pkgs/development/compilers/zig/0.13/hook.nix | 32 ++++++ .../compilers/zig/0.13/setup-hook.sh | 70 ++++++++++++ pkgs/top-level/all-packages.nix | 4 + 4 files changed, 211 insertions(+) create mode 100644 pkgs/development/compilers/zig/0.13/default.nix create mode 100644 pkgs/development/compilers/zig/0.13/hook.nix create mode 100644 pkgs/development/compilers/zig/0.13/setup-hook.sh diff --git a/pkgs/development/compilers/zig/0.13/default.nix b/pkgs/development/compilers/zig/0.13/default.nix new file mode 100644 index 000000000000..bdc01ba47f9f --- /dev/null +++ b/pkgs/development/compilers/zig/0.13/default.nix @@ -0,0 +1,105 @@ +{ + lib, + callPackage, + cmake, + coreutils, + fetchFromGitHub, + libxml2, + llvmPackages, + stdenv, + testers, + zlib, +}: + +stdenv.mkDerivation (finalAttrs: { + pname = "zig"; + version = "0.13.0"; + + src = fetchFromGitHub { + owner = "ziglang"; + repo = "zig"; + rev = finalAttrs.version; + hash = "sha256-5qSiTq+UWGOwjDVZMIrAt2cDKHkyNPBSAEjpRQUByFM="; + }; + + 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 breaks zig when clang is being used. + # https://github.com/NixOS/nixpkgs/issues/317055#issuecomment-2148438395 + strictDeps = !stdenv.cc.isClang; + + # 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 build langref + ''; + + postInstall = '' + install -Dm444 ../zig-out/doc/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.13/hook.nix b/pkgs/development/compilers/zig/0.13/hook.nix new file mode 100644 index 000000000000..a56b1a21e961 --- /dev/null +++ b/pkgs/development/compilers/zig/0.13/hook.nix @@ -0,0 +1,32 @@ +{ + 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). + 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.13/setup-hook.sh b/pkgs/development/compilers/zig/0.13/setup-hook.sh new file mode 100644 index 000000000000..689ebec8a307 --- /dev/null +++ b/pkgs/development/compilers/zig/0.13/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 06a9b63930ae..4b5ad183e6af 100644 --- a/pkgs/top-level/all-packages.nix +++ b/pkgs/top-level/all-packages.nix @@ -25038,6 +25038,10 @@ with pkgs; zig_0_12 = darwin.apple_sdk_11_0.callPackage ../development/compilers/zig/0.12 { llvmPackages = llvmPackages_17; }; + # requires a newer Apple SDK + zig_0_13 = darwin.apple_sdk_11_0.callPackage ../development/compilers/zig/0.13 { + llvmPackages = llvmPackages_18; + }; zig = zig_0_12; zimlib = callPackage ../development/libraries/zimlib { }; From f51fda9caba6192c87d67bcb774bc93e17e23f79 Mon Sep 17 00:00:00 2001 From: Tristan Ross Date: Thu, 6 Jun 2024 18:30:25 -0700 Subject: [PATCH 2/2] zig: 0.12 -> 0.13 --- pkgs/top-level/all-packages.nix | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkgs/top-level/all-packages.nix b/pkgs/top-level/all-packages.nix index 4b5ad183e6af..217171e3b156 100644 --- a/pkgs/top-level/all-packages.nix +++ b/pkgs/top-level/all-packages.nix @@ -25042,7 +25042,7 @@ with pkgs; zig_0_13 = darwin.apple_sdk_11_0.callPackage ../development/compilers/zig/0.13 { llvmPackages = llvmPackages_18; }; - zig = zig_0_12; + zig = zig_0_13; zimlib = callPackage ../development/libraries/zimlib { };