gnu: perl-cache-fastmmap: Update to 1.47.
[jackhill/guix/guix.git] / gnu / packages.scm
CommitLineData
233e7676 1;;; GNU Guix --- Functional package management for GNU
e2a903c8 2;;; Copyright © 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019 Ludovic Courtès <ludo@gnu.org>
c2868b1e 3;;; Copyright © 2013 Mark H Weaver <mhw@netris.org>
7d193ec3 4;;; Copyright © 2014 Eric Bavier <bavier@member.fsf.org>
96eaa55f 5;;; Copyright © 2016, 2017 Alex Kost <alezost@gmail.com>
fad155d4 6;;; Copyright © 2016 Mathieu Lirzin <mthl@gnu.org>
6b1891b0 7;;;
233e7676 8;;; This file is part of GNU Guix.
6b1891b0 9;;;
233e7676 10;;; GNU Guix is free software; you can redistribute it and/or modify it
6b1891b0
LC
11;;; under the terms of the GNU General Public License as published by
12;;; the Free Software Foundation; either version 3 of the License, or (at
13;;; your option) any later version.
14;;;
233e7676 15;;; GNU Guix is distributed in the hope that it will be useful, but
6b1891b0
LC
16;;; WITHOUT ANY WARRANTY; without even the implied warranty of
17;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18;;; GNU General Public License for more details.
19;;;
20;;; You should have received a copy of the GNU General Public License
233e7676 21;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
6b1891b0 22
59a43334 23(define-module (gnu packages)
6b1891b0 24 #:use-module (guix packages)
98eb8cbe 25 #:use-module (guix ui)
800cdeef 26 #:use-module (guix utils)
cd903ef7 27 #:use-module (guix discovery)
f9704f17 28 #:use-module (guix memoization)
95cd4971
LC
29 #:use-module ((guix build utils)
30 #:select ((package-name->name+version
5fbdc9a5
LC
31 . hyphen-separated-name->name+version)
32 mkdir-p))
c08ea55e 33 #:autoload (guix profiles) (packages->manifest)
fe634eaf 34 #:use-module (guix describe)
c2868b1e 35 #:use-module (ice-9 vlist)
dc5669cd 36 #:use-module (ice-9 match)
5fbdc9a5
LC
37 #:autoload (ice-9 binary-ports) (put-bytevector)
38 #:autoload (system base compile) (compile)
6b1891b0 39 #:use-module (srfi srfi-1)
5e3b388b 40 #:use-module (srfi srfi-11)
6b1891b0 41 #:use-module (srfi srfi-26)
dbab5150
LC
42 #:use-module (srfi srfi-34)
43 #:use-module (srfi srfi-35)
800cdeef
LC
44 #:use-module (srfi srfi-39)
45 #:export (search-patch
25897079 46 search-patches
96eaa55f 47 search-auxiliary-file
ac5aa288 48 search-bootstrap-binary
0492f4a2 49 %patch-path
96eaa55f 50 %auxiliary-files-path
0b3651bc 51 %bootstrap-binaries-path
c107b541 52 %package-module-path
fe634eaf 53 %default-package-module-path
7d193ec3 54
ba326ce4 55 fold-packages
0ea939fb 56 fold-available-packages
7d193ec3 57
dc5669cd 58 find-packages-by-name
ee8099f5 59 find-package-locations
3f26bfc1 60 find-best-packages-by-name
7d193ec3 61
84189ebc 62 specification->package
c08ea55e 63 specification->package+output
ee8099f5 64 specification->location
5fbdc9a5
LC
65 specifications->manifest
66
67 generate-package-cache))
6b1891b0
LC
68
69;;; Commentary:
70;;;
71;;; General utilities for the software distribution---i.e., the modules under
59a43334 72;;; (gnu packages ...).
6b1891b0
LC
73;;;
74;;; Code:
75
96eaa55f
AK
76;; By default, we store patches, auxiliary files and bootstrap binaries
77;; alongside Guile modules. This is so that these extra files can be
78;; found without requiring a special setup, such as a specific
79;; installation directory and an extra environment variable. One
80;; advantage of this setup is that everything just works in an
81;; auto-compilation setting.
a9f60c42 82
a9f60c42 83(define %bootstrap-binaries-path
ac5aa288 84 (make-parameter
1ffa7090 85 (map (cut string-append <> "/gnu/packages/bootstrap")
0b3651bc 86 %load-path)))
ac5aa288 87
96eaa55f
AK
88(define %auxiliary-files-path
89 (make-parameter
90 (map (cut string-append <> "/gnu/packages/aux-files")
91 %load-path)))
92
93(define (search-auxiliary-file file-name)
94 "Search the auxiliary FILE-NAME. Return #f if not found."
95 (search-path (%auxiliary-files-path) file-name))
96
800cdeef 97(define (search-patch file-name)
dbab5150
LC
98 "Search the patch FILE-NAME. Raise an error if not found."
99 (or (search-path (%patch-path) file-name)
100 (raise (condition
69daee23 101 (&message (message (format #f (G_ "~a: patch not found")
dbab5150 102 file-name)))))))
800cdeef 103
25897079
AK
104(define-syntax-rule (search-patches file-name ...)
105 "Return the list of absolute file names corresponding to each
106FILE-NAME found in %PATCH-PATH."
107 (list (search-patch file-name) ...))
108
ac5aa288 109(define (search-bootstrap-binary file-name system)
dfba5489
LC
110 "Search the bootstrap binary FILE-NAME for SYSTEM. Raise an error if not
111found."
112 (or (search-path (%bootstrap-binaries-path)
113 (string-append system "/" file-name))
114 (raise (condition
115 (&message
116 (message
69daee23 117 (format #f (G_ "could not find bootstrap binary '~a' \
dfba5489
LC
118for system '~a'")
119 file-name system)))))))
ac5aa288 120
84836a57 121(define %distro-root-directory
eaae07ec
LC
122 ;; Absolute file name of the module hierarchy. Since (gnu packages …) might
123 ;; live in a directory different from (guix), try to get the best match.
124 (letrec-syntax ((dirname* (syntax-rules ()
125 ((_ file)
126 (dirname file))
127 ((_ file head tail ...)
128 (dirname (dirname* file tail ...)))))
129 (try (syntax-rules ()
130 ((_ (file things ...) rest ...)
131 (match (search-path %load-path file)
132 (#f
133 (try rest ...))
134 (absolute
135 (dirname* absolute things ...))))
136 ((_)
137 #f))))
138 (try ("gnu/packages/base.scm" gnu/ packages/)
139 ("gnu/packages.scm" gnu/)
140 ("guix.scm"))))
6b1891b0 141
fe634eaf
LC
142(define %default-package-module-path
143 ;; Default search path for package modules.
144 `((,%distro-root-directory . "gnu/packages")))
145
5fbdc9a5
LC
146(define (cache-is-authoritative?)
147 "Return true if the pre-computed package cache is authoritative. It is not
148authoritative when entries have been added via GUIX_PACKAGE_PATH or '-L'
149flags."
150 (equal? (%package-module-path)
151 (append %default-package-module-path
152 (package-path-entries))))
153
c107b541
LC
154(define %package-module-path
155 ;; Search path for package modules. Each item must be either a directory
156 ;; name or a pair whose car is a directory and whose cdr is a sub-directory
157 ;; to narrow the search.
bfc9c339
LC
158 (let*-values (((not-colon)
159 (char-set-complement (char-set #\:)))
160 ((environment)
161 (string-tokenize (or (getenv "GUIX_PACKAGE_PATH") "")
162 not-colon))
163 ((channels-scm channels-go)
164 (package-path-entries)))
fe634eaf
LC
165 ;; Automatically add channels and items from $GUIX_PACKAGE_PATH to Guile's
166 ;; search path. For historical reasons, $GUIX_PACKAGE_PATH goes to the
167 ;; front; channels go to the back so that they don't override Guix' own
168 ;; modules.
169 (set! %load-path
bfc9c339 170 (append environment %load-path channels-scm))
fe634eaf 171 (set! %load-compiled-path
bfc9c339 172 (append environment %load-compiled-path channels-go))
8689901f
LC
173
174 (make-parameter
fe634eaf
LC
175 (append environment
176 %default-package-module-path
bfc9c339 177 channels-scm))))
c107b541 178
ee06af5b
LC
179(define %patch-path
180 ;; Define it after '%package-module-path' so that '%load-path' contains user
181 ;; directories, allowing patches in $GUIX_PACKAGE_PATH to be found.
182 (make-parameter
183 (map (lambda (directory)
184 (if (string=? directory %distro-root-directory)
185 (string-append directory "/gnu/packages/patches")
186 directory))
187 %load-path)))
188
0ea939fb
LC
189(define (fold-available-packages proc init)
190 "Fold PROC over the list of available packages. For each available package,
191PROC is called along these lines:
192
193 (PROC NAME VERSION RESULT
194 #:outputs OUTPUTS
195 #:location LOCATION
196 …)
197
198PROC can use #:allow-other-keys to ignore the bits it's not interested in.
199When a package cache is available, this procedure does not actually load any
200package module."
201 (define cache
202 (load-package-cache (current-profile)))
203
204 (if (and cache (cache-is-authoritative?))
205 (vhash-fold (lambda (name vector result)
206 (match vector
207 (#(name version module symbol outputs
208 supported? deprecated?
209 file line column)
210 (proc name version result
211 #:outputs outputs
212 #:location (and file
213 (location file line column))
214 #:supported? supported?
215 #:deprecated? deprecated?))))
216 init
217 cache)
218 (fold-packages (lambda (package result)
219 (proc (package-name package)
220 (package-version package)
221 result
222 #:outputs (package-outputs package)
223 #:location (package-location package)
224 #:supported?
225 (->bool
226 (member (%current-system)
227 (package-supported-systems package)))
228 #:deprecated?
229 (->bool
230 (package-superseded package))))
231 init)))
232
5c5ae46c
LC
233(define* (fold-packages proc init
234 #:optional
3c0128b0
LC
235 (modules (all-modules (%package-module-path)
236 #:warn
237 warn-about-load-error))
96dc8f35 238 #:key (select? (negate hidden-package?)))
5c5ae46c 239 "Call (PROC PACKAGE RESULT) for each available package defined in one of
96dc8f35
LC
240MODULES that matches SELECT?, using INIT as the initial value of RESULT. It
241is guaranteed to never traverse the same package twice."
cd903ef7 242 (fold-module-public-variables (lambda (object result)
96dc8f35 243 (if (and (package? object) (select? object))
cd903ef7
LC
244 (proc object result)
245 result))
246 init
5c5ae46c 247 modules))
ba326ce4 248
5fbdc9a5
LC
249(define %package-cache-file
250 ;; Location of the package cache.
251 "/lib/guix/package.cache")
252
253(define load-package-cache
254 (mlambda (profile)
255 "Attempt to load the package cache. On success return a vhash keyed by
256package names. Return #f on failure."
257 (match profile
258 (#f #f)
259 (profile
260 (catch 'system-error
261 (lambda ()
262 (define lst
263 (load-compiled (string-append profile %package-cache-file)))
264 (fold (lambda (item vhash)
265 (match item
266 (#(name version module symbol outputs
267 supported? deprecated?
268 file line column)
269 (vhash-cons name item vhash))))
270 vlist-null
271 lst))
272 (lambda args
273 (if (= ENOENT (system-error-errno args))
274 #f
275 (apply throw args))))))))
276
277(define find-packages-by-name/direct ;bypass the cache
9ffc1c00
LC
278 (let ((packages (delay
279 (fold-packages (lambda (p r)
280 (vhash-cons (package-name p) p r))
724311a2
LC
281 vlist-null)))
282 (version>? (lambda (p1 p2)
283 (version>? (package-version p1) (package-version p2)))))
9ffc1c00
LC
284 (lambda* (name #:optional version)
285 "Return the list of packages with the given NAME. If VERSION is not #f,
724311a2
LC
286then only return packages whose version is prefixed by VERSION, sorted in
287decreasing version order."
288 (let ((matching (sort (vhash-fold* cons '() name (force packages))
289 version>?)))
9ffc1c00
LC
290 (if version
291 (filter (lambda (package)
348987d3 292 (version-prefix? version (package-version package)))
9ffc1c00
LC
293 matching)
294 matching)))))
dc5669cd 295
5fbdc9a5
LC
296(define (cache-lookup cache name)
297 "Lookup package NAME in CACHE. Return a list sorted in increasing version
298order."
299 (define (package-version<? v1 v2)
300 (version>? (vector-ref v2 1) (vector-ref v1 1)))
301
302 (sort (vhash-fold* cons '() name cache)
303 package-version<?))
304
305(define* (find-packages-by-name name #:optional version)
306 "Return the list of packages with the given NAME. If VERSION is not #f,
307then only return packages whose version is prefixed by VERSION, sorted in
308decreasing version order."
309 (define cache
310 (load-package-cache (current-profile)))
311
312 (if (and (cache-is-authoritative?) cache)
313 (match (cache-lookup cache name)
314 (#f #f)
315 ((#(_ versions modules symbols _ _ _ _ _ _) ...)
316 (fold (lambda (version* module symbol result)
317 (if (or (not version)
318 (version-prefix? version version*))
319 (cons (module-ref (resolve-interface module)
320 symbol)
321 result)
322 result))
323 '()
324 versions modules symbols)))
325 (find-packages-by-name/direct name version)))
326
ee8099f5
LC
327(define* (find-package-locations name #:optional version)
328 "Return a list of version/location pairs corresponding to each package
329matching NAME and VERSION."
330 (define cache
331 (load-package-cache (current-profile)))
332
333 (if (and cache (cache-is-authoritative?))
334 (match (cache-lookup cache name)
335 (#f '())
336 ((#(name versions modules symbols outputs
337 supported? deprecated?
338 files lines columns) ...)
339 (fold (lambda (version* file line column result)
340 (if (and file
341 (or (not version)
342 (version-prefix? version version*)))
343 (alist-cons version* (location file line column)
344 result)
345 result))
346 '()
347 versions files lines columns)))
348 (map (lambda (package)
349 (cons (package-version package) (package-location package)))
350 (find-packages-by-name/direct name version))))
351
3f26bfc1
LC
352(define (find-best-packages-by-name name version)
353 "If version is #f, return the list of packages named NAME with the highest
354version numbers; otherwise, return the list of packages named NAME and at
355VERSION."
356 (if version
357 (find-packages-by-name name version)
e2a903c8
LC
358 (match (find-packages-by-name name)
359 (()
360 '())
361 ((matches ...)
362 ;; Return the subset of MATCHES with the higher version number.
363 (let ((highest (package-version (first matches))))
364 (take-while (lambda (p)
365 (string=? (package-version p) highest))
366 matches))))))
7d193ec3 367
5fbdc9a5
LC
368(define (generate-package-cache directory)
369 "Generate under DIRECTORY a cache of all the available packages.
370
371The primary purpose of the cache is to speed up package lookup by name such
372that we don't have to traverse and load all the package modules, thereby also
373reducing the memory footprint."
374 (define cache-file
375 (string-append directory %package-cache-file))
376
36754eee 377 (define (expand-cache module symbol variable result+seen)
5fbdc9a5
LC
378 (match (false-if-exception (variable-ref variable))
379 ((? package? package)
36754eee
LC
380 (match result+seen
381 ((result . seen)
382 (if (or (vhash-assq package seen)
383 (hidden-package? package))
384 (cons result seen)
385 (cons (cons `#(,(package-name package)
386 ,(package-version package)
387 ,(module-name module)
388 ,symbol
389 ,(package-outputs package)
390 ,(->bool
391 (member (%current-system)
392 (package-supported-systems package)))
393 ,(->bool (package-superseded package))
394 ,@(let ((loc (package-location package)))
395 (if loc
396 `(,(location-file loc)
397 ,(location-line loc)
398 ,(location-column loc))
399 '(#f #f #f))))
400 result)
401 (vhash-consq package #t seen))))))
5fbdc9a5 402 (_
36754eee 403 result+seen)))
5fbdc9a5
LC
404
405 (define exp
36754eee
LC
406 (first
407 (fold-module-public-variables* expand-cache
408 (cons '() vlist-null)
409 (all-modules (%package-module-path)
410 #:warn
411 warn-about-load-error))))
5fbdc9a5
LC
412
413 (mkdir-p (dirname cache-file))
414 (call-with-output-file cache-file
415 (lambda (port)
416 ;; Store the cache as a '.go' file. This makes loading fast and reduces
417 ;; heap usage since some of the static data is directly mmapped.
418 (put-bytevector port
419 (compile `'(,@exp)
420 #:to 'bytecode
421 #:opts '(#:to-file? #t)))))
422 cache-file)
423
7d193ec3 424\f
4ea44419
AK
425(define %sigint-prompt
426 ;; The prompt to jump to upon SIGINT.
427 (make-prompt-tag "interruptible"))
428
429(define (call-with-sigint-handler thunk handler)
430 "Call THUNK and return its value. Upon SIGINT, call HANDLER with the signal
431number in the context of the continuation of the call to this function, and
432return its return value."
433 (call-with-prompt %sigint-prompt
434 (lambda ()
435 (sigaction SIGINT
436 (lambda (signum)
437 (sigaction SIGINT SIG_DFL)
438 (abort-to-prompt %sigint-prompt signum)))
439 (dynamic-wind
440 (const #t)
441 thunk
442 (cut sigaction SIGINT SIG_DFL)))
443 (lambda (k signum)
444 (handler signum))))
445
fad155d4
ML
446\f
447;;;
448;;; Package specification.
449;;;
450
e30c2be1 451(define* (%find-package spec name version)
fad155d4
ML
452 (match (find-best-packages-by-name name version)
453 ((pkg . pkg*)
454 (unless (null? pkg*)
69daee23
LC
455 (warning (G_ "ambiguous package specification `~a'~%") spec)
456 (warning (G_ "choosing ~a@~a from ~a~%")
d75e8f36 457 (package-name pkg) (package-version pkg)
fad155d4 458 (location->string (package-location pkg))))
01afdab8
LC
459 (match (package-superseded pkg)
460 ((? package? new)
69daee23 461 (info (G_ "package '~a' has been superseded by '~a'~%")
01afdab8
LC
462 (package-name pkg) (package-name new))
463 new)
464 (#f
465 pkg)))
e465d9e1 466 (x
fad155d4 467 (if version
69daee23
LC
468 (leave (G_ "~A: package not found for version ~a~%") name version)
469 (leave (G_ "~A: unknown package~%") name)))))
fad155d4 470
5e3b388b
CR
471(define (specification->package spec)
472 "Return a package matching SPEC. SPEC may be a package name, or a package
1b846da8 473name followed by an at-sign and a version number. If the version number is not
5e3b388b 474present, return the preferred newest version."
fad155d4
ML
475 (let-values (((name version) (package-name->name+version spec)))
476 (%find-package spec name version)))
84189ebc 477
ee8099f5
LC
478(define (specification->location spec)
479 "Return the location of the highest-numbered package matching SPEC, a
480specification such as \"guile@2\" or \"emacs\"."
481 (let-values (((name version) (package-name->name+version spec)))
482 (match (find-package-locations name version)
483 (()
484 (if version
485 (leave (G_ "~A: package not found for version ~a~%") name version)
486 (leave (G_ "~A: unknown package~%") name)))
487 (lst
488 (let* ((highest (match lst (((version . _) _ ...) version)))
489 (locations (take-while (match-lambda
490 ((version . location)
491 (string=? version highest)))
492 lst)))
493 (match locations
494 (((version . location) . rest)
495 (unless (null? rest)
496 (warning (G_ "ambiguous package specification `~a'~%") spec)
497 (warning (G_ "choosing ~a@~a from ~a~%")
498 name version
499 (location->string location)))
500 location)))))))
501
84189ebc
LC
502(define* (specification->package+output spec #:optional (output "out"))
503 "Return the package and output specified by SPEC, or #f and #f; SPEC may
504optionally contain a version number and an output name, as in these examples:
505
506 guile
1b846da8 507 guile@2.0.9
84189ebc 508 guile:debug
1b846da8 509 guile@2.0.9:debug
84189ebc
LC
510
511If SPEC does not specify a version number, return the preferred newest
512version; if SPEC does not specify an output, return OUTPUT."
84189ebc
LC
513 (let-values (((name version sub-drv)
514 (package-specification->name+version+output spec output)))
fad155d4
ML
515 (match (%find-package spec name version)
516 (#f
517 (values #f #f))
518 (package
519 (if (member sub-drv (package-outputs package))
520 (values package sub-drv)
69daee23 521 (leave (G_ "package `~a' lacks output `~a'~%")
fad155d4
ML
522 (package-full-name package)
523 sub-drv))))))
c08ea55e
LC
524
525(define (specifications->manifest specs)
526 "Given SPECS, a list of specifications such as \"emacs@25.2\" or
527\"guile:debug\", return a profile manifest."
528 ;; This procedure exists mostly so users of 'guix package -m' don't have to
529 ;; fiddle with multiple-value returns.
530 (packages->manifest
531 (map (compose list specification->package+output) specs)))