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