guix build: Improve procedural decomposition.

* guix/scripts/build.scm (%store): Remove.
  (derivation-from-expression): Add 'store' parameter.  Adjust caller
  accordingly.
  (register-root): New procedure, formerly within 'guix-build'.
  (options->derivations): New procedure, formerly inline within
  'guix-build'.
  (guix-build): Adjust accordingly.
This commit is contained in:
Ludovic Courtès 2013-12-21 22:53:58 +01:00
parent 3f26bfc18a
commit 81fa80b245

View file

@ -35,10 +35,7 @@ (define-module (guix scripts build)
#:autoload (gnu packages) (find-best-packages-by-name) #:autoload (gnu packages) (find-best-packages-by-name)
#:export (guix-build)) #:export (guix-build))
(define %store (define (derivation-from-expression store str package-derivation
(make-parameter #f))
(define (derivation-from-expression str package-derivation
system source?) system source?)
"Read/eval STR and return the corresponding derivation path for SYSTEM. "Read/eval STR and return the corresponding derivation path for SYSTEM.
When SOURCE? is true and STR evaluates to a package, return the derivation of When SOURCE? is true and STR evaluates to a package, return the derivation of
@ -49,12 +46,12 @@ (define (derivation-from-expression str package-derivation
(if source? (if source?
(let ((source (package-source p))) (let ((source (package-source p)))
(if source (if source
(package-source-derivation (%store) source) (package-source-derivation store source)
(leave (_ "package `~a' has no source~%") (leave (_ "package `~a' has no source~%")
(package-name p)))) (package-name p))))
(package-derivation (%store) p system))) (package-derivation store p system)))
((? procedure? proc) ((? procedure? proc)
(run-with-store (%store) (proc) #:system system)))) (run-with-store store (proc) #:system system))))
(define (specification->package spec) (define (specification->package spec)
"Return a package matching SPEC. SPEC may be a package name, or a package "Return a package matching SPEC. SPEC may be a package name, or a package
@ -77,6 +74,30 @@ (define (specification->package spec)
name version) name version)
(leave (_ "~A: unknown package~%") name)))))) (leave (_ "~A: unknown package~%") name))))))
(define (register-root store paths root)
"Register ROOT as an indirect GC root for all of PATHS."
(let* ((root (string-append (canonicalize-path (dirname root))
"/" root)))
(catch 'system-error
(lambda ()
(match paths
((path)
(symlink path root)
(add-indirect-root store root))
((paths ...)
(fold (lambda (path count)
(let ((root (string-append root
"-"
(number->string count))))
(symlink path root)
(add-indirect-root store root))
(+ 1 count))
0
paths))))
(lambda args
(leave (_ "failed to create GC root `~a': ~a~%")
root (strerror (system-error-errno args)))))))
;;; ;;;
;;; Command-line options. ;;; Command-line options.
@ -193,6 +214,36 @@ (define %options
(lambda (opt name arg result) (lambda (opt name arg result)
(alist-cons 'log-file? #t result))))) (alist-cons 'log-file? #t result)))))
(define (options->derivations store opts)
"Given OPTS, the result of 'args-fold', return a list of derivations to
build."
(define package->derivation
(match (assoc-ref opts 'target)
(#f package-derivation)
(triplet
(cut package-cross-derivation <> <> triplet <>))))
(define src? (assoc-ref opts 'source?))
(define sys (assoc-ref opts 'system))
(filter-map (match-lambda
(('expression . str)
(derivation-from-expression store str package->derivation
sys src?))
(('argument . (? derivation-path? drv))
(call-with-input-file drv read-derivation))
(('argument . (? store-path?))
;; Nothing to do; maybe for --log-file.
#f)
(('argument . (? string? x))
(let ((p (specification->package x)))
(if src?
(let ((s (package-source p)))
(package-source-derivation store s))
(package->derivation store p sys))))
(_ #f))
opts))
;;; ;;;
;;; Entry point. ;;; Entry point.
@ -208,114 +259,65 @@ (define (parse-options)
(alist-cons 'argument arg result)) (alist-cons 'argument arg result))
%default-options)) %default-options))
(define (register-root paths root)
;; Register ROOT as an indirect GC root for all of PATHS.
(let* ((root (string-append (canonicalize-path (dirname root))
"/" root)))
(catch 'system-error
(lambda ()
(match paths
((path)
(symlink path root)
(add-indirect-root (%store) root))
((paths ...)
(fold (lambda (path count)
(let ((root (string-append root
"-"
(number->string count))))
(symlink path root)
(add-indirect-root (%store) root))
(+ 1 count))
0
paths))))
(lambda args
(leave (_ "failed to create GC root `~a': ~a~%")
root (strerror (system-error-errno args)))))))
(with-error-handling (with-error-handling
;; Ask for absolute file names so that .drv file names passed from the ;; Ask for absolute file names so that .drv file names passed from the
;; user to 'read-derivation' are absolute when it returns. ;; user to 'read-derivation' are absolute when it returns.
(with-fluids ((%file-port-name-canonicalization 'absolute)) (with-fluids ((%file-port-name-canonicalization 'absolute))
(let ((opts (parse-options))) (let* ((opts (parse-options))
(define package->derivation (store (open-connection))
(match (assoc-ref opts 'target) (drv (options->derivations store opts))
(#f package-derivation) (roots (filter-map (match-lambda
(triplet (('gc-root . root) root)
(cut package-cross-derivation <> <> triplet <>)))) (_ #f))
opts)))
(parameterize ((%store (open-connection))) (unless (assoc-ref opts 'log-file?)
(let* ((src? (assoc-ref opts 'source?)) (show-what-to-build store drv
(sys (assoc-ref opts 'system)) #:use-substitutes? (assoc-ref opts 'substitutes?)
(drv (filter-map (match-lambda #:dry-run? (assoc-ref opts 'dry-run?)))
(('expression . str)
(derivation-from-expression
str package->derivation sys src?))
(('argument . (? derivation-path? drv))
(call-with-input-file drv read-derivation))
(('argument . (? store-path?))
;; Nothing to do; maybe for --log-file.
#f)
(('argument . (? string? x))
(let ((p (specification->package x)))
(if src?
(let ((s (package-source p)))
(package-source-derivation
(%store) s))
(package->derivation (%store) p sys))))
(_ #f))
opts))
(roots (filter-map (match-lambda
(('gc-root . root) root)
(_ #f))
opts)))
(unless (assoc-ref opts 'log-file?) ;; TODO: Add more options.
(show-what-to-build (%store) drv (set-build-options store
#:use-substitutes? (assoc-ref opts 'substitutes?) #:keep-failed? (assoc-ref opts 'keep-failed?)
#:dry-run? (assoc-ref opts 'dry-run?))) #:build-cores (or (assoc-ref opts 'cores) 0)
#:fallback? (assoc-ref opts 'fallback?)
#:use-substitutes? (assoc-ref opts 'substitutes?)
#:max-silent-time (assoc-ref opts 'max-silent-time)
#:verbosity (assoc-ref opts 'verbosity))
;; TODO: Add more options. (cond ((assoc-ref opts 'log-file?)
(set-build-options (%store) (for-each (lambda (file)
#:keep-failed? (assoc-ref opts 'keep-failed?) (let ((log (log-file store file)))
#:build-cores (or (assoc-ref opts 'cores) 0) (if log
#:fallback? (assoc-ref opts 'fallback?) (format #t "~a~%" log)
#:use-substitutes? (assoc-ref opts 'substitutes?) (leave (_ "no build log for '~a'~%")
#:max-silent-time (assoc-ref opts 'max-silent-time) file))))
#:verbosity (assoc-ref opts 'verbosity)) (delete-duplicates
(append (map derivation-file-name drv)
(cond ((assoc-ref opts 'log-file?) (filter-map (match-lambda
(for-each (lambda (file) (('argument
(let ((log (log-file (%store) file))) . (? store-path? file))
(if log file)
(format #t "~a~%" log) (_ #f))
(leave (_ "no build log for '~a'~%") opts)))))
file)))) ((assoc-ref opts 'derivations-only?)
(delete-duplicates (format #t "~{~a~%~}" (map derivation-file-name drv))
(append (map derivation-file-name drv) (for-each (cut register-root store <> <>)
(filter-map (match-lambda (map (compose list derivation-file-name) drv)
(('argument roots))
. (? store-path? file)) ((not (assoc-ref opts 'dry-run?))
file) (and (build-derivations store drv)
(_ #f)) (for-each (lambda (d)
opts))))) (format #t "~{~a~%~}"
((assoc-ref opts 'derivations-only?) (map (match-lambda
(format #t "~{~a~%~}" (map derivation-file-name drv)) ((out-name . out)
(for-each (cut register-root <> <>) (derivation->output-path
(map (compose list derivation-file-name) drv) d out-name)))
roots)) (derivation-outputs d))))
((not (assoc-ref opts 'dry-run?)) drv)
(and (build-derivations (%store) drv) (for-each (cut register-root store <> <>)
(for-each (lambda (d) (map (lambda (drv)
(format #t "~{~a~%~}" (map cdr
(map (match-lambda (derivation->output-paths drv)))
((out-name . out) drv)
(derivation->output-path roots))))))))
d out-name)))
(derivation-outputs d))))
drv)
(for-each (cut register-root <> <>)
(map (lambda (drv)
(map cdr
(derivation->output-paths drv)))
drv)
roots))))))))))