cli: add more options

This commit is contained in:
puck 2024-10-04 16:05:24 +00:00
parent c251273ee7
commit ba58f94843
2 changed files with 33 additions and 9 deletions

View file

@ -3,7 +3,7 @@
(author "puck") (author "puck")
(dependencies r7rs zilch zilch-lang-go) (dependencies r7rs zilch zilch-lang-go)
(component-options (component-options
(csc-options "-X" "r7rs" "-R" "r7rs" "-optimize-level" "3")) (csc-options "-X" "r7rs" "-R" "r7rs" "-optimize-level" "3" "-C" "-D_GNU_SOURCE"))
(components (components
(program zilch-cli-go (program zilch-cli-go
(source "zilch-go.scm")))) (source "zilch-go.scm"))))

View file

@ -1,4 +1,9 @@
(import (scheme base) (scheme write) (zilch statusbar) (zilch nix daemon) (zilch lib getopt) (scheme process-context) (chicken process-context) (srfi 146) (chicken port)) (import (scheme base) (scheme write) (zilch statusbar) (zilch nix daemon) (zilch magic) (zilch lib getopt) (scheme process-context) (chicken process-context) (srfi 146) (chicken port) (chicken foreign))
(foreign-declare "#include <sched.h>")
(define get-cpu-count
(foreign-lambda* int ()
"cpu_set_t set; sched_getaffinity(0, sizeof(set), &set); C_return(CPU_COUNT(&set));"))
(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)))
@ -10,6 +15,10 @@ executables in the module, if unspecified)
-h, --help Print this help message. -h, --help Print this help message.
-b, --build Build the store paths, rather than show their -b, --build Build the store paths, rather than show their
derivations. derivations.
-j, --max-jobs COUNT The maximum amount of builds to run. Defaults
to the amount of cores.
-v, --verbose Increase the verbosity configured in the Nix
daemon.
-L, --print-build-logs Print derivation logs as they come in. -L, --print-build-logs Print derivation logs as they come in.
-m, --module-dir DIR The directory to use as root module. -m, --module-dir DIR The directory to use as root module.
-r, --replace DIR Replace the module specified by the go.mod -r, --replace DIR Replace the module specified by the go.mod
@ -25,19 +34,20 @@ executables in the module, if unspecified)
(define-values (options args) (define-values (options args)
(getopt (getopt
'((help #f #\h) '((help #f #\h)
(module-dir #t #\m)
(print-build-logs #f #\L)
(build #f #\b) (build #f #\b)
(max-jobs #t #\j)
(verbose #f #\v)
(print-build-logs #f #\L)
(module-dir #t #\m)
(replace #t #\r) (replace #t #\r)
(debug #f)) (debug #f))
(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))
(define module-dir (if (assoc 'module-dir options) (cdr (assoc 'module-dir options)) (current-directory))) ;; Set up the logger.
(define (set-print-logs val) #f) (define (set-print-logs val) #f)
(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)
@ -45,8 +55,18 @@ executables in the module, if unspecified)
(set! set-print-logs statusbar-set-print-logs) (set! set-print-logs statusbar-set-print-logs)
(*logger* logger))) (*logger* logger)))
(define do-build (assoc 'build options)) ;; Flags passed to the nix daemon:
(define do-debug (assoc 'debug options)) (define max-jobs (if (assoc 'max-jobs options) (string->number (cdr (assoc 'max-jobs options))) (get-cpu-count)))
(define verbosity 3)
(for-each (lambda (val) (when (eq? (car val) 'verbose) (set! verbosity (+ 1 verbosity)))) options)
(write-string (string-append "Connected to Nix daemon, version " (daemon-link-daemon-version (*daemon*)) "\n") (current-error-port))
;; Set the options, ensuring the Go toolchain is substituted where possible.
(daemon-wop-set-options (*daemon*) verbosity max-jobs #t)
(import (zilch lang go core))
(store-path-build go-toolchain)
(daemon-wop-set-options (*daemon*) verbosity max-jobs #f)
(import (import
(scheme file) (chicken file) (chicken format) (scheme file) (chicken file) (chicken format)
@ -55,6 +75,10 @@ executables in the module, if unspecified)
(zilch nix drv) (zilch nix drv)
(zilch lang go)) (zilch lang go))
(define module-dir (if (assoc 'module-dir options) (cdr (assoc 'module-dir options)) (current-directory)))
(define do-build (assoc 'build options))
(define do-debug (assoc 'debug options))
(unless (file-exists? (string-append module-dir "/go.mod")) (unless (file-exists? (string-append module-dir "/go.mod"))
(set-print-logs #t) (set-print-logs #t)
(fprintf (current-error-port) "Refusing to use directory ~S as it contains no go.mod.\n" module-dir) (fprintf (current-error-port) "Refusing to use directory ~S as it contains no go.mod.\n" module-dir)