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