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