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