diff --git a/cli/cli.egg b/cli/cli.egg index a703db7..e8977bb 100644 --- a/cli/cli.egg +++ b/cli/cli.egg @@ -3,7 +3,7 @@ (author "puck") (dependencies r7rs zilch zilch-lang-go) (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 (program zilch-cli-go (source "zilch-go.scm")))) diff --git a/cli/zilch-go.scm b/cli/zilch-go.scm index 605b9a1..1fd5e35 100644 --- a/cli/zilch-go.scm +++ b/cli/zilch-go.scm @@ -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 ") +(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) (when msg (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. -b, --build Build the store paths, rather than show their 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. -m, --module-dir DIR The directory to use as root module. -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) (getopt '((help #f #\h) - (module-dir #t #\m) - (print-build-logs #f #\L) (build #f #\b) + (max-jobs #t #\j) + (verbose #f #\v) + (print-build-logs #f #\L) + (module-dir #t #\m) (replace #t #\r) (debug #f)) (list->vector (cdr (command-line))) print-help)) + (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) - (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)))) (current-output-port new-out) @@ -45,8 +55,18 @@ executables in the module, if unspecified) (set! set-print-logs statusbar-set-print-logs) (*logger* logger))) -(define do-build (assoc 'build options)) -(define do-debug (assoc 'debug options)) +;; 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 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 (scheme file) (chicken file) (chicken format) @@ -55,6 +75,10 @@ executables in the module, if unspecified) (zilch nix drv) (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")) (set-print-logs #t) (fprintf (current-error-port) "Refusing to use directory ~S as it contains no go.mod.\n" module-dir)