packages: Add 'package-superseded' and associated support.
[jackhill/guix/guix.git] / gnu / packages.scm
CommitLineData
233e7676 1;;; GNU Guix --- Functional package management for GNU
7befee30 2;;; Copyright © 2012, 2013, 2014, 2015, 2016 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>
6caa4dfa 5;;; Copyright © 2016 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)
958dd3ce 27 #:use-module (guix combinators)
95cd4971
LC
28 #:use-module ((guix build utils)
29 #:select ((package-name->name+version
30 . hyphen-separated-name->name+version)))
6b1891b0 31 #:use-module (ice-9 ftw)
c2868b1e 32 #:use-module (ice-9 vlist)
dc5669cd 33 #:use-module (ice-9 match)
6b1891b0 34 #:use-module (srfi srfi-1)
5e3b388b 35 #:use-module (srfi srfi-11)
6b1891b0 36 #:use-module (srfi srfi-26)
dbab5150
LC
37 #:use-module (srfi srfi-34)
38 #:use-module (srfi srfi-35)
800cdeef
LC
39 #:use-module (srfi srfi-39)
40 #:export (search-patch
25897079 41 search-patches
ac5aa288 42 search-bootstrap-binary
0492f4a2 43 %patch-path
0b3651bc 44 %bootstrap-binaries-path
c107b541 45 %package-module-path
7d193ec3 46
ba326ce4 47 fold-packages
2a6ba870 48 scheme-modules ;XXX: for lack of a better place
7d193ec3 49
dc5669cd 50 find-packages-by-name
3f26bfc1 51 find-best-packages-by-name
7d193ec3
EB
52 find-newest-available-packages
53
84189ebc
LC
54 specification->package
55 specification->package+output))
6b1891b0
LC
56
57;;; Commentary:
58;;;
59;;; General utilities for the software distribution---i.e., the modules under
59a43334 60;;; (gnu packages ...).
6b1891b0
LC
61;;;
62;;; Code:
63
0b3651bc
LC
64;; By default, we store patches and bootstrap binaries alongside Guile
65;; modules. This is so that these extra files can be found without
66;; requiring a special setup, such as a specific installation directory
67;; and an extra environment variable. One advantage of this setup is
68;; that everything just works in an auto-compilation setting.
a9f60c42 69
a9f60c42 70(define %bootstrap-binaries-path
ac5aa288 71 (make-parameter
1ffa7090 72 (map (cut string-append <> "/gnu/packages/bootstrap")
0b3651bc 73 %load-path)))
ac5aa288 74
800cdeef 75(define (search-patch file-name)
dbab5150
LC
76 "Search the patch FILE-NAME. Raise an error if not found."
77 (or (search-path (%patch-path) file-name)
78 (raise (condition
79 (&message (message (format #f (_ "~a: patch not found")
80 file-name)))))))
800cdeef 81
25897079
AK
82(define-syntax-rule (search-patches file-name ...)
83 "Return the list of absolute file names corresponding to each
84FILE-NAME found in %PATCH-PATH."
85 (list (search-patch file-name) ...))
86
ac5aa288 87(define (search-bootstrap-binary file-name system)
dfba5489
LC
88 "Search the bootstrap binary FILE-NAME for SYSTEM. Raise an error if not
89found."
90 (or (search-path (%bootstrap-binaries-path)
91 (string-append system "/" file-name))
92 (raise (condition
93 (&message
94 (message
95 (format #f (_ "could not find bootstrap binary '~a' \
96for system '~a'")
97 file-name system)))))))
ac5aa288 98
84836a57
LC
99(define %distro-root-directory
100 ;; Absolute file name of the module hierarchy.
101 (dirname (search-path %load-path "guix.scm")))
6b1891b0 102
c107b541
LC
103(define %package-module-path
104 ;; Search path for package modules. Each item must be either a directory
105 ;; name or a pair whose car is a directory and whose cdr is a sub-directory
106 ;; to narrow the search.
8689901f
LC
107 (let* ((not-colon (char-set-complement (char-set #\:)))
108 (environment (string-tokenize (or (getenv "GUIX_PACKAGE_PATH") "")
109 not-colon)))
110 ;; Automatically add items from $GUIX_PACKAGE_PATH to Guile's search path.
111 (for-each (lambda (directory)
112 (set! %load-path (cons directory %load-path))
113 (set! %load-compiled-path
114 (cons directory %load-compiled-path)))
115 environment)
116
117 (make-parameter
118 (append environment `((,%distro-root-directory . "gnu/packages"))))))
c107b541 119
ee06af5b
LC
120(define %patch-path
121 ;; Define it after '%package-module-path' so that '%load-path' contains user
122 ;; directories, allowing patches in $GUIX_PACKAGE_PATH to be found.
123 (make-parameter
124 (map (lambda (directory)
125 (if (string=? directory %distro-root-directory)
126 (string-append directory "/gnu/packages/patches")
127 directory))
128 %load-path)))
129
84836a57 130(define* (scheme-files directory)
d95523fb
LC
131 "Return the list of Scheme files found under DIRECTORY, recursively. The
132returned list is sorted in alphabetical order."
133
134 ;; Sort entries so that 'fold-packages' works in a deterministic fashion
135 ;; regardless of details of the underlying file system.
136 (sort (file-system-fold (const #t) ; enter?
137 (lambda (path stat result) ; leaf
138 (if (string-suffix? ".scm" path)
139 (cons path result)
140 result))
141 (lambda (path stat result) ; down
142 result)
143 (lambda (path stat result) ; up
144 result)
145 (const #f) ; skip
146 (lambda (path stat errno result)
147 (warning (_ "cannot access `~a': ~a~%")
148 path (strerror errno))
149 result)
150 '()
151 directory
152 stat)
153 string<?))
6b1891b0 154
c107b541
LC
155(define file-name->module-name
156 (let ((not-slash (char-set-complement (char-set #\/))))
157 (lambda (file)
158 "Return the module name (a list of symbols) corresponding to FILE."
159 (map string->symbol
160 (string-tokenize (string-drop-right file 4) not-slash)))))
84836a57 161
2a6ba870
LC
162(define* (scheme-modules directory #:optional sub-directory)
163 "Return the list of Scheme modules available under DIRECTORY.
84836a57
LC
164Optionally, narrow the search to SUB-DIRECTORY."
165 (define prefix-len
166 (string-length directory))
167
168 (filter-map (lambda (file)
4ae7559f
LC
169 (let* ((file (substring file prefix-len))
170 (module (file-name->module-name file)))
171 (catch #t
172 (lambda ()
173 (resolve-interface module))
174 (lambda args
175 ;; Report the error, but keep going.
176 (warn-about-load-error module args)
177 #f))))
84836a57
LC
178 (scheme-files (if sub-directory
179 (string-append directory "/" sub-directory)
180 directory))))
6b1891b0 181
c107b541
LC
182(define* (all-package-modules #:optional (path (%package-module-path)))
183 "Return the list of package modules found in PATH, a list of directories to
184search."
185 (fold-right (lambda (spec result)
186 (match spec
187 ((? string? directory)
2a6ba870 188 (append (scheme-modules directory) result))
c107b541 189 ((directory . sub-directory)
2a6ba870 190 (append (scheme-modules directory sub-directory)
c107b541
LC
191 result))))
192 '()
193 path))
194
ba326ce4
LC
195(define (fold-packages proc init)
196 "Call (PROC PACKAGE RESULT) for each available package, using INIT as
c2868b1e
MW
197the initial value of RESULT. It is guaranteed to never traverse the
198same package twice."
199 (identity ; discard second return value
200 (fold2 (lambda (module result seen)
201 (fold2 (lambda (var result seen)
202 (if (and (package? var)
6980511b
LC
203 (not (vhash-assq var seen))
204 (not (hidden-package? var)))
c2868b1e
MW
205 (values (proc var result)
206 (vhash-consq var #t seen))
207 (values result seen)))
208 result
209 seen
210 (module-map (lambda (sym var)
211 (false-if-exception (variable-ref var)))
212 module)))
213 init
214 vlist-null
c107b541 215 (all-package-modules))))
ba326ce4 216
9ffc1c00
LC
217(define find-packages-by-name
218 (let ((packages (delay
219 (fold-packages (lambda (p r)
220 (vhash-cons (package-name p) p r))
724311a2
LC
221 vlist-null)))
222 (version>? (lambda (p1 p2)
223 (version>? (package-version p1) (package-version p2)))))
9ffc1c00
LC
224 (lambda* (name #:optional version)
225 "Return the list of packages with the given NAME. If VERSION is not #f,
724311a2
LC
226then only return packages whose version is prefixed by VERSION, sorted in
227decreasing version order."
228 (let ((matching (sort (vhash-fold* cons '() name (force packages))
229 version>?)))
9ffc1c00
LC
230 (if version
231 (filter (lambda (package)
724311a2 232 (string-prefix? version (package-version package)))
9ffc1c00
LC
233 matching)
234 matching)))))
dc5669cd 235
3f26bfc1
LC
236(define find-newest-available-packages
237 (memoize
238 (lambda ()
239 "Return a vhash keyed by package names, and with
dc5669cd
MW
240associated values of the form
241
242 (newest-version newest-package ...)
243
244where the preferred package is listed first."
245
3f26bfc1
LC
246 ;; FIXME: Currently, the preferred package is whichever one
247 ;; was found last by 'fold-packages'. Find a better solution.
248 (fold-packages (lambda (p r)
249 (let ((name (package-name p))
250 (version (package-version p)))
251 (match (vhash-assoc name r)
252 ((_ newest-so-far . pkgs)
253 (case (version-compare version newest-so-far)
254 ((>) (vhash-cons name `(,version ,p) r))
255 ((=) (vhash-cons name `(,version ,p ,@pkgs) r))
256 ((<) r)))
257 (#f (vhash-cons name `(,version ,p) r)))))
258 vlist-null))))
259
260(define (find-best-packages-by-name name version)
261 "If version is #f, return the list of packages named NAME with the highest
262version numbers; otherwise, return the list of packages named NAME and at
263VERSION."
264 (if version
265 (find-packages-by-name name version)
266 (match (vhash-assoc name (find-newest-available-packages))
267 ((_ version pkgs ...) pkgs)
268 (#f '()))))
7d193ec3
EB
269
270\f
4ea44419
AK
271(define %sigint-prompt
272 ;; The prompt to jump to upon SIGINT.
273 (make-prompt-tag "interruptible"))
274
275(define (call-with-sigint-handler thunk handler)
276 "Call THUNK and return its value. Upon SIGINT, call HANDLER with the signal
277number in the context of the continuation of the call to this function, and
278return its return value."
279 (call-with-prompt %sigint-prompt
280 (lambda ()
281 (sigaction SIGINT
282 (lambda (signum)
283 (sigaction SIGINT SIG_DFL)
284 (abort-to-prompt %sigint-prompt signum)))
285 (dynamic-wind
286 (const #t)
287 thunk
288 (cut sigaction SIGINT SIG_DFL)))
289 (lambda (k signum)
290 (handler signum))))
291
fad155d4
ML
292\f
293;;;
294;;; Package specification.
295;;;
296
1b846da8 297(define* (%find-package spec name version #:key fallback?)
fad155d4
ML
298 (match (find-best-packages-by-name name version)
299 ((pkg . pkg*)
300 (unless (null? pkg*)
301 (warning (_ "ambiguous package specification `~a'~%") spec)
302 (warning (_ "choosing ~a from ~a~%")
303 (package-full-name pkg)
304 (location->string (package-location pkg))))
1b846da8 305 (when fallback?
7befee30
LC
306 (warning (_ "deprecated NAME-VERSION syntax; \
307use NAME@VERSION instead~%")))
01afdab8
LC
308
309 (match (package-superseded pkg)
310 ((? package? new)
311 (info (_ "package '~a' has been superseded by '~a'~%")
312 (package-name pkg) (package-name new))
313 new)
314 (#f
315 pkg)))
fad155d4
ML
316 (_
317 (if version
318 (leave (_ "~A: package not found for version ~a~%") name version)
efb107e0 319 (if (not fallback?)
1b846da8
ML
320 ;; XXX: Fallback to the older specification style with an hyphen
321 ;; between NAME and VERSION, for backward compatibility.
95cd4971
LC
322 (call-with-values
323 (lambda ()
324 (hyphen-separated-name->name+version name))
325 (cut %find-package spec <> <> #:fallback? #t))
efb107e0
LC
326
327 ;; The fallback case didn't find anything either, so bail out.
1b846da8 328 (leave (_ "~A: unknown package~%") name))))))
fad155d4 329
5e3b388b
CR
330(define (specification->package spec)
331 "Return a package matching SPEC. SPEC may be a package name, or a package
1b846da8 332name followed by an at-sign and a version number. If the version number is not
5e3b388b 333present, return the preferred newest version."
fad155d4
ML
334 (let-values (((name version) (package-name->name+version spec)))
335 (%find-package spec name version)))
84189ebc
LC
336
337(define* (specification->package+output spec #:optional (output "out"))
338 "Return the package and output specified by SPEC, or #f and #f; SPEC may
339optionally contain a version number and an output name, as in these examples:
340
341 guile
1b846da8 342 guile@2.0.9
84189ebc 343 guile:debug
1b846da8 344 guile@2.0.9:debug
84189ebc
LC
345
346If SPEC does not specify a version number, return the preferred newest
347version; if SPEC does not specify an output, return OUTPUT."
84189ebc
LC
348 (let-values (((name version sub-drv)
349 (package-specification->name+version+output spec output)))
fad155d4
ML
350 (match (%find-package spec name version)
351 (#f
352 (values #f #f))
353 (package
354 (if (member sub-drv (package-outputs package))
355 (values package sub-drv)
356 (leave (_ "package `~a' lacks output `~a'~%")
357 (package-full-name package)
358 sub-drv))))))