packages: 'find-packages-by-name' properly honors version prefixes.
[jackhill/guix/guix.git] / gnu / packages.scm
CommitLineData
233e7676 1;;; GNU Guix --- Functional package management for GNU
3c0128b0 2;;; Copyright © 2012, 2013, 2014, 2015, 2016, 2017, 2018 Ludovic Courtès <ludo@gnu.org>
c2868b1e 3;;; Copyright © 2013 Mark H Weaver <mhw@netris.org>
7d193ec3 4;;; Copyright © 2014 Eric Bavier <bavier@member.fsf.org>
96eaa55f 5;;; Copyright © 2016, 2017 Alex Kost <alezost@gmail.com>
fad155d4 6;;; Copyright © 2016 Mathieu Lirzin <mthl@gnu.org>
6b1891b0 7;;;
233e7676 8;;; This file is part of GNU Guix.
6b1891b0 9;;;
233e7676 10;;; GNU Guix is free software; you can redistribute it and/or modify it
6b1891b0
LC
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;;;
233e7676 15;;; GNU Guix is distributed in the hope that it will be useful, but
6b1891b0
LC
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
233e7676 21;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
6b1891b0 22
59a43334 23(define-module (gnu packages)
6b1891b0 24 #:use-module (guix packages)
98eb8cbe 25 #:use-module (guix ui)
800cdeef 26 #:use-module (guix utils)
cd903ef7 27 #:use-module (guix discovery)
f9704f17 28 #:use-module (guix memoization)
95cd4971
LC
29 #:use-module ((guix build utils)
30 #:select ((package-name->name+version
31 . hyphen-separated-name->name+version)))
c08ea55e 32 #:autoload (guix profiles) (packages->manifest)
c2868b1e 33 #:use-module (ice-9 vlist)
dc5669cd 34 #:use-module (ice-9 match)
6b1891b0 35 #:use-module (srfi srfi-1)
5e3b388b 36 #:use-module (srfi srfi-11)
6b1891b0 37 #:use-module (srfi srfi-26)
dbab5150
LC
38 #:use-module (srfi srfi-34)
39 #:use-module (srfi srfi-35)
800cdeef
LC
40 #:use-module (srfi srfi-39)
41 #:export (search-patch
25897079 42 search-patches
96eaa55f 43 search-auxiliary-file
ac5aa288 44 search-bootstrap-binary
0492f4a2 45 %patch-path
96eaa55f 46 %auxiliary-files-path
0b3651bc 47 %bootstrap-binaries-path
c107b541 48 %package-module-path
7d193ec3 49
ba326ce4 50 fold-packages
7d193ec3 51
dc5669cd 52 find-packages-by-name
3f26bfc1 53 find-best-packages-by-name
7d193ec3
EB
54 find-newest-available-packages
55
84189ebc 56 specification->package
c08ea55e
LC
57 specification->package+output
58 specifications->manifest))
6b1891b0
LC
59
60;;; Commentary:
61;;;
62;;; General utilities for the software distribution---i.e., the modules under
59a43334 63;;; (gnu packages ...).
6b1891b0
LC
64;;;
65;;; Code:
66
96eaa55f
AK
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.
a9f60c42 73
a9f60c42 74(define %bootstrap-binaries-path
ac5aa288 75 (make-parameter
1ffa7090 76 (map (cut string-append <> "/gnu/packages/bootstrap")
0b3651bc 77 %load-path)))
ac5aa288 78
96eaa55f
AK
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
800cdeef 88(define (search-patch file-name)
dbab5150
LC
89 "Search the patch FILE-NAME. Raise an error if not found."
90 (or (search-path (%patch-path) file-name)
91 (raise (condition
69daee23 92 (&message (message (format #f (G_ "~a: patch not found")
dbab5150 93 file-name)))))))
800cdeef 94
25897079
AK
95(define-syntax-rule (search-patches file-name ...)
96 "Return the list of absolute file names corresponding to each
97FILE-NAME found in %PATCH-PATH."
98 (list (search-patch file-name) ...))
99
ac5aa288 100(define (search-bootstrap-binary file-name system)
dfba5489
LC
101 "Search the bootstrap binary FILE-NAME for SYSTEM. Raise an error if not
102found."
103 (or (search-path (%bootstrap-binaries-path)
104 (string-append system "/" file-name))
105 (raise (condition
106 (&message
107 (message
69daee23 108 (format #f (G_ "could not find bootstrap binary '~a' \
dfba5489
LC
109for system '~a'")
110 file-name system)))))))
ac5aa288 111
84836a57 112(define %distro-root-directory
eaae07ec
LC
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"))))
6b1891b0 132
c107b541
LC
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.
8689901f
LC
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"))))))
c107b541 149
ee06af5b
LC
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
5c5ae46c
LC
160(define* (fold-packages proc init
161 #:optional
3c0128b0
LC
162 (modules (all-modules (%package-module-path)
163 #:warn
164 warn-about-load-error))
96dc8f35 165 #:key (select? (negate hidden-package?)))
5c5ae46c 166 "Call (PROC PACKAGE RESULT) for each available package defined in one of
96dc8f35
LC
167MODULES that matches SELECT?, using INIT as the initial value of RESULT. It
168is guaranteed to never traverse the same package twice."
cd903ef7 169 (fold-module-public-variables (lambda (object result)
96dc8f35 170 (if (and (package? object) (select? object))
cd903ef7
LC
171 (proc object result)
172 result))
173 init
5c5ae46c 174 modules))
ba326ce4 175
9ffc1c00
LC
176(define find-packages-by-name
177 (let ((packages (delay
178 (fold-packages (lambda (p r)
179 (vhash-cons (package-name p) p r))
724311a2
LC
180 vlist-null)))
181 (version>? (lambda (p1 p2)
182 (version>? (package-version p1) (package-version p2)))))
9ffc1c00
LC
183 (lambda* (name #:optional version)
184 "Return the list of packages with the given NAME. If VERSION is not #f,
724311a2
LC
185then only return packages whose version is prefixed by VERSION, sorted in
186decreasing version order."
187 (let ((matching (sort (vhash-fold* cons '() name (force packages))
188 version>?)))
9ffc1c00
LC
189 (if version
190 (filter (lambda (package)
348987d3 191 (version-prefix? version (package-version package)))
9ffc1c00
LC
192 matching)
193 matching)))))
dc5669cd 194
3f26bfc1 195(define find-newest-available-packages
55b2d921
LC
196 (mlambda ()
197 "Return a vhash keyed by package names, and with
dc5669cd
MW
198associated values of the form
199
200 (newest-version newest-package ...)
201
202where the preferred package is listed first."
203
55b2d921
LC
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)))
3f26bfc1
LC
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
220version numbers; otherwise, return the list of packages named NAME and at
221VERSION."
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 '()))))
7d193ec3
EB
227
228\f
4ea44419
AK
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
235number in the context of the continuation of the call to this function, and
236return 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
fad155d4
ML
250\f
251;;;
252;;; Package specification.
253;;;
254
e30c2be1 255(define* (%find-package spec name version)
fad155d4
ML
256 (match (find-best-packages-by-name name version)
257 ((pkg . pkg*)
258 (unless (null? pkg*)
69daee23
LC
259 (warning (G_ "ambiguous package specification `~a'~%") spec)
260 (warning (G_ "choosing ~a@~a from ~a~%")
d75e8f36 261 (package-name pkg) (package-version pkg)
fad155d4 262 (location->string (package-location pkg))))
01afdab8
LC
263 (match (package-superseded pkg)
264 ((? package? new)
69daee23 265 (info (G_ "package '~a' has been superseded by '~a'~%")
01afdab8
LC
266 (package-name pkg) (package-name new))
267 new)
268 (#f
269 pkg)))
e465d9e1 270 (x
fad155d4 271 (if version
69daee23
LC
272 (leave (G_ "~A: package not found for version ~a~%") name version)
273 (leave (G_ "~A: unknown package~%") name)))))
fad155d4 274
5e3b388b
CR
275(define (specification->package spec)
276 "Return a package matching SPEC. SPEC may be a package name, or a package
1b846da8 277name followed by an at-sign and a version number. If the version number is not
5e3b388b 278present, return the preferred newest version."
fad155d4
ML
279 (let-values (((name version) (package-name->name+version spec)))
280 (%find-package spec name version)))
84189ebc
LC
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
284optionally contain a version number and an output name, as in these examples:
285
286 guile
1b846da8 287 guile@2.0.9
84189ebc 288 guile:debug
1b846da8 289 guile@2.0.9:debug
84189ebc
LC
290
291If SPEC does not specify a version number, return the preferred newest
292version; if SPEC does not specify an output, return OUTPUT."
84189ebc
LC
293 (let-values (((name version sub-drv)
294 (package-specification->name+version+output spec output)))
fad155d4
ML
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)
69daee23 301 (leave (G_ "package `~a' lacks output `~a'~%")
fad155d4
ML
302 (package-full-name package)
303 sub-drv))))))
c08ea55e
LC
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)))