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