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