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