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>
7 ;;; This file is part of GNU Guix.
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.
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.
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/>.
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
38 %bootstrap-binaries-path
44 find-best-packages-by-name
45 find-newest-available-packages
47 specification->package
48 specification->package+output))
52 ;;; General utilities for the software distribution---i.e., the modules under
53 ;;; (gnu packages ...).
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.
63 (define %bootstrap-binaries-path
65 (map (cut string-append <> "/gnu/packages/bootstrap")
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)
72 (&message (message (format #f (_ "~a: patch not found")
75 (define (search-bootstrap-binary file-name system)
76 "Search the bootstrap binary FILE-NAME for SYSTEM. Raise an error if not
78 (or (search-path (%bootstrap-binaries-path)
79 (string-append system "/" file-name))
83 (format #f (_ "could not find bootstrap binary '~a' \
85 file-name system)))))))
87 (define %distro-root-directory
88 ;; Absolute file name of the module hierarchy.
89 (dirname (search-path %load-path "guix.scm")))
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") "")
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)))
106 (append environment `((,%distro-root-directory . "gnu/packages"))))))
109 ;; Define it after '%package-module-path' so that '%load-path' contains user
110 ;; directories, allowing patches in $GUIX_PACKAGE_PATH to be found.
112 (map (lambda (directory)
113 (if (string=? directory %distro-root-directory)
114 (string-append directory "/gnu/packages/patches")
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."
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)
129 (lambda (path stat result) ; down
131 (lambda (path stat result) ; up
134 (lambda (path stat errno result)
135 (warning (_ "cannot access `~a': ~a~%")
136 path (strerror errno))
143 (define file-name->module-name
144 (let ((not-slash (char-set-complement (char-set #\/))))
146 "Return the module name (a list of symbols) corresponding to FILE."
148 (string-tokenize (string-drop-right file 4) not-slash)))))
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."
154 (string-length directory))
156 (filter-map (lambda (file)
157 (let* ((file (substring file prefix-len))
158 (module (file-name->module-name file)))
161 (resolve-interface module))
163 ;; Report the error, but keep going.
164 (warn-about-load-error module args)
166 (scheme-files (if sub-directory
167 (string-append directory "/" sub-directory)
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
173 (fold-right (lambda (spec result)
175 ((? string? directory)
176 (append (package-modules directory) result))
177 ((directory . sub-directory)
178 (append (package-modules directory sub-directory)
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
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)))
197 (module-map (lambda (sym var)
198 (false-if-exception (variable-ref var)))
202 (all-package-modules))))
204 (define find-packages-by-name
205 (let ((packages (delay
206 (fold-packages (lambda (p r)
207 (vhash-cons (package-name p) p r))
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))
218 (filter (lambda (package)
219 (string-prefix? version (package-version package)))
223 (define find-newest-available-packages
226 "Return a vhash keyed by package names, and with
227 associated values of the form
229 (newest-version newest-package ...)
231 where the preferred package is listed first."
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))
244 (#f (vhash-cons name `(,version ,p) r)))))
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
252 (find-packages-by-name name version)
253 (match (vhash-assoc name (find-newest-available-packages))
254 ((_ version pkgs ...) pkgs)
258 (define %sigint-prompt
259 ;; The prompt to jump to upon SIGINT.
260 (make-prompt-tag "interruptible"))
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
270 (sigaction SIGINT SIG_DFL)
271 (abort-to-prompt %sigint-prompt signum)))
275 (cut sigaction SIGINT SIG_DFL)))
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)
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)))
296 (leave (_ "~A: package not found for version ~a~%")
298 (leave (_ "~A: unknown package~%") name))))))
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:
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))
314 (leave (_ "package `~a' lacks output `~a'~%")
315 (package-full-name p)
318 (let-values (((name version sub-drv)
319 (package-specification->name+version+output spec output)))
320 (match (find-best-packages-by-name name version)
322 (values p (ensure-output p sub-drv)))
324 (warning (_ "ambiguous package specification `~a'~%")
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)))
331 (leave (_ "~a: package not found~%") spec)))))