Commit | Line | Data |
---|---|---|
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))) | |
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 | |
97 | FILE-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 |
102 | found." | |
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 |
109 | for system '~a'") |
110 | file-name system))))))) | |
ac5aa288 | 111 | |
84836a57 LC |
112 | (define %distro-root-directory |
113 | ;; Absolute file name of the module hierarchy. | |
114 | (dirname (search-path %load-path "guix.scm"))) | |
6b1891b0 | 115 | |
c107b541 LC |
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. | |
8689901f LC |
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")))))) | |
c107b541 | 132 | |
ee06af5b LC |
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 | ||
5c5ae46c LC |
143 | (define* (fold-packages proc init |
144 | #:optional | |
145 | (modules (all-modules (%package-module-path)))) | |
146 | "Call (PROC PACKAGE RESULT) for each available package defined in one of | |
147 | MODULES, using INIT as the initial value of RESULT. It is guaranteed to never | |
148 | traverse the same package twice." | |
cd903ef7 LC |
149 | (fold-module-public-variables (lambda (object result) |
150 | (if (and (package? object) | |
151 | (not (hidden-package? object))) | |
152 | (proc object result) | |
153 | result)) | |
154 | init | |
5c5ae46c | 155 | modules)) |
ba326ce4 | 156 | |
9ffc1c00 LC |
157 | (define find-packages-by-name |
158 | (let ((packages (delay | |
159 | (fold-packages (lambda (p r) | |
160 | (vhash-cons (package-name p) p r)) | |
724311a2 LC |
161 | vlist-null))) |
162 | (version>? (lambda (p1 p2) | |
163 | (version>? (package-version p1) (package-version p2))))) | |
9ffc1c00 LC |
164 | (lambda* (name #:optional version) |
165 | "Return the list of packages with the given NAME. If VERSION is not #f, | |
724311a2 LC |
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>?))) | |
9ffc1c00 LC |
170 | (if version |
171 | (filter (lambda (package) | |
724311a2 | 172 | (string-prefix? version (package-version package))) |
9ffc1c00 LC |
173 | matching) |
174 | matching))))) | |
dc5669cd | 175 | |
3f26bfc1 | 176 | (define find-newest-available-packages |
55b2d921 LC |
177 | (mlambda () |
178 | "Return a vhash keyed by package names, and with | |
dc5669cd MW |
179 | associated values of the form |
180 | ||
181 | (newest-version newest-package ...) | |
182 | ||
183 | where the preferred package is listed first." | |
184 | ||
55b2d921 LC |
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))) | |
3f26bfc1 LC |
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 '())))) | |
7d193ec3 EB |
208 | |
209 | \f | |
4ea44419 AK |
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 | ||
fad155d4 ML |
231 | \f |
232 | ;;; | |
233 | ;;; Package specification. | |
234 | ;;; | |
235 | ||
e30c2be1 | 236 | (define* (%find-package spec name version) |
fad155d4 ML |
237 | (match (find-best-packages-by-name name version) |
238 | ((pkg . pkg*) | |
239 | (unless (null? pkg*) | |
69daee23 LC |
240 | (warning (G_ "ambiguous package specification `~a'~%") spec) |
241 | (warning (G_ "choosing ~a@~a from ~a~%") | |
d75e8f36 | 242 | (package-name pkg) (package-version pkg) |
fad155d4 | 243 | (location->string (package-location pkg)))) |
01afdab8 LC |
244 | (match (package-superseded pkg) |
245 | ((? package? new) | |
69daee23 | 246 | (info (G_ "package '~a' has been superseded by '~a'~%") |
01afdab8 LC |
247 | (package-name pkg) (package-name new)) |
248 | new) | |
249 | (#f | |
250 | pkg))) | |
e465d9e1 | 251 | (x |
fad155d4 | 252 | (if version |
69daee23 LC |
253 | (leave (G_ "~A: package not found for version ~a~%") name version) |
254 | (leave (G_ "~A: unknown package~%") name))))) | |
fad155d4 | 255 | |
5e3b388b CR |
256 | (define (specification->package spec) |
257 | "Return a package matching SPEC. SPEC may be a package name, or a package | |
1b846da8 | 258 | name followed by an at-sign and a version number. If the version number is not |
5e3b388b | 259 | present, return the preferred newest version." |
fad155d4 ML |
260 | (let-values (((name version) (package-name->name+version spec))) |
261 | (%find-package spec name version))) | |
84189ebc LC |
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 | |
1b846da8 | 268 | guile@2.0.9 |
84189ebc | 269 | guile:debug |
1b846da8 | 270 | guile@2.0.9:debug |
84189ebc LC |
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." | |
84189ebc LC |
274 | (let-values (((name version sub-drv) |
275 | (package-specification->name+version+output spec output))) | |
fad155d4 ML |
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) | |
69daee23 | 282 | (leave (G_ "package `~a' lacks output `~a'~%") |
fad155d4 ML |
283 | (package-full-name package) |
284 | sub-drv)))))) | |
c08ea55e LC |
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))) |