projects/guix·blob javascript-team · refs

629 lines · 25.0 KiB

1;;; GNU Guix --- Functional package management for GNU
2;;; Copyright © 2012-2020, 2022-2024 Ludovic Courtès <[email protected]>
3;;; Copyright © 2013 Mark H Weaver <[email protected]>
4;;; Copyright © 2014 Eric Bavier <[email protected]>
5;;; Copyright © 2016, 2017 Alex Kost <[email protected]>
6;;; Copyright © 2016 Mathieu Lirzin <[email protected]>
7;;; Copyright © 2022 Antero Mejr <[email protected]>
8;;;
9;;; This file is part of GNU Guix.
10;;;
11;;; GNU Guix is free software; you can redistribute it and/or modify it
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;;;
16;;; GNU Guix is distributed in the hope that it will be useful, but
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
22;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
23
24;;; Commentary:
25;;;
26;;; General utilities for the software distribution---i.e., the modules under
27;;; (gnu packages ...).
28;;;
29;;; Code:
30
31(define-module (gnu packages)
32 #:use-module (guix packages)
33 #:use-module (guix ui)
34 #:use-module (guix utils)
35 #:use-module (guix diagnostics)
36 #:use-module (guix discovery)
37 #:use-module (guix memoization)
38 #:use-module ((guix build utils)
39 #:select ((package-name->name+version
40 . hyphen-separated-name->name+version)
41 mkdir-p))
42 #:use-module (guix profiles)
43 #:use-module (guix describe)
44 #:use-module (guix deprecation)
45 #:use-module (ice-9 vlist)
46 #:use-module (ice-9 match)
47 #:use-module (ice-9 binary-ports)
48 #:autoload (rnrs bytevectors) (bytevector?)
49 #:autoload (system base compile) (compile)
50 #:use-module (srfi srfi-1)
51 #:use-module (srfi srfi-26)
52 #:use-module (srfi srfi-34)
53 #:use-module (srfi srfi-35)
54 #:use-module (srfi srfi-39)
55 #:use-module (srfi srfi-71)
56 #:export (search-patch
57 search-patches
58 search-auxiliary-file
59 %patch-path
60 %auxiliary-files-path
61 %package-module-path
62 %default-package-module-path
63 cache-is-authoritative?
64
65 fold-packages
66 all-packages
67 fold-available-packages
68
69 find-newest-available-packages
70 find-packages-by-name
71 find-package-locations
72 find-best-packages-by-name
73
74 specification->package
75 specification->package+output
76 specification->location
77 specifications->manifest
78 specifications->packages
79
80 package-unique-version-prefix
81
82 generate-package-cache))
83
84;; By default, we store patches and auxiliary files
85;; alongside Guile modules. This is so that these extra files can be
86;; found without requiring a special setup, such as a specific
87;; installation directory and an extra environment variable. One
88;; advantage of this setup is that everything just works in an
89;; auto-compilation setting.
90
91(define %auxiliary-files-path
92 (make-parameter
93 (map (cut string-append <> "/gnu/packages/aux-files")
94 %load-path)))
95
96(define (search-auxiliary-file file-name)
97 "Search the auxiliary FILE-NAME. Return #f if not found."
98 (search-path (%auxiliary-files-path) file-name))
99
100(define (search-patch file-name)
101 "Search the patch FILE-NAME. Raise an error if not found."
102 (or (search-path (%patch-path) file-name)
103 (raise (formatted-message (G_ "~a: patch not found")
104 file-name))))
105
106(define-syntax-rule (search-patches file-name ...)
107 "Return the list of absolute file names corresponding to each
108FILE-NAME found in %PATCH-PATH."
109 (list (search-patch file-name) ...))
110
111(define %distro-root-directory
112 ;; Absolute file name of the module hierarchy. Since (gnu packages …) might
113 ;; live in a directory different from (guix), try to get the best match.
114 (letrec-syntax ((dirname* (syntax-rules ()
115 ((_ file)
116 (dirname file))
117 ((_ file head tail ...)
118 (dirname (dirname* file tail ...)))))
119 (try (syntax-rules ()
120 ((_ (file things ...) rest ...)
121 (match (search-path %load-path file)
122 (#f
123 (try rest ...))
124 (absolute
125 (dirname* absolute things ...))))
126 ((_)
127 #f))))
128 (try ("gnu/packages/base.scm" gnu/ packages/)
129 ("gnu/packages.scm" gnu/)
130 ("guix.scm"))))
131
132(define %default-package-module-path
133 ;; Default search path for package modules.
134 `((,%distro-root-directory . "gnu/packages")))
135
136(define (cache-is-authoritative?)
137 "Return true if the pre-computed package cache is authoritative. It is not
138authoritative when entries have been added via GUIX_PACKAGE_PATH or '-L'
139flags."
140 (equal? (%package-module-path)
141 (append %default-package-module-path
142 (package-path-entries))))
143
144(define %package-module-path
145 ;; Search path for package modules. Each item must be either a directory
146 ;; name or a pair whose car is a directory and whose cdr is a sub-directory
147 ;; to narrow the search.
148 (let* ((not-colon (char-set-complement (char-set #\:)))
149 (environment (string-tokenize (or (getenv "GUIX_PACKAGE_PATH") "")
150 not-colon))
151 (channels-scm (package-path-entries)))
152 ;; Automatically add channels and items from $GUIX_PACKAGE_PATH to Guile's
153 ;; search path. For historical reasons, $GUIX_PACKAGE_PATH goes to the
154 ;; front; channels go to the back so that they don't override Guix' own
155 ;; modules.
156 (append-channels-to-load-path!)
157 (set! %load-path
158 (append environment %load-path))
159 (set! %load-compiled-path
160 (append environment %load-compiled-path))
161
162 (make-parameter
163 (append environment
164 %default-package-module-path
165 channels-scm))))
166
167(define (strip-trailing-slash s)
168 ;; Strip the trailing slash of a string, if present.
169 (if (string-suffix? "/" s)
170 (string-drop-right s 1)
171 s))
172
173(define %patch-path
174 ;; Define it after '%package-module-path' so that '%load-path' contains user
175 ;; directories, allowing patches in $GUIX_PACKAGE_PATH to be found.
176 (make-parameter
177 (map (lambda (directory)
178 (if (string=? directory %distro-root-directory)
179 (string-append directory "/gnu/packages/patches")
180 (or (false-if-exception (canonicalize-path directory))
181 directory)))
182 (map strip-trailing-slash %load-path))))
183
184;; This procedure is used by Emacs-Guix up to 0.5.1.1, so keep it for now.
185;; See <https://github.com/alezost/guix.el/issues/30>.
186(define-deprecated find-newest-available-packages
187 find-packages-by-name
188 (mlambda ()
189 "Return a vhash keyed by package names, and with
190associated values of the form
191
192 (newest-version newest-package ...)
193
194where the preferred package is listed first."
195 (fold-packages (lambda (p r)
196 (let ((name (package-name p))
197 (version (package-version p)))
198 (match (vhash-assoc name r)
199 ((_ newest-so-far . pkgs)
200 (case (version-compare version newest-so-far)
201 ((>) (vhash-cons name `(,version ,p) r))
202 ((=) (vhash-cons name `(,version ,p ,@pkgs) r))
203 ((<) r)))
204 (#f (vhash-cons name `(,version ,p) r)))))
205 vlist-null)))
206
207(define (fold-available-packages proc init)
208 "Fold PROC over the list of available packages. For each available package,
209PROC is called along these lines:
210
211 (PROC NAME VERSION RESULT
212 #:outputs OUTPUTS
213 #:location LOCATION
214 …)
215
216PROC can use #:allow-other-keys to ignore the bits it's not interested in.
217When a package cache is available, this procedure does not actually load any
218package module."
219 (define cache
220 (load-package-cache (current-profile)))
221
222 (if (and cache (cache-is-authoritative?))
223 (vhash-fold (lambda (name vector result)
224 (match vector
225 (#(name version module symbol outputs
226 supported? deprecated?
227 file line column)
228 (proc name version result
229 #:outputs outputs
230 #:location (and file
231 (location file line column))
232 #:supported? supported?
233 #:deprecated? deprecated?))))
234 init
235 cache)
236 (fold-packages (lambda (package result)
237 (proc (package-name package)
238 (package-version package)
239 result
240 #:outputs (package-outputs package)
241 #:location (package-location package)
242 #:supported?
243 (->bool (supported-package? package))
244 #:deprecated?
245 (->bool
246 (package-superseded package))))
247 init)))
248
249(define* (fold-packages proc init
250 #:optional
251 (modules (all-modules (%package-module-path)
252 #:warn
253 warn-about-load-error))
254 #:key (select? (negate hidden-package?)))
255 "Call (PROC PACKAGE RESULT) for each available package defined in one of
256MODULES that matches SELECT?, using INIT as the initial value of RESULT. It
257is guaranteed to never traverse the same package twice."
258 (fold-module-public-variables (lambda (object result)
259 (if (and (package? object) (select? object))
260 (proc object result)
261 result))
262 init
263 modules))
264
265(define all-packages
266 (mlambda ()
267 "Return the list of all public packages, including replacements and hidden
268packages, excluding superseded packages."
269 ;; Note: 'fold-packages' never traverses the same package twice but
270 ;; replacements break that (they may or may not be visible to
271 ;; 'fold-packages'), hence this hash table to track visited packages.
272 (define visited (make-hash-table))
273
274 (fold-packages (lambda (package result)
275 (if (hashq-ref visited package)
276 result
277 (begin
278 (hashq-set! visited package #t)
279 (match (package-replacement package)
280 ((? package? replacement)
281 (hashq-set! visited replacement #t)
282 (cons* replacement package result))
283 (#f
284 (cons package result))))))
285 '()
286
287 ;; Dismiss deprecated packages but keep hidden packages.
288 #:select? (negate package-superseded))))
289
290(define %package-cache-file
291 ;; Location of the package cache.
292 "/lib/guix/package.cache")
293
294(define load-package-cache
295 (mlambda (profile)
296 "Attempt to load the package cache. On success return a vhash keyed by
297package names. Return #f on failure."
298 (match profile
299 (#f #f)
300 (profile
301 (catch 'system-error
302 (lambda ()
303 (define lst
304 (load-compiled (string-append profile %package-cache-file)))
305 (fold (lambda (item vhash)
306 (match item
307 (#(name version module symbol outputs
308 supported? deprecated?
309 file line column)
310 (vhash-cons name item vhash))))
311 vlist-null
312 lst))
313 (lambda args
314 (if (= ENOENT (system-error-errno args))
315 #f
316 (apply throw args))))))))
317
318(define find-packages-by-name/direct ;bypass the cache
319 (let ((packages (delay
320 (fold-packages (lambda (p r)
321 (vhash-cons (package-name p) p r))
322 vlist-null)))
323 (version>? (lambda (p1 p2)
324 (version>? (package-version p1) (package-version p2)))))
325 (lambda* (name #:optional version)
326 "Return the list of packages with the given NAME. If VERSION is not #f,
327then only return packages whose version is prefixed by VERSION, sorted in
328decreasing version order."
329 (let ((matching (sort (vhash-fold* cons '() name (force packages))
330 version>?)))
331 (if version
332 (filter (lambda (package)
333 (version-prefix? version (package-version package)))
334 matching)
335 matching)))))
336
337(define (cache-lookup cache name)
338 "Lookup package NAME in CACHE. Return a list sorted in increasing version
339order."
340 (define (package-version<? v1 v2)
341 (version>? (vector-ref v2 1) (vector-ref v1 1)))
342
343 (sort (vhash-fold* cons '() name cache)
344 package-version<?))
345
346(define* (find-packages-by-name name #:optional version)
347 "Return the list of packages with the given NAME. If VERSION is not #f,
348then only return packages whose version is prefixed by VERSION, sorted in
349decreasing version order."
350 (define cache
351 (load-package-cache (current-profile)))
352
353 (if (and (cache-is-authoritative?) cache)
354 (match (cache-lookup cache name)
355 (#f #f)
356 ((#(_ versions modules symbols _ _ _ _ _ _) ...)
357 (fold (lambda (version* module symbol result)
358 (if (or (not version)
359 (version-prefix? version version*))
360 (cons (module-ref (resolve-interface module)
361 symbol)
362 result)
363 result))
364 '()
365 versions modules symbols)))
366 (find-packages-by-name/direct name version)))
367
368(define* (find-package-locations name #:optional version)
369 "Return a list of version/location pairs corresponding to each package
370matching NAME and VERSION."
371 (define cache
372 (load-package-cache (current-profile)))
373
374 (if (and cache (cache-is-authoritative?))
375 (match (cache-lookup cache name)
376 (#f '())
377 ((#(name versions modules symbols outputs
378 supported? deprecated?
379 files lines columns) ...)
380 (fold (lambda (version* file line column result)
381 (if (and file
382 (or (not version)
383 (version-prefix? version version*)))
384 (alist-cons version* (location file line column)
385 result)
386 result))
387 '()
388 versions files lines columns)))
389 (map (lambda (package)
390 (cons (package-version package) (package-location package)))
391 (find-packages-by-name/direct name version))))
392
393(define (find-best-packages-by-name name version)
394 "If version is #f, return the list of packages named NAME with the highest
395version numbers; otherwise, return the list of packages named NAME and at
396VERSION."
397 (if version
398 (find-packages-by-name name version)
399 (match (find-packages-by-name name)
400 (()
401 '())
402 ((matches ...)
403 ;; Return the subset of MATCHES with the higher version number.
404 (let ((highest (package-version (first matches))))
405 (take-while (lambda (p)
406 (string=? (package-version p) highest))
407 matches))))))
408
409;; Prevent Guile 3 from inlining this procedure so we can mock it in tests.
410(set! find-best-packages-by-name find-best-packages-by-name)
411
412(define (generate-package-cache directory)
413 "Generate under DIRECTORY a cache of all the available packages.
414
415The primary purpose of the cache is to speed up package lookup by name such
416that we don't have to traverse and load all the package modules, thereby also
417reducing the memory footprint."
418 (define cache-file
419 (string-append directory %package-cache-file))
420
421 (define expand-cache
422 (match-lambda*
423 (((module symbol variable) (result . seen))
424 (let ((package (variable-ref variable)))
425 (if (or (vhash-assq package seen)
426 (hidden-package? package))
427 (cons result seen)
428 (cons (cons `#(,(package-name package)
429 ,(package-version package)
430 ,(module-name module)
431 ,symbol
432 ,(package-outputs package)
433 ,(->bool (supported-package? package))
434 ,(->bool (package-superseded package))
435 ,@(let ((loc (package-location package)))
436 (if loc
437 `(,(location-file loc)
438 ,(location-line loc)
439 ,(location-column loc))
440 '(#f #f #f))))
441 result)
442 (vhash-consq package #t seen)))))))
443
444 (define entry-key
445 (match-lambda
446 ((module symbol variable)
447 (let ((value (variable-ref variable)))
448 (string-append (package-name value) (package-version value)
449 (object->string module)
450 (symbol->string symbol))))))
451
452 (define (entry<? a b)
453 (string<? (entry-key a) (entry-key b)))
454
455 (define variables
456 ;; First sort variables so that 'expand-cache' later dismisses
457 ;; already-seen package objects in a deterministic fashion.
458 (sort
459 (fold-module-public-variables* (lambda (module symbol variable lst)
460 (let ((value (false-if-exception
461 (variable-ref variable))))
462 (if (package? value)
463 (cons (list module symbol variable)
464 lst)
465 lst)))
466 '()
467 (all-modules (%package-module-path)
468 #:warn
469 warn-about-load-error))
470 entry<?))
471
472 (define exp
473 (first (fold expand-cache (cons '() vlist-null) variables)))
474
475 (mkdir-p (dirname cache-file))
476 (call-with-output-file cache-file
477 (lambda (port)
478 ;; Store the cache as a '.go' file. This makes loading fast and reduces
479 ;; heap usage since some of the static data is directly mmapped.
480 (match (compile `'(,@exp)
481 #:to 'bytecode
482 #:opts '(#:to-file? #t))
483 ((? bytevector? bv)
484 (put-bytevector port bv))
485 (proc
486 ;; In Guile 3.0.9, the linker can return a procedure instead of a
487 ;; bytevector. Adjust to that.
488 (proc port)))))
489 cache-file)
490
491
492(define %sigint-prompt
493 ;; The prompt to jump to upon SIGINT.
494 (make-prompt-tag "interruptible"))
495
496(define (call-with-sigint-handler thunk handler)
497 "Call THUNK and return its value. Upon SIGINT, call HANDLER with the signal
498number in the context of the continuation of the call to this function, and
499return its return value."
500 (call-with-prompt %sigint-prompt
501 (lambda ()
502 (sigaction SIGINT
503 (lambda (signum)
504 (sigaction SIGINT SIG_DFL)
505 (abort-to-prompt %sigint-prompt signum)))
506 (dynamic-wind
507 (const #t)
508 thunk
509 (cut sigaction SIGINT SIG_DFL)))
510 (lambda (k signum)
511 (handler signum))))
512
513
514;;;
515;;; Package specification.
516;;;
517
518(define* (%find-package spec name version)
519 (match (find-best-packages-by-name name version)
520 ((pkg . pkg*)
521 (unless (null? pkg*)
522 (warning (G_ "ambiguous package specification `~a'~%") spec)
523 (warning (G_ "choosing ~a@~a from ~a~%")
524 (package-name pkg) (package-version pkg)
525 (location->string (package-location pkg))))
526 (match (package-superseded pkg)
527 ((? package? new)
528 (unless (assoc-ref (package-properties pkg) 'supress-supersession-info?)
529 (info (G_ "package '~a' has been superseded by '~a'~%")
530 (package-name pkg) (package-name new)))
531 new)
532 (#f
533 pkg)))
534 (x
535 (if version
536 (leave (G_ "~A: package not found for version ~a~%") name version)
537 (leave (G_ "~A: unknown package~%") name)))))
538
539(define (specification->package spec)
540 "Return a package matching SPEC. SPEC may be a package name, or a package
541name followed by an at-sign and a version number. If the version number is not
542present, return the preferred newest version."
543 (let ((name version (package-name->name+version spec)))
544 (%find-package spec name version)))
545
546(define (specification->location spec)
547 "Return the location of the highest-numbered package matching SPEC, a
548specification such as \"guile@2\" or \"emacs\"."
549 (let ((name version (package-name->name+version spec)))
550 (match (find-package-locations name version)
551 (()
552 (if version
553 (leave (G_ "~A: package not found for version ~a~%") name version)
554 (leave (G_ "~A: unknown package~%") name)))
555 (lst
556 (let* ((highest (match lst (((version . _) _ ...) version)))
557 (locations (take-while (match-lambda
558 ((version . location)
559 (string=? version highest)))
560 lst)))
561 (match locations
562 (((version . location) . rest)
563 (unless (null? rest)
564 (warning (G_ "ambiguous package specification `~a'~%") spec)
565 (warning (G_ "choosing ~a@~a from ~a~%")
566 name version
567 (location->string location)))
568 location)))))))
569
570(define* (specification->package+output spec #:optional (output "out"))
571 "Return the package and output specified by SPEC, or #f and #f; SPEC may
572optionally contain a version number and an output name, as in these examples:
573
574 guile
575 [email protected]
576 guile:debug
577 [email protected]:debug
578
579If SPEC does not specify a version number, return the preferred newest
580version; if SPEC does not specify an output, return OUTPUT.
581
582When OUTPUT is false and SPEC does not specify any output, return #f as the
583output."
584 (let ((name version sub-drv
585 (package-specification->name+version+output spec output)))
586 (match (%find-package spec name version)
587 (#f
588 (values #f #f))
589 (package
590 (if (or (and (not output) (not sub-drv))
591 (member sub-drv (package-outputs package)))
592 (values package sub-drv)
593 (leave (G_ "package `~a' lacks output `~a'~%")
594 (package-full-name package)
595 sub-drv))))))
596
597(define (specifications->packages specs)
598 "Given SPECS, a list of specifications such as \"[email protected]\" or
599\"guile:debug\", return a list of package/output tuples."
600 ;; This procedure exists so users of 'guix home' don't have to write out the
601 ;; (map (compose list specification->package+output)... boilerplate.
602 (map (compose list specification->package+output) specs))
603
604(define (specifications->manifest specs)
605 "Given SPECS, a list of specifications such as \"[email protected]\" or
606\"guile:debug\", return a profile manifest."
607 ;; This procedure exists mostly so users of 'guix package -m' don't have to
608 ;; fiddle with multiple-value returns.
609 (packages->manifest
610 (specifications->packages specs)))
611
612(define (package-unique-version-prefix name version)
613 "Search among all the versions of package NAME that are available, and
614return the shortest unambiguous version prefix to designate VERSION. If only
615one version of the package is available, return the empty string."
616 (match (map package-version (find-packages-by-name name))
617 ((_)
618 ;; A single version of NAME is available, so do not specify the version
619 ;; number, even if the available version doesn't match VERSION.
620 "")
621 (versions
622 ;; If VERSION is the latest version, don't specify any version.
623 ;; Otherwise return the shortest unique version prefix. Note that this
624 ;; is based on the currently available packages so the result may vary
625 ;; over time.
626 (if (every (cut version>? version <>)
627 (delete version versions))
628 ""
629 (version-unique-prefix version versions)))))