packages: Add 'hidden-package'.
[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 combinators)
28 #:use-module ((guix build utils)
29 #:select ((package-name->name+version
30 . hyphen-separated-name->name+version)))
31 #:use-module (ice-9 ftw)
32 #:use-module (ice-9 vlist)
33 #:use-module (ice-9 match)
34 #:use-module (srfi srfi-1)
35 #:use-module (srfi srfi-11)
36 #:use-module (srfi srfi-26)
37 #:use-module (srfi srfi-34)
38 #:use-module (srfi srfi-35)
39 #:use-module (srfi srfi-39)
40 #:export (search-patch
41 search-patches
42 search-bootstrap-binary
43 %patch-path
44 %bootstrap-binaries-path
45 %package-module-path
46
47 fold-packages
48 scheme-modules ;XXX: for lack of a better place
49
50 find-packages-by-name
51 find-best-packages-by-name
52 find-newest-available-packages
53
54 specification->package
55 specification->package+output))
56
57 ;;; Commentary:
58 ;;;
59 ;;; General utilities for the software distribution---i.e., the modules under
60 ;;; (gnu packages ...).
61 ;;;
62 ;;; Code:
63
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.
69
70 (define %bootstrap-binaries-path
71 (make-parameter
72 (map (cut string-append <> "/gnu/packages/bootstrap")
73 %load-path)))
74
75 (define (search-patch file-name)
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)))))))
81
82 (define-syntax-rule (search-patches file-name ...)
83 "Return the list of absolute file names corresponding to each
84 FILE-NAME found in %PATCH-PATH."
85 (list (search-patch file-name) ...))
86
87 (define (search-bootstrap-binary file-name system)
88 "Search the bootstrap binary FILE-NAME for SYSTEM. Raise an error if not
89 found."
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' \
96 for system '~a'")
97 file-name system)))))))
98
99 (define %distro-root-directory
100 ;; Absolute file name of the module hierarchy.
101 (dirname (search-path %load-path "guix.scm")))
102
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.
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"))))))
119
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
130 (define* (scheme-files directory)
131 "Return the list of Scheme files found under DIRECTORY, recursively. The
132 returned 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<?))
154
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)))))
161
162 (define* (scheme-modules directory #:optional sub-directory)
163 "Return the list of Scheme modules available under DIRECTORY.
164 Optionally, narrow the search to SUB-DIRECTORY."
165 (define prefix-len
166 (string-length directory))
167
168 (filter-map (lambda (file)
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))))
178 (scheme-files (if sub-directory
179 (string-append directory "/" sub-directory)
180 directory))))
181
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
184 search."
185 (fold-right (lambda (spec result)
186 (match spec
187 ((? string? directory)
188 (append (scheme-modules directory) result))
189 ((directory . sub-directory)
190 (append (scheme-modules directory sub-directory)
191 result))))
192 '()
193 path))
194
195 (define (fold-packages proc init)
196 "Call (PROC PACKAGE RESULT) for each available package, using INIT as
197 the initial value of RESULT. It is guaranteed to never traverse the
198 same 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)
203 (not (vhash-assq var seen))
204 (not (hidden-package? var)))
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
215 (all-package-modules))))
216
217 (define find-packages-by-name
218 (let ((packages (delay
219 (fold-packages (lambda (p r)
220 (vhash-cons (package-name p) p r))
221 vlist-null)))
222 (version>? (lambda (p1 p2)
223 (version>? (package-version p1) (package-version p2)))))
224 (lambda* (name #:optional version)
225 "Return the list of packages with the given NAME. If VERSION is not #f,
226 then only return packages whose version is prefixed by VERSION, sorted in
227 decreasing version order."
228 (let ((matching (sort (vhash-fold* cons '() name (force packages))
229 version>?)))
230 (if version
231 (filter (lambda (package)
232 (string-prefix? version (package-version package)))
233 matching)
234 matching)))))
235
236 (define find-newest-available-packages
237 (memoize
238 (lambda ()
239 "Return a vhash keyed by package names, and with
240 associated values of the form
241
242 (newest-version newest-package ...)
243
244 where the preferred package is listed first."
245
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
262 version numbers; otherwise, return the list of packages named NAME and at
263 VERSION."
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 '()))))
269
270 \f
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
277 number in the context of the continuation of the call to this function, and
278 return 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
292 \f
293 ;;;
294 ;;; Package specification.
295 ;;;
296
297 (define* (%find-package spec name version #:key fallback?)
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))))
305 (when fallback?
306 (warning (_ "deprecated NAME-VERSION syntax; \
307 use NAME@VERSION instead~%")))
308 pkg)
309 (_
310 (if version
311 (leave (_ "~A: package not found for version ~a~%") name version)
312 (if (not fallback?)
313 ;; XXX: Fallback to the older specification style with an hyphen
314 ;; between NAME and VERSION, for backward compatibility.
315 (call-with-values
316 (lambda ()
317 (hyphen-separated-name->name+version name))
318 (cut %find-package spec <> <> #:fallback? #t))
319
320 ;; The fallback case didn't find anything either, so bail out.
321 (leave (_ "~A: unknown package~%") name))))))
322
323 (define (specification->package spec)
324 "Return a package matching SPEC. SPEC may be a package name, or a package
325 name followed by an at-sign and a version number. If the version number is not
326 present, return the preferred newest version."
327 (let-values (((name version) (package-name->name+version spec)))
328 (%find-package spec name version)))
329
330 (define* (specification->package+output spec #:optional (output "out"))
331 "Return the package and output specified by SPEC, or #f and #f; SPEC may
332 optionally contain a version number and an output name, as in these examples:
333
334 guile
335 guile@2.0.9
336 guile:debug
337 guile@2.0.9:debug
338
339 If SPEC does not specify a version number, return the preferred newest
340 version; if SPEC does not specify an output, return OUTPUT."
341 (let-values (((name version sub-drv)
342 (package-specification->name+version+output spec output)))
343 (match (%find-package spec name version)
344 (#f
345 (values #f #f))
346 (package
347 (if (member sub-drv (package-outputs package))
348 (values package sub-drv)
349 (leave (_ "package `~a' lacks output `~a'~%")
350 (package-full-name package)
351 sub-drv))))))