ui: Rename '_' to 'G_'.
[jackhill/guix/guix.git] / guix / scripts / build.scm
CommitLineData
233e7676 1;;; GNU Guix --- Functional package management for GNU
d9da3a75 2;;; Copyright © 2012, 2013, 2014, 2015, 2016, 2017 Ludovic Courtès <ludo@gnu.org>
dc5669cd 3;;; Copyright © 2013 Mark H Weaver <mhw@netris.org>
14a1c319 4;;;
233e7676 5;;; This file is part of GNU Guix.
14a1c319 6;;;
233e7676 7;;; GNU Guix is free software; you can redistribute it and/or modify it
14a1c319
LC
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.
11;;;
233e7676 12;;; GNU Guix is distributed in the hope that it will be useful, but
14a1c319
LC
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.
16;;;
17;;; You should have received a copy of the GNU General Public License
233e7676 18;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
14a1c319 19
e49951eb 20(define-module (guix scripts build)
073c34d7 21 #:use-module (guix ui)
88981dd3 22 #:use-module (guix scripts)
14a1c319
LC
23 #:use-module (guix store)
24 #:use-module (guix derivations)
25 #:use-module (guix packages)
7adf9b84 26 #:use-module (guix grafts)
9a2a2005
LC
27
28 ;; Use the procedure that destructures "NAME-VERSION" forms.
29 #:use-module ((guix utils) #:hide (package-name->name+version))
30 #:use-module ((guix build utils) #:select (package-name->name+version))
31
ac5de156 32 #:use-module (guix monads)
56b82106 33 #:use-module (guix gexp)
3f208ad7 34 #:autoload (guix http-client) (http-fetch http-get-error?)
14a1c319
LC
35 #:use-module (ice-9 format)
36 #:use-module (ice-9 match)
dc5669cd 37 #:use-module (ice-9 vlist)
14a1c319 38 #:use-module (srfi srfi-1)
5401dd75 39 #:use-module (srfi srfi-11)
14a1c319 40 #:use-module (srfi srfi-26)
07783858 41 #:use-module (srfi srfi-34)
14a1c319 42 #:use-module (srfi srfi-37)
300868ba 43 #:autoload (gnu packages) (specification->package %package-module-path)
7f3673f2 44 #:autoload (guix download) (download-to-store)
257b9341 45 #:export (%standard-build-options
e7fc17b5 46 set-build-options-from-command-line
2d977638 47 set-build-options-from-command-line*
e7fc17b5 48 show-build-options-help
88ad6ded
LC
49
50 %transformation-options
629a064f 51 options->transformation
88ad6ded 52 show-transformation-options-help
e7fc17b5 53
1c6b2d3f 54 guix-build
a09b45da
CM
55 register-root
56 register-root*))
14a1c319 57
3f208ad7
LC
58(define %default-log-urls
59 ;; Default base URLs for build logs.
60 '("http://hydra.gnu.org/log"))
61
62;; XXX: The following procedure cannot be in (guix store) because of the
63;; dependency on (guix derivations).
64(define* (log-url store file #:key (base-urls %default-log-urls))
65 "Return a URL under one of the BASE-URLS where a build log for FILE can be
66found. Return #f if no build log was found."
67 (define (valid-url? url)
68 ;; Probe URL and return #t if it is accessible.
c22a4757
LC
69 (catch 'getaddrinfo-error
70 (lambda ()
71 (guard (c ((http-get-error? c) #f))
72 (close-port (http-fetch url #:buffered? #f))
73 #t))
74 (lambda _
75 #f)))
3f208ad7
LC
76
77 (define (find-url file)
78 (let ((base (basename file)))
79 (any (lambda (base-url)
80 (let ((url (string-append base-url "/" base)))
81 (and (valid-url? url) url)))
82 base-urls)))
83
84 (cond ((derivation-path? file)
85 (catch 'system-error
86 (lambda ()
87 ;; Usually we'll have more luck with the output file name since
88 ;; the deriver that was used by the server could be different, so
89 ;; try one of the output file names.
90 (let ((drv (call-with-input-file file read-derivation)))
91 (or (find-url (derivation->output-path drv))
92 (find-url file))))
93 (lambda args
94 ;; As a last resort, try the .drv.
95 (if (= ENOENT (system-error-errno args))
96 (find-url file)
97 (apply throw args)))))
98 (else
99 (find-url file))))
100
81fa80b2
LC
101(define (register-root store paths root)
102 "Register ROOT as an indirect GC root for all of PATHS."
840f38ba
LC
103 (let* ((root (if (string-prefix? "/" root)
104 root
105 (string-append (canonicalize-path (dirname root))
106 "/" root))))
81fa80b2
LC
107 (catch 'system-error
108 (lambda ()
109 (match paths
110 ((path)
111 (symlink path root)
112 (add-indirect-root store root))
113 ((paths ...)
114 (fold (lambda (path count)
115 (let ((root (string-append root
116 "-"
117 (number->string count))))
118 (symlink path root)
119 (add-indirect-root store root))
120 (+ 1 count))
121 0
122 paths))))
123 (lambda args
69daee23 124 (leave (G_ "failed to create GC root `~a': ~a~%")
81fa80b2
LC
125 root (strerror (system-error-errno args)))))))
126
a09b45da
CM
127(define register-root*
128 (store-lift register-root))
129
7f3673f2
LC
130(define (package-with-source store p uri)
131 "Return a package based on P but with its source taken from URI. Extract
132the new package's version number from URI."
133 (define (numeric-extension? file-name)
134 ;; Return true if FILE-NAME ends with digits.
135 (string-every char-set:hex-digit (file-extension file-name)))
136
137 (define (tarball-base-name file-name)
138 ;; Return the "base" of FILE-NAME, removing '.tar.gz' or similar
139 ;; extensions.
140 ;; TODO: Factorize.
a43b55f1
LC
141 (cond ((not (file-extension file-name))
142 file-name)
143 ((numeric-extension? file-name)
7f3673f2
LC
144 file-name)
145 ((string=? (file-extension file-name) "tar")
146 (file-sans-extension file-name))
a43b55f1
LC
147 ((file-extension file-name)
148 (tarball-base-name (file-sans-extension file-name)))
7f3673f2 149 (else
a43b55f1 150 file-name)))
7f3673f2
LC
151
152 (let ((base (tarball-base-name (basename uri))))
153 (let-values (((name version)
154 (package-name->name+version base)))
155 (package (inherit p)
156 (version (or version (package-version p)))
a43b55f1
LC
157
158 ;; Use #:recursive? #t to allow for directories.
159 (source (download-to-store store uri
7c247809
LC
160 #:recursive? #t))
161
162 ;; Override the replacement, otherwise '--with-source' would
163 ;; have no effect.
164 (replacement #f)))))
7f3673f2 165
14a1c319 166\f
88ad6ded
LC
167;;;
168;;; Transformations.
169;;;
170
171(define (transform-package-source sources)
172 "Return a transformation procedure that replaces package sources with the
173matching URIs given in SOURCES."
174 (define new-sources
175 (map (lambda (uri)
176 (cons (package-name->name+version (basename uri))
177 uri))
178 sources))
179
180 (lambda (store obj)
181 (let loop ((sources new-sources)
182 (result '()))
183 (match obj
184 ((? package? p)
185 (let ((source (assoc-ref sources (package-name p))))
186 (if source
187 (package-with-source store p source)
188 p)))
189 (_
190 obj)))))
191
5cf01aa5
LC
192(define (evaluate-replacement-specs specs proc)
193 "Parse SPECS, a list of strings like \"guile=guile@2.1\", and invoke PROC on
194each package pair specified by SPECS. Return the resulting list. Raise an
195error if an element of SPECS uses invalid syntax, or if a package it refers to
196could not be found."
47c0f92c
LC
197 (define not-equal
198 (char-set-complement (char-set #\=)))
199
5cf01aa5
LC
200 (map (lambda (spec)
201 (match (string-tokenize spec not-equal)
202 ((old new)
203 (proc (specification->package old)
204 (specification->package new)))
205 (x
69daee23 206 (leave (G_ "invalid replacement specification: ~s~%") spec))))
5cf01aa5
LC
207 specs))
208
209(define (transform-package-inputs replacement-specs)
210 "Return a procedure that, when passed a package, replaces its direct
211dependencies according to REPLACEMENT-SPECS. REPLACEMENT-SPECS is a list of
212strings like \"guile=guile@2.1\" meaning that, any dependency on a package
213called \"guile\" must be replaced with a dependency on a version 2.1 of
214\"guile\"."
215 (let* ((replacements (evaluate-replacement-specs replacement-specs cons))
216 (rewrite (package-input-rewriting replacements)))
4e49163f
LC
217 (lambda (store obj)
218 (if (package? obj)
219 (rewrite obj)
220 obj))))
47c0f92c 221
645b9df8
LC
222(define (transform-package-inputs/graft replacement-specs)
223 "Return a procedure that, when passed a package, replaces its direct
224dependencies according to REPLACEMENT-SPECS. REPLACEMENT-SPECS is a list of
225strings like \"gnutls=gnutls@3.5.4\" meaning that packages are built using the
226current 'gnutls' package, after which version 3.5.4 is grafted onto them."
227 (define (replacement-pair old new)
228 (cons old
229 (package (inherit old) (replacement new))))
230
231 (let* ((replacements (evaluate-replacement-specs replacement-specs
232 replacement-pair))
233 (rewrite (package-input-rewriting replacements)))
234 (lambda (store obj)
235 (if (package? obj)
236 (rewrite obj)
237 obj))))
238
88ad6ded
LC
239(define %transformations
240 ;; Transformations that can be applied to things to build. The car is the
241 ;; key used in the option alist, and the cdr is the transformation
242 ;; procedure; it is called with two arguments: the store, and a list of
243 ;; things to build.
47c0f92c 244 `((with-source . ,transform-package-source)
645b9df8
LC
245 (with-input . ,transform-package-inputs)
246 (with-graft . ,transform-package-inputs/graft)))
88ad6ded
LC
247
248(define %transformation-options
249 ;; The command-line interface to the above transformations.
31c2fd1e
LC
250 (let ((parser (lambda (symbol)
251 (lambda (opt name arg result . rest)
252 (apply values
253 (alist-cons symbol arg result)
254 rest)))))
255 (list (option '("with-source") #t #f
256 (parser 'with-source))
257 (option '("with-input") #t #f
645b9df8
LC
258 (parser 'with-input))
259 (option '("with-graft") #t #f
260 (parser 'with-graft)))))
88ad6ded
LC
261
262(define (show-transformation-options-help)
69daee23 263 (display (G_ "
88ad6ded 264 --with-source=SOURCE
47c0f92c 265 use SOURCE when building the corresponding package"))
69daee23 266 (display (G_ "
47c0f92c 267 --with-input=PACKAGE=REPLACEMENT
645b9df8 268 replace dependency PACKAGE by REPLACEMENT"))
69daee23 269 (display (G_ "
645b9df8
LC
270 --with-graft=PACKAGE=REPLACEMENT
271 graft REPLACEMENT on packages that refer to PACKAGE")))
88ad6ded
LC
272
273
274(define (options->transformation opts)
275 "Return a procedure that, when passed an object to build (package,
276derivation, etc.), applies the transformations specified by OPTS."
277 (define applicable
278 ;; List of applicable transformations as symbol/procedure pairs.
279 (filter-map (match-lambda
280 ((key . transform)
281 (match (filter-map (match-lambda
282 ((k . arg)
283 (and (eq? k key) arg)))
284 opts)
285 (() #f)
286 (args (cons key (transform args))))))
287 %transformations))
288
289 (lambda (store obj)
290 (fold (match-lambda*
291 (((name . transform) obj)
292 (let ((new (transform store obj)))
293 (when (eq? new obj)
69daee23 294 (warning (G_ "transformation '~a' had no effect on ~a~%")
88ad6ded
LC
295 name
296 (if (package? obj)
297 (package-full-name obj)
298 obj)))
299 new)))
300 obj
301 applicable)))
302
303\f
14a1c319 304;;;
e7fc17b5 305;;; Standard command-line build options.
14a1c319
LC
306;;;
307
e7fc17b5
LC
308(define (show-build-options-help)
309 "Display on the current output port help about the standard command-line
310options handled by 'set-build-options-from-command-line', and listed in
311'%standard-build-options'."
69daee23 312 (display (G_ "
300868ba 313 -L, --load-path=DIR prepend DIR to the package module search path"))
69daee23 314 (display (G_ "
14a1c319 315 -K, --keep-failed keep build tree of failed builds"))
69daee23 316 (display (G_ "
f4953019 317 -k, --keep-going keep going when some of the derivations fail"))
69daee23 318 (display (G_ "
14a1c319 319 -n, --dry-run do not build the derivations"))
69daee23 320 (display (G_ "
56b1f4b7 321 --fallback fall back to building when the substituter fails"))
69daee23 322 (display (G_ "
692c6c15 323 --no-substitutes build instead of resorting to pre-built substitutes"))
69daee23 324 (display (G_ "
f8a8e0fe
LC
325 --substitute-urls=URLS
326 fetch substitute from URLS if they are authorized"))
69daee23 327 (display (G_ "
7573d30f 328 --no-grafts do not graft packages"))
69daee23 329 (display (G_ "
425b0bfc 330 --no-build-hook do not attempt to offload builds via the build hook"))
69daee23 331 (display (G_ "
969e678e
LC
332 --max-silent-time=SECONDS
333 mark the build as failed after SECONDS of silence"))
69daee23 334 (display (G_ "
002622b6 335 --timeout=SECONDS mark the build as failed after SECONDS of activity"))
69daee23 336 (display (G_ "
07ab4bf1 337 --verbosity=LEVEL use the given verbosity LEVEL"))
69daee23 338 (display (G_ "
5b74fe06 339 --rounds=N build N times in a row to detect non-determinism"))
69daee23 340 (display (G_ "
f6526eb3 341 -c, --cores=N allow the use of up to N CPU cores for the build"))
69daee23 342 (display (G_ "
f6526eb3 343 -M, --max-jobs=N allow at most N build jobs")))
14a1c319 344
e7fc17b5
LC
345(define (set-build-options-from-command-line store opts)
346 "Given OPTS, an alist as returned by 'args-fold' given
347'%standard-build-options', set the corresponding build options on STORE."
348 ;; TODO: Add more options.
349 (set-build-options store
350 #:keep-failed? (assoc-ref opts 'keep-failed?)
f4953019 351 #:keep-going? (assoc-ref opts 'keep-going?)
5b74fe06 352 #:rounds (assoc-ref opts 'rounds)
d9da3a75
LC
353 #:build-cores (assoc-ref opts 'cores)
354 #:max-build-jobs (assoc-ref opts 'max-jobs)
e7fc17b5
LC
355 #:fallback? (assoc-ref opts 'fallback?)
356 #:use-substitutes? (assoc-ref opts 'substitutes?)
fb4bf72b 357 #:substitute-urls (assoc-ref opts 'substitute-urls)
e7fc17b5
LC
358 #:use-build-hook? (assoc-ref opts 'build-hook?)
359 #:max-silent-time (assoc-ref opts 'max-silent-time)
002622b6 360 #:timeout (assoc-ref opts 'timeout)
b6b097ac 361 #:print-build-trace (assoc-ref opts 'print-build-trace?)
e7fc17b5 362 #:verbosity (assoc-ref opts 'verbosity)))
14a1c319 363
2d977638
DT
364(define set-build-options-from-command-line*
365 (store-lift set-build-options-from-command-line))
366
e7fc17b5
LC
367(define %standard-build-options
368 ;; List of standard command-line options for tools that build something.
300868ba
LC
369 (list (option '(#\L "load-path") #t #f
370 (lambda (opt name arg result . rest)
371 ;; XXX: Imperatively modify the search paths.
372 (%package-module-path (cons arg (%package-module-path)))
223d7939 373 (%patch-path (cons arg (%patch-path)))
300868ba
LC
374 (set! %load-path (cons arg %load-path))
375 (set! %load-compiled-path (cons arg %load-compiled-path))
376
377 (apply values (cons result rest))))
378 (option '(#\K "keep-failed") #f #f
dd67b429
LC
379 (lambda (opt name arg result . rest)
380 (apply values
381 (alist-cons 'keep-failed? #t result)
382 rest)))
f4953019
LC
383 (option '(#\k "keep-going") #f #f
384 (lambda (opt name arg result . rest)
385 (apply values
386 (alist-cons 'keep-going? #t result)
387 rest)))
5b74fe06
LC
388 (option '("rounds") #t #f
389 (lambda (opt name arg result . rest)
390 (apply values
391 (alist-cons 'rounds (string->number* arg)
392 result)
393 rest)))
56b1f4b7 394 (option '("fallback") #f #f
dd67b429
LC
395 (lambda (opt name arg result . rest)
396 (apply values
397 (alist-cons 'fallback? #t
398 (alist-delete 'fallback? result))
399 rest)))
692c6c15 400 (option '("no-substitutes") #f #f
dd67b429
LC
401 (lambda (opt name arg result . rest)
402 (apply values
403 (alist-cons 'substitutes? #f
404 (alist-delete 'substitutes? result))
405 rest)))
f8a8e0fe
LC
406 (option '("substitute-urls") #t #f
407 (lambda (opt name arg result . rest)
408 (apply values
409 (alist-cons 'substitute-urls
410 (string-tokenize arg)
411 (alist-delete 'substitute-urls result))
412 rest)))
7573d30f
LC
413 (option '("no-grafts") #f #f
414 (lambda (opt name arg result . rest)
415 (apply values
416 (alist-cons 'graft? #f
417 (alist-delete 'graft? result eq?))
418 rest)))
425b0bfc 419 (option '("no-build-hook") #f #f
dd67b429
LC
420 (lambda (opt name arg result . rest)
421 (apply values
422 (alist-cons 'build-hook? #f
423 (alist-delete 'build-hook? result))
424 rest)))
969e678e 425 (option '("max-silent-time") #t #f
dd67b429
LC
426 (lambda (opt name arg result . rest)
427 (apply values
428 (alist-cons 'max-silent-time (string->number* arg)
429 result)
430 rest)))
002622b6
LC
431 (option '("timeout") #t #f
432 (lambda (opt name arg result . rest)
433 (apply values
434 (alist-cons 'timeout (string->number* arg) result)
435 rest)))
07ab4bf1 436 (option '("verbosity") #t #f
dd67b429 437 (lambda (opt name arg result . rest)
07ab4bf1 438 (let ((level (string->number arg)))
dd67b429
LC
439 (apply values
440 (alist-cons 'verbosity level
441 (alist-delete 'verbosity result))
442 rest))))
e7fc17b5 443 (option '(#\c "cores") #t #f
dd67b429 444 (lambda (opt name arg result . rest)
e7fc17b5
LC
445 (let ((c (false-if-exception (string->number arg))))
446 (if c
dd67b429 447 (apply values (alist-cons 'cores c result) rest)
69daee23 448 (leave (G_ "not a number: '~a' option argument: ~a~%")
f6526eb3
LC
449 name arg)))))
450 (option '(#\M "max-jobs") #t #f
451 (lambda (opt name arg result . rest)
452 (let ((c (false-if-exception (string->number arg))))
453 (if c
454 (apply values (alist-cons 'max-jobs c result) rest)
69daee23 455 (leave (G_ "not a number: '~a' option argument: ~a~%")
f6526eb3 456 name arg)))))))
e7fc17b5
LC
457
458\f
459;;;
460;;; Command-line options.
461;;;
462
463(define %default-options
464 ;; Alist of default option values.
465 `((system . ,(%current-system))
a8d65643 466 (build-mode . ,(build-mode normal))
05962f29 467 (graft? . #t)
e7fc17b5
LC
468 (substitutes? . #t)
469 (build-hook? . #t)
b6b097ac 470 (print-build-trace? . #t)
e7fc17b5
LC
471 (verbosity . 0)))
472
473(define (show-help)
69daee23 474 (display (G_ "Usage: guix build [OPTION]... PACKAGE-OR-DERIVATION...
e7fc17b5 475Build the given PACKAGE-OR-DERIVATION and return their output paths.\n"))
69daee23 476 (display (G_ "
e7fc17b5 477 -e, --expression=EXPR build the package or derivation EXPR evaluates to"))
69daee23 478 (display (G_ "
34a1783f
DT
479 -f, --file=FILE build the package or derivation that the code within
480 FILE evaluates to"))
69daee23 481 (display (G_ "
e7fc17b5 482 -S, --source build the packages' source derivations"))
69daee23 483 (display (G_ "
2087023d 484 --sources[=TYPE] build source derivations; TYPE may optionally be one
2cdfe13d 485 of \"package\", \"all\" (default), or \"transitive\""))
69daee23 486 (display (G_ "
e7fc17b5 487 -s, --system=SYSTEM attempt to build for SYSTEM--e.g., \"i686-linux\""))
69daee23 488 (display (G_ "
e7fc17b5 489 --target=TRIPLET cross-build for TRIPLET--e.g., \"armel-linux-gnu\""))
69daee23 490 (display (G_ "
e7fc17b5 491 -d, --derivations return the derivation paths of the given packages"))
69daee23 492 (display (G_ "
a8d65643 493 --check rebuild items to check for non-determinism issues"))
69daee23 494 (display (G_ "
6da5bb7b 495 --repair repair the specified items"))
69daee23 496 (display (G_ "
e7fc17b5
LC
497 -r, --root=FILE make FILE a symlink to the result, and register it
498 as a garbage collector root"))
69daee23 499 (display (G_ "
5284339d 500 -q, --quiet do not show the build log"))
69daee23 501 (display (G_ "
e7fc17b5
LC
502 --log-file return the log file names for the given derivations"))
503 (newline)
504 (show-build-options-help)
505 (newline)
88ad6ded
LC
506 (show-transformation-options-help)
507 (newline)
69daee23 508 (display (G_ "
e7fc17b5 509 -h, --help display this help and exit"))
69daee23 510 (display (G_ "
e7fc17b5
LC
511 -V, --version display version information and exit"))
512 (newline)
513 (show-bug-report-information))
514
515(define %options
516 ;; Specifications of the command-line options.
517 (cons* (option '(#\h "help") #f #f
518 (lambda args
519 (show-help)
520 (exit 0)))
521 (option '(#\V "version") #f #f
522 (lambda args
523 (show-version-and-exit "guix build")))
e7fc17b5
LC
524 (option '(#\S "source") #f #f
525 (lambda (opt name arg result)
2cdfe13d
EB
526 (alist-cons 'source #t result)))
527 (option '("sources") #f #t
528 (lambda (opt name arg result)
529 (match arg
530 ("package"
531 (alist-cons 'source #t result))
532 ((or "all" #f)
533 (alist-cons 'source package-direct-sources result))
534 ("transitive"
535 (alist-cons 'source package-transitive-sources result))
536 (else
69daee23 537 (leave (G_ "invalid argument: '~a' option argument: ~a, ~
2cdfe13d
EB
538must be one of 'package', 'all', or 'transitive'~%")
539 name arg)))))
88ad6ded
LC
540 (option '("check") #f #f
541 (lambda (opt name arg result . rest)
542 (apply values
543 (alist-cons 'build-mode (build-mode check)
544 result)
545 rest)))
6da5bb7b
LC
546 (option '("repair") #f #f
547 (lambda (opt name arg result . rest)
548 (apply values
549 (alist-cons 'build-mode (build-mode repair)
550 result)
551 rest)))
e7fc17b5
LC
552 (option '(#\s "system") #t #f
553 (lambda (opt name arg result)
554 (alist-cons 'system arg
555 (alist-delete 'system result eq?))))
556 (option '("target") #t #f
557 (lambda (opt name arg result)
558 (alist-cons 'target arg
559 (alist-delete 'target result eq?))))
560 (option '(#\d "derivations") #f #f
561 (lambda (opt name arg result)
562 (alist-cons 'derivations-only? #t result)))
563 (option '(#\e "expression") #t #f
564 (lambda (opt name arg result)
565 (alist-cons 'expression arg result)))
34a1783f
DT
566 (option '(#\f "file") #t #f
567 (lambda (opt name arg result)
568 (alist-cons 'file arg result)))
e7fc17b5
LC
569 (option '(#\n "dry-run") #f #f
570 (lambda (opt name arg result)
fd59105c 571 (alist-cons 'dry-run? #t (alist-cons 'graft? #f result))))
e7fc17b5
LC
572 (option '(#\r "root") #t #f
573 (lambda (opt name arg result)
574 (alist-cons 'gc-root arg result)))
5284339d
LC
575 (option '(#\q "quiet") #f #f
576 (lambda (opt name arg result)
577 (alist-cons 'quiet? #t result)))
e7fc17b5
LC
578 (option '("log-file") #f #f
579 (lambda (opt name arg result)
580 (alist-cons 'log-file? #t result)))
581
88ad6ded
LC
582 (append %transformation-options
583 %standard-build-options)))
14a1c319 584
64ec0e29
LC
585(define (options->things-to-build opts)
586 "Read the arguments from OPTS and return a list of high-level objects to
587build---packages, gexps, derivations, and so on."
20464dde
LC
588 (define (validate-type x)
589 (unless (or (package? x) (derivation? x) (gexp? x) (procedure? x))
69daee23 590 (leave (G_ "~s: not something we can build~%") x)))
20464dde
LC
591
592 (define (ensure-list x)
593 (let ((lst (match x
594 ((x ...) x)
595 (x (list x)))))
596 (for-each validate-type lst)
597 lst))
64ec0e29
LC
598
599 (append-map (match-lambda
600 (('argument . (? string? spec))
601 (cond ((derivation-path? spec)
602 (list (call-with-input-file spec read-derivation)))
603 ((store-path? spec)
604 ;; Nothing to do; maybe for --log-file.
605 '())
606 (else
607 (list (specification->package spec)))))
608 (('file . file)
609 (ensure-list (load* file (make-user-module '()))))
610 (('expression . str)
611 (ensure-list (read/eval str)))
612 (('argument . (? derivation? drv))
613 drv)
64ec0e29
LC
614 (_ '()))
615 opts))
616
81fa80b2
LC
617(define (options->derivations store opts)
618 "Given OPTS, the result of 'args-fold', return a list of derivations to
619build."
64ec0e29
LC
620 (define transform
621 (options->transformation opts))
622
81fa80b2
LC
623 (define package->derivation
624 (match (assoc-ref opts 'target)
625 (#f package-derivation)
626 (triplet
627 (cut package-cross-derivation <> <> triplet <>))))
628
2cdfe13d 629 (define src (assoc-ref opts 'source))
64ec0e29 630 (define system (assoc-ref opts 'system))
05962f29 631 (define graft? (assoc-ref opts 'graft?))
81fa80b2 632
05962f29 633 (parameterize ((%graft? graft?))
64ec0e29
LC
634 (append-map (match-lambda
635 ((? package? p)
94d609ab
LC
636 (let ((p (or (and graft? (package-replacement p)) p)))
637 (match src
638 (#f
639 (list (package->derivation store p system)))
640 (#t
8a54c0ec
LC
641 (match (package-source p)
642 (#f
643 (format (current-error-port)
69daee23 644 (G_ "~a: warning: \
8a54c0ec
LC
645package '~a' has no source~%")
646 (location->string (package-location p))
647 (package-name p))
648 '())
649 (s
650 (list (package-source-derivation store s)))))
94d609ab
LC
651 (proc
652 (map (cut package-source-derivation store <>)
653 (proc p))))))
64ec0e29
LC
654 ((? derivation? drv)
655 (list drv))
656 ((? procedure? proc)
657 (list (run-with-store store
658 (mbegin %store-monad
659 (set-guile-for-build (default-guile))
660 (proc))
661 #:system system)))
662 ((? gexp? gexp)
663 (list (run-with-store store
664 (mbegin %store-monad
665 (set-guile-for-build (default-guile))
666 (gexp->derivation "gexp" gexp
667 #:system system))))))
629a064f
LC
668 (map (cut transform store <>)
669 (options->things-to-build opts)))))
64ec0e29 670
841cb43c
LC
671(define (show-build-log store file urls)
672 "Show the build log for FILE, falling back to remote logs from URLS if
673needed."
674 (let ((log (or (log-file store file)
675 (log-url store file #:base-urls urls))))
676 (if log
677 (format #t "~a~%" log)
69daee23 678 (leave (G_ "no build log for '~a'~%") file))))
841cb43c 679
14a1c319
LC
680\f
681;;;
682;;; Entry point.
683;;;
684
685(define (guix-build . args)
c8f9f247
LC
686 (define opts
687 (parse-command-line args %options
688 (list %default-options)))
689
5284339d
LC
690 (define quiet?
691 (assoc-ref opts 'quiet?))
692
073c34d7 693 (with-error-handling
bf421152
LC
694 ;; Ask for absolute file names so that .drv file names passed from the
695 ;; user to 'read-derivation' are absolute when it returns.
696 (with-fluids ((%file-port-name-canonicalization 'absolute))
c8f9f247
LC
697 (with-store store
698 ;; Set the build options before we do anything else.
e7fc17b5 699 (set-build-options-from-command-line store opts)
c8f9f247 700
5284339d
LC
701 (parameterize ((current-build-output-port (if quiet?
702 (%make-void-port "w")
703 (current-error-port))))
704 (let* ((mode (assoc-ref opts 'build-mode))
705 (drv (options->derivations store opts))
706 (urls (map (cut string-append <> "/log")
707 (if (assoc-ref opts 'substitutes?)
708 (or (assoc-ref opts 'substitute-urls)
709 ;; XXX: This does not necessarily match the
710 ;; daemon's substitute URLs.
711 %default-substitute-urls)
712 '())))
713 (items (filter-map (match-lambda
714 (('argument . (? store-path? file))
715 file)
716 (_ #f))
717 opts))
718 (roots (filter-map (match-lambda
719 (('gc-root . root) root)
720 (_ #f))
721 opts)))
722
6e94a574
LC
723 (unless (or (assoc-ref opts 'log-file?)
724 (assoc-ref opts 'derivations-only?))
5284339d
LC
725 (show-what-to-build store drv
726 #:use-substitutes?
727 (assoc-ref opts 'substitutes?)
728 #:dry-run? (assoc-ref opts 'dry-run?)
729 #:mode mode))
730
731 (cond ((assoc-ref opts 'log-file?)
732 (for-each (cut show-build-log store <> urls)
733 (delete-duplicates
734 (append (map derivation-file-name drv)
735 items))))
736 ((assoc-ref opts 'derivations-only?)
737 (format #t "~{~a~%~}" (map derivation-file-name drv))
738 (for-each (cut register-root store <> <>)
739 (map (compose list derivation-file-name) drv)
740 roots))
741 ((not (assoc-ref opts 'dry-run?))
742 (and (build-derivations store drv mode)
743 (for-each show-derivation-outputs drv)
744 (for-each (cut register-root store <> <>)
745 (map (lambda (drv)
746 (map cdr
747 (derivation->output-paths drv)))
748 drv)
749 roots))))))))))