scripts: show: Replace 'args-fold*' by 'parse-command-line'.
[jackhill/guix/guix.git] / guix / packages.scm
CommitLineData
233e7676 1;;; GNU Guix --- Functional package management for GNU
36930b24 2;;; Copyright © 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020 Ludovic Courtès <ludo@gnu.org>
57320087 3;;; Copyright © 2014, 2015, 2017, 2018 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>
6741f543 6;;; Copyright © 2017, 2019, 2020 Efraim Flashner <efraim@flashner.co.il>
814e12dc 7;;; Copyright © 2019 Marius Bakke <mbakke@fastmail.com>
a16eb6c5 8;;; Copyright © 2021 Chris Marusich <cmmarusich@gmail.com>
e3ce5d70 9;;;
233e7676 10;;; This file is part of GNU Guix.
e3ce5d70 11;;;
233e7676 12;;; GNU Guix is free software; you can redistribute it and/or modify it
e3ce5d70
LC
13;;; under the terms of the GNU General Public License as published by
14;;; the Free Software Foundation; either version 3 of the License, or (at
15;;; your option) any later version.
16;;;
233e7676 17;;; GNU Guix is distributed in the hope that it will be useful, but
e3ce5d70
LC
18;;; WITHOUT ANY WARRANTY; without even the implied warranty of
19;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20;;; GNU General Public License for more details.
21;;;
22;;; You should have received a copy of the GNU General Public License
233e7676 23;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
e3ce5d70
LC
24
25(define-module (guix packages)
26 #:use-module (guix utils)
c0cd1b3e 27 #:use-module (guix records)
e3ce5d70 28 #:use-module (guix store)
e87f0591 29 #:use-module (guix monads)
ff40e9b7 30 #:use-module (guix gexp)
ddc29a78 31 #:use-module (guix base32)
56f7ca6e 32 #:autoload (guix base64) (base64-decode)
7adf9b84 33 #:use-module (guix grafts)
d510ab46 34 #:use-module (guix derivations)
c9134e82 35 #:use-module (guix memoization)
e3ce5d70 36 #:use-module (guix build-system)
e89431bf 37 #:use-module (guix search-paths)
c22a1324 38 #:use-module (guix sets)
ce0be567
LC
39 #:use-module (guix deprecation)
40 #:use-module (guix i18n)
e3ce5d70 41 #:use-module (ice-9 match)
c37a74bd 42 #:use-module (ice-9 vlist)
c423ae89 43 #:use-module (ice-9 regex)
062c6927 44 #:use-module (srfi srfi-1)
946b72c9 45 #:use-module (srfi srfi-9 gnu)
05962f29 46 #:use-module (srfi srfi-11)
a63062b5 47 #:use-module (srfi srfi-26)
d36622dc
LC
48 #:use-module (srfi srfi-34)
49 #:use-module (srfi srfi-35)
ce0be567 50 #:use-module (rnrs bytevectors)
3b4d0103 51 #:use-module (web uri)
cd52703a 52 #:re-export (%current-system
e89431bf
LC
53 %current-target-system
54 search-path-specification) ;for convenience
ce0be567
LC
55 #:export (content-hash
56 content-hash?
57 content-hash-algorithm
58 content-hash-value
59
60 origin
90c68be8 61 origin?
adb6462c 62 this-origin
90c68be8
LC
63 origin-uri
64 origin-method
ce0be567
LC
65 origin-hash
66 origin-sha256 ;deprecated
90c68be8 67 origin-file-name
3b4d0103 68 origin-actual-file-name
ac10e0e1
LC
69 origin-patches
70 origin-patch-flags
71 origin-patch-inputs
72 origin-patch-guile
f9cc8971
LC
73 origin-snippet
74 origin-modules
e4c245f8 75 base32
56f7ca6e 76 base64
e3ce5d70
LC
77
78 package
79 package?
adb6462c 80 this-package
e3ce5d70 81 package-name
3b0fcc67 82 package-upstream-name
e3ce5d70 83 package-version
2847050a 84 package-full-name
e3ce5d70
LC
85 package-source
86 package-build-system
87 package-arguments
88 package-inputs
89 package-native-inputs
062c6927 90 package-propagated-inputs
e3ce5d70 91 package-outputs
a18eda27 92 package-native-search-paths
e3ce5d70 93 package-search-paths
05962f29 94 package-replacement
d45122f5 95 package-synopsis
e3ce5d70 96 package-description
e3ce5d70 97 package-license
52bda18a 98 package-home-page
4e097f86 99 package-supported-systems
062c6927 100 package-properties
35f3c5f5 101 package-location
6980511b
LC
102 hidden-package
103 hidden-package?
01afdab8
LC
104 package-superseded
105 deprecated-package
d66c7096 106 package-field-location
e3ce5d70 107
f77bcbc3
EB
108 package-direct-sources
109 package-transitive-sources
7d193ec3 110 package-direct-inputs
a3d73f59 111 package-transitive-inputs
9c1edabd
LC
112 package-transitive-target-inputs
113 package-transitive-native-inputs
113aef68 114 package-transitive-propagated-inputs
aa8e0515 115 package-transitive-native-search-paths
7c3c0374 116 package-transitive-supported-systems
f37f2b83 117 package-mapping
2a75b0b6 118 package-input-rewriting
f258d886 119 package-input-rewriting/spec
e3ce5d70
LC
120 package-source-derivation
121 package-derivation
d36622dc 122 package-cross-derivation
d510ab46 123 package-output
05962f29 124 package-grafts
c423ae89 125 package-patched-vulnerabilities
b066c250
CD
126 package-with-patches
127 package-with-extra-patches
46135ce4 128 package-with-c-toolchain
bedba064 129 package/inherit
d36622dc 130
a6d0b306
EB
131 transitive-input-references
132
4e097f86 133 %supported-systems
035b6ff7 134 %hurd-systems
76bea3f8 135 %cuirass-supported-systems
bbceb0ef 136 supported-package?
4e097f86 137
d36622dc 138 &package-error
07783858 139 package-error?
d36622dc
LC
140 package-error-package
141 &package-input-error
07783858 142 package-input-error?
9b222abe
LC
143 package-error-invalid-input
144 &package-cross-build-system-error
0d5a559f
LC
145 package-cross-build-system-error?
146
147 package->bag
d3d337d2 148 bag->derivation
cceab875 149 bag-direct-inputs
0d5a559f
LC
150 bag-transitive-inputs
151 bag-transitive-host-inputs
152 bag-transitive-build-inputs
e87f0591 153 bag-transitive-target-inputs
3e223a22 154 package-closure
e87f0591
LC
155
156 default-guile
ff40e9b7 157 default-guile-derivation
e87f0591
LC
158 set-guile-for-build
159 package-file
160 package->derivation
161 package->cross-derivation
162 origin->derivation))
e3ce5d70
LC
163
164;;; Commentary:
165;;;
166;;; This module provides a high-level mechanism to define packages in a
167;;; Guix-based distribution.
168;;;
169;;; Code:
170
8152fd1a
MC
171(define-syntax-rule (define-compile-time-decoder name string->bytevector)
172 "Define NAME as a macro that runs STRING->BYTEVECTOR at macro expansion time
173if possible."
174 (define-syntax name
175 (lambda (s)
176 "Return the bytevector corresponding to the given textual
177representation."
178 (syntax-case s ()
179 ((_ str)
180 (string? (syntax->datum #'str))
181 ;; A literal string: do the conversion at expansion time.
182 (with-syntax ((bv (string->bytevector (syntax->datum #'str))))
183 #''bv))
184 ((_ str)
185 #'(string->bytevector str))))))
186
187(define-compile-time-decoder base32 nix-base32-string->bytevector)
188(define-compile-time-decoder base64 base64-decode)
189
ce0be567
LC
190;; Crytographic content hash.
191(define-immutable-record-type <content-hash>
192 (%content-hash algorithm value)
193 content-hash?
194 (algorithm content-hash-algorithm) ;symbol
195 (value content-hash-value)) ;bytevector
196
197(define-syntax-rule (define-content-hash-constructor name
198 (algorithm size) ...)
199 "Define NAME as a <content-hash> constructor that ensures that (1) its
200second argument is among the listed ALGORITHM, and (2), when possible, that
201its first argument has the right size for the chosen algorithm."
202 (define-syntax name
203 (lambda (s)
204 (syntax-case s (algorithm ...)
205 ((_ bv algorithm)
206 (let ((bv* (syntax->datum #'bv)))
207 (when (and (bytevector? bv*)
208 (not (= size (bytevector-length bv*))))
209 (syntax-violation 'content-hash "invalid content hash length" s))
210 #'(%content-hash 'algorithm bv)))
211 ...))))
212
213(define-content-hash-constructor build-content-hash
214 (sha256 32)
0505eda9
LC
215 (sha512 64)
216 (sha3-256 32)
217 (sha3-512 64)
218 (blake2s-256 64))
ce0be567
LC
219
220(define-syntax content-hash
221 (lambda (s)
222 "Return a content hash with the given parameters. The default hash
223algorithm is sha256. If the first argument is a literal string, it is decoded
224as base32. Otherwise, it must be a bytevector."
225 ;; What we'd really want here is something like C++ 'constexpr'.
226 (syntax-case s ()
227 ((_ str)
228 (string? (syntax->datum #'str))
229 #'(content-hash str sha256))
230 ((_ str algorithm)
231 (string? (syntax->datum #'str))
232 (with-syntax ((bv (base32 (syntax->datum #'str))))
233 #'(content-hash bv algorithm)))
234 ((_ (id str) algorithm)
235 (and (string? (syntax->datum #'str))
236 (free-identifier=? #'id #'base32))
237 (with-syntax ((bv (nix-base32-string->bytevector (syntax->datum #'str))))
238 #'(content-hash bv algorithm)))
239 ((_ (id str) algorithm)
240 (and (string? (syntax->datum #'str))
241 (free-identifier=? #'id #'base64))
242 (with-syntax ((bv (base64-decode (syntax->datum #'str))))
243 #'(content-hash bv algorithm)))
244 ((_ bv)
245 #'(content-hash bv sha256))
246 ((_ bv hash)
247 #'(build-content-hash bv hash)))))
248
249(define (print-content-hash hash port)
250 (format port "#<content-hash ~a:~a>"
251 (content-hash-algorithm hash)
83ec969c
LC
252 (and=> (content-hash-value hash)
253 bytevector->nix-base32-string)))
ce0be567
LC
254
255(set-record-type-printer! <content-hash> print-content-hash)
256
257\f
90c68be8
LC
258;; The source of a package, such as a tarball URL and fetcher---called
259;; "origin" to avoid name clash with `package-source', `source', etc.
260(define-record-type* <origin>
ce0be567 261 %origin make-origin
90c68be8 262 origin?
adb6462c 263 this-origin
90c68be8 264 (uri origin-uri) ; string
9b5b5c17 265 (method origin-method) ; procedure
ce0be567 266 (hash origin-hash) ; <content-hash>
ac10e0e1 267 (file-name origin-file-name (default #f)) ; optional file name
6b1f9721
LC
268
269 ;; Patches are delayed so that the 'search-patch' calls are made lazily,
270 ;; which reduces I/O on startup and allows patch-not-found errors to be
271 ;; gracefully handled at run time.
272 (patches origin-patches ; list of file names
273 (default '()) (delayed))
274
f9cc8971 275 (snippet origin-snippet (default #f)) ; sexp or #f
ac10e0e1
LC
276 (patch-flags origin-patch-flags ; list of strings
277 (default '("-p1")))
1d9bc459
LC
278
279 ;; Patching requires Guile, GNU Patch, and a few more. These two fields are
280 ;; used to specify these dependencies when needed.
ac10e0e1
LC
281 (patch-inputs origin-patch-inputs ; input list or #f
282 (default #f))
f9cc8971
LC
283 (modules origin-modules ; list of module names
284 (default '()))
1929fdba 285
1d9bc459 286 (patch-guile origin-patch-guile ; package or #f
ac10e0e1 287 (default #f)))
e3ce5d70 288
ce0be567
LC
289(define-syntax origin-compatibility-helper
290 (syntax-rules (sha256)
291 ((_ () (fields ...))
292 (%origin fields ...))
293 ((_ ((sha256 exp) rest ...) (others ...))
294 (%origin others ...
295 (hash (content-hash exp sha256))
296 rest ...))
297 ((_ (field rest ...) (others ...))
298 (origin-compatibility-helper (rest ...)
299 (others ... field)))))
300
301(define-syntax-rule (origin fields ...)
302 "Build an <origin> record, automatically converting 'sha256' field
303specifications to 'hash'."
304 (origin-compatibility-helper (fields ...) ()))
305
306(define-deprecated (origin-sha256 origin)
307 origin-hash
308 (let ((hash (origin-hash origin)))
309 (unless (eq? (content-hash-algorithm hash) 'sha256)
310 (raise (condition (&message
311 (message (G_ "no SHA256 hash for origin"))))))
312 (content-hash-value hash)))
313
f1096964
LC
314(define (print-origin origin port)
315 "Write a concise representation of ORIGIN to PORT."
316 (match origin
ce0be567 317 (($ <origin> uri method hash file-name patches)
f1096964 318 (simple-format port "#<origin ~s ~a ~s ~a>"
ce0be567 319 uri hash
6b1f9721 320 (force patches)
f1096964
LC
321 (number->string (object-address origin) 16)))))
322
323(set-record-type-printer! <origin> print-origin)
324
3b4d0103
EB
325(define (origin-actual-file-name origin)
326 "Return the file name of ORIGIN, either its 'file-name' field or the file
327name of its URI."
328 (define (uri->file-name uri)
329 ;; Return the 'base name' of URI or URI itself, where URI is a string.
330 (let ((path (and=> (string->uri uri) uri-path)))
331 (if path
332 (basename path)
333 uri)))
334
335 (or (origin-file-name origin)
336 (match (origin-uri origin)
337 ((head . tail)
338 (uri->file-name head))
339 ((? string? uri)
340 (uri->file-name uri))
341 (else
342 ;; git, svn, cvs, etc. reference
343 #f))))
344
ce0be567 345\f
4e097f86
LC
346(define %supported-systems
347 ;; This is the list of system types that are supported. By default, we
348 ;; expect all packages to build successfully here.
a16eb6c5
CM
349 '("x86_64-linux" "i686-linux" "armhf-linux" "aarch64-linux" "mips64el-linux" "i586-gnu"
350 "powerpc64le-linux"))
4e097f86 351
035b6ff7
LC
352(define %hurd-systems
353 ;; The GNU/Hurd systems for which support is being developed.
003fcf23 354 '("i586-gnu" "i686-gnu"))
035b6ff7 355
76bea3f8 356(define %cuirass-supported-systems
cdb3f734
LC
357 ;; This is the list of system types for which build machines are available.
358 ;;
d0428564 359 ;; XXX: MIPS is unavailable in CI:
cdb3f734 360 ;; <https://lists.gnu.org/archive/html/guix-devel/2017-03/msg00790.html>.
d0428564 361 (fold delete %supported-systems '("mips64el-linux")))
abcbda48
LC
362
363
a18eda27 364;; A package.
e3ce5d70
LC
365(define-record-type* <package>
366 package make-package
367 package?
adb6462c 368 this-package
e3ce5d70
LC
369 (name package-name) ; string
370 (version package-version) ; string
90c68be8 371 (source package-source) ; <origin> instance
e3ce5d70 372 (build-system package-build-system) ; build system
64fddd74 373 (arguments package-arguments ; arguments for the build method
21c203a5 374 (default '()) (thunked))
062c6927 375
e3ce5d70 376 (inputs package-inputs ; input packages or derivations
dd6b9a37 377 (default '()) (thunked))
062c6927 378 (propagated-inputs package-propagated-inputs ; same, but propagated
9d97a1b3 379 (default '()) (thunked))
e3ce5d70 380 (native-inputs package-native-inputs ; native input packages/derivations
a7dc055b 381 (default '()) (thunked))
062c6927 382
e3ce5d70
LC
383 (outputs package-outputs ; list of strings
384 (default '("out")))
a18eda27
LC
385
386 ; lists of
387 ; <search-path-specification>,
388 ; for native and cross
389 ; inputs
390 (native-search-paths package-native-search-paths (default '()))
391 (search-paths package-search-paths (default '()))
d5ec5ed7
LC
392
393 ;; The 'replacement' field is marked as "innate" because it never makes
394 ;; sense to inherit a replacement as is. See the 'package/inherit' macro.
05962f29 395 (replacement package-replacement ; package | #f
d5ec5ed7 396 (default #f) (thunked) (innate))
e3ce5d70 397
d45122f5
LC
398 (synopsis package-synopsis) ; one-line description
399 (description package-description) ; one or two paragraphs
1fb78cb2 400 (license package-license)
45753b65 401 (home-page package-home-page)
4e097f86
LC
402 (supported-systems package-supported-systems ; list of strings
403 (default %supported-systems))
45753b65 404
062c6927
LC
405 (properties package-properties (default '())) ; alist for anything else
406
35f3c5f5
LC
407 (location package-location
408 (default (and=> (current-source-location)
0004c590
LC
409 source-properties->location))
410 (innate)))
e3ce5d70 411
946b72c9
LC
412(set-record-type-printer! <package>
413 (lambda (package port)
414 (let ((loc (package-location package))
415 (format simple-format))
74e667d1 416 (format port "#<package ~a@~a ~a~a>"
946b72c9
LC
417 (package-name package)
418 (package-version package)
2e1bafb0
LC
419 (if loc
420 (format #f "~a:~a "
421 (location-file loc)
422 (location-line loc))
423 "")
946b72c9
LC
424 (number->string (object-address
425 package)
426 16)))))
427
2bf6f962
LC
428(define-syntax-rule (package/inherit p overrides ...)
429 "Like (package (inherit P) OVERRIDES ...), except that the same
56733080 430transformation is done to the package P's replacement, if any. P must be a bare
2bf6f962
LC
431identifier, and will be bound to either P or its replacement when evaluating
432OVERRIDES."
433 (let loop ((p p))
434 (package (inherit p)
435 overrides ...
436 (replacement (and=> (package-replacement p) loop)))))
437
3b0fcc67
LC
438(define (package-upstream-name package)
439 "Return the upstream name of PACKAGE, which could be different from the name
440it has in Guix."
441 (or (assq-ref (package-properties package) 'upstream-name)
442 (package-name package)))
443
6980511b
LC
444(define (hidden-package p)
445 "Return a \"hidden\" version of P--i.e., one that 'fold-packages' and thus,
446user interfaces, ignores."
447 (package
448 (inherit p)
449 (properties `((hidden? . #t)
450 ,@(package-properties p)))))
451
452(define (hidden-package? p)
453 "Return true if P is \"hidden\"--i.e., must not be visible to user
454interfaces."
455 (assoc-ref (package-properties p) 'hidden?))
456
01afdab8
LC
457(define (package-superseded p)
458 "Return the package the supersedes P, or #f if P is still current."
459 (assoc-ref (package-properties p) 'superseded))
460
461(define (deprecated-package old-name p)
462 "Return a package called OLD-NAME and marked as superseded by P, a package
463object."
464 (package
465 (inherit p)
466 (name old-name)
467 (properties `((superseded . ,p)))))
468
d66c7096 469(define (package-field-location package field)
f903dc05
LC
470 "Return the source code location of the definition of FIELD for PACKAGE, or
471#f if it could not be determined."
472 (define (goto port line column)
473 (unless (and (= (port-column port) (- column 1))
474 (= (port-line port) (- line 1)))
475 (unless (eof-object? (read-char port))
476 (goto port line column))))
d66c7096
LC
477
478 (match (package-location package)
479 (($ <location> file line column)
9a38bed2 480 (match (search-path %load-path file)
66c9bc35 481 ((? string? file-found)
9a38bed2
LC
482 (catch 'system-error
483 (lambda ()
484 ;; In general we want to keep relative file names for modules.
66c9bc35 485 (call-with-input-file file-found
9a38bed2
LC
486 (lambda (port)
487 (goto port line column)
488 (match (read port)
489 (('package inits ...)
490 (let ((field (assoc field inits)))
491 (match field
492 ((_ value)
493 (let ((loc (and=> (source-properties value)
494 source-properties->location)))
495 (and loc
496 ;; Preserve the original file name, which may be a
497 ;; relative file name.
498 (set-field loc (location-file) file))))
499 (_
500 #f))))
501 (_
502 #f)))))
503 (lambda _
504 #f)))
505 (#f
506 ;; FILE could not be found in %LOAD-PATH.
507 #f)))
d66c7096
LC
508 (_ #f)))
509
d36622dc
LC
510
511;; Error conditions.
512
513(define-condition-type &package-error &error
514 package-error?
515 (package package-error-package))
516
517(define-condition-type &package-input-error &package-error
518 package-input-error?
519 (input package-error-invalid-input))
520
9b222abe
LC
521(define-condition-type &package-cross-build-system-error &package-error
522 package-cross-build-system-error?)
523
ede121de
CM
524(define* (package-full-name package #:optional (delimiter "@"))
525 "Return the full name of PACKAGE--i.e., `NAME@VERSION'. By specifying
526DELIMITER (a string), you can customize what will appear between the name and
527the version. By default, DELIMITER is \"@\"."
528 (string-append (package-name package) delimiter (package-version package)))
2847050a 529
c423ae89
LC
530(define (patch-file-name patch)
531 "Return the basename of PATCH's file name, or #f if the file name could not
532be determined."
533 (match patch
534 ((? string?)
535 (basename patch))
536 ((? origin?)
537 (and=> (origin-actual-file-name patch) basename))))
538
539(define %vulnerability-regexp
540 ;; Regexp matching a CVE identifier in patch file names.
541 (make-regexp "CVE-[0-9]{4}-[0-9]+"))
542
543(define (package-patched-vulnerabilities package)
544 "Return the list of patched vulnerabilities of PACKAGE as a list of CVE
545identifiers. The result is inferred from the file names of patches."
546 (define (patch-vulnerabilities patch)
547 (map (cut match:substring <> 0)
548 (list-matches %vulnerability-regexp patch)))
549
550 (let ((patches (filter-map patch-file-name
551 (or (and=> (package-source package)
552 origin-patches)
553 '()))))
554 (append-map patch-vulnerabilities patches)))
555
ac10e0e1 556(define (%standard-patch-inputs)
5ae4169c
LC
557 (let* ((canonical (module-ref (resolve-interface '(gnu packages base))
558 'canonical-package))
559 (ref (lambda (module var)
560 (canonical
561 (module-ref (resolve-interface module) var)))))
ac10e0e1
LC
562 `(("tar" ,(ref '(gnu packages base) 'tar))
563 ("xz" ,(ref '(gnu packages compression) 'xz))
564 ("bzip2" ,(ref '(gnu packages compression) 'bzip2))
565 ("gzip" ,(ref '(gnu packages compression) 'gzip))
566 ("lzip" ,(ref '(gnu packages compression) 'lzip))
148585c2 567 ("unzip" ,(ref '(gnu packages compression) 'unzip))
9cca706c 568 ("patch" ,(ref '(gnu packages base) 'patch))
5ae4169c 569 ("locales" ,(ref '(gnu packages base) 'glibc-utf8-locales)))))
ac10e0e1 570
1d9bc459 571(define (default-guile)
e87f0591
LC
572 "Return the default Guile package used to run the build code of
573derivations."
bdb36958 574 (let ((distro (resolve-interface '(gnu packages commencement))))
1d9bc459 575 (module-ref distro 'guile-final)))
ac10e0e1 576
2b6fe605
LC
577(define (guile-for-grafts)
578 "Return the Guile package used to build grafting derivations."
579 ;; Guile 2.2 would not work due to <https://bugs.gnu.org/28211> when
e4925e00
LC
580 ;; grafting packages.
581 (let ((distro (resolve-interface '(gnu packages guile))))
582 (module-ref distro 'guile-2.0)))
583
ff40e9b7
LC
584(define* (default-guile-derivation #:optional (system (%current-system)))
585 "Return the derivation for SYSTEM of the default Guile package used to run
586the build code of derivation."
587 (package->derivation (default-guile) system
588 #:graft? #f))
589
cf87cc89 590(define* (patch-and-repack source patches
ac10e0e1 591 #:key
a158484d 592 inputs
f9cc8971 593 (snippet #f)
ac10e0e1 594 (flags '("-p1"))
f9cc8971 595 (modules '())
ac10e0e1
LC
596 (guile-for-build (%guile-for-build))
597 (system (%current-system)))
f9cc8971
LC
598 "Unpack SOURCE (a derivation or store path), apply all of PATCHES, and
599repack the tarball using the tools listed in INPUTS. When SNIPPET is true,
600it must be an s-expression that will run from within the directory where
1929fdba
LC
601SOURCE was unpacked, after all of PATCHES have been applied. MODULES
602specifies modules in scope when evaluating SNIPPET."
f9cc8971
LC
603 (define source-file-name
604 ;; SOURCE is usually a derivation, but it could be a store file.
605 (if (derivation? source)
606 (derivation->output-path source)
607 source))
608
a158484d
LC
609 (define lookup-input
610 ;; The default value of the 'patch-inputs' field, and thus INPUTS is #f,
611 ;; so deal with that.
612 (let ((inputs (or inputs (%standard-patch-inputs))))
613 (lambda (name)
614 (match (assoc-ref inputs name)
615 ((package) package)
616 (#f #f)))))
cf87cc89 617
ac10e0e1 618 (define decompression-type
f9cc8971 619 (cond ((string-suffix? "gz" source-file-name) "gzip")
5257ab6d 620 ((string-suffix? "Z" source-file-name) "gzip")
f9cc8971
LC
621 ((string-suffix? "bz2" source-file-name) "bzip2")
622 ((string-suffix? "lz" source-file-name) "lzip")
17287d7d 623 ((string-suffix? "zip" source-file-name) "unzip")
f9cc8971 624 (else "xz")))
ac10e0e1
LC
625
626 (define original-file-name
f9cc8971
LC
627 ;; Remove the store prefix plus the slash, hash, and hyphen.
628 (let* ((sans (string-drop source-file-name
629 (+ (string-length (%store-prefix)) 1)))
630 (dash (string-index sans #\-)))
631 (string-drop sans (+ 1 dash))))
ac10e0e1 632
3ca00bb5
LC
633 (define (numeric-extension? file-name)
634 ;; Return true if FILE-NAME ends with digits.
857ecb3d
LC
635 (and=> (file-extension file-name)
636 (cut string-every char-set:hex-digit <>)))
3ca00bb5 637
814e12dc
MB
638 (define (checkout? directory)
639 ;; Return true if DIRECTORY is a checkout (git, svn, etc).
640 (string-suffix? "-checkout" directory))
641
3ca00bb5
LC
642 (define (tarxz-name file-name)
643 ;; Return a '.tar.xz' file name based on FILE-NAME.
814e12dc
MB
644 (let ((base (cond ((numeric-extension? file-name)
645 original-file-name)
646 ((checkout? file-name)
647 (string-drop-right file-name 9))
648 (else (file-sans-extension file-name)))))
3ca00bb5
LC
649 (string-append base
650 (if (equal? (file-extension base) "tar")
651 ".xz"
652 ".tar.xz"))))
653
cf87cc89
LC
654 (define instantiate-patch
655 (match-lambda
7ebc6cf8 656 ((? string? patch) ;deprecated
cf87cc89 657 (interned-file patch #:recursive? #t))
7ebc6cf8
LC
658 ((? struct? patch) ;origin, local-file, etc.
659 (lower-object patch system))))
cf87cc89
LC
660
661 (mlet %store-monad ((tar -> (lookup-input "tar"))
662 (xz -> (lookup-input "xz"))
663 (patch -> (lookup-input "patch"))
664 (locales -> (lookup-input "locales"))
665 (decomp -> (lookup-input decompression-type))
666 (patches (sequence %store-monad
667 (map instantiate-patch patches))))
668 (define build
1929fdba
LC
669 (with-imported-modules '((guix build utils))
670 #~(begin
671 (use-modules (ice-9 ftw)
672 (srfi srfi-1)
673 (guix build utils))
674
675 ;; The --sort option was added to GNU tar in version 1.28, released
676 ;; 2014-07-28. During bootstrap we must cope with older versions.
677 (define tar-supports-sort?
678 (zero? (system* (string-append #+tar "/bin/tar")
679 "cf" "/dev/null" "--files-from=/dev/null"
680 "--sort=name")))
681
682 (define (apply-patch patch)
683 (format (current-error-port) "applying '~a'...~%" patch)
684
685 ;; Use '--force' so that patches that do not apply perfectly are
8d65c71f
AK
686 ;; rejected. Use '--no-backup-if-mismatch' to prevent making
687 ;; "*.orig" file if a patch is applied with offset.
7ac1b408
MW
688 (invoke (string-append #+patch "/bin/patch")
689 "--force" "--no-backup-if-mismatch"
690 #+@flags "--input" patch))
1929fdba
LC
691
692 (define (first-file directory)
693 ;; Return the name of the first file in DIRECTORY.
694 (car (scandir directory
695 (lambda (name)
696 (not (member name '("." "..")))))))
697
698 ;; Encoding/decoding errors shouldn't be silent.
699 (fluid-set! %default-port-conversion-strategy 'error)
700
701 (when #+locales
702 ;; First of all, install a UTF-8 locale so that UTF-8 file names
703 ;; are correctly interpreted. During bootstrap, LOCALES is #f.
704 (setenv "LOCPATH"
705 (string-append #+locales "/lib/locale/"
706 #+(and locales
c6bc8e22
MB
707 (version-major+minor
708 (package-version locales)))))
1929fdba
LC
709 (setlocale LC_ALL "en_US.utf8"))
710
711 (setenv "PATH" (string-append #+xz "/bin" ":"
712 #+decomp "/bin"))
713
714 ;; SOURCE may be either a directory or a tarball.
7ac1b408
MW
715 (if (file-is-directory? #+source)
716 (let* ((store (%store-directory))
717 (len (+ 1 (string-length store)))
718 (base (string-drop #+source len))
719 (dash (string-index base #\-))
720 (directory (string-drop base (+ 1 dash))))
721 (mkdir directory)
722 (copy-recursively #+source directory))
723 #+(if (string=? decompression-type "unzip")
724 #~(invoke "unzip" #+source)
725 #~(invoke (string-append #+tar "/bin/tar")
726 "xvf" #+source)))
727
728 (let ((directory (first-file ".")))
729 (format (current-error-port)
730 "source is under '~a'~%" directory)
731 (chdir directory)
732
733 (for-each apply-patch '#+patches)
734
daac9c77
MW
735 (let ((result #+(if snippet
736 #~(let ((module (make-fresh-user-module)))
737 (module-use-interfaces!
738 module
739 (map resolve-interface '#+modules))
740 ((@ (system base compile) compile)
741 '#+snippet
742 #:to 'value
743 #:opts %auto-compilation-options
744 #:env module))
745 #~#t)))
746 ;; Issue a warning unless the result is #t.
747 (unless (eqv? result #t)
748 (format (current-error-port) "\
749## WARNING: the snippet returned `~s'. Return values other than #t
750## are deprecated. Please migrate this package so that its snippet
751## reports errors by raising an exception, and otherwise returns #t.~%"
752 result))
753 (unless result
754 (error "snippet returned false")))
7ac1b408
MW
755
756 (chdir "..")
757
758 (unless tar-supports-sort?
759 (call-with-output-file ".file_list"
760 (lambda (port)
761 (for-each (lambda (name)
762 (format port "~a~%" name))
763 (find-files directory
764 #:directories? #t
765 #:fail-on-error? #t)))))
766 (apply invoke
767 (string-append #+tar "/bin/tar")
28c4905b 768 "cvfa" #$output
36930b24
LC
769 ;; Avoid non-determinism in the archive. Set the mtime
770 ;; to 1 as is the case in the store (software like gzip
771 ;; behaves differently when it stumbles upon mtime = 0).
772 "--mtime=@1"
7ac1b408
MW
773 "--owner=root:0"
774 "--group=root:0"
775 (if tar-supports-sort?
776 `("--sort=name"
777 ,directory)
778 '("--no-recursion"
779 "--files-from=.file_list")))))))
1929fdba
LC
780
781 (let ((name (tarxz-name original-file-name)))
cf87cc89
LC
782 (gexp->derivation name build
783 #:graft? #f
784 #:system system
ea89b62a
LC
785 #:guile-for-build guile-for-build
786 #:properties `((type . origin)
787 (patches . ,(length patches)))))))
ac10e0e1 788
b066c250
CD
789(define (package-with-patches original patches)
790 "Return package ORIGINAL with PATCHES applied."
791 (package (inherit original)
792 (source (origin (inherit (package-source original))
793 (patches patches)))))
794
795(define (package-with-extra-patches original patches)
796 "Return package ORIGINAL with all PATCHES appended to its list of patches."
797 (package-with-patches original
798 (append (origin-patches (package-source original))
799 patches)))
800
46135ce4
LC
801(define (package-with-c-toolchain package toolchain)
802 "Return a variant of PACKAGE that uses TOOLCHAIN instead of the default GNU
803C/C++ toolchain. TOOLCHAIN must be a list of inputs (label/package tuples)
804providing equivalent functionality, such as the 'gcc-toolchain' package."
805 (let ((bs (package-build-system package)))
806 (package/inherit package
807 (build-system (build-system-with-c-toolchain bs toolchain)))))
808
113aef68 809(define (transitive-inputs inputs)
161094c8
LC
810 "Return the closure of INPUTS when considering the 'propagated-inputs'
811edges. Omit duplicate inputs, except for those already present in INPUTS
812itself.
813
814This is implemented as a breadth-first traversal such that INPUTS is
815preserved, and only duplicate propagated inputs are removed."
816 (define (seen? seen item outputs)
8102cf0b
LC
817 ;; FIXME: We're using pointer identity here, which is extremely sensitive
818 ;; to memoization in package-producing procedures; see
819 ;; <https://bugs.gnu.org/30155>.
161094c8
LC
820 (match (vhash-assq item seen)
821 ((_ . o) (equal? o outputs))
822 (_ #f)))
823
824 (let loop ((inputs inputs)
825 (result '())
826 (propagated '())
827 (first? #t)
828 (seen vlist-null))
a3d73f59
LC
829 (match inputs
830 (()
161094c8
LC
831 (if (null? propagated)
832 (reverse result)
833 (loop (reverse (concatenate propagated)) result '() #f seen)))
834 (((and input (label (? package? package) outputs ...)) rest ...)
835 (if (and (not first?) (seen? seen package outputs))
836 (loop rest result propagated first? seen)
837 (loop rest
838 (cons input result)
839 (cons (package-propagated-inputs package) propagated)
840 first?
841 (vhash-consq package outputs seen))))
a3d73f59 842 ((input rest ...)
161094c8 843 (loop rest (cons input result) propagated first? seen)))))
a3d73f59 844
f77bcbc3
EB
845(define (package-direct-sources package)
846 "Return all source origins associated with PACKAGE; including origins in
847PACKAGE's inputs."
848 `(,@(or (and=> (package-source package) list) '())
849 ,@(filter-map (match-lambda
850 ((_ (? origin? orig) _ ...)
851 orig)
852 (_ #f))
853 (package-direct-inputs package))))
854
855(define (package-transitive-sources package)
856 "Return PACKAGE's direct sources, and their direct sources, recursively."
857 (delete-duplicates
858 (concatenate (filter-map (match-lambda
859 ((_ (? origin? orig) _ ...)
860 (list orig))
861 ((_ (? package? p) _ ...)
862 (package-direct-sources p))
863 (_ #f))
864 (bag-transitive-inputs
865 (package->bag package))))))
866
7d193ec3
EB
867(define (package-direct-inputs package)
868 "Return all the direct inputs of PACKAGE---i.e, its direct inputs along
869with their propagated inputs."
870 (append (package-native-inputs package)
871 (package-inputs package)
872 (package-propagated-inputs package)))
873
113aef68
LC
874(define (package-transitive-inputs package)
875 "Return the transitive inputs of PACKAGE---i.e., its direct inputs along
876with their propagated inputs, recursively."
7d193ec3 877 (transitive-inputs (package-direct-inputs package)))
113aef68 878
9c1edabd
LC
879(define (package-transitive-target-inputs package)
880 "Return the transitive target inputs of PACKAGE---i.e., its direct inputs
881along with their propagated inputs, recursively. This only includes inputs
882for the target system, and not native inputs."
883 (transitive-inputs (append (package-inputs package)
884 (package-propagated-inputs package))))
885
886(define (package-transitive-native-inputs package)
887 "Return the transitive native inputs of PACKAGE---i.e., its direct inputs
888along with their propagated inputs, recursively. This only includes inputs
889for the host system (\"native inputs\"), and not target inputs."
890 (transitive-inputs (package-native-inputs package)))
891
113aef68
LC
892(define (package-transitive-propagated-inputs package)
893 "Return the propagated inputs of PACKAGE, and their propagated inputs,
894recursively."
895 (transitive-inputs (package-propagated-inputs package)))
896
aa8e0515
LC
897(define (package-transitive-native-search-paths package)
898 "Return the list of search paths for PACKAGE and its propagated inputs,
899recursively."
900 (append (package-native-search-paths package)
901 (append-map (match-lambda
902 ((label (? package? p) _ ...)
903 (package-native-search-paths p))
904 (_
905 '()))
906 (package-transitive-propagated-inputs package))))
907
a6d0b306
EB
908(define (transitive-input-references alist inputs)
909 "Return a list of (assoc-ref ALIST <label>) for each (<label> <package> . _)
910in INPUTS and their transitive propagated inputs."
911 (define label
912 (match-lambda
913 ((label . _)
914 label)))
915
916 (map (lambda (input)
917 `(assoc-ref ,alist ,(label input)))
918 (transitive-inputs inputs)))
919
c9134e82 920(define package-transitive-supported-systems
bc60349b
LC
921 (let ()
922 (define supported-systems
923 (mlambda (package system)
924 (parameterize ((%current-system system))
925 (fold (lambda (input systems)
926 (match input
927 ((label (? package? package) . _)
928 (lset-intersection string=? systems
929 (supported-systems package system)))
930 (_
931 systems)))
932 (package-supported-systems package)
933 (bag-direct-inputs (package->bag package))))))
934
935 (lambda* (package #:optional (system (%current-system)))
936 "Return the intersection of the systems supported by PACKAGE and those
7c3c0374 937supported by its dependencies."
bc60349b 938 (supported-systems package system))))
7c3c0374 939
bbceb0ef
LC
940(define* (supported-package? package #:optional (system (%current-system)))
941 "Return true if PACKAGE is supported on SYSTEM--i.e., if PACKAGE and all its
942dependencies are known to build on SYSTEM."
bc60349b 943 (member system (package-transitive-supported-systems package system)))
bbceb0ef 944
cceab875
LC
945(define (bag-direct-inputs bag)
946 "Same as 'package-direct-inputs', but applied to a bag."
947 (append (bag-build-inputs bag)
948 (bag-host-inputs bag)
949 (bag-target-inputs bag)))
950
0d5a559f
LC
951(define (bag-transitive-inputs bag)
952 "Same as 'package-transitive-inputs', but applied to a bag."
efb10f17
LC
953 (parameterize ((%current-target-system #f)
954 (%current-system (bag-system bag)))
f52fbf70 955 (transitive-inputs (bag-direct-inputs bag))))
0d5a559f
LC
956
957(define (bag-transitive-build-inputs bag)
958 "Same as 'package-transitive-native-inputs', but applied to a bag."
efb10f17
LC
959 (parameterize ((%current-target-system #f)
960 (%current-system (bag-system bag)))
f52fbf70 961 (transitive-inputs (bag-build-inputs bag))))
0d5a559f
LC
962
963(define (bag-transitive-host-inputs bag)
964 "Same as 'package-transitive-target-inputs', but applied to a bag."
efb10f17
LC
965 (parameterize ((%current-target-system (bag-target bag))
966 (%current-system (bag-system bag)))
6cef554b 967 (transitive-inputs (bag-host-inputs bag))))
0d5a559f
LC
968
969(define (bag-transitive-target-inputs bag)
970 "Return the \"target inputs\" of BAG, recursively."
efb10f17
LC
971 (parameterize ((%current-target-system (bag-target bag))
972 (%current-system (bag-system bag)))
f52fbf70 973 (transitive-inputs (bag-target-inputs bag))))
0d5a559f 974
3e223a22
LC
975(define* (package-closure packages #:key (system (%current-system)))
976 "Return the closure of PACKAGES on SYSTEM--i.e., PACKAGES and the list of
977packages they depend on, recursively."
978 (let loop ((packages packages)
979 (visited vlist-null)
980 (closure (list->setq packages)))
981 (match packages
982 (()
983 (set->list closure))
984 ((package . rest)
985 (if (vhash-assq package visited)
986 (loop rest visited closure)
987 (let* ((bag (package->bag package system))
988 (dependencies (filter-map (match-lambda
989 ((label (? package? package) . _)
990 package)
991 (_ #f))
992 (bag-direct-inputs bag))))
993 (loop (append dependencies rest)
994 (vhash-consq package #t visited)
995 (fold set-insert closure dependencies))))))))
996
ff39361c
LC
997(define (build-system-with-package-mapping bs rewrite)
998 "Return a variant of BS, a build system, that rewrites a bag's inputs by
999passing them through REWRITE, a procedure that takes an input tuplet and
1000returns a \"rewritten\" input tuplet."
1001 (define lower
1002 (build-system-lower bs))
1003
1004 (define (lower* . args)
1005 (let ((lowered (apply lower args)))
1006 (bag
1007 (inherit lowered)
1008 (build-inputs (map rewrite (bag-build-inputs lowered)))
1009 (host-inputs (map rewrite (bag-host-inputs lowered)))
1010 (target-inputs (map rewrite (bag-target-inputs lowered))))))
1011
1012 (build-system
1013 (inherit bs)
1014 (lower lower*)))
1015
1016(define* (package-mapping proc #:optional (cut? (const #f))
1017 #:key deep?)
f37f2b83
LC
1018 "Return a procedure that, given a package, applies PROC to all the packages
1019depended on and returns the resulting package. The procedure stops recursion
ff39361c
LC
1020when CUT? returns true for a given package. When DEEP? is true, PROC is
1021applied to implicit inputs as well."
2a75b0b6
LC
1022 (define (rewrite input)
1023 (match input
1024 ((label (? package? package) outputs ...)
8db4ebb0 1025 (cons* label (replace package) outputs))
2a75b0b6
LC
1026 (_
1027 input)))
1028
ff39361c
LC
1029 (define mapping-property
1030 ;; Property indicating whether the package has already been processed.
1031 (gensym " package-mapping-done"))
1032
c9134e82
LC
1033 (define replace
1034 (mlambdaq (p)
ff39361c 1035 ;; If P is the result of a previous call, return it.
8db4ebb0
LC
1036 (cond ((assq-ref (package-properties p) mapping-property)
1037 p)
1038
1039 ((cut? p)
1040 ;; Since P's propagated inputs are really inputs of its dependents,
1041 ;; rewrite them as well, unless we're doing a "shallow" rewrite.
1042 (let ((p (proc p)))
1043 (if (or (not deep?)
1044 (null? (package-propagated-inputs p)))
1045 p
1046 (package
1047 (inherit p)
1048 (location (package-location p))
1049 (replacement (package-replacement p))
1050 (propagated-inputs (map rewrite (package-propagated-inputs p)))
1051 (properties `((,mapping-property . #t)
1052 ,@(package-properties p)))))))
1053
1054 (else
1055 ;; Return a variant of P with PROC applied to P and its explicit
1056 ;; dependencies, recursively. Memoize the transformations. Failing
1057 ;; to do that, we would build a huge object graph with lots of
1058 ;; duplicates, which in turns prevents us from benefiting from
1059 ;; memoization in 'package-derivation'.
1060 (let ((p (proc p)))
1061 (package
1062 (inherit p)
1063 (location (package-location p))
1064 (build-system (if deep?
1065 (build-system-with-package-mapping
1066 (package-build-system p) rewrite)
1067 (package-build-system p)))
1068 (inputs (map rewrite (package-inputs p)))
1069 (native-inputs (map rewrite (package-native-inputs p)))
1070 (propagated-inputs (map rewrite (package-propagated-inputs p)))
1071 (replacement (and=> (package-replacement p) replace))
1072 (properties `((,mapping-property . #t)
1073 ,@(package-properties p)))))))))
2a75b0b6
LC
1074
1075 replace)
1076
f37f2b83 1077(define* (package-input-rewriting replacements
8819551c
LC
1078 #:optional (rewrite-name identity)
1079 #:key (deep? #t))
f37f2b83 1080 "Return a procedure that, when passed a package, replaces its direct and
8819551c
LC
1081indirect dependencies, including implicit inputs when DEEP? is true, according
1082to REPLACEMENTS. REPLACEMENTS is a list of package pairs; the first element
1083of each pair is the package to replace, and the second one is the replacement.
f37f2b83
LC
1084
1085Optionally, REWRITE-NAME is a one-argument procedure that takes the name of a
1086package and returns its new name after rewrite."
8819551c
LC
1087 (define replacement-property
1088 ;; Property to tag right-hand sides in REPLACEMENTS.
1089 (gensym " package-replacement"))
f37f2b83 1090
8819551c
LC
1091 (define (rewrite p)
1092 (if (assq-ref (package-properties p) replacement-property)
1093 p
1094 (match (assq-ref replacements p)
1095 (#f (package/inherit p
1096 (name (rewrite-name (package-name p)))))
1097 (new (if deep?
1098 (package/inherit new
1099 (properties `((,replacement-property . #t)
1100 ,@(package-properties new))))
1101 new)))))
1102
1103 (define (cut? p)
1104 (or (assq-ref (package-properties p) replacement-property)
1105 (assq-ref replacements p)))
1106
1107 (package-mapping rewrite cut?
1108 #:deep? deep?))
f37f2b83 1109
2bf6f962 1110(define* (package-input-rewriting/spec replacements #:key (deep? #t))
f258d886 1111 "Return a procedure that, given a package, applies the given REPLACEMENTS to
2bf6f962
LC
1112all the package graph, including implicit inputs unless DEEP? is false.
1113REPLACEMENTS is a list of spec/procedures pair; each spec is a package
1114specification such as \"gcc\" or \"guile@2\", and each procedure takes a
1115matching package and returns a replacement for that package."
f258d886
LC
1116 (define table
1117 (fold (lambda (replacement table)
1118 (match replacement
1119 ((spec . proc)
1120 (let-values (((name version)
1121 (package-name->name+version spec)))
1122 (vhash-cons name (list version proc) table)))))
1123 vlist-null
1124 replacements))
1125
1126 (define (find-replacement package)
1127 (vhash-fold* (lambda (item proc)
1128 (or proc
1129 (match item
1130 ((#f proc)
1131 proc)
1132 ((version proc)
1133 (and (version-prefix? version
1134 (package-version package))
1135 proc)))))
1136 #f
1137 (package-name package)
1138 table))
1139
2bf6f962
LC
1140 (define replacement-property
1141 (gensym " package-replacement"))
f258d886 1142
2bf6f962
LC
1143 (define (rewrite p)
1144 (if (assq-ref (package-properties p) replacement-property)
1145 p
1146 (match (find-replacement p)
1147 (#f p)
1148 (proc
1149 (let ((new (proc p)))
1150 ;; Mark NEW as already processed.
1151 (package/inherit new
1152 (properties `((,replacement-property . #t)
1153 ,@(package-properties new)))))))))
1154
1155 (define (cut? p)
1156 (or (assq-ref (package-properties p) replacement-property)
1157 (find-replacement p)))
1158
1159 (package-mapping rewrite cut?
1160 #:deep? deep?))
bedba064 1161
a2ebaddd
LC
1162\f
1163;;;
1164;;; Package derivations.
1165;;;
1166
1167(define %derivation-cache
1168 ;; Package to derivation-path mapping.
e4588af9 1169 (make-weak-key-hash-table 100))
a2ebaddd 1170
198d84b7
LC
1171(define (cache! cache package system thunk)
1172 "Memoize in CACHE the return values of THUNK as the derivation of PACKAGE on
e509d152 1173SYSTEM."
bce7526f
LC
1174 ;; FIXME: This memoization should be associated with the open store, because
1175 ;; otherwise it breaks when switching to a different store.
0b1be8fd 1176 (let ((result (thunk)))
e509d152
LC
1177 ;; Use `hashq-set!' instead of `hash-set!' because `hash' returns the
1178 ;; same value for all structs (as of Guile 2.0.6), and because pointer
1179 ;; equality is sufficient in practice.
198d84b7 1180 (hashq-set! cache package
0b1be8fd 1181 `((,system . ,result)
198d84b7 1182 ,@(or (hashq-ref cache package) '())))
0b1be8fd 1183 result))
e509d152 1184
198d84b7
LC
1185(define-syntax cached
1186 (syntax-rules (=>)
1187 "Memoize the result of BODY for the arguments PACKAGE and SYSTEM.
e509d152 1188Return the cached result when available."
198d84b7
LC
1189 ((_ (=> cache) package system body ...)
1190 (let ((thunk (lambda () body ...))
1191 (key system))
1192 (match (hashq-ref cache package)
1193 ((alist (... ...))
1194 (match (assoc-ref alist key)
0b1be8fd
LC
1195 (#f (cache! cache package key thunk))
1196 (value value)))
e509d152 1197 (#f
198d84b7
LC
1198 (cache! cache package key thunk)))))
1199 ((_ package system body ...)
1200 (cached (=> %derivation-cache) package system body ...))))
a2ebaddd 1201
a63062b5
LC
1202(define* (expand-input store package input system #:optional cross-system)
1203 "Expand INPUT, an input tuple, such that it contains only references to
1204derivation paths or store paths. PACKAGE is only used to provide contextual
1205information in exceptions."
592ef6c8
LC
1206 (define (intern file)
1207 ;; Add FILE to the store. Set the `recursive?' bit to #t, so that
1208 ;; file permissions are preserved.
a9ebd9ef 1209 (add-to-store store (basename file) #t "sha256" file))
592ef6c8 1210
a63062b5
LC
1211 (define derivation
1212 (if cross-system
05962f29
LC
1213 (cut package-cross-derivation store <> cross-system system
1214 #:graft? #f)
1215 (cut package-derivation store <> system #:graft? #f)))
a63062b5
LC
1216
1217 (match input
1218 (((? string? name) (? package? package))
1219 (list name (derivation package)))
1220 (((? string? name) (? package? package)
1221 (? string? sub-drv))
1222 (list name (derivation package)
1223 sub-drv))
1224 (((? string? name)
1225 (and (? string?) (? derivation-path?) drv))
1226 (list name drv))
1227 (((? string? name)
1228 (and (? string?) (? file-exists? file)))
1229 ;; Add FILE to the store. When FILE is in the sub-directory of a
1230 ;; store path, it needs to be added anyway, so it can be used as a
1231 ;; source.
1232 (list name (intern file)))
da675305 1233 (((? string? name) (? struct? source))
76c48619
LC
1234 ;; 'package-source-derivation' calls 'lower-object', which can throw
1235 ;; '&gexp-input-error'. However '&gexp-input-error' lacks source
1236 ;; location info, so we catch and rethrow here (XXX: not optimal
1237 ;; performance-wise).
1238 (guard (c ((gexp-input-error? c)
1239 (raise (condition
1240 (&package-input-error
1241 (package package)
1242 (input (gexp-error-invalid-input c)))))))
1243 (list name (package-source-derivation store source system))))
a63062b5
LC
1244 (x
1245 (raise (condition (&package-input-error
1246 (package package)
1247 (input x)))))))
592ef6c8 1248
9775412e
LC
1249(define %bag-cache
1250 ;; 'eq?' cache mapping packages to system+target+graft?-dependent bags.
1251 ;; It significantly speeds things up when doing repeated calls to
1252 ;; 'package->bag' as is the case when building a profile.
1253 (make-weak-key-hash-table 200))
1254
0d5a559f
LC
1255(define* (package->bag package #:optional
1256 (system (%current-system))
05962f29
LC
1257 (target (%current-target-system))
1258 #:key (graft? (%graft?)))
0d5a559f
LC
1259 "Compile PACKAGE into a bag for SYSTEM, possibly cross-compiled to TARGET,
1260and return it."
9f785529
LC
1261 (let ((package (or (and graft? (package-replacement package))
1262 package)))
1263 (cached (=> %bag-cache)
1264 package (list system target)
1265 ;; Bind %CURRENT-SYSTEM and %CURRENT-TARGET-SYSTEM so that thunked
1266 ;; field values can refer to it.
1267 (parameterize ((%current-system system)
1268 (%current-target-system target))
1269 (match package
1270 ((and self
1271 ($ <package> name version source build-system
1272 args inputs propagated-inputs native-inputs
1273 outputs))
1274 ;; Even though we prefer to use "@" to separate the package
1275 ;; name from the package version in various user-facing parts
1276 ;; of Guix, checkStoreName (in nix/libstore/store-api.cc)
1277 ;; prohibits the use of "@", so use "-" instead.
1278 (or (make-bag build-system (string-append name "-" version)
1279 #:system system
1280 #:target target
1281 #:source source
1282 #:inputs (append (inputs self)
1283 (propagated-inputs self))
1284 #:outputs outputs
1285 #:native-inputs (native-inputs self)
1286 #:arguments (args self))
1287 (raise (if target
1288 (condition
1289 (&package-cross-build-system-error
1290 (package package)))
1291 (condition
1292 (&package-error
1293 (package package))))))))))))
0d5a559f 1294
ced71ac7
LC
1295(define %graft-cache
1296 ;; 'eq?' cache mapping package objects to a graft corresponding to their
1297 ;; replacement package.
1298 (make-weak-key-hash-table 200))
1299
05962f29 1300(define (input-graft store system)
03a70e4c
LC
1301 "Return a procedure that, given a package with a replacement and an output name,
1302returns a graft, and #f otherwise."
1303 (match-lambda*
1304 (((? package? package) output)
c22a1324
LC
1305 (let ((replacement (package-replacement package)))
1306 (and replacement
03a70e4c 1307 (cached (=> %graft-cache) package (cons output system)
ced71ac7
LC
1308 (let ((orig (package-derivation store package system
1309 #:graft? #f))
d0025d01
LC
1310 (new (package-derivation store replacement system
1311 #:graft? #t)))
ced71ac7
LC
1312 (graft
1313 (origin orig)
03a70e4c
LC
1314 (origin-output output)
1315 (replacement new)
1316 (replacement-output output)))))))))
05962f29
LC
1317
1318(define (input-cross-graft store target system)
1319 "Same as 'input-graft', but for cross-compilation inputs."
03a70e4c
LC
1320 (match-lambda*
1321 (((? package? package) output)
1322 (let ((replacement (package-replacement package)))
1323 (and replacement
1324 (let ((orig (package-cross-derivation store package target system
1325 #:graft? #f))
1326 (new (package-cross-derivation store replacement
1327 target system
1328 #:graft? #t)))
1329 (graft
1330 (origin orig)
1331 (origin-output output)
1332 (replacement new)
1333 (replacement-output output))))))))
05962f29 1334
c22a1324
LC
1335(define* (fold-bag-dependencies proc seed bag
1336 #:key (native? #t))
1337 "Fold PROC over the packages BAG depends on. Each package is visited only
1338once, in depth-first order. If NATIVE? is true, restrict to native
1339dependencies; otherwise, restrict to target dependencies."
ff0e0041
LC
1340 (define bag-direct-inputs*
1341 (if native?
1342 (lambda (bag)
1343 (append (bag-build-inputs bag)
1344 (bag-target-inputs bag)
1345 (if (bag-target bag)
1346 '()
1347 (bag-host-inputs bag))))
609d126e 1348 bag-host-inputs))
ff0e0041 1349
03a70e4c 1350 (let loop ((inputs (bag-direct-inputs* bag))
c22a1324 1351 (result seed)
03a70e4c
LC
1352 (visited vlist-null))
1353 (match inputs
c22a1324
LC
1354 (()
1355 result)
03a70e4c
LC
1356 (((label (? package? head) . rest) . tail)
1357 (let ((output (match rest (() "out") ((output) output)))
1358 (outputs (vhash-foldq* cons '() head visited)))
1359 (if (member output outputs)
1360 (loop tail result visited)
1361 (let ((inputs (bag-direct-inputs* (package->bag head))))
1362 (loop (append inputs tail)
1363 (proc head output result)
1364 (vhash-consq head output visited))))))
c22a1324
LC
1365 ((head . tail)
1366 (loop tail result visited)))))
1367
05962f29 1368(define* (bag-grafts store bag)
c22a1324
LC
1369 "Return the list of grafts potentially applicable to BAG. Potentially
1370applicable grafts are collected by looking at direct or indirect dependencies
1371of BAG that have a 'replacement'. Whether a graft is actually applicable
1372depends on whether the outputs of BAG depend on the items the grafts refer
1373to (see 'graft-derivation'.)"
1374 (define system (bag-system bag))
1375 (define target (bag-target bag))
1376
1377 (define native-grafts
1378 (let ((->graft (input-graft store system)))
b49caaa2
LC
1379 (parameterize ((%current-system system)
1380 (%current-target-system #f))
03a70e4c
LC
1381 (fold-bag-dependencies (lambda (package output grafts)
1382 (match (->graft package output)
b49caaa2
LC
1383 (#f grafts)
1384 (graft (cons graft grafts))))
1385 '()
1386 bag))))
c22a1324
LC
1387
1388 (define target-grafts
1389 (if target
1390 (let ((->graft (input-cross-graft store target system)))
b49caaa2
LC
1391 (parameterize ((%current-system system)
1392 (%current-target-system target))
03a70e4c
LC
1393 (fold-bag-dependencies (lambda (package output grafts)
1394 (match (->graft package output)
b49caaa2
LC
1395 (#f grafts)
1396 (graft (cons graft grafts))))
1397 '()
1398 bag
1399 #:native? #f)))
c22a1324
LC
1400 '()))
1401
fcadd9ff
LC
1402 ;; We can end up with several identical grafts if we stumble upon packages
1403 ;; that are not 'eq?' but map to the same derivation (this can happen when
1404 ;; using things like 'package-with-explicit-inputs'.) Hence the
1405 ;; 'delete-duplicates' call.
1406 (delete-duplicates
1407 (append native-grafts target-grafts)))
05962f29
LC
1408
1409(define* (package-grafts store package
1410 #:optional (system (%current-system))
1411 #:key target)
1412 "Return the list of grafts applicable to PACKAGE as built for SYSTEM and
1413TARGET."
1414 (let* ((package (or (package-replacement package) package))
1415 (bag (package->bag package system target)))
1416 (bag-grafts store bag)))
1417
d3d337d2
LC
1418(define* (bag->derivation store bag
1419 #:optional context)
1420 "Return the derivation to build BAG for SYSTEM. Optionally, CONTEXT can be
1421a package object describing the context in which the call occurs, for improved
1422error reporting."
1423 (if (bag-target bag)
1424 (bag->cross-derivation store bag)
1425 (let* ((system (bag-system bag))
1426 (inputs (bag-transitive-inputs bag))
1427 (input-drvs (map (cut expand-input store context <> system)
1428 inputs))
1429 (paths (delete-duplicates
1430 (append-map (match-lambda
1431 ((_ (? package? p) _ ...)
1432 (package-native-search-paths
1433 p))
1434 (_ '()))
1435 inputs))))
1436
1437 (apply (bag-build bag)
1438 store (bag-name bag) input-drvs
1439 #:search-paths paths
1440 #:outputs (bag-outputs bag) #:system system
1441 (bag-arguments bag)))))
1442
1443(define* (bag->cross-derivation store bag
1444 #:optional context)
1445 "Return the derivation to build BAG, which is actually a cross build.
1446Optionally, CONTEXT can be a package object denoting the context of the call.
1447This is an internal procedure."
1448 (let* ((system (bag-system bag))
1449 (target (bag-target bag))
1450 (host (bag-transitive-host-inputs bag))
1451 (host-drvs (map (cut expand-input store context <> system target)
1452 host))
1453 (target* (bag-transitive-target-inputs bag))
1454 (target-drvs (map (cut expand-input store context <> system)
1455 target*))
1456 (build (bag-transitive-build-inputs bag))
1457 (build-drvs (map (cut expand-input store context <> system)
1458 build))
1459 (all (append build target* host))
1460 (paths (delete-duplicates
1461 (append-map (match-lambda
1462 ((_ (? package? p) _ ...)
1463 (package-search-paths p))
1464 (_ '()))
1465 all)))
1466 (npaths (delete-duplicates
1467 (append-map (match-lambda
1468 ((_ (? package? p) _ ...)
1469 (package-native-search-paths
1470 p))
1471 (_ '()))
1472 all))))
1473
1474 (apply (bag-build bag)
1475 store (bag-name bag)
1476 #:native-drvs build-drvs
1477 #:target-drvs (append host-drvs target-drvs)
1478 #:search-paths paths
1479 #:native-search-paths npaths
1480 #:outputs (bag-outputs bag)
1481 #:system system #:target target
1482 (bag-arguments bag))))
1483
a63062b5 1484(define* (package-derivation store package
05962f29
LC
1485 #:optional (system (%current-system))
1486 #:key (graft? (%graft?)))
59688fc4
LC
1487 "Return the <derivation> object of PACKAGE for SYSTEM."
1488
e509d152
LC
1489 ;; Compute the derivation and cache the result. Caching is important
1490 ;; because some derivations, such as the implicit inputs of the GNU build
1491 ;; system, will be queried many, many times in a row.
05962f29
LC
1492 (cached package (cons system graft?)
1493 (let* ((bag (package->bag package system #f #:graft? graft?))
1494 (drv (bag->derivation store bag package)))
1495 (if graft?
1496 (match (bag-grafts store bag)
1497 (()
1498 drv)
1499 (grafts
2b6fe605 1500 (let ((guile (package-derivation store (guile-for-grafts)
05962f29 1501 system #:graft? #f)))
c22a1324
LC
1502 ;; TODO: As an optimization, we can simply graft the tip
1503 ;; of the derivation graph since 'graft-derivation'
1504 ;; recurses anyway.
b0fef4d6 1505 (graft-derivation store drv grafts
05962f29
LC
1506 #:system system
1507 #:guile guile))))
1508 drv))))
e3ce5d70 1509
9c1edabd 1510(define* (package-cross-derivation store package target
05962f29
LC
1511 #:optional (system (%current-system))
1512 #:key (graft? (%graft?)))
9c1edabd
LC
1513 "Cross-build PACKAGE for TARGET (a GNU triplet) from host SYSTEM (a Guix
1514system identifying string)."
05962f29
LC
1515 (cached package (list system target graft?)
1516 (let* ((bag (package->bag package system target #:graft? graft?))
1517 (drv (bag->derivation store bag package)))
1518 (if graft?
1519 (match (bag-grafts store bag)
1520 (()
1521 drv)
1522 (grafts
b0fef4d6 1523 (graft-derivation store drv grafts
05962f29
LC
1524 #:system system
1525 #:guile
2b6fe605 1526 (package-derivation store (guile-for-grafts)
05962f29
LC
1527 system #:graft? #f))))
1528 drv))))
d510ab46 1529
de8bcdae
LC
1530(define* (package-output store package
1531 #:optional (output "out") (system (%current-system)))
d510ab46
LC
1532 "Return the output path of PACKAGE's OUTPUT for SYSTEM---where OUTPUT is the
1533symbolic output name, such as \"out\". Note that this procedure calls
1534`package-derivation', which is costly."
59688fc4
LC
1535 (let ((drv (package-derivation store package system)))
1536 (derivation->output-path drv output)))
e87f0591
LC
1537
1538\f
1539;;;
1540;;; Monadic interface.
1541;;;
1542
1543(define (set-guile-for-build guile)
1544 "This monadic procedure changes the Guile currently used to run the build
1545code of derivations to GUILE, a package object."
1546 (lambda (store)
1547 (let ((guile (package-derivation store guile)))
4e190c28 1548 (values (%guile-for-build guile) store))))
e87f0591
LC
1549
1550(define* (package-file package
1551 #:optional file
1552 #:key
1553 system (output "out") target)
1554 "Return as a monadic value the absolute file name of FILE within the
1555OUTPUT directory of PACKAGE. When FILE is omitted, return the name of the
1556OUTPUT directory of PACKAGE. When TARGET is true, use it as a
c8d8f616
LC
1557cross-compilation target triplet.
1558
1559Note that this procedure does _not_ build PACKAGE. Thus, the result might or
1560might not designate an existing file. We recommend not using this procedure
1561unless you know what you are doing."
e87f0591
LC
1562 (lambda (store)
1563 (define compute-derivation
1564 (if target
1565 (cut package-cross-derivation <> <> target <>)
1566 package-derivation))
1567
1568 (let* ((system (or system (%current-system)))
1569 (drv (compute-derivation store package system))
1570 (out (derivation->output-path drv output)))
4e190c28
LC
1571 (values (if file
1572 (string-append out "/" file)
1573 out)
1574 store))))
e87f0591
LC
1575
1576(define package->derivation
1577 (store-lift package-derivation))
1578
1579(define package->cross-derivation
1580 (store-lift package-cross-derivation))
1581
1cdecf24 1582(define-gexp-compiler (package-compiler (package <package>) system target)
ff40e9b7
LC
1583 ;; Compile PACKAGE to a derivation for SYSTEM, optionally cross-compiled for
1584 ;; TARGET. This is used when referring to a package from within a gexp.
1585 (if target
1586 (package->cross-derivation package target system)
1587 (package->derivation package system)))
1588
78951064 1589(define* (origin->derivation origin
f220a838 1590 #:optional (system (%current-system)))
78951064
LC
1591 "Return the derivation corresponding to ORIGIN."
1592 (match origin
ce0be567 1593 (($ <origin> uri method hash name (= force ()) #f)
f220a838 1594 ;; No patches, no snippet: this is a fixed-output derivation.
ce0be567
LC
1595 (method uri
1596 (content-hash-algorithm hash)
1597 (content-hash-value hash)
1598 name #:system system))
1599 (($ <origin> uri method hash name (= force (patches ...)) snippet
1929fdba 1600 (flags ...) inputs (modules ...) guile-for-build)
f220a838 1601 ;; Patches and/or a snippet.
ce0be567
LC
1602 (mlet %store-monad ((source (method uri
1603 (content-hash-algorithm hash)
1604 (content-hash-value hash)
1605 name #:system system))
f220a838
LC
1606 (guile (package->derivation (or guile-for-build
1607 (default-guile))
1608 system
1609 #:graft? #f)))
cf87cc89
LC
1610 (patch-and-repack source patches
1611 #:inputs inputs
1612 #:snippet snippet
1613 #:flags flags
1614 #:system system
1615 #:modules modules
78951064 1616 #:guile-for-build guile)))))
f220a838 1617
1cdecf24 1618(define-gexp-compiler (origin-compiler (origin <origin>) system target)
ff40e9b7
LC
1619 ;; Compile ORIGIN to a derivation for SYSTEM. This is used when referring
1620 ;; to an origin from within a gexp.
1621 (origin->derivation origin system))
1622
78951064 1623(define package-source-derivation ;somewhat deprecated
da675305 1624 (let ((lower (store-lower lower-object)))
78951064
LC
1625 (lambda* (store source #:optional (system (%current-system)))
1626 "Return the derivation or file corresponding to SOURCE, which can be an
da675305
LC
1627a file name or any object handled by 'lower-object', such as an <origin>.
1628When SOURCE is a file name, return either the interned file name (if SOURCE is
1629outside of the store) or SOURCE itself (if SOURCE is already a store item.)"
78951064
LC
1630 (match source
1631 ((and (? string?) (? direct-store-path?) file)
1632 file)
1633 ((? string? file)
1634 (add-to-store store (basename file) #t "sha256" file))
1635 (_
1636 (lower store source system))))))