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