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 (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
39 %bootstrap-binaries-path
45 find-best-packages-by-name
46 find-newest-available-packages
48 specification->package
49 specification->package+output))
53 ;;; General utilities for the software distribution---i.e., the modules under
54 ;;; (gnu packages ...).
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.
64 (define %bootstrap-binaries-path
66 (map (cut string-append <> "/gnu/packages/bootstrap")
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)
73 (&message (message (format #f (_ "~a: patch not found")
76 (define (search-bootstrap-binary file-name system)
77 "Search the bootstrap binary FILE-NAME for SYSTEM. Raise an error if not
79 (or (search-path (%bootstrap-binaries-path)
80 (string-append system "/" file-name))
84 (format #f (_ "could not find bootstrap binary '~a' \
86 file-name system)))))))
88 (define %distro-root-directory
89 ;; Absolute file name of the module hierarchy.
90 (dirname (search-path %load-path "guix.scm")))
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") "")
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)))
107 (append environment `((,%distro-root-directory . "gnu/packages"))))))
110 ;; Define it after '%package-module-path' so that '%load-path' contains user
111 ;; directories, allowing patches in $GUIX_PACKAGE_PATH to be found.
113 (map (lambda (directory)
114 (if (string=? directory %distro-root-directory)
115 (string-append directory "/gnu/packages/patches")
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."
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)
130 (lambda (path stat result) ; down
132 (lambda (path stat result) ; up
135 (lambda (path stat errno result)
136 (warning (_ "cannot access `~a': ~a~%")
137 path (strerror errno))
144 (define file-name->module-name
145 (let ((not-slash (char-set-complement (char-set #\/))))
147 "Return the module name (a list of symbols) corresponding to FILE."
149 (string-tokenize (string-drop-right file 4) not-slash)))))
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."
155 (string-length directory))
157 (filter-map (lambda (file)
158 (let* ((file (substring file prefix-len))
159 (module (file-name->module-name file)))
162 (resolve-interface module))
164 ;; Report the error, but keep going.
165 (warn-about-load-error module args)
167 (scheme-files (if sub-directory
168 (string-append directory "/" sub-directory)
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
174 (fold-right (lambda (spec result)
176 ((? string? directory)
177 (append (package-modules directory) result))
178 ((directory . sub-directory)
179 (append (package-modules directory sub-directory)
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
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)))
198 (module-map (lambda (sym var)
199 (false-if-exception (variable-ref var)))
203 (all-package-modules))))
205 (define find-packages-by-name
206 (let ((packages (delay
207 (fold-packages (lambda (p r)
208 (vhash-cons (package-name p) p r))
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))
219 (filter (lambda (package)
220 (string-prefix? version (package-version package)))
224 (define find-newest-available-packages
227 "Return a vhash keyed by package names, and with
228 associated values of the form
230 (newest-version newest-package ...)
232 where the preferred package is listed first."
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))
245 (#f (vhash-cons name `(,version ,p) r)))))
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
253 (find-packages-by-name name version)
254 (match (vhash-assoc name (find-newest-available-packages))
255 ((_ version pkgs ...) pkgs)
259 (define %sigint-prompt
260 ;; The prompt to jump to upon SIGINT.
261 (make-prompt-tag "interruptible"))
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
271 (sigaction SIGINT SIG_DFL)
272 (abort-to-prompt %sigint-prompt signum)))
276 (cut sigaction SIGINT SIG_DFL)))
282 ;;; Package specification.
285 (define* (%find-package spec name version #:key fallback?)
286 (match (find-best-packages-by-name name version)
289 (warning (_ "ambiguous package specification `~a'~%") spec)
290 (warning (_ "choosing ~a from ~a~%")
291 (package-full-name pkg)
292 (location->string (package-location pkg))))
294 (warning (_ "deprecated NAME-VERSION syntax; \
295 use NAME@VERSION instead~%")))
299 (leave (_ "~A: package not found for version ~a~%") name version)
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))))))
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)))
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:
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)
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)