guix-commits
[Top][All Lists]
Advanced

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

61/72: DRAFT: build-system: zig: Support Zig package manager.


From: guix-commits
Subject: 61/72: DRAFT: build-system: zig: Support Zig package manager.
Date: Thu, 21 Nov 2024 07:19:51 -0500 (EST)

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

commit e6c2818a28a14afe3be4648f111a8a0c5e91d38c
Author: Hilton Chain <hako@ultrarare.space>
AuthorDate: Wed Nov 20 07:41:06 2024 +0800

    DRAFT: build-system: zig: Support Zig package manager.
    
    * guix/build-system/zig.scm (zig-inputs->source-tuples): New procedure.
    (zig-build,zig-cross-build)[#:skip-build?,#:zig-inputs]: New arguments.
    [#:tests?]: Honor #:skip-build?.
    (lower)[#:zig-inputs]: New argument.
    * guix/build/zig-build-system.scm (unpack-dependencies): New procedure.
    (build,install): Honor #:skip-build?.
    (%standard-phases): Add 'unpack-dependencies.
    * doc/guix.texi (Build Systems)[zig-build-system]: Update documentation.
    
    Change-Id: I76ba483b67fdd26b33fa707a8d3cccc9f6a37e76
---
 doc/guix.texi                   | 36 +++++++++++++++++--------
 guix/build-system/zig.scm       | 30 ++++++++++++++++++---
 guix/build/zig-build-system.scm | 60 ++++++++++++++++++++++++++---------------
 3 files changed, 91 insertions(+), 35 deletions(-)

diff --git a/doc/guix.texi b/doc/guix.texi
index 1c39628ffa..728fceac7d 100644
--- a/doc/guix.texi
+++ b/doc/guix.texi
@@ -10084,17 +10084,31 @@ build system (@command{zig build} command).
 Selecting this build system adds @code{zig} to the package inputs, in
 addition to the packages of @code{gnu-build-system}.
 
-There is no @code{configure} phase because Zig packages typically do not
-need to be configured.  The @code{#:zig-build-flags} parameter is a list of
-flags that are passed to the @code{zig} command during the build.  The
-@code{#:zig-test-flags} parameter is a list of flags that are passed to the
-@code{zig test} command during the @code{check} phase.  The default compiler
-package can be overridden with the @code{#:zig} argument.
-
-The optional @code{zig-release-type} parameter declares the type of release.
-Possible values are: @code{safe}, @code{fast}, or @code{small}. The default
-value is @code{#f}, which causes the release flag to be omitted from the
-@code{zig} command.  That results in a @code{debug} build.
+The @code{configure} phase sets up environment for @command{zig build}.
+You need to add custom phases after it if you want to invoke @command{zig}.
+
+The @code{#:zig-build-flags} parameter is a list of flags that are passed
+to @command{zig build} in @code{build} phase.  The @code{#:zig-test-flags}
+parameter is a list of flags that are passed to @command{zig build test} in
+@code{check} phase.  The default compiler package can be overridden with
+the @code{#:zig} parameter.
+
+The optional @code{#:zig-release-type} parameter declares the type of
+release.  Possible values are: @code{safe}, @code{fast}, or
+@code{small}. The default value is @code{#f}, which causes the release flag
+to be omitted from the @code{zig} command and results in a @code{debug}
+build.
+
+For packages that don't install anything and don't come with a test suite
+(likely library packages to be used by other Zig packages), you can set
+@code{#:skip-build?} parameter to @code{#t}, which skips @code{build},
+@code{check} and @code{install} phases.
+
+When source-only Zig dependencies (those defined in @file{build.zig.zon},
+for example) are needed, you should add them to the @code{#:zig-inputs}
+parameter as a list of name and spec pairs, where the name should match the
+one specified by the package you're building and the spec can be a package
+or a source definition.
 @end defvar
 
 @defvar scons-build-system
diff --git a/guix/build-system/zig.scm b/guix/build-system/zig.scm
index 97bd9361c8..0ac40b1bde 100644
--- a/guix/build-system/zig.scm
+++ b/guix/build-system/zig.scm
@@ -42,14 +42,30 @@
     (guix build zig-utils)
     ,@%default-gnu-imported-modules))
 
+;; `zig fetch --name=<NAME>` overwrites dependency with NAME in build.zig.zon.
+;; Need to keep a label here since the name is specified by package developer.
+(define (zig-inputs->source-tuples inputs)
+  (map (lambda (input)
+         (match input
+           (((? string? label) (? package? package))
+            (list label (package-source package)))
+           (((? string?) (? file-like?))
+            input)
+           ((? package? package)
+            (list (package-name package)
+                  (package-source package)))))
+       inputs))
+
 (define* (zig-build name inputs
                     #:key
                     source
                     (tests? #t)
                     (test-target #f)
+                    (skip-build? #f)
                     (zig-build-flags ''())
                     (zig-test-flags ''())
                     (zig-release-type #f)
+                    (zig-inputs #f)
                     (phases '%standard-phases)
                     (outputs '("out"))
                     (search-paths '())
@@ -67,10 +83,13 @@
                      #:source #+source
                      #:system #$system
                      #:test-target #$test-target
+                     #:skip-build? #$skip-build?
                      #:zig-build-flags #$zig-build-flags
                      #:zig-test-flags #$zig-test-flags
                      #:zig-release-type #$zig-release-type
-                     #:tests? #$tests?
+                     #:zig-inputs
+                     '#+(and=> zig-inputs zig-inputs->source-tuples)
+                     #:tests? #$(and tests? (not skip-build?))
                      #:phases #$phases
                      #:outputs #$(outputs->gexp outputs)
                      #:search-paths '#$(sexp->gexp
@@ -94,11 +113,13 @@
                           (native-search-paths '())
                           (tests? #t)
                           (test-target #f)
+                          (skip-build? #f)
                           (zig-build-flags ''())
                           (zig-test-flags ''())
                           (zig-destdir "out")
                           (zig-test-destdir "test-out")
                           (zig-release-type #f)
+                          (zig-inputs #f)
                           (system (%current-system))
                           (guile #f)
                           (imported-modules %zig-build-system-modules)
@@ -137,12 +158,15 @@
                      #:native-search-paths '#$(map
                                                 search-path-specification->sexp
                                                 native-search-paths)
+                     #:skip-build? #$skip-build?
                      #:zig-build-flags #$zig-build-flags
                      #:zig-test-flags #$zig-test-flags
                      #:zig-release-type #$zig-release-type
+                     #:zig-inputs
+                     '#+(and=> zig-inputs zig-inputs->source-tuples)
                      #:zig-destdir #$zig-destdir
                      #:zig-test-destdir #$zig-test-destdir
-                     #:tests? #$tests?
+                     #:tests? #$(and tests? (not skip-build?))
                      #:search-paths '#$(sexp->gexp
                                         (map search-path-specification->sexp
                                              search-paths))))))
@@ -159,7 +183,7 @@
 
 (define* (lower name
                 #:key source inputs native-inputs outputs system target
-                (zig (default-zig))
+                (zig (default-zig)) zig-inputs
                 #:allow-other-keys
                 #:rest arguments)
   "Return a bag for NAME."
diff --git a/guix/build/zig-build-system.scm b/guix/build/zig-build-system.scm
index b646b79039..0d4a354186 100644
--- a/guix/build/zig-build-system.scm
+++ b/guix/build/zig-build-system.scm
@@ -34,30 +34,45 @@
 ;; Interesting guide here:
 ;; https://github.com/riverwm/river/blob/master/PACKAGING.md
 
+(define* (unpack-dependencies #:key zig-inputs skip-build? #:allow-other-keys)
+  "Unpack and overwrite dependencies specified in build.zig.zon with the ones
+provided in ZIG-INPUTS."
+  (when (and zig-inputs (not skip-build?))
+    (for-each
+     (lambda (input)
+       (match input
+         ((label source)
+          (let ((call `("zig" "fetch" ,source
+                        ,(string-append "--save=" label))))
+            (format #t "running: ~s~%" call)
+            (apply invoke call)))))
+     zig-inputs)))
+
 (define* (build #:key
                 zig-build-flags
-                zig-release-type       ;; "safe", "fast" or "small" empty for a
-                                       ;; debug build"
+                ;; "safe", "fast" or "small", empty for a "debug" build.
+                zig-release-type
                 target
+                skip-build?
                 #:allow-other-keys)
   "Build a given Zig package."
-
-  (setenv "DESTDIR" "out")
-  (let ((call `("zig" "build"
-                     "--prefix"             ""            ;; Don't add /usr
-                     "--prefix-lib-dir"     "lib"
-                     "--prefix-exe-dir"     "bin"
-                     "--prefix-include-dir" "include"
-                     ,@(if target
-                         (list (string-append "-Dtarget=" (zig-target target)))
-                         '())
-                     ,@(if zig-release-type
-                         (list (string-append "-Drelease-" zig-release-type))
-                         '())
-                     "--libc" "/tmp/guix-zig-libc-paths"
-                     ,@zig-build-flags)))
-  (format #t "running: ~s~%" call)
-  (apply invoke call)))
+  (when (not skip-build?)
+    (setenv "DESTDIR" "out")
+    (let ((call `("zig" "build"
+                  "--prefix"             ""            ;; Don't add /usr
+                  "--prefix-lib-dir"     "lib"
+                  "--prefix-exe-dir"     "bin"
+                  "--prefix-include-dir" "include"
+                  ,@(if target
+                        (list (string-append "-Dtarget=" (zig-target target)))
+                        '())
+                  ,@(if zig-release-type
+                        (list (string-append "-Drelease-" zig-release-type))
+                        '())
+                  "--libc" "/tmp/guix-zig-libc-paths"
+                  ,@zig-build-flags)))
+      (format #t "running: ~s~%" call)
+      (apply invoke call))))
 
 (define* (check #:key tests?
                 zig-test-flags
@@ -75,15 +90,18 @@
         (setenv "DESTDIR" old-destdir)
         (unsetenv "DESTDIR")))))
 
-(define* (install #:key inputs outputs #:allow-other-keys)
+(define* (install #:key inputs outputs skip-build? #:allow-other-keys)
   "Install a given Zig package."
   (let ((out (assoc-ref outputs "out")))
-    (copy-recursively "out" out)))
+    (if skip-build?
+        (mkdir-p out)
+        (copy-recursively "out" out))))
 
 (define %standard-phases
   (modify-phases gnu:%standard-phases
     (delete 'bootstrap)
     (replace 'configure zig-configure)
+    (add-after 'configure 'unpack-dependencies unpack-dependencies)
     (replace 'build build)
     (replace 'check check)
     (replace 'install install)))



reply via email to

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