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