diff options
| author | Hilton Chain <hako@ultrarare.space> | 2024-11-19 20:27:49 +0800 |
|---|---|---|
| committer | Hilton Chain <hako@ultrarare.space> | 2024-12-31 10:56:34 +0800 |
| commit | 3ef8c9307ce10ca59af9bcf84161a88fd08310e7 (patch) | |
| tree | e778ed993ab65c9112bb45672dcd79fe018fb0a2 | |
| parent | 05bbf0ec43abcc8c49e1e49e95baca24f6fea950 (diff) | |
gnu: zig: Build reproducibly.
* guix/build/zig-utils.scm: New file.
* Makefile.am (MODULES): Add it.
* guix/build-system/zig.scm (%zig-build-system-modules): Add it.
(zig-build,zig-cross-build): Add #:zig-build-target.
* guix/build/zig-build-system.scm (zig-target): Move to (guix build zig-utils).
(configure): Move to (guix build zig-utils) as zig-configure.
(build): Replace target with zig-build-target.
(%standard-phases): Adjust accordingly.
* gnu/packages/zig.scm (zig-0.9)[arguments]<#:imported-modules,#:modules>: Add
zig-build-system modules.
<#:configure-flags>: Set ZIG_TARGET_TRIPLE for native builds too.
Move applicable flags from zig-0.10 here.
<#:phases>: Apply 'zig-configure.
Unset ZIG_LIBC in 'check.
Remove 'set-cache-dir.
(zig-0.10)[arguments]<#:configure-flags>: Adjust accordingly.
(zig-0.10.0-610)[arguments]<#:configure-flags>: Remove ZIG_TARGET_TRIPLE.
| -rw-r--r-- | Makefile.am | 1 | ||||
| -rw-r--r-- | gnu/packages/zig.scm | 52 | ||||
| -rw-r--r-- | guix/build-system/zig.scm | 6 | ||||
| -rw-r--r-- | guix/build/zig-build-system.scm | 52 | ||||
| -rw-r--r-- | guix/build/zig-utils.scm | 79 |
5 files changed, 124 insertions, 66 deletions
diff --git a/Makefile.am b/Makefile.am index 908c48b4efb..403808b0f65 100644 --- a/Makefile.am +++ b/Makefile.am | |||
| @@ -284,6 +284,7 @@ MODULES = \ | |||
| 284 | guix/build/bournish.scm \ | 284 | guix/build/bournish.scm \ |
| 285 | guix/build/qt-utils.scm \ | 285 | guix/build/qt-utils.scm \ |
| 286 | guix/build/zig-build-system.scm \ | 286 | guix/build/zig-build-system.scm \ |
| 287 | guix/build/zig-utils.scm \ | ||
| 287 | guix/build/make-bootstrap.scm \ | 288 | guix/build/make-bootstrap.scm \ |
| 288 | guix/build/toml.scm \ | 289 | guix/build/toml.scm \ |
| 289 | guix/search-paths.scm \ | 290 | guix/search-paths.scm \ |
diff --git a/gnu/packages/zig.scm b/gnu/packages/zig.scm index 62e224b760a..99335c54048 100644 --- a/gnu/packages/zig.scm +++ b/gnu/packages/zig.scm | |||
| @@ -23,6 +23,7 @@ | |||
| 23 | (define-module (gnu packages zig) | 23 | (define-module (gnu packages zig) |
| 24 | #:use-module (guix gexp) | 24 | #:use-module (guix gexp) |
| 25 | #:use-module (guix packages) | 25 | #:use-module (guix packages) |
| 26 | #:use-module (guix platform) | ||
| 26 | #:use-module (guix utils) | 27 | #:use-module (guix utils) |
| 27 | #:use-module (guix download) | 28 | #:use-module (guix download) |
| 28 | #:use-module (guix git-download) | 29 | #:use-module (guix git-download) |
| @@ -87,11 +88,24 @@ | |||
| 87 | (build-system cmake-build-system) | 88 | (build-system cmake-build-system) |
| 88 | (arguments | 89 | (arguments |
| 89 | (list | 90 | (list |
| 91 | #:imported-modules | ||
| 92 | (cons '(guix build zig-utils) | ||
| 93 | %cmake-build-system-modules) | ||
| 94 | #:modules | ||
| 95 | (cons '(guix build zig-utils) | ||
| 96 | '((guix build cmake-build-system) | ||
| 97 | (guix build utils))) | ||
| 90 | #:configure-flags | 98 | #:configure-flags |
| 91 | #~(list #$@(if (%current-target-system) | 99 | #~(list (string-append "-DZIG_LIB_DIR=" #$output "/lib/zig") |
| 92 | (list (string-append "-DZIG_TARGET_TRIPLE=" | 100 | "-DZIG_TARGET_MCPU=baseline" |
| 93 | (%current-target-system))) | 101 | (string-append |
| 94 | '())) | 102 | "-DZIG_TARGET_TRIPLE=" |
| 103 | (zig-target | ||
| 104 | #$(platform-target | ||
| 105 | (lookup-platform-by-target-or-system | ||
| 106 | (or (%current-target-system) | ||
| 107 | (%current-system)))))) | ||
| 108 | "-DZIG_USE_LLVM_CONFIG=ON") | ||
| 95 | #:out-of-source? #f ; for tests | 109 | #:out-of-source? #f ; for tests |
| 96 | ;; There are too many unclear test failures. | 110 | ;; There are too many unclear test failures. |
| 97 | #:tests? (not (or (target-riscv64?) | 111 | #:tests? (not (or (target-riscv64?) |
| @@ -114,15 +128,14 @@ | |||
| 114 | ;; Is this symbol x86 only in glibc? | 128 | ;; Is this symbol x86 only in glibc? |
| 115 | ((".*link_static_lib_as_system_lib.*") ""))))) | 129 | ((".*link_static_lib_as_system_lib.*") ""))))) |
| 116 | '()) | 130 | '()) |
| 117 | (add-after 'configure 'set-cache-dir | 131 | (add-before 'configure 'zig-configure zig-configure) |
| 118 | (lambda _ | ||
| 119 | ;; Set cache dir, otherwise Zig looks for `$HOME/.cache'. | ||
| 120 | (setenv "ZIG_GLOBAL_CACHE_DIR" | ||
| 121 | (string-append (getcwd) "/zig-cache")))) | ||
| 122 | (delete 'check) | 132 | (delete 'check) |
| 123 | (add-after 'install 'check | 133 | (add-after 'install 'check |
| 124 | (lambda* (#:key tests? #:allow-other-keys) | 134 | (lambda* (#:key tests? #:allow-other-keys) |
| 125 | (when tests? | 135 | (when tests? |
| 136 | ;; error(libc_installation): msvc_lib_dir may not be empty for | ||
| 137 | ;; windows-msvc. | ||
| 138 | (unsetenv "ZIG_LIBC") | ||
| 126 | (invoke (string-append #$output "/bin/zig") | 139 | (invoke (string-append #$output "/bin/zig") |
| 127 | ;; Testing the standard library takes >7.5GB RAM, and | 140 | ;; Testing the standard library takes >7.5GB RAM, and |
| 128 | ;; will fail if it is OOM-killed. The 'test-toolchain' | 141 | ;; will fail if it is OOM-killed. The 'test-toolchain' |
| @@ -222,20 +235,13 @@ toolchain. Among other features it provides | |||
| 222 | ((#:tests? _ #t) | 235 | ((#:tests? _ #t) |
| 223 | (not (%current-target-system))) | 236 | (not (%current-target-system))) |
| 224 | ((#:configure-flags flags ''()) | 237 | ((#:configure-flags flags ''()) |
| 225 | #~(cons* "-DZIG_TARGET_MCPU=baseline" | 238 | #~(cons "-DZIG_SHARED_LLVM=ON" |
| 226 | "-DZIG_SHARED_LLVM=ON" | 239 | #$flags)) |
| 227 | (string-append "-DZIG_LIB_DIR=" #$output "/lib/zig") | ||
| 228 | #$flags)) | ||
| 229 | ((#:phases phases '%standard-phases) | 240 | ((#:phases phases '%standard-phases) |
| 230 | #~(modify-phases #$phases | 241 | #~(modify-phases #$phases |
| 231 | #$@(if (target-riscv64?) | 242 | #$@(if (target-riscv64?) |
| 232 | `((delete 'adjust-tests)) | 243 | `((delete 'adjust-tests)) |
| 233 | '()) | 244 | '()) |
| 234 | (add-after 'unpack 'set-CC | ||
| 235 | (lambda _ | ||
| 236 | ;; Set CC, since the stage 2 zig relies on it to find the libc | ||
| 237 | ;; installation, and otherwise silently links against its own. | ||
| 238 | (setenv "CC" #$(cc-for-target)))) | ||
| 239 | (add-after 'patch-source-shebangs 'patch-more-shebangs | 245 | (add-after 'patch-source-shebangs 'patch-more-shebangs |
| 240 | (lambda* (#:key inputs #:allow-other-keys) | 246 | (lambda* (#:key inputs #:allow-other-keys) |
| 241 | ;; Zig uses information about an ELF file to determine the | 247 | ;; Zig uses information about an ELF file to determine the |
| @@ -245,6 +251,9 @@ toolchain. Among other features it provides | |||
| 245 | (replace 'check | 251 | (replace 'check |
| 246 | (lambda* (#:key tests? #:allow-other-keys) | 252 | (lambda* (#:key tests? #:allow-other-keys) |
| 247 | (when tests? | 253 | (when tests? |
| 254 | ;; error(libc_installation): msvc_lib_dir may not be empty for | ||
| 255 | ;; windows-msvc. | ||
| 256 | (unsetenv "ZIG_LIBC") | ||
| 248 | (invoke (string-append #$output "/bin/zig") | 257 | (invoke (string-append #$output "/bin/zig") |
| 249 | "build" "test" | 258 | "build" "test" |
| 250 | ;; We're not testing the compiler bootstrap chain. | 259 | ;; We're not testing the compiler bootstrap chain. |
| @@ -314,6 +323,13 @@ toolchain. Among other features it provides | |||
| 314 | (substitute-keyword-arguments (package-arguments base) | 323 | (substitute-keyword-arguments (package-arguments base) |
| 315 | ;; Patch for fixing RUNPATH not applied to intermediate versions. | 324 | ;; Patch for fixing RUNPATH not applied to intermediate versions. |
| 316 | ((#:validate-runpath? _ #t) #f) | 325 | ((#:validate-runpath? _ #t) #f) |
| 326 | ;; Patch for cross-compilation not applied to intermediate versions. | ||
| 327 | ((#:modules modules '()) | ||
| 328 | (cons '(srfi srfi-1) modules)) | ||
| 329 | ((#:configure-flags flags ''()) | ||
| 330 | #~(filter (lambda (flag) | ||
| 331 | (not (string-contains flag "ZIG_TARGET_TRIPLE"))) | ||
| 332 | #$flags)) | ||
| 317 | ;; Disable tests for intermediate versions. | 333 | ;; Disable tests for intermediate versions. |
| 318 | ((#:tests? _ #t) #f) | 334 | ((#:tests? _ #t) #f) |
| 319 | ((#:phases phases '%standard-phases) | 335 | ((#:phases phases '%standard-phases) |
diff --git a/guix/build-system/zig.scm b/guix/build-system/zig.scm index ad8a96b6079..82df75729c6 100644 --- a/guix/build-system/zig.scm +++ b/guix/build-system/zig.scm | |||
| @@ -23,6 +23,7 @@ | |||
| 23 | #:use-module (guix gexp) | 23 | #:use-module (guix gexp) |
| 24 | #:use-module (guix monads) | 24 | #:use-module (guix monads) |
| 25 | #:use-module (guix packages) | 25 | #:use-module (guix packages) |
| 26 | #:use-module (guix platform) | ||
| 26 | #:use-module (guix build-system) | 27 | #:use-module (guix build-system) |
| 27 | #:use-module (guix build-system gnu) | 28 | #:use-module (guix build-system gnu) |
| 28 | #:use-module (ice-9 match) | 29 | #:use-module (ice-9 match) |
| @@ -39,6 +40,7 @@ | |||
| 39 | (define %zig-build-system-modules | 40 | (define %zig-build-system-modules |
| 40 | ;; Build-side modules imported by default. | 41 | ;; Build-side modules imported by default. |
| 41 | `((guix build zig-build-system) | 42 | `((guix build zig-build-system) |
| 43 | (guix build zig-utils) | ||
| 42 | ,@%default-gnu-imported-modules)) | 44 | ,@%default-gnu-imported-modules)) |
| 43 | 45 | ||
| 44 | (define* (zig-build name inputs | 46 | (define* (zig-build name inputs |
| @@ -67,6 +69,9 @@ | |||
| 67 | #:system #$system | 69 | #:system #$system |
| 68 | #:test-target #$test-target | 70 | #:test-target #$test-target |
| 69 | #:zig-build-flags #$zig-build-flags | 71 | #:zig-build-flags #$zig-build-flags |
| 72 | ;; For reproducibility. | ||
| 73 | #:zig-build-target #$(platform-target | ||
| 74 | (lookup-platform-by-system system)) | ||
| 70 | #:zig-test-flags #$zig-test-flags | 75 | #:zig-test-flags #$zig-test-flags |
| 71 | #:zig-release-type #$zig-release-type | 76 | #:zig-release-type #$zig-release-type |
| 72 | #:tests? #$tests? | 77 | #:tests? #$tests? |
| @@ -137,6 +142,7 @@ | |||
| 137 | search-path-specification->sexp | 142 | search-path-specification->sexp |
| 138 | native-search-paths) | 143 | native-search-paths) |
| 139 | #:zig-build-flags #$zig-build-flags | 144 | #:zig-build-flags #$zig-build-flags |
| 145 | #:zig-build-target #$target | ||
| 140 | #:zig-test-flags #$zig-test-flags | 146 | #:zig-test-flags #$zig-test-flags |
| 141 | #:zig-release-type #$zig-release-type | 147 | #:zig-release-type #$zig-release-type |
| 142 | #:zig-destdir #$zig-destdir | 148 | #:zig-destdir #$zig-destdir |
diff --git a/guix/build/zig-build-system.scm b/guix/build/zig-build-system.scm index 9959a95ce40..75ebdb7ca90 100644 --- a/guix/build/zig-build-system.scm +++ b/guix/build/zig-build-system.scm | |||
| @@ -20,6 +20,7 @@ | |||
| 20 | (define-module (guix build zig-build-system) | 20 | (define-module (guix build zig-build-system) |
| 21 | #:use-module ((guix build gnu-build-system) #:prefix gnu:) | 21 | #:use-module ((guix build gnu-build-system) #:prefix gnu:) |
| 22 | #:use-module (guix build utils) | 22 | #:use-module (guix build utils) |
| 23 | #:use-module (guix build zig-utils) | ||
| 23 | #:use-module (ice-9 popen) | 24 | #:use-module (ice-9 popen) |
| 24 | #:use-module (ice-9 rdelim) | 25 | #:use-module (ice-9 rdelim) |
| 25 | #:use-module (ice-9 ftw) | 26 | #:use-module (ice-9 ftw) |
| @@ -33,55 +34,12 @@ | |||
| 33 | 34 | ||
| 34 | ;; Interesting guide here: | 35 | ;; Interesting guide here: |
| 35 | ;; https://github.com/riverwm/river/blob/master/PACKAGING.md | 36 | ;; https://github.com/riverwm/river/blob/master/PACKAGING.md |
| 36 | (define global-cache-dir "zig-cache") | ||
| 37 | |||
| 38 | (define* (configure #:key inputs target #:allow-other-keys) | ||
| 39 | ;; Set cache dir, otherwise Zig looks for `$HOME/.cache'. | ||
| 40 | (setenv "ZIG_GLOBAL_CACHE_DIR" global-cache-dir) | ||
| 41 | |||
| 42 | (setenv "PKG_CONFIG" | ||
| 43 | (if target | ||
| 44 | (string-append target "-pkg-config") | ||
| 45 | "pkg-config")) | ||
| 46 | |||
| 47 | ;; Libc paths for target. | ||
| 48 | (let ((libc (assoc-ref inputs (if target "cross-libc" "libc"))) | ||
| 49 | (port (open-file "/tmp/guix-zig-libc-paths" "w" #:encoding "utf8"))) | ||
| 50 | (display | ||
| 51 | (string-append "\ | ||
| 52 | include_dir=" libc "/include | ||
| 53 | sys_include_dir=" libc "/include | ||
| 54 | crt_dir=" libc "/lib | ||
| 55 | msvc_lib_dir= | ||
| 56 | kernel32_lib_dir= | ||
| 57 | gcc_dir=") | ||
| 58 | port) | ||
| 59 | (close-port port)) | ||
| 60 | (setenv "ZIG_LIBC" "/tmp/guix-zig-libc-paths")) | ||
| 61 | |||
| 62 | (define (zig-target target) | ||
| 63 | (cond | ||
| 64 | ((string=? target "i586-pc-gnu") | ||
| 65 | "x86-hurd-gnu") | ||
| 66 | ((string=? target "i686-linux-gnu") | ||
| 67 | "x86-linux-gnu") | ||
| 68 | ((string=? target "i686-w64-mingw32") | ||
| 69 | "x86-windows-gnu") | ||
| 70 | ((string=? target "mips64el-linux-gnu") | ||
| 71 | "mips64el-linux-gnuabi64") | ||
| 72 | ((string=? target "powerpc-linux-gnu") | ||
| 73 | "powerpc-linux-gnueabi") | ||
| 74 | ((string=? target "x86_64-pc-gnu") | ||
| 75 | "x86_64-hurd-gnu") | ||
| 76 | ((string=? target "x86_64-w64-mingw32") | ||
| 77 | "x86_64-windows-gnu") | ||
| 78 | (else target))) | ||
| 79 | 37 | ||
| 80 | (define* (build #:key | 38 | (define* (build #:key |
| 81 | zig-build-flags | 39 | zig-build-flags |
| 40 | zig-build-target | ||
| 82 | zig-release-type ;; "safe", "fast" or "small" empty for a | 41 | zig-release-type ;; "safe", "fast" or "small" empty for a |
| 83 | ;; debug build" | 42 | ;; debug build" |
| 84 | target | ||
| 85 | #:allow-other-keys) | 43 | #:allow-other-keys) |
| 86 | "Build a given Zig package." | 44 | "Build a given Zig package." |
| 87 | 45 | ||
| @@ -91,9 +49,7 @@ gcc_dir=") | |||
| 91 | "--prefix-lib-dir" "lib" | 49 | "--prefix-lib-dir" "lib" |
| 92 | "--prefix-exe-dir" "bin" | 50 | "--prefix-exe-dir" "bin" |
| 93 | "--prefix-include-dir" "include" | 51 | "--prefix-include-dir" "include" |
| 94 | ,@(if target | 52 | ,(string-append "-Dtarget=" (zig-target zig-build-target)) |
| 95 | (list (string-append "-Dtarget=" (zig-target target))) | ||
| 96 | '()) | ||
| 97 | ,@(if zig-release-type | 53 | ,@(if zig-release-type |
| 98 | (list (string-append "-Drelease-" zig-release-type)) | 54 | (list (string-append "-Drelease-" zig-release-type)) |
| 99 | '()) | 55 | '()) |
| @@ -125,7 +81,7 @@ gcc_dir=") | |||
| 125 | (define %standard-phases | 81 | (define %standard-phases |
| 126 | (modify-phases gnu:%standard-phases | 82 | (modify-phases gnu:%standard-phases |
| 127 | (delete 'bootstrap) | 83 | (delete 'bootstrap) |
| 128 | (replace 'configure configure) | 84 | (replace 'configure zig-configure) |
| 129 | (replace 'build build) | 85 | (replace 'build build) |
| 130 | (replace 'check check) | 86 | (replace 'check check) |
| 131 | (replace 'install install))) | 87 | (replace 'install install))) |
diff --git a/guix/build/zig-utils.scm b/guix/build/zig-utils.scm new file mode 100644 index 00000000000..67480df4587 --- /dev/null +++ b/guix/build/zig-utils.scm | |||
| @@ -0,0 +1,79 @@ | |||
| 1 | ;;; GNU Guix --- Functional package management for GNU | ||
| 2 | ;;; Copyright © 2024 Hilton Chain <hako@ultrarare.space> | ||
| 3 | ;;; Copyright © 2024 Efraim Flashner <efraim@flashner.co.il> | ||
| 4 | ;;; | ||
| 5 | ;;; This file is part of GNU Guix. | ||
| 6 | ;;; | ||
| 7 | ;;; GNU Guix is free software; you can redistribute it and/or modify it | ||
| 8 | ;;; under the terms of the GNU General Public License as published by | ||
| 9 | ;;; the Free Software Foundation; either version 3 of the License, or (at | ||
| 10 | ;;; your option) any later version. | ||
| 11 | ;;; | ||
| 12 | ;;; GNU Guix is distributed in the hope that it will be useful, but | ||
| 13 | ;;; WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 14 | ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 15 | ;;; GNU General Public License for more details. | ||
| 16 | ;;; | ||
| 17 | ;;; You should have received a copy of the GNU General Public License | ||
| 18 | ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>. | ||
| 19 | |||
| 20 | (define-module (guix build zig-utils) | ||
| 21 | #:use-module (guix build utils) | ||
| 22 | #:export (zig-configure | ||
| 23 | zig-target)) | ||
| 24 | |||
| 25 | ;;; | ||
| 26 | ;;; Common procedures for zig and zig-build-system. | ||
| 27 | ;;; | ||
| 28 | |||
| 29 | (define* (zig-configure #:key inputs target #:allow-other-keys) | ||
| 30 | ;; Set cache dir, otherwise Zig looks for `$HOME/.cache'. | ||
| 31 | (setenv "ZIG_GLOBAL_CACHE_DIR" "/tmp/zig-cache") | ||
| 32 | (setenv "ZIG_LOCAL_CACHE_DIR" "/tmp/zig-cache") | ||
| 33 | |||
| 34 | ;; XXX: Required for unpatched intermediade versions? | ||
| 35 | ;; Set CC, since the stage 2 zig relies on it to find the libc | ||
| 36 | ;; installation, and otherwise silently links against its own. | ||
| 37 | (setenv "CC" | ||
| 38 | (if target | ||
| 39 | (string-append target "-gcc") | ||
| 40 | "gcc")) | ||
| 41 | |||
| 42 | (setenv "PKG_CONFIG" | ||
| 43 | (if target | ||
| 44 | (string-append target "-pkg-config") | ||
| 45 | "pkg-config")) | ||
| 46 | |||
| 47 | ;; Libc paths for target. | ||
| 48 | (let ((libc (assoc-ref inputs (if target "cross-libc" "libc"))) | ||
| 49 | (port (open-file "/tmp/guix-zig-libc-paths" "w" #:encoding "utf8"))) | ||
| 50 | (when libc | ||
| 51 | (display | ||
| 52 | (string-append "\ | ||
| 53 | include_dir=" libc "/include | ||
| 54 | sys_include_dir=" libc "/include | ||
| 55 | crt_dir=" libc "/lib | ||
| 56 | msvc_lib_dir= | ||
| 57 | kernel32_lib_dir= | ||
| 58 | gcc_dir=") | ||
| 59 | port) | ||
| 60 | (setenv "ZIG_LIBC" "/tmp/guix-zig-libc-paths")) | ||
| 61 | (close-port port))) | ||
| 62 | |||
| 63 | (define (zig-target target) | ||
| 64 | (cond | ||
| 65 | ((string=? target "i586-pc-gnu") | ||
| 66 | "x86-hurd-gnu") | ||
| 67 | ((string=? target "i686-linux-gnu") | ||
| 68 | "x86-linux-gnu") | ||
| 69 | ((string=? target "i686-w64-mingw32") | ||
| 70 | "x86-windows-gnu") | ||
| 71 | ((string=? target "mips64el-linux-gnu") | ||
| 72 | "mips64el-linux-gnuabi64") | ||
| 73 | ((string=? target "powerpc-linux-gnu") | ||
| 74 | "powerpc-linux-gnueabi") | ||
| 75 | ((string=? target "x86_64-pc-gnu") | ||
| 76 | "x86_64-hurd-gnu") | ||
| 77 | ((string=? target "x86_64-w64-mingw32") | ||
| 78 | "x86_64-windows-gnu") | ||
| 79 | (else target))) | ||
