gnu: texlive-latex-base: Declare a source file-name.
[jackhill/guix/guix.git] / gnu / packages.scm
1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2012, 2013, 2014, 2015, 2016, 2017 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, 2017 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 discovery)
28 #:use-module (guix memoization)
29 #:use-module ((guix build utils)
30 #:select ((package-name->name+version
31 . hyphen-separated-name->name+version)))
32 #:autoload (guix profiles) (packages->manifest)
33 #:use-module (ice-9 vlist)
34 #:use-module (ice-9 match)
35 #:use-module (srfi srfi-1)
36 #:use-module (srfi srfi-11)
37 #:use-module (srfi srfi-26)
38 #:use-module (srfi srfi-34)
39 #:use-module (srfi srfi-35)
40 #:use-module (srfi srfi-39)
41 #:export (search-patch
42 search-patches
43 search-auxiliary-file
44 search-bootstrap-binary
45 %patch-path
46 %auxiliary-files-path
47 %bootstrap-binaries-path
48 %package-module-path
49
50 fold-packages
51
52 find-packages-by-name
53 find-best-packages-by-name
54 find-newest-available-packages
55
56 specification->package
57 specification->package+output
58 specifications->manifest))
59
60 ;;; Commentary:
61 ;;;
62 ;;; General utilities for the software distribution---i.e., the modules under
63 ;;; (gnu packages ...).
64 ;;;
65 ;;; Code:
66
67 ;; By default, we store patches, auxiliary files and bootstrap binaries
68 ;; alongside Guile modules. This is so that these extra files can be
69 ;; found without requiring a special setup, such as a specific
70 ;; installation directory and an extra environment variable. One
71 ;; advantage of this setup is that everything just works in an
72 ;; auto-compilation setting.
73
74 (define %bootstrap-binaries-path
75 (make-parameter
76 (map (cut string-append <> "/gnu/packages/bootstrap")
77 %load-path)))
78
79 (define %auxiliary-files-path
80 (make-parameter
81 (map (cut string-append <> "/gnu/packages/aux-files")
82 %load-path)))
83
84 (define (search-auxiliary-file file-name)
85 "Search the auxiliary FILE-NAME. Return #f if not found."
86 (search-path (%auxiliary-files-path) file-name))
87
88 (define (search-patch file-name)
89 "Search the patch FILE-NAME. Raise an error if not found."
90 (or (search-path (%patch-path) file-name)
91 (raise (condition
92 (&message (message (format #f (G_ "~a: patch not found")
93 file-name)))))))
94
95 (define-syntax-rule (search-patches file-name ...)
96 "Return the list of absolute file names corresponding to each
97 FILE-NAME found in %PATCH-PATH."
98 (list (search-patch file-name) ...))
99
100 (define (search-bootstrap-binary file-name system)
101 "Search the bootstrap binary FILE-NAME for SYSTEM. Raise an error if not
102 found."
103 (or (search-path (%bootstrap-binaries-path)
104 (string-append system "/" file-name))
105 (raise (condition
106 (&message
107 (message
108 (format #f (G_ "could not find bootstrap binary '~a' \
109 for system '~a'")
110 file-name system)))))))
111
112 (define %distro-root-directory
113 ;; Absolute file name of the module hierarchy.
114 (dirname (search-path %load-path "guix.scm")))
115
116 (define %package-module-path
117 ;; Search path for package modules. Each item must be either a directory
118 ;; name or a pair whose car is a directory and whose cdr is a sub-directory
119 ;; to narrow the search.
120 (let* ((not-colon (char-set-complement (char-set #\:)))
121 (environment (string-tokenize (or (getenv "GUIX_PACKAGE_PATH") "")
122 not-colon)))
123 ;; Automatically add items from $GUIX_PACKAGE_PATH to Guile's search path.
124 (for-each (lambda (directory)
125 (set! %load-path (cons directory %load-path))
126 (set! %load-compiled-path
127 (cons directory %load-compiled-path)))
128 environment)
129
130 (make-parameter
131 (append environment `((,%distro-root-directory . "gnu/packages"))))))
132
133 (define %patch-path
134 ;; Define it after '%package-module-path' so that '%load-path' contains user
135 ;; directories, allowing patches in $GUIX_PACKAGE_PATH to be found.
136 (make-parameter
137 (map (lambda (directory)
138 (if (string=? directory %distro-root-directory)
139 (string-append directory "/gnu/packages/patches")
140 directory))
141 %load-path)))
142
143 (define* (fold-packages proc init
144 #:optional
145 (modules (all-modules (%package-module-path)))
146 #:key (select? (negate hidden-package?)))
147 "Call (PROC PACKAGE RESULT) for each available package defined in one of
148 MODULES that matches SELECT?, using INIT as the initial value of RESULT. It
149 is guaranteed to never traverse the same package twice."
150 (fold-module-public-variables (lambda (object result)
151 (if (and (package? object) (select? object))
152 (proc object result)
153 result))
154 init
155 modules))
156
157 (define find-packages-by-name
158 (let ((packages (delay
159 (fold-packages (lambda (p r)
160 (vhash-cons (package-name p) p r))
161 vlist-null)))
162 (version>? (lambda (p1 p2)
163 (version>? (package-version p1) (package-version p2)))))
164 (lambda* (name #:optional version)
165 "Return the list of packages with the given NAME. If VERSION is not #f,
166 then only return packages whose version is prefixed by VERSION, sorted in
167 decreasing version order."
168 (let ((matching (sort (vhash-fold* cons '() name (force packages))
169 version>?)))
170 (if version
171 (filter (lambda (package)
172 (string-prefix? version (package-version package)))
173 matching)
174 matching)))))
175
176 (define find-newest-available-packages
177 (mlambda ()
178 "Return a vhash keyed by package names, and with
179 associated values of the form
180
181 (newest-version newest-package ...)
182
183 where the preferred package is listed first."
184
185 ;; FIXME: Currently, the preferred package is whichever one
186 ;; was found last by 'fold-packages'. Find a better solution.
187 (fold-packages (lambda (p r)
188 (let ((name (package-name p))
189 (version (package-version p)))
190 (match (vhash-assoc name r)
191 ((_ newest-so-far . pkgs)
192 (case (version-compare version newest-so-far)
193 ((>) (vhash-cons name `(,version ,p) r))
194 ((=) (vhash-cons name `(,version ,p ,@pkgs) r))
195 ((<) r)))
196 (#f (vhash-cons name `(,version ,p) r)))))
197 vlist-null)))
198
199 (define (find-best-packages-by-name name version)
200 "If version is #f, return the list of packages named NAME with the highest
201 version numbers; otherwise, return the list of packages named NAME and at
202 VERSION."
203 (if version
204 (find-packages-by-name name version)
205 (match (vhash-assoc name (find-newest-available-packages))
206 ((_ version pkgs ...) pkgs)
207 (#f '()))))
208
209 \f
210 (define %sigint-prompt
211 ;; The prompt to jump to upon SIGINT.
212 (make-prompt-tag "interruptible"))
213
214 (define (call-with-sigint-handler thunk handler)
215 "Call THUNK and return its value. Upon SIGINT, call HANDLER with the signal
216 number in the context of the continuation of the call to this function, and
217 return its return value."
218 (call-with-prompt %sigint-prompt
219 (lambda ()
220 (sigaction SIGINT
221 (lambda (signum)
222 (sigaction SIGINT SIG_DFL)
223 (abort-to-prompt %sigint-prompt signum)))
224 (dynamic-wind
225 (const #t)
226 thunk
227 (cut sigaction SIGINT SIG_DFL)))
228 (lambda (k signum)
229 (handler signum))))
230
231 \f
232 ;;;
233 ;;; Package specification.
234 ;;;
235
236 (define* (%find-package spec name version)
237 (match (find-best-packages-by-name name version)
238 ((pkg . pkg*)
239 (unless (null? pkg*)
240 (warning (G_ "ambiguous package specification `~a'~%") spec)
241 (warning (G_ "choosing ~a@~a from ~a~%")
242 (package-name pkg) (package-version pkg)
243 (location->string (package-location pkg))))
244 (match (package-superseded pkg)
245 ((? package? new)
246 (info (G_ "package '~a' has been superseded by '~a'~%")
247 (package-name pkg) (package-name new))
248 new)
249 (#f
250 pkg)))
251 (x
252 (if version
253 (leave (G_ "~A: package not found for version ~a~%") name version)
254 (leave (G_ "~A: unknown package~%") name)))))
255
256 (define (specification->package spec)
257 "Return a package matching SPEC. SPEC may be a package name, or a package
258 name followed by an at-sign and a version number. If the version number is not
259 present, return the preferred newest version."
260 (let-values (((name version) (package-name->name+version spec)))
261 (%find-package spec name version)))
262
263 (define* (specification->package+output spec #:optional (output "out"))
264 "Return the package and output specified by SPEC, or #f and #f; SPEC may
265 optionally contain a version number and an output name, as in these examples:
266
267 guile
268 guile@2.0.9
269 guile:debug
270 guile@2.0.9:debug
271
272 If SPEC does not specify a version number, return the preferred newest
273 version; if SPEC does not specify an output, return OUTPUT."
274 (let-values (((name version sub-drv)
275 (package-specification->name+version+output spec output)))
276 (match (%find-package spec name version)
277 (#f
278 (values #f #f))
279 (package
280 (if (member sub-drv (package-outputs package))
281 (values package sub-drv)
282 (leave (G_ "package `~a' lacks output `~a'~%")
283 (package-full-name package)
284 sub-drv))))))
285
286 (define (specifications->manifest specs)
287 "Given SPECS, a list of specifications such as \"emacs@25.2\" or
288 \"guile:debug\", return a profile manifest."
289 ;; This procedure exists mostly so users of 'guix package -m' don't have to
290 ;; fiddle with multiple-value returns.
291 (packages->manifest
292 (map (compose list specification->package+output) specs)))