zilch-cli-ninja: add subcommands for extracting source and running diff

Change-Id: I6a6a6964eb887c7a56b4a150196403fac5066bec
This commit is contained in:
puck 2025-05-11 22:21:07 +00:00
parent 275b56622d
commit 38d792ff04

View file

@ -8,13 +8,26 @@
(define (print-help msg) (define (print-help msg)
(when msg (when msg
(write-string (string-append msg "\n\n") (current-error-port))) (write-string (string-append msg "\n\n") (current-error-port)))
(write-string "Usage: zilch-cli-ninja [OPTION] [TARGET...] (write-string "Usage: zilch-cli-ninja [OPTION] SUBCOMMAND ...
Processes a Ninja build based on the configuration in the passed-in Processes a Ninja build based on the configuration in the passed-in
config file (or zilch.scm, if not set), and reproducibly builds either config file (or zilch.scm, if not set), and reproducibly builds either
to final build, or to specific Ninja targets. to final build, or to specific Ninja targets.
Supported commands:
build [TARGET] Build the full drv, or specific targets from
its Ninja file.
source [DIR] Extract the source of the derivation to DIR
(or src, if unspecified), ready for Zilch.
diff Print the difference between the original and
modified sources as found in either `src' or
the directory selected by --source.
Arguments:
-h, --help Print this help message. -h, --help Print this help message.
-f, --config-file PATH Path to the Zilch config file. -f, --config-file PATH Path to the Zilch config file.
-s, --source DIR Override the input source for builds. Doesn't
reconfigure the build, so changes to the
build system will not apply.
-j, --max-jobs COUNT The maximum amount of builds to run. Defaults -j, --max-jobs COUNT The maximum amount of builds to run. Defaults
to the amount of cores. to the amount of cores.
-v, --verbose Increase the verbosity configured in the Nix -v, --verbose Increase the verbosity configured in the Nix
@ -29,21 +42,25 @@ to final build, or to specific Ninja targets.
(config-file #t #\f) (config-file #t #\f)
(max-jobs #t #\j) (max-jobs #t #\j)
(verbose #f #\v) (verbose #f #\v)
(source #t #\s)
(print-build-logs #f #\L)) (print-build-logs #f #\L))
(list->vector (cdr (command-line))) (list->vector (cdr (command-line)))
print-help)) print-help))
(when (assoc 'help options) (print-help #f)) (when (assoc 'help options) (print-help #f))
(when (null? args)
(print-help "No subcommand"))
; Set up the logger. ; Set up the logger.
(define (set-print-logs val) #f) (define (set-print-logs val) #f)
(let ((prev-error-handler (current-exception-handler))) (current-exception-handler (lambda data (set-print-logs #t) (apply prev-error-handler data)))) (let ((prev-error-handler (current-exception-handler))) (current-exception-handler (lambda data (set-print-logs #t) (apply prev-error-handler data))))
; (when (terminal-port? (current-error-port)) (when (terminal-port? (current-error-port))
; (let-values (((new-out new-err statusbar-set-print-logs logger) (statusbar-logger (current-output-port) (current-error-port) (assoc 'print-build-logs options)))) (let-values (((new-out new-err statusbar-set-print-logs logger) (statusbar-logger (current-output-port) (current-error-port) (assoc 'print-build-logs options))))
; (current-output-port new-out) (current-output-port new-out)
; (current-error-port new-err) (current-error-port new-err)
; (set! set-print-logs statusbar-set-print-logs) (set! set-print-logs statusbar-set-print-logs)
; (*logger* logger))) (*logger* logger)))
;; Flags passed to the nix daemon: ;; Flags passed to the nix daemon:
(define max-jobs (if (assoc 'max-jobs options) (string->number (cdr (assoc 'max-jobs options))) (get-cpu-count))) (define max-jobs (if (assoc 'max-jobs options) (string->number (cdr (assoc 'max-jobs options))) (get-cpu-count)))
@ -56,7 +73,7 @@ to final build, or to specific Ninja targets.
(import (import
(scheme base) (scheme file) (scheme read) (scheme base) (scheme file) (scheme read)
(chicken format) (chicken format) (chicken process)
(zilch lang ninja) (zilch lang ninja build) (zilch lang ninja) (zilch lang ninja build)
(zilch lang ninja nixpkgs) (zilch lang ninja nixpkgs)
(zilch lang ninja config) (zilch lang ninja config)
@ -65,19 +82,38 @@ to final build, or to specific Ninja targets.
(zilch zexpr) (zilch zexpr)
(srfi 152)) (srfi 152))
(define config-path (if (assoc 'config-file options) (cdr (assoc 'config-file options)) "zilch.scm")) (define source (and (assoc 'source options) (cdr (assoc 'source options))))
(define config (parse-ninja-config (call-with-input-file config-path read)))
(if (null? args) (define config-path (if (assoc 'config-file options) (cdr (assoc 'config-file options)) "zilch.scm"))
(let ((built (build-nixpkgs-drv-reproducibly config))) (define config (parse-ninja-config `(override-source: ,source ,@(call-with-input-file config-path read))))
(for-each
(lambda (output-and-path) (cond
(store-path-realised (cdr output-and-path)) ((string=? (car args) "source")
(printf "~A\t-> ~S\n" (car output-and-path) (cdr output-and-path))) (let*-values
built)) (((_ configured-drv _ _ _) (setup-ninja-environment config))
(let-values (((_ _ _ edge-ref defaults) (setup-ninja-environment config))) ((realised) (store-path-realised configured-drv))
(for-each ((path) (if (null? (cdr args)) "src" (cadr args))))
(lambda (target) (system* (string-append "cp -rf --no-preserve=ownership " realised "/src " (qs path)))
(define built-target (edge-ref target)) (system* (string-append "chmod -R u+rw " (qs path)))))
(printf "~A\t-> ~S\n" target (store-path-realised (force (built-edge-out-drv (cdr built-target)))))) ((string=? (car args) "build")
args))) (if (null? (cdr args))
(let ((built (build-nixpkgs-drv-reproducibly config)))
(for-each
(lambda (output-and-path)
(store-path-realised (cdr output-and-path))
(printf "~A\t-> ~S\n" (car output-and-path) (cdr output-and-path)))
built))
(let-values (((_ _ _ edge-ref defaults) (setup-ninja-environment config)))
(for-each
(lambda (target)
(define built-target (edge-ref target))
(printf "~A\t-> ~S\n" target (store-path-realised (force (built-edge-out-drv (cdr built-target))))))
(cdr args)))))
((string=? (car args) "diff")
(let*-values
(((_ configured-drv _ _ _) (setup-ninja-environment config))
((realised) (store-path-realised configured-drv))
((path) (or source "src")))
(process-execute "git" (list "diff" "--no-index" "--" (string-append realised "/src") path))))
(else
(print-help (string-append "Unknown subcommand " (car args)))))