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