gexp: 'lower-object' raises an exception when passed an invalid object.
[jackhill/guix/guix.git] / guix / packages.scm
CommitLineData
233e7676 1;;; GNU Guix --- Functional package management for GNU
3b0fcc67 2;;; Copyright © 2012, 2013, 2014, 2015, 2016, 2017 Ludovic Courtès <ludo@gnu.org>
2e9511df 3;;; Copyright © 2014, 2015 Mark H Weaver <mhw@netris.org>
a6d0b306 4;;; Copyright © 2015 Eric Bavier <bavier@member.fsf.org>
8d65c71f 5;;; Copyright © 2016 Alex Kost <alezost@gmail.com>
e3ce5d70 6;;;
233e7676 7;;; This file is part of GNU Guix.
e3ce5d70 8;;;
233e7676 9;;; GNU Guix is free software; you can redistribute it and/or modify it
e3ce5d70
LC
10;;; under the terms of the GNU General Public License as published by
11;;; the Free Software Foundation; either version 3 of the License, or (at
12;;; your option) any later version.
13;;;
233e7676 14;;; GNU Guix is distributed in the hope that it will be useful, but
e3ce5d70
LC
15;;; WITHOUT ANY WARRANTY; without even the implied warranty of
16;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17;;; GNU General Public License for more details.
18;;;
19;;; You should have received a copy of the GNU General Public License
233e7676 20;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
e3ce5d70
LC
21
22(define-module (guix packages)
23 #:use-module (guix utils)
c0cd1b3e 24 #:use-module (guix records)
e3ce5d70 25 #:use-module (guix store)
e87f0591 26 #:use-module (guix monads)
ff40e9b7 27 #:use-module (guix gexp)
ddc29a78 28 #:use-module (guix base32)
7adf9b84 29 #:use-module (guix grafts)
d510ab46 30 #:use-module (guix derivations)
c9134e82 31 #:use-module (guix memoization)
e3ce5d70 32 #:use-module (guix build-system)
e89431bf 33 #:use-module (guix search-paths)
cf87cc89 34 #:use-module (guix gexp)
c22a1324 35 #:use-module (guix sets)
e3ce5d70 36 #:use-module (ice-9 match)
c37a74bd 37 #:use-module (ice-9 vlist)
062c6927 38 #:use-module (srfi srfi-1)
946b72c9 39 #:use-module (srfi srfi-9 gnu)
05962f29 40 #:use-module (srfi srfi-11)
a63062b5 41 #:use-module (srfi srfi-26)
d36622dc
LC
42 #:use-module (srfi srfi-34)
43 #:use-module (srfi srfi-35)
3b4d0103 44 #:use-module (web uri)
cd52703a 45 #:re-export (%current-system
e89431bf
LC
46 %current-target-system
47 search-path-specification) ;for convenience
ff352cfb 48 #:export (origin
90c68be8
LC
49 origin?
50 origin-uri
51 origin-method
52 origin-sha256
53 origin-file-name
3b4d0103 54 origin-actual-file-name
ac10e0e1
LC
55 origin-patches
56 origin-patch-flags
57 origin-patch-inputs
58 origin-patch-guile
f9cc8971
LC
59 origin-snippet
60 origin-modules
e4c245f8 61 base32
e3ce5d70
LC
62
63 package
64 package?
65 package-name
3b0fcc67 66 package-upstream-name
e3ce5d70 67 package-version
2847050a 68 package-full-name
e3ce5d70
LC
69 package-source
70 package-build-system
71 package-arguments
72 package-inputs
73 package-native-inputs
062c6927 74 package-propagated-inputs
e3ce5d70 75 package-outputs
a18eda27 76 package-native-search-paths
e3ce5d70 77 package-search-paths
05962f29 78 package-replacement
d45122f5 79 package-synopsis
e3ce5d70 80 package-description
e3ce5d70 81 package-license
52bda18a 82 package-home-page
4e097f86 83 package-supported-systems
e3ce5d70 84 package-maintainers
062c6927 85 package-properties
35f3c5f5 86 package-location
6980511b
LC
87 hidden-package
88 hidden-package?
01afdab8
LC
89 package-superseded
90 deprecated-package
d66c7096 91 package-field-location
e3ce5d70 92
f77bcbc3
EB
93 package-direct-sources
94 package-transitive-sources
7d193ec3 95 package-direct-inputs
a3d73f59 96 package-transitive-inputs
9c1edabd
LC
97 package-transitive-target-inputs
98 package-transitive-native-inputs
113aef68 99 package-transitive-propagated-inputs
aa8e0515 100 package-transitive-native-search-paths
7c3c0374 101 package-transitive-supported-systems
2a75b0b6 102 package-input-rewriting
e3ce5d70
LC
103 package-source-derivation
104 package-derivation
d36622dc 105 package-cross-derivation
d510ab46 106 package-output
05962f29 107 package-grafts
d36622dc 108
a6d0b306
EB
109 transitive-input-references
110
4e097f86 111 %supported-systems
035b6ff7 112 %hurd-systems
abcbda48 113 %hydra-supported-systems
bbceb0ef 114 supported-package?
4e097f86 115
d36622dc 116 &package-error
07783858 117 package-error?
d36622dc
LC
118 package-error-package
119 &package-input-error
07783858 120 package-input-error?
9b222abe
LC
121 package-error-invalid-input
122 &package-cross-build-system-error
0d5a559f
LC
123 package-cross-build-system-error?
124
125 package->bag
d3d337d2 126 bag->derivation
cceab875 127 bag-direct-inputs
0d5a559f
LC
128 bag-transitive-inputs
129 bag-transitive-host-inputs
130 bag-transitive-build-inputs
e87f0591
LC
131 bag-transitive-target-inputs
132
133 default-guile
ff40e9b7 134 default-guile-derivation
e87f0591
LC
135 set-guile-for-build
136 package-file
137 package->derivation
138 package->cross-derivation
139 origin->derivation))
e3ce5d70
LC
140
141;;; Commentary:
142;;;
143;;; This module provides a high-level mechanism to define packages in a
144;;; Guix-based distribution.
145;;;
146;;; Code:
147
90c68be8
LC
148;; The source of a package, such as a tarball URL and fetcher---called
149;; "origin" to avoid name clash with `package-source', `source', etc.
150(define-record-type* <origin>
151 origin make-origin
152 origin?
153 (uri origin-uri) ; string
9b5b5c17 154 (method origin-method) ; procedure
90c68be8 155 (sha256 origin-sha256) ; bytevector
ac10e0e1 156 (file-name origin-file-name (default #f)) ; optional file name
6b1f9721
LC
157
158 ;; Patches are delayed so that the 'search-patch' calls are made lazily,
159 ;; which reduces I/O on startup and allows patch-not-found errors to be
160 ;; gracefully handled at run time.
161 (patches origin-patches ; list of file names
162 (default '()) (delayed))
163
f9cc8971 164 (snippet origin-snippet (default #f)) ; sexp or #f
ac10e0e1
LC
165 (patch-flags origin-patch-flags ; list of strings
166 (default '("-p1")))
1d9bc459
LC
167
168 ;; Patching requires Guile, GNU Patch, and a few more. These two fields are
169 ;; used to specify these dependencies when needed.
ac10e0e1
LC
170 (patch-inputs origin-patch-inputs ; input list or #f
171 (default #f))
f9cc8971
LC
172 (modules origin-modules ; list of module names
173 (default '()))
1929fdba 174
1d9bc459 175 (patch-guile origin-patch-guile ; package or #f
ac10e0e1 176 (default #f)))
e3ce5d70 177
f1096964
LC
178(define (print-origin origin port)
179 "Write a concise representation of ORIGIN to PORT."
180 (match origin
181 (($ <origin> uri method sha256 file-name patches)
182 (simple-format port "#<origin ~s ~a ~s ~a>"
183 uri (bytevector->base32-string sha256)
6b1f9721 184 (force patches)
f1096964
LC
185 (number->string (object-address origin) 16)))))
186
187(set-record-type-printer! <origin> print-origin)
188
e4c245f8
LC
189(define-syntax base32
190 (lambda (s)
191 "Return the bytevector corresponding to the given Nix-base32
192representation."
193 (syntax-case s ()
194 ((_ str)
195 (string? (syntax->datum #'str))
aba326f7 196 ;; A literal string: do the conversion at expansion time.
e4c245f8
LC
197 (with-syntax ((bv (nix-base32-string->bytevector
198 (syntax->datum #'str))))
aba326f7
LC
199 #''bv))
200 ((_ str)
201 #'(nix-base32-string->bytevector str)))))
e4c245f8 202
3b4d0103
EB
203(define (origin-actual-file-name origin)
204 "Return the file name of ORIGIN, either its 'file-name' field or the file
205name of its URI."
206 (define (uri->file-name uri)
207 ;; Return the 'base name' of URI or URI itself, where URI is a string.
208 (let ((path (and=> (string->uri uri) uri-path)))
209 (if path
210 (basename path)
211 uri)))
212
213 (or (origin-file-name origin)
214 (match (origin-uri origin)
215 ((head . tail)
216 (uri->file-name head))
217 ((? string? uri)
218 (uri->file-name uri))
219 (else
220 ;; git, svn, cvs, etc. reference
221 #f))))
222
4e097f86
LC
223(define %supported-systems
224 ;; This is the list of system types that are supported. By default, we
225 ;; expect all packages to build successfully here.
fc34deea 226 '("x86_64-linux" "i686-linux" "armhf-linux" "mips64el-linux"))
4e097f86 227
035b6ff7
LC
228(define %hurd-systems
229 ;; The GNU/Hurd systems for which support is being developed.
230 '("i585-gnu" "i686-gnu"))
231
abcbda48 232(define %hydra-supported-systems
cdb3f734
LC
233 ;; This is the list of system types for which build machines are available.
234 ;;
235 ;; XXX: MIPS is temporarily unavailable on Hydra:
236 ;; <https://lists.gnu.org/archive/html/guix-devel/2017-03/msg00790.html>.
237 (delete "mips64el-linux" %supported-systems))
abcbda48
LC
238
239
a18eda27 240;; A package.
e3ce5d70
LC
241(define-record-type* <package>
242 package make-package
243 package?
244 (name package-name) ; string
245 (version package-version) ; string
90c68be8 246 (source package-source) ; <origin> instance
e3ce5d70 247 (build-system package-build-system) ; build system
64fddd74 248 (arguments package-arguments ; arguments for the build method
21c203a5 249 (default '()) (thunked))
062c6927 250
e3ce5d70 251 (inputs package-inputs ; input packages or derivations
dd6b9a37 252 (default '()) (thunked))
062c6927 253 (propagated-inputs package-propagated-inputs ; same, but propagated
9d97a1b3 254 (default '()) (thunked))
e3ce5d70 255 (native-inputs package-native-inputs ; native input packages/derivations
a7dc055b 256 (default '()) (thunked))
c9d01150
LC
257 (self-native-input? package-self-native-input? ; whether to use itself as
258 ; a native input when cross-
259 (default #f)) ; compiling
062c6927 260
e3ce5d70
LC
261 (outputs package-outputs ; list of strings
262 (default '("out")))
a18eda27
LC
263
264 ; lists of
265 ; <search-path-specification>,
266 ; for native and cross
267 ; inputs
268 (native-search-paths package-native-search-paths (default '()))
269 (search-paths package-search-paths (default '()))
05962f29
LC
270 (replacement package-replacement ; package | #f
271 (default #f) (thunked))
e3ce5d70 272
d45122f5
LC
273 (synopsis package-synopsis) ; one-line description
274 (description package-description) ; one or two paragraphs
1fb78cb2 275 (license package-license)
45753b65 276 (home-page package-home-page)
4e097f86
LC
277 (supported-systems package-supported-systems ; list of strings
278 (default %supported-systems))
35f3c5f5 279 (maintainers package-maintainers (default '()))
45753b65 280
062c6927
LC
281 (properties package-properties (default '())) ; alist for anything else
282
35f3c5f5
LC
283 (location package-location
284 (default (and=> (current-source-location)
0004c590
LC
285 source-properties->location))
286 (innate)))
e3ce5d70 287
946b72c9
LC
288(set-record-type-printer! <package>
289 (lambda (package port)
290 (let ((loc (package-location package))
291 (format simple-format))
74e667d1 292 (format port "#<package ~a@~a ~a~a>"
946b72c9
LC
293 (package-name package)
294 (package-version package)
2e1bafb0
LC
295 (if loc
296 (format #f "~a:~a "
297 (location-file loc)
298 (location-line loc))
299 "")
946b72c9
LC
300 (number->string (object-address
301 package)
302 16)))))
303
3b0fcc67
LC
304(define (package-upstream-name package)
305 "Return the upstream name of PACKAGE, which could be different from the name
306it has in Guix."
307 (or (assq-ref (package-properties package) 'upstream-name)
308 (package-name package)))
309
6980511b
LC
310(define (hidden-package p)
311 "Return a \"hidden\" version of P--i.e., one that 'fold-packages' and thus,
312user interfaces, ignores."
313 (package
314 (inherit p)
315 (properties `((hidden? . #t)
316 ,@(package-properties p)))))
317
318(define (hidden-package? p)
319 "Return true if P is \"hidden\"--i.e., must not be visible to user
320interfaces."
321 (assoc-ref (package-properties p) 'hidden?))
322
01afdab8
LC
323(define (package-superseded p)
324 "Return the package the supersedes P, or #f if P is still current."
325 (assoc-ref (package-properties p) 'superseded))
326
327(define (deprecated-package old-name p)
328 "Return a package called OLD-NAME and marked as superseded by P, a package
329object."
330 (package
331 (inherit p)
332 (name old-name)
333 (properties `((superseded . ,p)))))
334
d66c7096 335(define (package-field-location package field)
f903dc05
LC
336 "Return the source code location of the definition of FIELD for PACKAGE, or
337#f if it could not be determined."
338 (define (goto port line column)
339 (unless (and (= (port-column port) (- column 1))
340 (= (port-line port) (- line 1)))
341 (unless (eof-object? (read-char port))
342 (goto port line column))))
d66c7096
LC
343
344 (match (package-location package)
345 (($ <location> file line column)
346 (catch 'system
347 (lambda ()
0b8749b7
LC
348 ;; In general we want to keep relative file names for modules.
349 (with-fluids ((%file-port-name-canonicalization 'relative))
350 (call-with-input-file (search-path %load-path file)
351 (lambda (port)
352 (goto port line column)
353 (match (read port)
354 (('package inits ...)
355 (let ((field (assoc field inits)))
356 (match field
357 ((_ value)
358 ;; Put the `or' here, and not in the first argument of
359 ;; `and=>', to work around a compiler bug in 2.0.5.
360 (or (and=> (source-properties value)
361 source-properties->location)
362 (and=> (source-properties field)
363 source-properties->location)))
364 (_
365 #f))))
366 (_
367 #f))))))
d66c7096 368 (lambda _
f903dc05 369 #f)))
d66c7096
LC
370 (_ #f)))
371
d36622dc
LC
372
373;; Error conditions.
374
375(define-condition-type &package-error &error
376 package-error?
377 (package package-error-package))
378
379(define-condition-type &package-input-error &package-error
380 package-input-error?
381 (input package-error-invalid-input))
382
9b222abe
LC
383(define-condition-type &package-cross-build-system-error &package-error
384 package-cross-build-system-error?)
385
d36622dc 386
2847050a
LC
387(define (package-full-name package)
388 "Return the full name of PACKAGE--i.e., `NAME-VERSION'."
389 (string-append (package-name package) "-" (package-version package)))
390
ac10e0e1 391(define (%standard-patch-inputs)
5ae4169c
LC
392 (let* ((canonical (module-ref (resolve-interface '(gnu packages base))
393 'canonical-package))
394 (ref (lambda (module var)
395 (canonical
396 (module-ref (resolve-interface module) var)))))
ac10e0e1
LC
397 `(("tar" ,(ref '(gnu packages base) 'tar))
398 ("xz" ,(ref '(gnu packages compression) 'xz))
399 ("bzip2" ,(ref '(gnu packages compression) 'bzip2))
400 ("gzip" ,(ref '(gnu packages compression) 'gzip))
401 ("lzip" ,(ref '(gnu packages compression) 'lzip))
17287d7d 402 ("unzip" ,(ref '(gnu packages zip) 'unzip))
9cca706c 403 ("patch" ,(ref '(gnu packages base) 'patch))
5ae4169c 404 ("locales" ,(ref '(gnu packages base) 'glibc-utf8-locales)))))
ac10e0e1 405
1d9bc459 406(define (default-guile)
e87f0591
LC
407 "Return the default Guile package used to run the build code of
408derivations."
bdb36958 409 (let ((distro (resolve-interface '(gnu packages commencement))))
1d9bc459 410 (module-ref distro 'guile-final)))
ac10e0e1 411
ff40e9b7
LC
412(define* (default-guile-derivation #:optional (system (%current-system)))
413 "Return the derivation for SYSTEM of the default Guile package used to run
414the build code of derivation."
415 (package->derivation (default-guile) system
416 #:graft? #f))
417
cf87cc89 418(define* (patch-and-repack source patches
ac10e0e1 419 #:key
a158484d 420 inputs
f9cc8971 421 (snippet #f)
ac10e0e1 422 (flags '("-p1"))
f9cc8971 423 (modules '())
ac10e0e1
LC
424 (guile-for-build (%guile-for-build))
425 (system (%current-system)))
f9cc8971
LC
426 "Unpack SOURCE (a derivation or store path), apply all of PATCHES, and
427repack the tarball using the tools listed in INPUTS. When SNIPPET is true,
428it must be an s-expression that will run from within the directory where
1929fdba
LC
429SOURCE was unpacked, after all of PATCHES have been applied. MODULES
430specifies modules in scope when evaluating SNIPPET."
f9cc8971
LC
431 (define source-file-name
432 ;; SOURCE is usually a derivation, but it could be a store file.
433 (if (derivation? source)
434 (derivation->output-path source)
435 source))
436
a158484d
LC
437 (define lookup-input
438 ;; The default value of the 'patch-inputs' field, and thus INPUTS is #f,
439 ;; so deal with that.
440 (let ((inputs (or inputs (%standard-patch-inputs))))
441 (lambda (name)
442 (match (assoc-ref inputs name)
443 ((package) package)
444 (#f #f)))))
cf87cc89 445
ac10e0e1 446 (define decompression-type
f9cc8971 447 (cond ((string-suffix? "gz" source-file-name) "gzip")
5257ab6d 448 ((string-suffix? "Z" source-file-name) "gzip")
f9cc8971
LC
449 ((string-suffix? "bz2" source-file-name) "bzip2")
450 ((string-suffix? "lz" source-file-name) "lzip")
17287d7d 451 ((string-suffix? "zip" source-file-name) "unzip")
f9cc8971 452 (else "xz")))
ac10e0e1
LC
453
454 (define original-file-name
f9cc8971
LC
455 ;; Remove the store prefix plus the slash, hash, and hyphen.
456 (let* ((sans (string-drop source-file-name
457 (+ (string-length (%store-prefix)) 1)))
458 (dash (string-index sans #\-)))
459 (string-drop sans (+ 1 dash))))
ac10e0e1 460
3ca00bb5
LC
461 (define (numeric-extension? file-name)
462 ;; Return true if FILE-NAME ends with digits.
857ecb3d
LC
463 (and=> (file-extension file-name)
464 (cut string-every char-set:hex-digit <>)))
3ca00bb5
LC
465
466 (define (tarxz-name file-name)
467 ;; Return a '.tar.xz' file name based on FILE-NAME.
468 (let ((base (if (numeric-extension? file-name)
469 original-file-name
470 (file-sans-extension file-name))))
471 (string-append base
472 (if (equal? (file-extension base) "tar")
473 ".xz"
474 ".tar.xz"))))
475
cf87cc89
LC
476 (define instantiate-patch
477 (match-lambda
478 ((? string? patch)
479 (interned-file patch #:recursive? #t))
480 ((? origin? patch)
481 (origin->derivation patch system))))
482
483 (mlet %store-monad ((tar -> (lookup-input "tar"))
484 (xz -> (lookup-input "xz"))
485 (patch -> (lookup-input "patch"))
486 (locales -> (lookup-input "locales"))
487 (decomp -> (lookup-input decompression-type))
488 (patches (sequence %store-monad
489 (map instantiate-patch patches))))
490 (define build
1929fdba
LC
491 (with-imported-modules '((guix build utils))
492 #~(begin
493 (use-modules (ice-9 ftw)
494 (srfi srfi-1)
495 (guix build utils))
496
497 ;; The --sort option was added to GNU tar in version 1.28, released
498 ;; 2014-07-28. During bootstrap we must cope with older versions.
499 (define tar-supports-sort?
500 (zero? (system* (string-append #+tar "/bin/tar")
501 "cf" "/dev/null" "--files-from=/dev/null"
502 "--sort=name")))
503
504 (define (apply-patch patch)
505 (format (current-error-port) "applying '~a'...~%" patch)
506
507 ;; Use '--force' so that patches that do not apply perfectly are
8d65c71f
AK
508 ;; rejected. Use '--no-backup-if-mismatch' to prevent making
509 ;; "*.orig" file if a patch is applied with offset.
1929fdba 510 (zero? (system* (string-append #+patch "/bin/patch")
8d65c71f
AK
511 "--force" "--no-backup-if-mismatch"
512 #+@flags "--input" patch)))
1929fdba
LC
513
514 (define (first-file directory)
515 ;; Return the name of the first file in DIRECTORY.
516 (car (scandir directory
517 (lambda (name)
518 (not (member name '("." "..")))))))
519
520 ;; Encoding/decoding errors shouldn't be silent.
521 (fluid-set! %default-port-conversion-strategy 'error)
522
523 (when #+locales
524 ;; First of all, install a UTF-8 locale so that UTF-8 file names
525 ;; are correctly interpreted. During bootstrap, LOCALES is #f.
526 (setenv "LOCPATH"
527 (string-append #+locales "/lib/locale/"
528 #+(and locales
529 (package-version locales))))
530 (setlocale LC_ALL "en_US.utf8"))
531
532 (setenv "PATH" (string-append #+xz "/bin" ":"
533 #+decomp "/bin"))
534
535 ;; SOURCE may be either a directory or a tarball.
536 (and (if (file-is-directory? #+source)
537 (let* ((store (%store-directory))
538 (len (+ 1 (string-length store)))
539 (base (string-drop #+source len))
540 (dash (string-index base #\-))
541 (directory (string-drop base (+ 1 dash))))
542 (mkdir directory)
543 (copy-recursively #+source directory)
544 #t)
545 #+(if (string=? decompression-type "unzip")
546 #~(zero? (system* "unzip" #+source))
547 #~(zero? (system* (string-append #+tar "/bin/tar")
548 "xvf" #+source))))
549 (let ((directory (first-file ".")))
550 (format (current-error-port)
551 "source is under '~a'~%" directory)
552 (chdir directory)
553
554 (and (every apply-patch '#+patches)
555 #+@(if snippet
556 #~((let ((module (make-fresh-user-module)))
557 (module-use-interfaces!
558 module
559 (map resolve-interface '#+modules))
560 ((@ (system base compile) compile)
561 '#+snippet
562 #:to 'value
563 #:opts %auto-compilation-options
564 #:env module)))
565 #~())
566
567 (begin (chdir "..") #t)
568
569 (unless tar-supports-sort?
570 (call-with-output-file ".file_list"
571 (lambda (port)
572 (for-each (lambda (name)
573 (format port "~a~%" name))
574 (find-files directory
575 #:directories? #t
576 #:fail-on-error? #t)))))
577 (zero? (apply system*
578 (string-append #+tar "/bin/tar")
579 "cvfa" #$output
580 ;; avoid non-determinism in the archive
581 "--mtime=@0"
582 "--owner=root:0"
583 "--group=root:0"
584 (if tar-supports-sort?
585 `("--sort=name"
586 ,directory)
587 '("--no-recursion"
588 "--files-from=.file_list"))))))))))
589
590 (let ((name (tarxz-name original-file-name)))
cf87cc89
LC
591 (gexp->derivation name build
592 #:graft? #f
593 #:system system
cf87cc89 594 #:guile-for-build guile-for-build))))
ac10e0e1 595
113aef68 596(define (transitive-inputs inputs)
161094c8
LC
597 "Return the closure of INPUTS when considering the 'propagated-inputs'
598edges. Omit duplicate inputs, except for those already present in INPUTS
599itself.
600
601This is implemented as a breadth-first traversal such that INPUTS is
602preserved, and only duplicate propagated inputs are removed."
603 (define (seen? seen item outputs)
604 (match (vhash-assq item seen)
605 ((_ . o) (equal? o outputs))
606 (_ #f)))
607
608 (let loop ((inputs inputs)
609 (result '())
610 (propagated '())
611 (first? #t)
612 (seen vlist-null))
a3d73f59
LC
613 (match inputs
614 (()
161094c8
LC
615 (if (null? propagated)
616 (reverse result)
617 (loop (reverse (concatenate propagated)) result '() #f seen)))
618 (((and input (label (? package? package) outputs ...)) rest ...)
619 (if (and (not first?) (seen? seen package outputs))
620 (loop rest result propagated first? seen)
621 (loop rest
622 (cons input result)
623 (cons (package-propagated-inputs package) propagated)
624 first?
625 (vhash-consq package outputs seen))))
a3d73f59 626 ((input rest ...)
161094c8 627 (loop rest (cons input result) propagated first? seen)))))
a3d73f59 628
f77bcbc3
EB
629(define (package-direct-sources package)
630 "Return all source origins associated with PACKAGE; including origins in
631PACKAGE's inputs."
632 `(,@(or (and=> (package-source package) list) '())
633 ,@(filter-map (match-lambda
634 ((_ (? origin? orig) _ ...)
635 orig)
636 (_ #f))
637 (package-direct-inputs package))))
638
639(define (package-transitive-sources package)
640 "Return PACKAGE's direct sources, and their direct sources, recursively."
641 (delete-duplicates
642 (concatenate (filter-map (match-lambda
643 ((_ (? origin? orig) _ ...)
644 (list orig))
645 ((_ (? package? p) _ ...)
646 (package-direct-sources p))
647 (_ #f))
648 (bag-transitive-inputs
649 (package->bag package))))))
650
7d193ec3
EB
651(define (package-direct-inputs package)
652 "Return all the direct inputs of PACKAGE---i.e, its direct inputs along
653with their propagated inputs."
654 (append (package-native-inputs package)
655 (package-inputs package)
656 (package-propagated-inputs package)))
657
113aef68
LC
658(define (package-transitive-inputs package)
659 "Return the transitive inputs of PACKAGE---i.e., its direct inputs along
660with their propagated inputs, recursively."
7d193ec3 661 (transitive-inputs (package-direct-inputs package)))
113aef68 662
9c1edabd
LC
663(define (package-transitive-target-inputs package)
664 "Return the transitive target inputs of PACKAGE---i.e., its direct inputs
665along with their propagated inputs, recursively. This only includes inputs
666for the target system, and not native inputs."
667 (transitive-inputs (append (package-inputs package)
668 (package-propagated-inputs package))))
669
670(define (package-transitive-native-inputs package)
671 "Return the transitive native inputs of PACKAGE---i.e., its direct inputs
672along with their propagated inputs, recursively. This only includes inputs
673for the host system (\"native inputs\"), and not target inputs."
674 (transitive-inputs (package-native-inputs package)))
675
113aef68
LC
676(define (package-transitive-propagated-inputs package)
677 "Return the propagated inputs of PACKAGE, and their propagated inputs,
678recursively."
679 (transitive-inputs (package-propagated-inputs package)))
680
aa8e0515
LC
681(define (package-transitive-native-search-paths package)
682 "Return the list of search paths for PACKAGE and its propagated inputs,
683recursively."
684 (append (package-native-search-paths package)
685 (append-map (match-lambda
686 ((label (? package? p) _ ...)
687 (package-native-search-paths p))
688 (_
689 '()))
690 (package-transitive-propagated-inputs package))))
691
a6d0b306
EB
692(define (transitive-input-references alist inputs)
693 "Return a list of (assoc-ref ALIST <label>) for each (<label> <package> . _)
694in INPUTS and their transitive propagated inputs."
695 (define label
696 (match-lambda
697 ((label . _)
698 label)))
699
700 (map (lambda (input)
701 `(assoc-ref ,alist ,(label input)))
702 (transitive-inputs inputs)))
703
c9134e82
LC
704(define package-transitive-supported-systems
705 (mlambdaq (package)
706 "Return the intersection of the systems supported by PACKAGE and those
7c3c0374 707supported by its dependencies."
c9134e82
LC
708 (fold (lambda (input systems)
709 (match input
710 ((label (? package? p) . _)
711 (lset-intersection
712 string=? systems (package-transitive-supported-systems p)))
713 (_
714 systems)))
715 (package-supported-systems package)
716 (bag-direct-inputs (package->bag package)))))
7c3c0374 717
bbceb0ef
LC
718(define* (supported-package? package #:optional (system (%current-system)))
719 "Return true if PACKAGE is supported on SYSTEM--i.e., if PACKAGE and all its
720dependencies are known to build on SYSTEM."
721 (member system (package-transitive-supported-systems package)))
722
cceab875
LC
723(define (bag-direct-inputs bag)
724 "Same as 'package-direct-inputs', but applied to a bag."
725 (append (bag-build-inputs bag)
726 (bag-host-inputs bag)
727 (bag-target-inputs bag)))
728
0d5a559f
LC
729(define (bag-transitive-inputs bag)
730 "Same as 'package-transitive-inputs', but applied to a bag."
cceab875 731 (transitive-inputs (bag-direct-inputs bag)))
0d5a559f
LC
732
733(define (bag-transitive-build-inputs bag)
734 "Same as 'package-transitive-native-inputs', but applied to a bag."
735 (transitive-inputs (bag-build-inputs bag)))
736
737(define (bag-transitive-host-inputs bag)
738 "Same as 'package-transitive-target-inputs', but applied to a bag."
739 (transitive-inputs (bag-host-inputs bag)))
740
741(define (bag-transitive-target-inputs bag)
742 "Return the \"target inputs\" of BAG, recursively."
743 (transitive-inputs (bag-target-inputs bag)))
744
2a75b0b6
LC
745(define* (package-input-rewriting replacements
746 #:optional (rewrite-name identity))
747 "Return a procedure that, when passed a package, replaces its direct and
748indirect dependencies (but not its implicit inputs) according to REPLACEMENTS.
749REPLACEMENTS is a list of package pairs; the first element of each pair is the
750package to replace, and the second one is the replacement.
751
752Optionally, REWRITE-NAME is a one-argument procedure that takes the name of a
753package and returns its new name after rewrite."
754 (define (rewrite input)
755 (match input
756 ((label (? package? package) outputs ...)
757 (match (assq-ref replacements package)
758 (#f (cons* label (replace package) outputs))
759 (new (cons* label new outputs))))
760 (_
761 input)))
762
c9134e82
LC
763 (define replace
764 (mlambdaq (p)
765 ;; Return a variant of P with its inputs rewritten.
766 (package
767 (inherit p)
768 (name (rewrite-name (package-name p)))
769 (inputs (map rewrite (package-inputs p)))
770 (native-inputs (map rewrite (package-native-inputs p)))
771 (propagated-inputs (map rewrite (package-propagated-inputs p))))))
2a75b0b6
LC
772
773 replace)
774
a2ebaddd
LC
775\f
776;;;
777;;; Package derivations.
778;;;
779
780(define %derivation-cache
781 ;; Package to derivation-path mapping.
e4588af9 782 (make-weak-key-hash-table 100))
a2ebaddd 783
198d84b7
LC
784(define (cache! cache package system thunk)
785 "Memoize in CACHE the return values of THUNK as the derivation of PACKAGE on
e509d152 786SYSTEM."
bce7526f
LC
787 ;; FIXME: This memoization should be associated with the open store, because
788 ;; otherwise it breaks when switching to a different store.
e509d152
LC
789 (let ((vals (call-with-values thunk list)))
790 ;; Use `hashq-set!' instead of `hash-set!' because `hash' returns the
791 ;; same value for all structs (as of Guile 2.0.6), and because pointer
792 ;; equality is sufficient in practice.
198d84b7 793 (hashq-set! cache package
8dcec914 794 `((,system ,@vals)
198d84b7 795 ,@(or (hashq-ref cache package) '())))
e509d152
LC
796 (apply values vals)))
797
198d84b7
LC
798(define-syntax cached
799 (syntax-rules (=>)
800 "Memoize the result of BODY for the arguments PACKAGE and SYSTEM.
e509d152 801Return the cached result when available."
198d84b7
LC
802 ((_ (=> cache) package system body ...)
803 (let ((thunk (lambda () body ...))
804 (key system))
805 (match (hashq-ref cache package)
806 ((alist (... ...))
807 (match (assoc-ref alist key)
808 ((vals (... ...))
809 (apply values vals))
810 (#f
811 (cache! cache package key thunk))))
e509d152 812 (#f
198d84b7
LC
813 (cache! cache package key thunk)))))
814 ((_ package system body ...)
815 (cached (=> %derivation-cache) package system body ...))))
a2ebaddd 816
a63062b5
LC
817(define* (expand-input store package input system #:optional cross-system)
818 "Expand INPUT, an input tuple, such that it contains only references to
819derivation paths or store paths. PACKAGE is only used to provide contextual
820information in exceptions."
592ef6c8
LC
821 (define (intern file)
822 ;; Add FILE to the store. Set the `recursive?' bit to #t, so that
823 ;; file permissions are preserved.
a9ebd9ef 824 (add-to-store store (basename file) #t "sha256" file))
592ef6c8 825
a63062b5
LC
826 (define derivation
827 (if cross-system
05962f29
LC
828 (cut package-cross-derivation store <> cross-system system
829 #:graft? #f)
830 (cut package-derivation store <> system #:graft? #f)))
a63062b5
LC
831
832 (match input
833 (((? string? name) (? package? package))
834 (list name (derivation package)))
835 (((? string? name) (? package? package)
836 (? string? sub-drv))
837 (list name (derivation package)
838 sub-drv))
839 (((? string? name)
840 (and (? string?) (? derivation-path?) drv))
841 (list name drv))
842 (((? string? name)
843 (and (? string?) (? file-exists? file)))
844 ;; Add FILE to the store. When FILE is in the sub-directory of a
845 ;; store path, it needs to be added anyway, so it can be used as a
846 ;; source.
847 (list name (intern file)))
da675305 848 (((? string? name) (? struct? source))
a63062b5
LC
849 (list name (package-source-derivation store source system)))
850 (x
851 (raise (condition (&package-input-error
852 (package package)
853 (input x)))))))
592ef6c8 854
9775412e
LC
855(define %bag-cache
856 ;; 'eq?' cache mapping packages to system+target+graft?-dependent bags.
857 ;; It significantly speeds things up when doing repeated calls to
858 ;; 'package->bag' as is the case when building a profile.
859 (make-weak-key-hash-table 200))
860
0d5a559f
LC
861(define* (package->bag package #:optional
862 (system (%current-system))
05962f29
LC
863 (target (%current-target-system))
864 #:key (graft? (%graft?)))
0d5a559f
LC
865 "Compile PACKAGE into a bag for SYSTEM, possibly cross-compiled to TARGET,
866and return it."
9775412e
LC
867 (cached (=> %bag-cache)
868 package (list system target graft?)
869 ;; Bind %CURRENT-SYSTEM and %CURRENT-TARGET-SYSTEM so that thunked
870 ;; field values can refer to it.
871 (parameterize ((%current-system system)
872 (%current-target-system target))
873 (match (if graft?
874 (or (package-replacement package) package)
875 package)
876 (($ <package> name version source build-system
877 args inputs propagated-inputs native-inputs
878 self-native-input? outputs)
879 (or (make-bag build-system (string-append name "-" version)
880 #:system system
881 #:target target
882 #:source source
883 #:inputs (append (inputs)
884 (propagated-inputs))
885 #:outputs outputs
886 #:native-inputs `(,@(if (and target
887 self-native-input?)
888 `(("self" ,package))
889 '())
890 ,@(native-inputs))
891 #:arguments (args))
892 (raise (if target
893 (condition
894 (&package-cross-build-system-error
895 (package package)))
896 (condition
897 (&package-error
898 (package package)))))))))))
0d5a559f 899
ced71ac7
LC
900(define %graft-cache
901 ;; 'eq?' cache mapping package objects to a graft corresponding to their
902 ;; replacement package.
903 (make-weak-key-hash-table 200))
904
05962f29 905(define (input-graft store system)
c22a1324
LC
906 "Return a procedure that, given a package with a graft, returns a graft, and
907#f otherwise."
05962f29 908 (match-lambda
c22a1324
LC
909 ((? package? package)
910 (let ((replacement (package-replacement package)))
911 (and replacement
ced71ac7
LC
912 (cached (=> %graft-cache) package system
913 (let ((orig (package-derivation store package system
914 #:graft? #f))
d0025d01
LC
915 (new (package-derivation store replacement system
916 #:graft? #t)))
ced71ac7
LC
917 (graft
918 (origin orig)
919 (replacement new)))))))
c22a1324
LC
920 (x
921 #f)))
05962f29
LC
922
923(define (input-cross-graft store target system)
924 "Same as 'input-graft', but for cross-compilation inputs."
925 (match-lambda
c22a1324 926 ((? package? package)
05962f29
LC
927 (let ((replacement (package-replacement package)))
928 (and replacement
929 (let ((orig (package-cross-derivation store package target system
930 #:graft? #f))
931 (new (package-cross-derivation store replacement
d0025d01
LC
932 target system
933 #:graft? #t)))
05962f29
LC
934 (graft
935 (origin orig)
c22a1324 936 (replacement new))))))
05962f29
LC
937 (_
938 #f)))
939
c22a1324
LC
940(define* (fold-bag-dependencies proc seed bag
941 #:key (native? #t))
942 "Fold PROC over the packages BAG depends on. Each package is visited only
943once, in depth-first order. If NATIVE? is true, restrict to native
944dependencies; otherwise, restrict to target dependencies."
945 (define nodes
946 (match (if native?
947 (append (bag-build-inputs bag)
948 (bag-target-inputs bag)
949 (if (bag-target bag)
950 '()
951 (bag-host-inputs bag)))
952 (bag-host-inputs bag))
953 (((labels things _ ...) ...)
954 things)))
955
956 (let loop ((nodes nodes)
957 (result seed)
958 (visited (setq)))
959 (match nodes
960 (()
961 result)
962 (((? package? head) . tail)
963 (if (set-contains? visited head)
964 (loop tail result visited)
965 (let ((inputs (bag-direct-inputs (package->bag head))))
966 (loop (match inputs
967 (((labels things _ ...) ...)
968 (append things tail)))
969 (proc head result)
970 (set-insert head visited)))))
971 ((head . tail)
972 (loop tail result visited)))))
973
05962f29 974(define* (bag-grafts store bag)
c22a1324
LC
975 "Return the list of grafts potentially applicable to BAG. Potentially
976applicable grafts are collected by looking at direct or indirect dependencies
977of BAG that have a 'replacement'. Whether a graft is actually applicable
978depends on whether the outputs of BAG depend on the items the grafts refer
979to (see 'graft-derivation'.)"
980 (define system (bag-system bag))
981 (define target (bag-target bag))
982
983 (define native-grafts
984 (let ((->graft (input-graft store system)))
985 (fold-bag-dependencies (lambda (package grafts)
986 (match (->graft package)
987 (#f grafts)
988 (graft (cons graft grafts))))
989 '()
990 bag)))
991
992 (define target-grafts
993 (if target
994 (let ((->graft (input-cross-graft store target system)))
995 (fold-bag-dependencies (lambda (package grafts)
996 (match (->graft package)
997 (#f grafts)
998 (graft (cons graft grafts))))
999 '()
1000 bag
1001 #:native? #f))
1002 '()))
1003
fcadd9ff
LC
1004 ;; We can end up with several identical grafts if we stumble upon packages
1005 ;; that are not 'eq?' but map to the same derivation (this can happen when
1006 ;; using things like 'package-with-explicit-inputs'.) Hence the
1007 ;; 'delete-duplicates' call.
1008 (delete-duplicates
1009 (append native-grafts target-grafts)))
05962f29
LC
1010
1011(define* (package-grafts store package
1012 #:optional (system (%current-system))
1013 #:key target)
1014 "Return the list of grafts applicable to PACKAGE as built for SYSTEM and
1015TARGET."
1016 (let* ((package (or (package-replacement package) package))
1017 (bag (package->bag package system target)))
1018 (bag-grafts store bag)))
1019
d3d337d2
LC
1020(define* (bag->derivation store bag
1021 #:optional context)
1022 "Return the derivation to build BAG for SYSTEM. Optionally, CONTEXT can be
1023a package object describing the context in which the call occurs, for improved
1024error reporting."
1025 (if (bag-target bag)
1026 (bag->cross-derivation store bag)
1027 (let* ((system (bag-system bag))
1028 (inputs (bag-transitive-inputs bag))
1029 (input-drvs (map (cut expand-input store context <> system)
1030 inputs))
1031 (paths (delete-duplicates
1032 (append-map (match-lambda
1033 ((_ (? package? p) _ ...)
1034 (package-native-search-paths
1035 p))
1036 (_ '()))
1037 inputs))))
1038
1039 (apply (bag-build bag)
1040 store (bag-name bag) input-drvs
1041 #:search-paths paths
1042 #:outputs (bag-outputs bag) #:system system
1043 (bag-arguments bag)))))
1044
1045(define* (bag->cross-derivation store bag
1046 #:optional context)
1047 "Return the derivation to build BAG, which is actually a cross build.
1048Optionally, CONTEXT can be a package object denoting the context of the call.
1049This is an internal procedure."
1050 (let* ((system (bag-system bag))
1051 (target (bag-target bag))
1052 (host (bag-transitive-host-inputs bag))
1053 (host-drvs (map (cut expand-input store context <> system target)
1054 host))
1055 (target* (bag-transitive-target-inputs bag))
1056 (target-drvs (map (cut expand-input store context <> system)
1057 target*))
1058 (build (bag-transitive-build-inputs bag))
1059 (build-drvs (map (cut expand-input store context <> system)
1060 build))
1061 (all (append build target* host))
1062 (paths (delete-duplicates
1063 (append-map (match-lambda
1064 ((_ (? package? p) _ ...)
1065 (package-search-paths p))
1066 (_ '()))
1067 all)))
1068 (npaths (delete-duplicates
1069 (append-map (match-lambda
1070 ((_ (? package? p) _ ...)
1071 (package-native-search-paths
1072 p))
1073 (_ '()))
1074 all))))
1075
1076 (apply (bag-build bag)
1077 store (bag-name bag)
1078 #:native-drvs build-drvs
1079 #:target-drvs (append host-drvs target-drvs)
1080 #:search-paths paths
1081 #:native-search-paths npaths
1082 #:outputs (bag-outputs bag)
1083 #:system system #:target target
1084 (bag-arguments bag))))
1085
a63062b5 1086(define* (package-derivation store package
05962f29
LC
1087 #:optional (system (%current-system))
1088 #:key (graft? (%graft?)))
59688fc4
LC
1089 "Return the <derivation> object of PACKAGE for SYSTEM."
1090
e509d152
LC
1091 ;; Compute the derivation and cache the result. Caching is important
1092 ;; because some derivations, such as the implicit inputs of the GNU build
1093 ;; system, will be queried many, many times in a row.
05962f29
LC
1094 (cached package (cons system graft?)
1095 (let* ((bag (package->bag package system #f #:graft? graft?))
1096 (drv (bag->derivation store bag package)))
1097 (if graft?
1098 (match (bag-grafts store bag)
1099 (()
1100 drv)
1101 (grafts
1102 (let ((guile (package-derivation store (default-guile)
1103 system #:graft? #f)))
c22a1324
LC
1104 ;; TODO: As an optimization, we can simply graft the tip
1105 ;; of the derivation graph since 'graft-derivation'
1106 ;; recurses anyway.
b0fef4d6 1107 (graft-derivation store drv grafts
05962f29
LC
1108 #:system system
1109 #:guile guile))))
1110 drv))))
e3ce5d70 1111
9c1edabd 1112(define* (package-cross-derivation store package target
05962f29
LC
1113 #:optional (system (%current-system))
1114 #:key (graft? (%graft?)))
9c1edabd
LC
1115 "Cross-build PACKAGE for TARGET (a GNU triplet) from host SYSTEM (a Guix
1116system identifying string)."
05962f29
LC
1117 (cached package (list system target graft?)
1118 (let* ((bag (package->bag package system target #:graft? graft?))
1119 (drv (bag->derivation store bag package)))
1120 (if graft?
1121 (match (bag-grafts store bag)
1122 (()
1123 drv)
1124 (grafts
b0fef4d6 1125 (graft-derivation store drv grafts
05962f29
LC
1126 #:system system
1127 #:guile
1128 (package-derivation store (default-guile)
1129 system #:graft? #f))))
1130 drv))))
d510ab46 1131
de8bcdae
LC
1132(define* (package-output store package
1133 #:optional (output "out") (system (%current-system)))
d510ab46
LC
1134 "Return the output path of PACKAGE's OUTPUT for SYSTEM---where OUTPUT is the
1135symbolic output name, such as \"out\". Note that this procedure calls
1136`package-derivation', which is costly."
59688fc4
LC
1137 (let ((drv (package-derivation store package system)))
1138 (derivation->output-path drv output)))
e87f0591
LC
1139
1140\f
1141;;;
1142;;; Monadic interface.
1143;;;
1144
1145(define (set-guile-for-build guile)
1146 "This monadic procedure changes the Guile currently used to run the build
1147code of derivations to GUILE, a package object."
1148 (lambda (store)
1149 (let ((guile (package-derivation store guile)))
4e190c28 1150 (values (%guile-for-build guile) store))))
e87f0591
LC
1151
1152(define* (package-file package
1153 #:optional file
1154 #:key
1155 system (output "out") target)
1156 "Return as a monadic value the absolute file name of FILE within the
1157OUTPUT directory of PACKAGE. When FILE is omitted, return the name of the
1158OUTPUT directory of PACKAGE. When TARGET is true, use it as a
1159cross-compilation target triplet."
1160 (lambda (store)
1161 (define compute-derivation
1162 (if target
1163 (cut package-cross-derivation <> <> target <>)
1164 package-derivation))
1165
1166 (let* ((system (or system (%current-system)))
1167 (drv (compute-derivation store package system))
1168 (out (derivation->output-path drv output)))
4e190c28
LC
1169 (values (if file
1170 (string-append out "/" file)
1171 out)
1172 store))))
e87f0591
LC
1173
1174(define package->derivation
1175 (store-lift package-derivation))
1176
1177(define package->cross-derivation
1178 (store-lift package-cross-derivation))
1179
1cdecf24 1180(define-gexp-compiler (package-compiler (package <package>) system target)
ff40e9b7
LC
1181 ;; Compile PACKAGE to a derivation for SYSTEM, optionally cross-compiled for
1182 ;; TARGET. This is used when referring to a package from within a gexp.
1183 (if target
1184 (package->cross-derivation package target system)
1185 (package->derivation package system)))
1186
78951064 1187(define* (origin->derivation origin
f220a838 1188 #:optional (system (%current-system)))
78951064
LC
1189 "Return the derivation corresponding to ORIGIN."
1190 (match origin
6b1f9721 1191 (($ <origin> uri method sha256 name (= force ()) #f)
f220a838
LC
1192 ;; No patches, no snippet: this is a fixed-output derivation.
1193 (method uri 'sha256 sha256 name #:system system))
6b1f9721 1194 (($ <origin> uri method sha256 name (= force (patches ...)) snippet
1929fdba 1195 (flags ...) inputs (modules ...) guile-for-build)
f220a838
LC
1196 ;; Patches and/or a snippet.
1197 (mlet %store-monad ((source (method uri 'sha256 sha256 name
1198 #:system system))
1199 (guile (package->derivation (or guile-for-build
1200 (default-guile))
1201 system
1202 #:graft? #f)))
cf87cc89
LC
1203 (patch-and-repack source patches
1204 #:inputs inputs
1205 #:snippet snippet
1206 #:flags flags
1207 #:system system
1208 #:modules modules
78951064 1209 #:guile-for-build guile)))))
f220a838 1210
1cdecf24 1211(define-gexp-compiler (origin-compiler (origin <origin>) system target)
ff40e9b7
LC
1212 ;; Compile ORIGIN to a derivation for SYSTEM. This is used when referring
1213 ;; to an origin from within a gexp.
1214 (origin->derivation origin system))
1215
78951064 1216(define package-source-derivation ;somewhat deprecated
da675305 1217 (let ((lower (store-lower lower-object)))
78951064
LC
1218 (lambda* (store source #:optional (system (%current-system)))
1219 "Return the derivation or file corresponding to SOURCE, which can be an
da675305
LC
1220a file name or any object handled by 'lower-object', such as an <origin>.
1221When SOURCE is a file name, return either the interned file name (if SOURCE is
1222outside of the store) or SOURCE itself (if SOURCE is already a store item.)"
78951064
LC
1223 (match source
1224 ((and (? string?) (? direct-store-path?) file)
1225 file)
1226 ((? string? file)
1227 (add-to-store store (basename file) #t "sha256" file))
1228 (_
1229 (lower store source system))))))