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>
8 ;;; This file is part of GNU Guix.
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.
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.
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/>.
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
42 search-bootstrap-binary
44 %bootstrap-binaries-path
48 scheme-modules ;XXX: for lack of a better place
51 find-best-packages-by-name
52 find-newest-available-packages
54 specification->package
55 specification->package+output))
59 ;;; General utilities for the software distribution---i.e., the modules under
60 ;;; (gnu packages ...).
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.
70 (define %bootstrap-binaries-path
72 (map (cut string-append <> "/gnu/packages/bootstrap")
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)
79 (&message (message (format #f (_ "~a: patch not found")
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) ...))
87 (define (search-bootstrap-binary file-name system)
88 "Search the bootstrap binary FILE-NAME for SYSTEM. Raise an error if not
90 (or (search-path (%bootstrap-binaries-path)
91 (string-append system "/" file-name))
95 (format #f (_ "could not find bootstrap binary '~a' \
97 file-name system)))))))
99 (define %distro-root-directory
100 ;; Absolute file name of the module hierarchy.
101 (dirname (search-path %load-path "guix.scm")))
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") "")
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)))
118 (append environment `((,%distro-root-directory . "gnu/packages"))))))
121 ;; Define it after '%package-module-path' so that '%load-path' contains user
122 ;; directories, allowing patches in $GUIX_PACKAGE_PATH to be found.
124 (map (lambda (directory)
125 (if (string=? directory %distro-root-directory)
126 (string-append directory "/gnu/packages/patches")
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."
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)
141 (lambda (path stat result) ; down
143 (lambda (path stat result) ; up
146 (lambda (path stat errno result)
147 (warning (_ "cannot access `~a': ~a~%")
148 path (strerror errno))
155 (define file-name->module-name
156 (let ((not-slash (char-set-complement (char-set #\/))))
158 "Return the module name (a list of symbols) corresponding to FILE."
160 (string-tokenize (string-drop-right file 4) not-slash)))))
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."
166 (string-length directory))
168 (filter-map (lambda (file)
169 (let* ((file (substring file prefix-len))
170 (module (file-name->module-name file)))
173 (resolve-interface module))
175 ;; Report the error, but keep going.
176 (warn-about-load-error module args)
178 (scheme-files (if sub-directory
179 (string-append directory "/" sub-directory)
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
185 (fold-right (lambda (spec result)
187 ((? string? directory)
188 (append (scheme-modules directory) result))
189 ((directory . sub-directory)
190 (append (scheme-modules directory sub-directory)
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
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)))
210 (module-map (lambda (sym var)
211 (false-if-exception (variable-ref var)))
215 (all-package-modules))))
217 (define find-packages-by-name
218 (let ((packages (delay
219 (fold-packages (lambda (p r)
220 (vhash-cons (package-name p) p r))
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))
231 (filter (lambda (package)
232 (string-prefix? version (package-version package)))
236 (define find-newest-available-packages
239 "Return a vhash keyed by package names, and with
240 associated values of the form
242 (newest-version newest-package ...)
244 where the preferred package is listed first."
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))
257 (#f (vhash-cons name `(,version ,p) r)))))
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
265 (find-packages-by-name name version)
266 (match (vhash-assoc name (find-newest-available-packages))
267 ((_ version pkgs ...) pkgs)
271 (define %sigint-prompt
272 ;; The prompt to jump to upon SIGINT.
273 (make-prompt-tag "interruptible"))
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
283 (sigaction SIGINT SIG_DFL)
284 (abort-to-prompt %sigint-prompt signum)))
288 (cut sigaction SIGINT SIG_DFL)))
294 ;;; Package specification.
297 (define* (%find-package spec name version #:key fallback?)
298 (match (find-best-packages-by-name name version)
301 (warning (_ "ambiguous package specification `~a'~%") spec)
302 (warning (_ "choosing ~a from ~a~%")
303 (package-full-name pkg)
304 (location->string (package-location pkg))))
306 (warning (_ "deprecated NAME-VERSION syntax; \
307 use NAME@VERSION instead~%")))
309 (match (package-superseded pkg)
311 (info (_ "package '~a' has been superseded by '~a'~%")
312 (package-name pkg) (package-name new))
318 (leave (_ "~A: package not found for version ~a~%") name version)
320 ;; XXX: Fallback to the older specification style with an hyphen
321 ;; between NAME and VERSION, for backward compatibility.
324 (hyphen-separated-name->name+version name))
325 (cut %find-package spec <> <> #:fallback? #t))
327 ;; The fallback case didn't find anything either, so bail out.
328 (leave (_ "~A: unknown package~%") name))))))
330 (define (specification->package spec)
331 "Return a package matching SPEC. SPEC may be a package name, or a package
332 name followed by an at-sign and a version number. If the version number is not
333 present, return the preferred newest version."
334 (let-values (((name version) (package-name->name+version spec)))
335 (%find-package spec name version)))
337 (define* (specification->package+output spec #:optional (output "out"))
338 "Return the package and output specified by SPEC, or #f and #f; SPEC may
339 optionally contain a version number and an output name, as in these examples:
346 If SPEC does not specify a version number, return the preferred newest
347 version; if SPEC does not specify an output, return OUTPUT."
348 (let-values (((name version sub-drv)
349 (package-specification->name+version+output spec output)))
350 (match (%find-package spec name version)
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)