gnu: gajim: Update to 1.1.2.
[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.
8689901f
LC
158 (let* ((not-colon (char-set-complement (char-set #\:)))
159 (environment (string-tokenize (or (getenv "GUIX_PACKAGE_PATH") "")
fe634eaf
LC
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))
8689901f
LC
170
171 (make-parameter
fe634eaf
LC
172 (append environment
173 %default-package-module-path
174 channels))))
c107b541 175
ee06af5b
LC
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
0ea939fb
LC
186(define (fold-available-packages proc init)
187 "Fold PROC over the list of available packages. For each available package,
188PROC is called along these lines:
189
190 (PROC NAME VERSION RESULT
191 #:outputs OUTPUTS
192 #:location LOCATION
193 …)
194
195PROC can use #:allow-other-keys to ignore the bits it's not interested in.
196When a package cache is available, this procedure does not actually load any
197package 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
5c5ae46c
LC
230(define* (fold-packages proc init
231 #:optional
3c0128b0
LC
232 (modules (all-modules (%package-module-path)
233 #:warn
234 warn-about-load-error))
96dc8f35 235 #:key (select? (negate hidden-package?)))
5c5ae46c 236 "Call (PROC PACKAGE RESULT) for each available package defined in one of
96dc8f35
LC
237MODULES that matches SELECT?, using INIT as the initial value of RESULT. It
238is guaranteed to never traverse the same package twice."
cd903ef7 239 (fold-module-public-variables (lambda (object result)
96dc8f35 240 (if (and (package? object) (select? object))
cd903ef7
LC
241 (proc object result)
242 result))
243 init
5c5ae46c 244 modules))
ba326ce4 245
5fbdc9a5
LC
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
253package 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
9ffc1c00
LC
275 (let ((packages (delay
276 (fold-packages (lambda (p r)
277 (vhash-cons (package-name p) p r))
724311a2
LC
278 vlist-null)))
279 (version>? (lambda (p1 p2)
280 (version>? (package-version p1) (package-version p2)))))
9ffc1c00
LC
281 (lambda* (name #:optional version)
282 "Return the list of packages with the given NAME. If VERSION is not #f,
724311a2
LC
283then only return packages whose version is prefixed by VERSION, sorted in
284decreasing version order."
285 (let ((matching (sort (vhash-fold* cons '() name (force packages))
286 version>?)))
9ffc1c00
LC
287 (if version
288 (filter (lambda (package)
348987d3 289 (version-prefix? version (package-version package)))
9ffc1c00
LC
290 matching)
291 matching)))))
dc5669cd 292
5fbdc9a5
LC
293(define (cache-lookup cache name)
294 "Lookup package NAME in CACHE. Return a list sorted in increasing version
295order."
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,
304then only return packages whose version is prefixed by VERSION, sorted in
305decreasing 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
ee8099f5
LC
324(define* (find-package-locations name #:optional version)
325 "Return a list of version/location pairs corresponding to each package
326matching 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
3f26bfc1
LC
349(define (find-best-packages-by-name name version)
350 "If version is #f, return the list of packages named NAME with the highest
351version numbers; otherwise, return the list of packages named NAME and at
352VERSION."
353 (if version
354 (find-packages-by-name name version)
e2a903c8
LC
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))))))
7d193ec3 364
5fbdc9a5
LC
365(define (generate-package-cache directory)
366 "Generate under DIRECTORY a cache of all the available packages.
367
368The primary purpose of the cache is to speed up package lookup by name such
369that we don't have to traverse and load all the package modules, thereby also
370reducing the memory footprint."
371 (define cache-file
372 (string-append directory %package-cache-file))
373
374 (define (expand-cache module symbol variable result)
375 (match (false-if-exception (variable-ref variable))
376 ((? package? package)
377 (if (hidden-package? package)
378 result
379 (cons `#(,(package-name package)
380 ,(package-version package)
381 ,(module-name module)
382 ,symbol
383 ,(package-outputs package)
384 ,(->bool (member (%current-system)
385 (package-supported-systems package)))
386 ,(->bool (package-superseded package))
387 ,@(let ((loc (package-location package)))
388 (if loc
389 `(,(location-file loc)
390 ,(location-line loc)
391 ,(location-column loc))
392 '(#f #f #f))))
393 result)))
394 (_
395 result)))
396
397 (define exp
398 (fold-module-public-variables* expand-cache '()
399 (all-modules (%package-module-path)
400 #:warn
401 warn-about-load-error)))
402
403 (mkdir-p (dirname cache-file))
404 (call-with-output-file cache-file
405 (lambda (port)
406 ;; Store the cache as a '.go' file. This makes loading fast and reduces
407 ;; heap usage since some of the static data is directly mmapped.
408 (put-bytevector port
409 (compile `'(,@exp)
410 #:to 'bytecode
411 #:opts '(#:to-file? #t)))))
412 cache-file)
413
7d193ec3 414\f
4ea44419
AK
415(define %sigint-prompt
416 ;; The prompt to jump to upon SIGINT.
417 (make-prompt-tag "interruptible"))
418
419(define (call-with-sigint-handler thunk handler)
420 "Call THUNK and return its value. Upon SIGINT, call HANDLER with the signal
421number in the context of the continuation of the call to this function, and
422return its return value."
423 (call-with-prompt %sigint-prompt
424 (lambda ()
425 (sigaction SIGINT
426 (lambda (signum)
427 (sigaction SIGINT SIG_DFL)
428 (abort-to-prompt %sigint-prompt signum)))
429 (dynamic-wind
430 (const #t)
431 thunk
432 (cut sigaction SIGINT SIG_DFL)))
433 (lambda (k signum)
434 (handler signum))))
435
fad155d4
ML
436\f
437;;;
438;;; Package specification.
439;;;
440
e30c2be1 441(define* (%find-package spec name version)
fad155d4
ML
442 (match (find-best-packages-by-name name version)
443 ((pkg . pkg*)
444 (unless (null? pkg*)
69daee23
LC
445 (warning (G_ "ambiguous package specification `~a'~%") spec)
446 (warning (G_ "choosing ~a@~a from ~a~%")
d75e8f36 447 (package-name pkg) (package-version pkg)
fad155d4 448 (location->string (package-location pkg))))
01afdab8
LC
449 (match (package-superseded pkg)
450 ((? package? new)
69daee23 451 (info (G_ "package '~a' has been superseded by '~a'~%")
01afdab8
LC
452 (package-name pkg) (package-name new))
453 new)
454 (#f
455 pkg)))
e465d9e1 456 (x
fad155d4 457 (if version
69daee23
LC
458 (leave (G_ "~A: package not found for version ~a~%") name version)
459 (leave (G_ "~A: unknown package~%") name)))))
fad155d4 460
5e3b388b
CR
461(define (specification->package spec)
462 "Return a package matching SPEC. SPEC may be a package name, or a package
1b846da8 463name followed by an at-sign and a version number. If the version number is not
5e3b388b 464present, return the preferred newest version."
fad155d4
ML
465 (let-values (((name version) (package-name->name+version spec)))
466 (%find-package spec name version)))
84189ebc 467
ee8099f5
LC
468(define (specification->location spec)
469 "Return the location of the highest-numbered package matching SPEC, a
470specification such as \"guile@2\" or \"emacs\"."
471 (let-values (((name version) (package-name->name+version spec)))
472 (match (find-package-locations name version)
473 (()
474 (if version
475 (leave (G_ "~A: package not found for version ~a~%") name version)
476 (leave (G_ "~A: unknown package~%") name)))
477 (lst
478 (let* ((highest (match lst (((version . _) _ ...) version)))
479 (locations (take-while (match-lambda
480 ((version . location)
481 (string=? version highest)))
482 lst)))
483 (match locations
484 (((version . location) . rest)
485 (unless (null? rest)
486 (warning (G_ "ambiguous package specification `~a'~%") spec)
487 (warning (G_ "choosing ~a@~a from ~a~%")
488 name version
489 (location->string location)))
490 location)))))))
491
84189ebc
LC
492(define* (specification->package+output spec #:optional (output "out"))
493 "Return the package and output specified by SPEC, or #f and #f; SPEC may
494optionally contain a version number and an output name, as in these examples:
495
496 guile
1b846da8 497 guile@2.0.9
84189ebc 498 guile:debug
1b846da8 499 guile@2.0.9:debug
84189ebc
LC
500
501If SPEC does not specify a version number, return the preferred newest
502version; if SPEC does not specify an output, return OUTPUT."
84189ebc
LC
503 (let-values (((name version sub-drv)
504 (package-specification->name+version+output spec output)))
fad155d4
ML
505 (match (%find-package spec name version)
506 (#f
507 (values #f #f))
508 (package
509 (if (member sub-drv (package-outputs package))
510 (values package sub-drv)
69daee23 511 (leave (G_ "package `~a' lacks output `~a'~%")
fad155d4
ML
512 (package-full-name package)
513 sub-drv))))))
c08ea55e
LC
514
515(define (specifications->manifest specs)
516 "Given SPECS, a list of specifications such as \"emacs@25.2\" or
517\"guile:debug\", return a profile manifest."
518 ;; This procedure exists mostly so users of 'guix package -m' don't have to
519 ;; fiddle with multiple-value returns.
520 (packages->manifest
521 (map (compose list specification->package+output) specs)))