1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2012, 2013, 2014, 2015, 2016 Ludovic Courtès <ludo@gnu.org>
3 ;;; Copyright © 2013 Mark H Weaver <mhw@netris.org>
5 ;;; This file is part of GNU Guix.
7 ;;; GNU Guix is free software; you can redistribute it and/or modify it
8 ;;; under the terms of the GNU General Public License as published by
9 ;;; the Free Software Foundation; either version 3 of the License, or (at
10 ;;; your option) any later version.
12 ;;; GNU Guix is distributed in the hope that it will be useful, but
13 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
14 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 ;;; GNU General Public License for more details.
17 ;;; You should have received a copy of the GNU General Public License
18 ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
20 (define-module (guix scripts build)
21 #:use-module (guix ui)
22 #:use-module (guix scripts)
23 #:use-module (guix store)
24 #:use-module (guix derivations)
25 #:use-module (guix packages)
26 #:use-module (guix utils)
27 #:use-module (guix monads)
28 #:use-module (guix gexp)
29 #:autoload (guix http-client) (http-fetch http-get-error?)
30 #:use-module (ice-9 format)
31 #:use-module (ice-9 match)
32 #:use-module (ice-9 vlist)
33 #:use-module (srfi srfi-1)
34 #:use-module (srfi srfi-11)
35 #:use-module (srfi srfi-26)
36 #:use-module (srfi srfi-34)
37 #:use-module (srfi srfi-37)
38 #:autoload (gnu packages) (specification->package %package-module-path)
39 #:autoload (guix download) (download-to-store)
40 #:export (%standard-build-options
41 set-build-options-from-command-line
42 set-build-options-from-command-line*
43 show-build-options-help
45 %transformation-options
46 options->transformation
47 show-transformation-options-help
51 (define %default-log-urls
52 ;; Default base URLs for build logs.
53 '("http://hydra.gnu.org/log"))
55 ;; XXX: The following procedure cannot be in (guix store) because of the
56 ;; dependency on (guix derivations).
57 (define* (log-url store file #:key (base-urls %default-log-urls))
58 "Return a URL under one of the BASE-URLS where a build log for FILE can be
59 found. Return #f if no build log was found."
60 (define (valid-url? url)
61 ;; Probe URL and return #t if it is accessible.
62 (guard (c ((http-get-error? c) #f))
63 (close-port (http-fetch url #:buffered? #f))
66 (define (find-url file)
67 (let ((base (basename file)))
68 (any (lambda (base-url)
69 (let ((url (string-append base-url "/" base)))
70 (and (valid-url? url) url)))
73 (cond ((derivation-path? file)
76 ;; Usually we'll have more luck with the output file name since
77 ;; the deriver that was used by the server could be different, so
78 ;; try one of the output file names.
79 (let ((drv (call-with-input-file file read-derivation)))
80 (or (find-url (derivation->output-path drv))
83 ;; As a last resort, try the .drv.
84 (if (= ENOENT (system-error-errno args))
86 (apply throw args)))))
90 (define (register-root store paths root)
91 "Register ROOT as an indirect GC root for all of PATHS."
92 (let* ((root (string-append (canonicalize-path (dirname root))
99 (add-indirect-root store root))
101 (fold (lambda (path count)
102 (let ((root (string-append root
104 (number->string count))))
106 (add-indirect-root store root))
111 (leave (_ "failed to create GC root `~a': ~a~%")
112 root (strerror (system-error-errno args)))))))
114 (define (package-with-source store p uri)
115 "Return a package based on P but with its source taken from URI. Extract
116 the new package's version number from URI."
117 (define (numeric-extension? file-name)
118 ;; Return true if FILE-NAME ends with digits.
119 (string-every char-set:hex-digit (file-extension file-name)))
121 (define (tarball-base-name file-name)
122 ;; Return the "base" of FILE-NAME, removing '.tar.gz' or similar
125 (cond ((not (file-extension file-name))
127 ((numeric-extension? file-name)
129 ((string=? (file-extension file-name) "tar")
130 (file-sans-extension file-name))
131 ((file-extension file-name)
132 (tarball-base-name (file-sans-extension file-name)))
136 (let ((base (tarball-base-name (basename uri))))
137 (let-values (((name version)
138 (package-name->name+version base)))
140 (version (or version (package-version p)))
142 ;; Use #:recursive? #t to allow for directories.
143 (source (download-to-store store uri
144 #:recursive? #t))))))
151 (define (transform-package-source sources)
152 "Return a transformation procedure that replaces package sources with the
153 matching URIs given in SOURCES."
156 (cons (package-name->name+version (basename uri))
161 (let loop ((sources new-sources)
165 (let ((source (assoc-ref sources (package-name p))))
167 (package-with-source store p source)
172 (define (transform-package-inputs replacement-specs)
173 "Return a procedure that, when passed a package, replaces its direct
174 dependencies according to REPLACEMENT-SPECS. REPLACEMENT-SPECS is a list of
175 strings like \"guile=guile@2.1\" meaning that, any direct dependency on a
176 package called \"guile\" must be replaced with a dependency on a version 2.1
179 (char-set-complement (char-set #\=)))
182 ;; List of name/package pairs.
184 (match (string-tokenize spec not-equal)
186 (cons old (specification->package new)))
188 (leave (_ "invalid replacement specification: ~s~%") spec))))
191 (define (rewrite input)
193 ((label (? package? package) outputs ...)
194 (match (assoc-ref replacements (package-name package))
195 (#f (cons* label (replace package) outputs))
196 (new (cons* label new outputs))))
201 (memoize ;XXX: use eq?
205 (inputs (map rewrite (package-inputs p)))
206 (native-inputs (map rewrite (package-native-inputs p)))
207 (propagated-inputs (map rewrite (package-propagated-inputs p)))))))
214 (define %transformations
215 ;; Transformations that can be applied to things to build. The car is the
216 ;; key used in the option alist, and the cdr is the transformation
217 ;; procedure; it is called with two arguments: the store, and a list of
219 `((with-source . ,transform-package-source)
220 (with-input . ,transform-package-inputs)))
222 (define %transformation-options
223 ;; The command-line interface to the above transformations.
224 (list (option '("with-source") #t #f
225 (lambda (opt name arg result . rest)
227 (cons (alist-cons 'with-source arg result)
229 (option '("with-input") #t #f
230 (lambda (opt name arg result . rest)
232 (cons (alist-cons 'with-input arg result)
235 (define (show-transformation-options-help)
238 use SOURCE when building the corresponding package"))
240 --with-input=PACKAGE=REPLACEMENT
241 replace dependency PACKAGE by REPLACEMENT")))
244 (define (options->transformation opts)
245 "Return a procedure that, when passed an object to build (package,
246 derivation, etc.), applies the transformations specified by OPTS."
248 ;; List of applicable transformations as symbol/procedure pairs.
249 (filter-map (match-lambda
251 (match (filter-map (match-lambda
253 (and (eq? k key) arg)))
256 (args (cons key (transform args))))))
261 (((name . transform) obj)
262 (let ((new (transform store obj)))
264 (warning (_ "transformation '~a' had no effect on ~a~%")
267 (package-full-name obj)
275 ;;; Standard command-line build options.
278 (define (show-build-options-help)
279 "Display on the current output port help about the standard command-line
280 options handled by 'set-build-options-from-command-line', and listed in
281 '%standard-build-options'."
283 -L, --load-path=DIR prepend DIR to the package module search path"))
285 -K, --keep-failed keep build tree of failed builds"))
287 -k, --keep-going keep going when some of the derivations fail"))
289 -n, --dry-run do not build the derivations"))
291 --fallback fall back to building when the substituter fails"))
293 --no-substitutes build instead of resorting to pre-built substitutes"))
295 --substitute-urls=URLS
296 fetch substitute from URLS if they are authorized"))
298 --no-build-hook do not attempt to offload builds via the build hook"))
300 --max-silent-time=SECONDS
301 mark the build as failed after SECONDS of silence"))
303 --timeout=SECONDS mark the build as failed after SECONDS of activity"))
305 --verbosity=LEVEL use the given verbosity LEVEL"))
307 --rounds=N build N times in a row to detect non-determinism"))
309 -c, --cores=N allow the use of up to N CPU cores for the build"))
311 -M, --max-jobs=N allow at most N build jobs")))
313 (define (set-build-options-from-command-line store opts)
314 "Given OPTS, an alist as returned by 'args-fold' given
315 '%standard-build-options', set the corresponding build options on STORE."
316 ;; TODO: Add more options.
317 (set-build-options store
318 #:keep-failed? (assoc-ref opts 'keep-failed?)
319 #:keep-going? (assoc-ref opts 'keep-going?)
320 #:rounds (assoc-ref opts 'rounds)
321 #:build-cores (or (assoc-ref opts 'cores) 0)
322 #:max-build-jobs (or (assoc-ref opts 'max-jobs) 1)
323 #:fallback? (assoc-ref opts 'fallback?)
324 #:use-substitutes? (assoc-ref opts 'substitutes?)
325 #:substitute-urls (assoc-ref opts 'substitute-urls)
326 #:use-build-hook? (assoc-ref opts 'build-hook?)
327 #:max-silent-time (assoc-ref opts 'max-silent-time)
328 #:timeout (assoc-ref opts 'timeout)
329 #:print-build-trace (assoc-ref opts 'print-build-trace?)
330 #:verbosity (assoc-ref opts 'verbosity)))
332 (define set-build-options-from-command-line*
333 (store-lift set-build-options-from-command-line))
335 (define %standard-build-options
336 ;; List of standard command-line options for tools that build something.
337 (list (option '(#\L "load-path") #t #f
338 (lambda (opt name arg result . rest)
339 ;; XXX: Imperatively modify the search paths.
340 (%package-module-path (cons arg (%package-module-path)))
341 (%patch-path (cons arg (%patch-path)))
342 (set! %load-path (cons arg %load-path))
343 (set! %load-compiled-path (cons arg %load-compiled-path))
345 (apply values (cons result rest))))
346 (option '(#\K "keep-failed") #f #f
347 (lambda (opt name arg result . rest)
349 (alist-cons 'keep-failed? #t result)
351 (option '(#\k "keep-going") #f #f
352 (lambda (opt name arg result . rest)
354 (alist-cons 'keep-going? #t result)
356 (option '("rounds") #t #f
357 (lambda (opt name arg result . rest)
359 (alist-cons 'rounds (string->number* arg)
362 (option '("fallback") #f #f
363 (lambda (opt name arg result . rest)
365 (alist-cons 'fallback? #t
366 (alist-delete 'fallback? result))
368 (option '("no-substitutes") #f #f
369 (lambda (opt name arg result . rest)
371 (alist-cons 'substitutes? #f
372 (alist-delete 'substitutes? result))
374 (option '("substitute-urls") #t #f
375 (lambda (opt name arg result . rest)
377 (alist-cons 'substitute-urls
378 (string-tokenize arg)
379 (alist-delete 'substitute-urls result))
381 (option '("no-build-hook") #f #f
382 (lambda (opt name arg result . rest)
384 (alist-cons 'build-hook? #f
385 (alist-delete 'build-hook? result))
387 (option '("max-silent-time") #t #f
388 (lambda (opt name arg result . rest)
390 (alist-cons 'max-silent-time (string->number* arg)
393 (option '("timeout") #t #f
394 (lambda (opt name arg result . rest)
396 (alist-cons 'timeout (string->number* arg) result)
398 (option '("verbosity") #t #f
399 (lambda (opt name arg result . rest)
400 (let ((level (string->number arg)))
402 (alist-cons 'verbosity level
403 (alist-delete 'verbosity result))
405 (option '(#\c "cores") #t #f
406 (lambda (opt name arg result . rest)
407 (let ((c (false-if-exception (string->number arg))))
409 (apply values (alist-cons 'cores c result) rest)
410 (leave (_ "not a number: '~a' option argument: ~a~%")
412 (option '(#\M "max-jobs") #t #f
413 (lambda (opt name arg result . rest)
414 (let ((c (false-if-exception (string->number arg))))
416 (apply values (alist-cons 'max-jobs c result) rest)
417 (leave (_ "not a number: '~a' option argument: ~a~%")
422 ;;; Command-line options.
425 (define %default-options
426 ;; Alist of default option values.
427 `((system . ,(%current-system))
428 (build-mode . ,(build-mode normal))
432 (print-build-trace? . #t)
433 (max-silent-time . 3600)
437 (display (_ "Usage: guix build [OPTION]... PACKAGE-OR-DERIVATION...
438 Build the given PACKAGE-OR-DERIVATION and return their output paths.\n"))
440 -e, --expression=EXPR build the package or derivation EXPR evaluates to"))
442 -f, --file=FILE build the package or derivation that the code within
445 -S, --source build the packages' source derivations"))
447 --sources[=TYPE] build source derivations; TYPE may optionally be one
448 of \"package\", \"all\" (default), or \"transitive\""))
450 -s, --system=SYSTEM attempt to build for SYSTEM--e.g., \"i686-linux\""))
452 --target=TRIPLET cross-build for TRIPLET--e.g., \"armel-linux-gnu\""))
454 --no-grafts do not graft packages"))
456 -d, --derivations return the derivation paths of the given packages"))
458 --check rebuild items to check for non-determinism issues"))
460 -r, --root=FILE make FILE a symlink to the result, and register it
461 as a garbage collector root"))
463 --log-file return the log file names for the given derivations"))
465 (show-build-options-help)
467 (show-transformation-options-help)
470 -h, --help display this help and exit"))
472 -V, --version display version information and exit"))
474 (show-bug-report-information))
477 ;; Specifications of the command-line options.
478 (cons* (option '(#\h "help") #f #f
482 (option '(#\V "version") #f #f
484 (show-version-and-exit "guix build")))
485 (option '(#\S "source") #f #f
486 (lambda (opt name arg result)
487 (alist-cons 'source #t result)))
488 (option '("sources") #f #t
489 (lambda (opt name arg result)
492 (alist-cons 'source #t result))
494 (alist-cons 'source package-direct-sources result))
496 (alist-cons 'source package-transitive-sources result))
498 (leave (_ "invalid argument: '~a' option argument: ~a, ~
499 must be one of 'package', 'all', or 'transitive'~%")
501 (option '("check") #f #f
502 (lambda (opt name arg result . rest)
504 (alist-cons 'build-mode (build-mode check)
507 (option '(#\s "system") #t #f
508 (lambda (opt name arg result)
509 (alist-cons 'system arg
510 (alist-delete 'system result eq?))))
511 (option '("target") #t #f
512 (lambda (opt name arg result)
513 (alist-cons 'target arg
514 (alist-delete 'target result eq?))))
515 (option '(#\d "derivations") #f #f
516 (lambda (opt name arg result)
517 (alist-cons 'derivations-only? #t result)))
518 (option '(#\e "expression") #t #f
519 (lambda (opt name arg result)
520 (alist-cons 'expression arg result)))
521 (option '(#\f "file") #t #f
522 (lambda (opt name arg result)
523 (alist-cons 'file arg result)))
524 (option '(#\n "dry-run") #f #f
525 (lambda (opt name arg result)
526 (alist-cons 'dry-run? #t result)))
527 (option '(#\r "root") #t #f
528 (lambda (opt name arg result)
529 (alist-cons 'gc-root arg result)))
530 (option '("log-file") #f #f
531 (lambda (opt name arg result)
532 (alist-cons 'log-file? #t result)))
533 (option '("no-grafts") #f #f
534 (lambda (opt name arg result)
535 (alist-cons 'graft? #f
536 (alist-delete 'graft? result eq?))))
538 (append %transformation-options
539 %standard-build-options)))
541 (define (options->things-to-build opts)
542 "Read the arguments from OPTS and return a list of high-level objects to
543 build---packages, gexps, derivations, and so on."
544 (define (validate-type x)
545 (unless (or (package? x) (derivation? x) (gexp? x) (procedure? x))
546 (leave (_ "~s: not something we can build~%") x)))
548 (define (ensure-list x)
552 (for-each validate-type lst)
555 (append-map (match-lambda
556 (('argument . (? string? spec))
557 (cond ((derivation-path? spec)
558 (list (call-with-input-file spec read-derivation)))
560 ;; Nothing to do; maybe for --log-file.
563 (list (specification->package spec)))))
565 (ensure-list (load* file (make-user-module '()))))
567 (ensure-list (read/eval str)))
568 (('argument . (? derivation? drv))
573 (define (options->derivations store opts)
574 "Given OPTS, the result of 'args-fold', return a list of derivations to
577 (options->transformation opts))
579 (define package->derivation
580 (match (assoc-ref opts 'target)
581 (#f package-derivation)
583 (cut package-cross-derivation <> <> triplet <>))))
585 (define src (assoc-ref opts 'source))
586 (define system (assoc-ref opts 'system))
587 (define graft? (assoc-ref opts 'graft?))
589 (parameterize ((%graft? graft?))
590 (append-map (match-lambda
594 (list (package->derivation store p system)))
596 (let ((s (package-source p)))
597 (list (package-source-derivation store s))))
599 (map (cut package-source-derivation store <>)
604 (list (run-with-store store
606 (set-guile-for-build (default-guile))
610 (list (run-with-store store
612 (set-guile-for-build (default-guile))
613 (gexp->derivation "gexp" gexp
614 #:system system))))))
615 (map (cut transform store <>)
616 (options->things-to-build opts)))))
618 (define (show-build-log store file urls)
619 "Show the build log for FILE, falling back to remote logs from URLS if
621 (let ((log (or (log-file store file)
622 (log-url store file #:base-urls urls))))
624 (format #t "~a~%" log)
625 (leave (_ "no build log for '~a'~%") file))))
632 (define (guix-build . args)
634 ;; Ask for absolute file names so that .drv file names passed from the
635 ;; user to 'read-derivation' are absolute when it returns.
636 (with-fluids ((%file-port-name-canonicalization 'absolute))
637 (let* ((opts (parse-command-line args %options
638 (list %default-options)))
639 (store (open-connection))
640 (mode (assoc-ref opts 'build-mode))
641 (drv (options->derivations store opts))
642 (urls (map (cut string-append <> "/log")
643 (if (assoc-ref opts 'substitutes?)
644 (or (assoc-ref opts 'substitute-urls)
645 ;; XXX: This does not necessarily match the
646 ;; daemon's substitute URLs.
647 %default-substitute-urls)
649 (items (filter-map (match-lambda
650 (('argument . (? store-path? file))
654 (roots (filter-map (match-lambda
655 (('gc-root . root) root)
659 (set-build-options-from-command-line store opts)
660 (unless (assoc-ref opts 'log-file?)
661 (show-what-to-build store drv
662 #:use-substitutes? (assoc-ref opts 'substitutes?)
663 #:dry-run? (assoc-ref opts 'dry-run?)
666 (cond ((assoc-ref opts 'log-file?)
667 (for-each (cut show-build-log store <> urls)
669 (append (map derivation-file-name drv)
671 ((assoc-ref opts 'derivations-only?)
672 (format #t "~{~a~%~}" (map derivation-file-name drv))
673 (for-each (cut register-root store <> <>)
674 (map (compose list derivation-file-name) drv)
676 ((not (assoc-ref opts 'dry-run?))
677 (and (build-derivations store drv mode)
678 (for-each show-derivation-outputs drv)
679 (for-each (cut register-root store <> <>)
682 (derivation->output-paths drv)))