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