guix-commits
[Top][All Lists]
Advanced

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

60/60: gnu: zig: Build reproducibly.


From: guix-commits
Subject: 60/60: gnu: zig: Build reproducibly.
Date: Tue, 19 Nov 2024 08:13:06 -0500 (EST)

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

commit c75fd5e8e34b30954cd9901d721a698b360e4222
Author: Hilton Chain <hako@ultrarare.space>
AuthorDate: Tue Nov 19 20:27:49 2024 +0800

    gnu: zig: Build reproducibly.
    
    * gnu/packages/patches/zig-0.10-cmake-support-libc-paths-file.patch: New 
file.
    * gnu/packages/patches/zig-0.11-cmake-support-libc-paths-file.patch: New 
file.
    * gnu/packages/patches/zig-0.12-cmake-support-libc-paths-file.patch: New 
file.
    * gnu/packages/patches/zig-0.13-cmake-support-libc-paths-file.patch: New 
file.
    * gnu/local.mk (dist_patch_DATA): Regisiter them.
    * guix/build/zig-utils.scm; New file.
    * Makefile.am (MODULES): Add it.
    * guix/build-system/zig.scm (%zig-build-system-modules): Add it.
    * guix/build/zig-build-system.scm (configure,zig-target): Move to zig-utils
    and export.
    * gnu/packages/zig.scm (zig-0.9)[arguments]: 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.
    Remove 'set-cache-dir.
    (zig-0.10)[source]: Add patch.
    [arguments]<#:configure-flags>: Adjust accordingly.
    (zig-0.10.0-610)[arguments]<#:configure-flags>: Remove ZIG_TARGET_TRIPLE.
    (zig-0.11,zig-0.12,zig-0.13): Add patches.
    
    Change-Id: Ie5d7d249ae7f199409136bf201d95cad2dc7a781
---
 Makefile.am                                        |  1 +
 gnu/local.mk                                       |  4 ++
 .../zig-0.10-cmake-support-libc-paths-file.patch   | 33 ++++++++++++
 .../zig-0.11-cmake-support-libc-paths-file.patch   | 33 ++++++++++++
 .../zig-0.12-cmake-support-libc-paths-file.patch   | 33 ++++++++++++
 .../zig-0.13-cmake-support-libc-paths-file.patch   | 33 ++++++++++++
 gnu/packages/zig.scm                               | 49 +++++++++++------
 guix/build-system/zig.scm                          |  1 +
 guix/build/zig-build-system.scm                    | 38 +-------------
 guix/build/zig-utils.scm                           | 61 ++++++++++++++++++++++
 10 files changed, 233 insertions(+), 53 deletions(-)

diff --git a/Makefile.am b/Makefile.am
index e94ba87797..0e4883b6bb 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -284,6 +284,7 @@ MODULES =                                   \
   guix/build/bournish.scm                      \
   guix/build/qt-utils.scm                      \
   guix/build/zig-build-system.scm              \
+  guix/build/zig-utils.scm                     \
   guix/build/make-bootstrap.scm                        \
   guix/search-paths.scm                                \
   guix/packages.scm                            \
diff --git a/gnu/local.mk b/gnu/local.mk
index 66e933468f..31c8d0f422 100644
--- a/gnu/local.mk
+++ b/gnu/local.mk
@@ -2371,6 +2371,7 @@ dist_patch_DATA =                                         
\
   %D%/packages/patches/zig-0.9-use-baseline-cpu-by-default.patch       \
   %D%/packages/patches/zig-0.9-use-system-paths.patch          \
   %D%/packages/patches/zig-0.10-build-respect-PKG_CONFIG-env-var.patch \
+  %D%/packages/patches/zig-0.10-cmake-support-libc-paths-file.patch    \
   %D%/packages/patches/zig-0.10-fix-runpath.patch              \
   %D%/packages/patches/zig-0.10-use-system-paths.patch         \
   %D%/packages/patches/zig-0.10.0-610-bootstrap-resolve-conflicts.patch        
\
@@ -2378,13 +2379,16 @@ dist_patch_DATA =                                       
        \
   %D%/packages/patches/zig-0.10.0-747-CallOptions.patch                \
   %D%/packages/patches/zig-0.10.0-1638-re-add-qualCast.patch   \
   %D%/packages/patches/zig-0.11-build-respect-PKG_CONFIG-env-var.patch \
+  %D%/packages/patches/zig-0.11-cmake-support-libc-paths-file.patch    \
   %D%/packages/patches/zig-0.11-fix-runpath.patch              \
   %D%/packages/patches/zig-0.11-use-system-paths.patch         \
   %D%/packages/patches/zig-0.12-build-respect-PKG_CONFIG-env-var.patch \
+  %D%/packages/patches/zig-0.12-cmake-support-libc-paths-file.patch    \
   %D%/packages/patches/zig-0.12-fix-runpath.patch              \
   %D%/packages/patches/zig-0.12-use-baseline-cpu-by-default.patch      \
   %D%/packages/patches/zig-0.12-use-system-paths.patch         \
   %D%/packages/patches/zig-0.13-build-respect-PKG_CONFIG-env-var.patch \
+  %D%/packages/patches/zig-0.13-cmake-support-libc-paths-file.patch    \
   %D%/packages/patches/zsh-egrep-failing-test.patch            \
   %D%/packages/patches/zuo-bin-sh.patch
 
diff --git a/gnu/packages/patches/zig-0.10-cmake-support-libc-paths-file.patch 
b/gnu/packages/patches/zig-0.10-cmake-support-libc-paths-file.patch
new file mode 100644
index 0000000000..1dc42fd14d
--- /dev/null
+++ b/gnu/packages/patches/zig-0.10-cmake-support-libc-paths-file.patch
@@ -0,0 +1,33 @@
+From a2726fb040d3852043afd46591f8ac5b126bc5fc Mon Sep 17 00:00:00 2001
+From: Hilton Chain <hako@ultrarare.space>
+Date: Mon, 18 Nov 2024 16:38:14 +0800
+Subject: [PATCH] cmake: Support libc paths file.
+
+---
+ CMakeLists.txt | 3 +++
+ 1 file changed, 3 insertions(+)
+
+diff --git a/CMakeLists.txt b/CMakeLists.txt
+index b02677365f..24de2dc96c 100644
+--- a/CMakeLists.txt
++++ b/CMakeLists.txt
+@@ -1077,6 +1077,8 @@ elseif(MINGW)
+   target_link_libraries(zig2 ntdll)
+ endif()
+ 
++set(ZIG_LIBC_PATHS_FILE "" CACHE STRING "a file which specifies libc paths")
++
+ set(ZIG_BUILD_ARGS
+     --zig-lib-dir "${CMAKE_SOURCE_DIR}/lib"
+     "-Dconfig_h=${ZIG_CONFIG_H_OUT}"
+@@ -1089,6 +1091,7 @@ set(ZIG_BUILD_ARGS
+     "-Dtarget=${ZIG_TARGET_TRIPLE}"
+     "-Dcpu=${ZIG_TARGET_MCPU}"
+     "-Dversion-string=${ZIG_VERSION}"
++    --libc ${ZIG_LIBC_PATHS_FILE}
+ )
+ 
+ add_custom_target(stage3 ALL
+-- 
+2.46.0
+
diff --git a/gnu/packages/patches/zig-0.11-cmake-support-libc-paths-file.patch 
b/gnu/packages/patches/zig-0.11-cmake-support-libc-paths-file.patch
new file mode 100644
index 0000000000..07b7a2baa6
--- /dev/null
+++ b/gnu/packages/patches/zig-0.11-cmake-support-libc-paths-file.patch
@@ -0,0 +1,33 @@
+From 25c75fe13126be8df6c5ae00829454c6f058f75e Mon Sep 17 00:00:00 2001
+From: Hilton Chain <hako@ultrarare.space>
+Date: Mon, 18 Nov 2024 16:39:14 +0800
+Subject: [PATCH] cmake: Support libc paths file.
+
+---
+ CMakeLists.txt | 3 +++
+ 1 file changed, 3 insertions(+)
+
+diff --git a/CMakeLists.txt b/CMakeLists.txt
+index 92be8fbfe8..d13b79d2f6 100644
+--- a/CMakeLists.txt
++++ b/CMakeLists.txt
+@@ -859,6 +859,8 @@ else()
+   set(ZIG_PIE_ARG "")
+ endif()
+ 
++set(ZIG_LIBC_PATHS_FILE "" CACHE STRING "a file which specifies libc paths")
++
+ # -Dno-langref is currently hardcoded because building the langref takes too 
damn long
+ # -Dno-autodocs is currently hardcoded because the C backend generates a 
miscompilation
+ #   that prevents it from working.
+@@ -877,6 +879,7 @@ set(ZIG_BUILD_ARGS
+   "-Dtarget=${ZIG_TARGET_TRIPLE}"
+   "-Dcpu=${ZIG_TARGET_MCPU}"
+   "-Dversion-string=${RESOLVED_ZIG_VERSION}"
++  --libc ${ZIG_LIBC_PATHS_FILE}  
+ )
+ 
+ add_custom_target(stage3 ALL
+-- 
+2.46.0
+
diff --git a/gnu/packages/patches/zig-0.12-cmake-support-libc-paths-file.patch 
b/gnu/packages/patches/zig-0.12-cmake-support-libc-paths-file.patch
new file mode 100644
index 0000000000..0276042365
--- /dev/null
+++ b/gnu/packages/patches/zig-0.12-cmake-support-libc-paths-file.patch
@@ -0,0 +1,33 @@
+From 5d63b0f52d034a07cee2919c783526f9dac66135 Mon Sep 17 00:00:00 2001
+From: Hilton Chain <hako@ultrarare.space>
+Date: Mon, 18 Nov 2024 15:59:20 +0800
+Subject: [PATCH] cmake: Support libc paths file.
+
+---
+ CMakeLists.txt | 3 +++
+ 1 file changed, 3 insertions(+)
+
+diff --git a/CMakeLists.txt b/CMakeLists.txt
+index 8a409096e3..cf02677892 100644
+--- a/CMakeLists.txt
++++ b/CMakeLists.txt
+@@ -916,6 +916,8 @@ else()
+   set(ZIG_DYNAMIC_LINKER_ARG "-Ddynamic-linker=${ZIG_TARGET_DYNAMIC_LINKER}")
+ endif()
+ 
++set(ZIG_LIBC_PATHS_FILE "" CACHE STRING "a file which specifies libc paths")
++
+ # -Dno-langref is currently hardcoded because building the langref takes too 
damn long
+ # To obtain these two forms of documentation, run zig build against stage3 
rather than stage2.
+ set(ZIG_BUILD_ARGS
+@@ -932,6 +934,7 @@ set(ZIG_BUILD_ARGS
+   "-Dcpu=${ZIG_TARGET_MCPU}"
+   ${ZIG_DYNAMIC_LINKER_ARG}
+   "-Dversion-string=${RESOLVED_ZIG_VERSION}"
++  --libc ${ZIG_LIBC_PATHS_FILE}
+ )
+ 
+ add_custom_target(stage3 ALL
+-- 
+2.46.0
+
diff --git a/gnu/packages/patches/zig-0.13-cmake-support-libc-paths-file.patch 
b/gnu/packages/patches/zig-0.13-cmake-support-libc-paths-file.patch
new file mode 100644
index 0000000000..d187bc7ddc
--- /dev/null
+++ b/gnu/packages/patches/zig-0.13-cmake-support-libc-paths-file.patch
@@ -0,0 +1,33 @@
+From b3893d5a3fca9b0a53c0cb16e8873bee30c7b6e3 Mon Sep 17 00:00:00 2001
+From: Hilton Chain <hako@ultrarare.space>
+Date: Mon, 18 Nov 2024 15:24:47 +0800
+Subject: [PATCH] cmake: Support libc paths file.
+
+---
+ CMakeLists.txt | 3 +++
+ 1 file changed, 3 insertions(+)
+
+diff --git a/CMakeLists.txt b/CMakeLists.txt
+index 22051f1902..2f44d3d01f 100644
+--- a/CMakeLists.txt
++++ b/CMakeLists.txt
+@@ -922,6 +922,7 @@ if(MSVC OR MINGW)
+     target_link_libraries(zig2 LINK_PUBLIC version)
+ endif()
+ 
++set(ZIG_LIBC_PATHS_FILE "" CACHE STRING "a file which specifies libc paths")
+ 
+ # "-Dno-langref" and "-Dstd-docs=false" are hardcoded because they take too 
long to build.
+ # To obtain these two forms of documentation, run zig build against stage3 
rather than stage2.
+@@ -937,6 +938,8 @@ set(ZIG_BUILD_ARGS
+ 
+   -Dno-langref
+   -Dstd-docs=false
++
++  --libc ${ZIG_LIBC_PATHS_FILE}
+ )
+ 
+ if("${CMAKE_BUILD_TYPE}" STREQUAL "Debug")
+-- 
+2.46.0
+
diff --git a/gnu/packages/zig.scm b/gnu/packages/zig.scm
index 171f0e322f..cf733eaee8 100644
--- a/gnu/packages/zig.scm
+++ b/gnu/packages/zig.scm
@@ -22,6 +22,7 @@
 (define-module (gnu packages zig)
   #:use-module (guix gexp)
   #:use-module (guix packages)
+  #:use-module (guix platform)
   #:use-module (guix utils)
   #:use-module (guix download)
   #:use-module (guix git-download)
@@ -73,11 +74,24 @@
     (build-system cmake-build-system)
     (arguments
      (list
+      #:imported-modules
+      (cons '(guix build zig-utils)
+            %cmake-build-system-modules)
+      #:modules
+      (cons '(guix build zig-utils)
+            '((guix build cmake-build-system)
+              (guix build utils)))
       #:configure-flags
-      #~(list #$@(if (%current-target-system)
-                     (list (string-append "-DZIG_TARGET_TRIPLE="
-                                          (%current-target-system)))
-                     '()))
+      #~(list (string-append "-DZIG_LIB_DIR=" #$output "/lib/zig")
+              "-DZIG_TARGET_MCPU=baseline"
+              (string-append
+               "-DZIG_TARGET_TRIPLE="
+               (zig-target
+                #$(platform-target
+                   (lookup-platform-by-target-or-system
+                    (or (%current-target-system)
+                        (%current-system))))))
+              "-DZIG_USE_LLVM_CONFIG=ON")
       #:out-of-source? #f         ; for tests
       ;; There are too many unclear test failures.
       #:tests? (not (or (target-riscv64?)
@@ -100,11 +114,7 @@
                          ;; Is this symbol x86 only in glibc?
                          ((".*link_static_lib_as_system_lib.*") "")))))
                  '())
-          (add-after 'configure 'set-cache-dir
-            (lambda _
-              ;; Set cache dir, otherwise Zig looks for `$HOME/.cache'.
-              (setenv "ZIG_GLOBAL_CACHE_DIR"
-                      (string-append (getcwd) "/zig-cache"))))
+          (add-before 'configure 'zig-configure zig-configure)
           (delete 'check)
           (add-after 'install 'check
             (lambda* (#:key tests? #:allow-other-keys)
@@ -170,6 +180,7 @@ toolchain.  Among other features it provides
        (patches
         (search-patches
          "zig-0.10-build-respect-PKG_CONFIG-env-var.patch"
+         "zig-0.10-cmake-support-libc-paths-file.patch"
          "zig-0.10-fix-runpath.patch"
          "zig-0.9-use-baseline-cpu-by-default.patch"
          "zig-0.10-use-system-paths.patch"))))
@@ -177,20 +188,14 @@ toolchain.  Among other features it provides
      (substitute-keyword-arguments
          (strip-keyword-arguments '(#:tests?) (package-arguments zig-0.9))
        ((#:configure-flags flags ''())
-        #~(cons* "-DZIG_TARGET_MCPU=baseline"
-                 "-DZIG_SHARED_LLVM=ON"
-                 (string-append "-DZIG_LIB_DIR=" #$output "/lib/zig")
+        #~(cons* "-DZIG_SHARED_LLVM=ON"
+                 "-DZIG_LIBC_PATHS_FILE=/tmp/guix-zig-libc-paths"
                  #$flags))
        ((#:phases phases '%standard-phases)
         #~(modify-phases #$phases
             #$@(if (target-riscv64?)
                    `((delete 'adjust-tests))
                    '())
-            (add-after 'unpack 'set-CC
-              (lambda _
-                ;; Set CC, since the stage 2 zig relies on it to find the libc
-                ;; installation, and otherwise silently links against its own.
-                (setenv "CC" #$(cc-for-target))))
             (add-after 'patch-source-shebangs 'patch-more-shebangs
               (lambda* (#:key inputs #:allow-other-keys)
                 ;; Zig uses information about an ELF file to determine the
@@ -268,6 +273,13 @@ toolchain.  Among other features it provides
        (substitute-keyword-arguments (package-arguments base)
          ;; Patch for fixing RUNPATH not applied to intermediate versions.
          ((#:validate-runpath? _ #t) #f)
+         ;; Patch for cross-compilation not applied to intermediate versions.
+         ((#:modules modules '())
+          (cons '(srfi srfi-1) modules))
+         ((#:configure-flags flags #~())
+          #~(filter (lambda (flag)
+                      (not (string-contains flag "ZIG_TARGET_TRIPLE")))
+                    #$flags))
          ;; Disable tests for intermediate versions.
          ((#:tests? _ #t) #f)
          ((#:phases phases '%standard-phases)
@@ -1047,6 +1059,7 @@ toolchain.  Among other features it provides
        (patches
         (search-patches
          "zig-0.11-build-respect-PKG_CONFIG-env-var.patch"
+         "zig-0.11-cmake-support-libc-paths-file.patch"
          "zig-0.11-fix-runpath.patch"
          "zig-0.9-use-baseline-cpu-by-default.patch"
          "zig-0.11-use-system-paths.patch"))))
@@ -1388,6 +1401,7 @@ toolchain.  Among other features it provides
        (patches
         (search-patches
          "zig-0.12-build-respect-PKG_CONFIG-env-var.patch"
+         "zig-0.12-cmake-support-libc-paths-file.patch"
          "zig-0.12-fix-runpath.patch"
          "zig-0.12-use-baseline-cpu-by-default.patch"
          "zig-0.12-use-system-paths.patch"))))
@@ -1459,6 +1473,7 @@ toolchain.  Among other features it provides
        (patches
         (search-patches
          "zig-0.13-build-respect-PKG_CONFIG-env-var.patch"
+         "zig-0.13-cmake-support-libc-paths-file.patch"
          "zig-0.12-fix-runpath.patch"
          "zig-0.12-use-baseline-cpu-by-default.patch"
          "zig-0.12-use-system-paths.patch"))))
diff --git a/guix/build-system/zig.scm b/guix/build-system/zig.scm
index ad8a96b607..97bd9361c8 100644
--- a/guix/build-system/zig.scm
+++ b/guix/build-system/zig.scm
@@ -39,6 +39,7 @@
 (define %zig-build-system-modules
   ;; Build-side modules imported by default.
   `((guix build zig-build-system)
+    (guix build zig-utils)
     ,@%default-gnu-imported-modules))
 
 (define* (zig-build name inputs
diff --git a/guix/build/zig-build-system.scm b/guix/build/zig-build-system.scm
index 97fd86482a..b646b79039 100644
--- a/guix/build/zig-build-system.scm
+++ b/guix/build/zig-build-system.scm
@@ -19,6 +19,7 @@
 (define-module (guix build zig-build-system)
   #:use-module ((guix build gnu-build-system) #:prefix gnu:)
   #:use-module (guix build utils)
+  #:use-module (guix build zig-utils)
   #:use-module (ice-9 popen)
   #:use-module (ice-9 rdelim)
   #:use-module (ice-9 ftw)
@@ -32,41 +33,6 @@
 
 ;; Interesting guide here:
 ;; https://github.com/riverwm/river/blob/master/PACKAGING.md
-(define global-cache-dir "zig-cache")
-
-(define* (configure #:key inputs target #:allow-other-keys)
-  ;; Set cache dir, otherwise Zig looks for `$HOME/.cache'.
-  (setenv "ZIG_GLOBAL_CACHE_DIR" "zig-cache")
-
-  (setenv "PKG_CONFIG"
-          (if target
-              (string-append target "-pkg-config")
-              "pkg-config"))
-
-  ;; Configure file required for `--libc' option of `zig build'.
-  (let ((libc (assoc-ref inputs (if target "cross-libc" "libc")))
-        (port (open-file "/tmp/guix-zig-libc-paths" "w" #:encoding "utf8")))
-    (display
-     (string-append "\
-include_dir=" libc "/include
-sys_include_dir=" libc "/include
-crt_dir=" libc "/lib
-msvc_lib_dir=
-kernel32_lib_dir=
-gcc_dir=")
-     port)
-    (close-port port)))
-
-(define (zig-target target)
-  (cond ((string=? "i686-linux-gnu" target)
-         "x86-linux-gnu")
-        ((string=? "i586-pc-gnu" target)
-         "x86-hurd-gnu")
-        ((string=? "x86_64-w64-mingw32" target)
-         "x86_64-windows-gnu")
-        ((string=? "i686-w64-mingw32" target)
-         "x86-windows-gnu")
-        (else target)))
 
 (define* (build #:key
                 zig-build-flags
@@ -117,7 +83,7 @@ gcc_dir=")
 (define %standard-phases
   (modify-phases gnu:%standard-phases
     (delete 'bootstrap)
-    (replace 'configure configure)
+    (replace 'configure zig-configure)
     (replace 'build build)
     (replace 'check check)
     (replace 'install install)))
diff --git a/guix/build/zig-utils.scm b/guix/build/zig-utils.scm
new file mode 100644
index 0000000000..2e592ac7ea
--- /dev/null
+++ b/guix/build/zig-utils.scm
@@ -0,0 +1,61 @@
+;;; GNU Guix --- Functional package management for GNU
+;;; Copyright © 2024 Hilton Chain <hako@ultrarare.space>
+;;; Copyright © 2024 Efraim Flashner <efraim@flashner.co.il>
+;;;
+;;; This file is part of GNU Guix.
+;;;
+;;; GNU Guix is free software; you can redistribute it and/or modify it
+;;; under the terms of the GNU General Public License as published by
+;;; the Free Software Foundation; either version 3 of the License, or (at
+;;; your option) any later version.
+;;;
+;;; GNU Guix is distributed in the hope that it will be useful, but
+;;; WITHOUT ANY WARRANTY; without even the implied warranty of
+;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+;;; GNU General Public License for more details.
+;;;
+;;; You should have received a copy of the GNU General Public License
+;;; along with GNU Guix.  If not, see <http://www.gnu.org/licenses/>.
+
+(define-module (guix build zig-utils)
+  #:use-module (guix build utils)
+  #:export (zig-configure
+            zig-target))
+
+;;;
+;;;  Common procedures for zig and zig-build-system.
+;;;
+
+(define* (zig-configure #:key inputs target #:allow-other-keys)
+  ;; Set cache dir, otherwise Zig looks for `$HOME/.cache'.
+  (setenv "ZIG_GLOBAL_CACHE_DIR" "zig-cache")
+
+  (setenv "PKG_CONFIG"
+          (if target
+              (string-append target "-pkg-config")
+              "pkg-config"))
+
+  ;; Configure file required for `--libc' option of `zig build'.
+  (let ((libc (assoc-ref inputs (if target "cross-libc" "libc")))
+        (port (open-file "/tmp/guix-zig-libc-paths" "w" #:encoding "utf8")))
+    (display
+     (string-append "\
+include_dir=" libc "/include
+sys_include_dir=" libc "/include
+crt_dir=" libc "/lib
+msvc_lib_dir=
+kernel32_lib_dir=
+gcc_dir=")
+     port)
+    (close-port port)))
+
+(define (zig-target target)
+  (cond ((string=? "i686-linux-gnu" target)
+         "x86-linux-gnu")
+        ((string=? "i586-pc-gnu" target)
+         "x86-hurd-gnu")
+        ((string=? "x86_64-w64-mingw32" target)
+         "x86_64-windows-gnu")
+        ((string=? "i686-w64-mingw32" target)
+         "x86-windows-gnu")
+        (else target)))



reply via email to

[Prev in Thread] Current Thread [Next in Thread]