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