gnu: Add go-github-com-michiwend-golang-pretty.
[jackhill/guix/guix.git] / gnu / packages.scm
1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2012, 2013, 2014, 2015, 2016, 2017, 2018 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. Since (gnu packages …) might
114 ;; live in a directory different from (guix), try to get the best match.
115 (letrec-syntax ((dirname* (syntax-rules ()
116 ((_ file)
117 (dirname file))
118 ((_ file head tail ...)
119 (dirname (dirname* file tail ...)))))
120 (try (syntax-rules ()
121 ((_ (file things ...) rest ...)
122 (match (search-path %load-path file)
123 (#f
124 (try rest ...))
125 (absolute
126 (dirname* absolute things ...))))
127 ((_)
128 #f))))
129 (try ("gnu/packages/base.scm" gnu/ packages/)
130 ("gnu/packages.scm" gnu/)
131 ("guix.scm"))))
132
133 (define %package-module-path
134 ;; Search path for package modules. Each item must be either a directory
135 ;; name or a pair whose car is a directory and whose cdr is a sub-directory
136 ;; to narrow the search.
137 (let* ((not-colon (char-set-complement (char-set #\:)))
138 (environment (string-tokenize (or (getenv "GUIX_PACKAGE_PATH") "")
139 not-colon)))
140 ;; Automatically add items from $GUIX_PACKAGE_PATH to Guile's search path.
141 (for-each (lambda (directory)
142 (set! %load-path (cons directory %load-path))
143 (set! %load-compiled-path
144 (cons directory %load-compiled-path)))
145 environment)
146
147 (make-parameter
148 (append environment `((,%distro-root-directory . "gnu/packages"))))))
149
150 (define %patch-path
151 ;; Define it after '%package-module-path' so that '%load-path' contains user
152 ;; directories, allowing patches in $GUIX_PACKAGE_PATH to be found.
153 (make-parameter
154 (map (lambda (directory)
155 (if (string=? directory %distro-root-directory)
156 (string-append directory "/gnu/packages/patches")
157 directory))
158 %load-path)))
159
160 (define* (fold-packages proc init
161 #:optional
162 (modules (all-modules (%package-module-path)
163 #:warn
164 warn-about-load-error))
165 #:key (select? (negate hidden-package?)))
166 "Call (PROC PACKAGE RESULT) for each available package defined in one of
167 MODULES that matches SELECT?, using INIT as the initial value of RESULT. It
168 is guaranteed to never traverse the same package twice."
169 (fold-module-public-variables (lambda (object result)
170 (if (and (package? object) (select? object))
171 (proc object result)
172 result))
173 init
174 modules))
175
176 (define find-packages-by-name
177 (let ((packages (delay
178 (fold-packages (lambda (p r)
179 (vhash-cons (package-name p) p r))
180 vlist-null)))
181 (version>? (lambda (p1 p2)
182 (version>? (package-version p1) (package-version p2)))))
183 (lambda* (name #:optional version)
184 "Return the list of packages with the given NAME. If VERSION is not #f,
185 then only return packages whose version is prefixed by VERSION, sorted in
186 decreasing version order."
187 (let ((matching (sort (vhash-fold* cons '() name (force packages))
188 version>?)))
189 (if version
190 (filter (lambda (package)
191 (version-prefix? version (package-version package)))
192 matching)
193 matching)))))
194
195 (define find-newest-available-packages
196 (mlambda ()
197 "Return a vhash keyed by package names, and with
198 associated values of the form
199
200 (newest-version newest-package ...)
201
202 where the preferred package is listed first."
203
204 ;; FIXME: Currently, the preferred package is whichever one
205 ;; was found last by 'fold-packages'. Find a better solution.
206 (fold-packages (lambda (p r)
207 (let ((name (package-name p))
208 (version (package-version p)))
209 (match (vhash-assoc name r)
210 ((_ newest-so-far . pkgs)
211 (case (version-compare version newest-so-far)
212 ((>) (vhash-cons name `(,version ,p) r))
213 ((=) (vhash-cons name `(,version ,p ,@pkgs) r))
214 ((<) r)))
215 (#f (vhash-cons name `(,version ,p) r)))))
216 vlist-null)))
217
218 (define (find-best-packages-by-name name version)
219 "If version is #f, return the list of packages named NAME with the highest
220 version numbers; otherwise, return the list of packages named NAME and at
221 VERSION."
222 (if version
223 (find-packages-by-name name version)
224 (match (vhash-assoc name (find-newest-available-packages))
225 ((_ version pkgs ...) pkgs)
226 (#f '()))))
227
228 \f
229 (define %sigint-prompt
230 ;; The prompt to jump to upon SIGINT.
231 (make-prompt-tag "interruptible"))
232
233 (define (call-with-sigint-handler thunk handler)
234 "Call THUNK and return its value. Upon SIGINT, call HANDLER with the signal
235 number in the context of the continuation of the call to this function, and
236 return its return value."
237 (call-with-prompt %sigint-prompt
238 (lambda ()
239 (sigaction SIGINT
240 (lambda (signum)
241 (sigaction SIGINT SIG_DFL)
242 (abort-to-prompt %sigint-prompt signum)))
243 (dynamic-wind
244 (const #t)
245 thunk
246 (cut sigaction SIGINT SIG_DFL)))
247 (lambda (k signum)
248 (handler signum))))
249
250 \f
251 ;;;
252 ;;; Package specification.
253 ;;;
254
255 (define* (%find-package spec name version)
256 (match (find-best-packages-by-name name version)
257 ((pkg . pkg*)
258 (unless (null? pkg*)
259 (warning (G_ "ambiguous package specification `~a'~%") spec)
260 (warning (G_ "choosing ~a@~a from ~a~%")
261 (package-name pkg) (package-version pkg)
262 (location->string (package-location pkg))))
263 (match (package-superseded pkg)
264 ((? package? new)
265 (info (G_ "package '~a' has been superseded by '~a'~%")
266 (package-name pkg) (package-name new))
267 new)
268 (#f
269 pkg)))
270 (x
271 (if version
272 (leave (G_ "~A: package not found for version ~a~%") name version)
273 (leave (G_ "~A: unknown package~%") name)))))
274
275 (define (specification->package spec)
276 "Return a package matching SPEC. SPEC may be a package name, or a package
277 name followed by an at-sign and a version number. If the version number is not
278 present, return the preferred newest version."
279 (let-values (((name version) (package-name->name+version spec)))
280 (%find-package spec name version)))
281
282 (define* (specification->package+output spec #:optional (output "out"))
283 "Return the package and output specified by SPEC, or #f and #f; SPEC may
284 optionally contain a version number and an output name, as in these examples:
285
286 guile
287 guile@2.0.9
288 guile:debug
289 guile@2.0.9:debug
290
291 If SPEC does not specify a version number, return the preferred newest
292 version; if SPEC does not specify an output, return OUTPUT."
293 (let-values (((name version sub-drv)
294 (package-specification->name+version+output spec output)))
295 (match (%find-package spec name version)
296 (#f
297 (values #f #f))
298 (package
299 (if (member sub-drv (package-outputs package))
300 (values package sub-drv)
301 (leave (G_ "package `~a' lacks output `~a'~%")
302 (package-full-name package)
303 sub-drv))))))
304
305 (define (specifications->manifest specs)
306 "Given SPECS, a list of specifications such as \"emacs@25.2\" or
307 \"guile:debug\", return a profile manifest."
308 ;; This procedure exists mostly so users of 'guix package -m' don't have to
309 ;; fiddle with multiple-value returns.
310 (packages->manifest
311 (map (compose list specification->package+output) specs)))