Commit | Line | Data |
---|---|---|
233e7676 | 1 | ;;; GNU Guix --- Functional package management for GNU |
6071122b | 2 | ;;; Copyright © 2012, 2013, 2014, 2015, 2016 Ludovic Courtès <ludo@gnu.org> |
3f00ff8b | 3 | ;;; Copyright © 2013, 2014, 2015 Mark H Weaver <mhw@netris.org> |
516e3b6f | 4 | ;;; Copyright © 2014 Eric Bavier <bavier@member.fsf.org> |
29a7c98a | 5 | ;;; Copyright © 2014 Ian Denhardt <ian@zenhack.net> |
1b846da8 | 6 | ;;; Copyright © 2016 Mathieu Lirzin <mthl@gnu.org> |
bbd00d20 | 7 | ;;; Copyright © 2015 David Thompson <davet@gnu.org> |
e3deeebb | 8 | ;;; |
233e7676 | 9 | ;;; This file is part of GNU Guix. |
e3deeebb | 10 | ;;; |
233e7676 | 11 | ;;; GNU Guix is free software; you can redistribute it and/or modify it |
e3deeebb LC |
12 | ;;; under the terms of the GNU General Public License as published by |
13 | ;;; the Free Software Foundation; either version 3 of the License, or (at | |
14 | ;;; your option) any later version. | |
15 | ;;; | |
233e7676 | 16 | ;;; GNU Guix is distributed in the hope that it will be useful, but |
e3deeebb LC |
17 | ;;; WITHOUT ANY WARRANTY; without even the implied warranty of |
18 | ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
19 | ;;; GNU General Public License for more details. | |
20 | ;;; | |
21 | ;;; You should have received a copy of the GNU General Public License | |
233e7676 | 22 | ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>. |
e3deeebb LC |
23 | |
24 | (define-module (guix utils) | |
00e219d1 | 25 | #:use-module (guix config) |
38b3122a | 26 | #:use-module (srfi srfi-1) |
72d86963 | 27 | #:use-module (srfi srfi-9) |
01ac19dc | 28 | #:use-module (srfi srfi-11) |
38b3122a | 29 | #:use-module (srfi srfi-26) |
de4c3f26 | 30 | #:use-module (srfi srfi-39) |
e3deeebb LC |
31 | #:use-module (srfi srfi-60) |
32 | #:use-module (rnrs bytevectors) | |
c8be6f0d FB |
33 | #:use-module (rnrs io ports) |
34 | #:use-module ((rnrs bytevectors) #:select (bytevector-u8-set!)) | |
958dd3ce | 35 | #:use-module (guix combinators) |
1b846da8 | 36 | #:use-module ((guix build utils) #:select (dump-port)) |
1752a17a | 37 | #:use-module ((guix build syscalls) #:select (mkdtemp! fdatasync)) |
c8369cac | 38 | #:use-module (ice-9 vlist) |
38b3122a | 39 | #:use-module (ice-9 format) |
de4c3f26 LC |
40 | #:autoload (ice-9 popen) (open-pipe*) |
41 | #:autoload (ice-9 rdelim) (read-line) | |
98090557 | 42 | #:use-module (ice-9 regex) |
72d86963 | 43 | #:use-module (ice-9 match) |
8ef3401f | 44 | #:use-module (ice-9 format) |
50a3d594 | 45 | #:use-module ((ice-9 iconv) #:select (bytevector->string)) |
2cd5c038 | 46 | #:use-module (system foreign) |
19e1d5f7 | 47 | #:re-export (memoize) ; for backwards compatibility |
ddc29a78 | 48 | #:export (bytevector->base16-string |
6d800a80 | 49 | base16-string->bytevector |
de4c3f26 | 50 | |
5e110382 | 51 | strip-keyword-arguments |
0af2c24e LC |
52 | default-keyword-arguments |
53 | substitute-keyword-arguments | |
6071122b | 54 | ensure-keyword-arguments |
ff352cfb | 55 | |
07c8a98c LC |
56 | current-source-directory |
57 | ||
64fc89b6 | 58 | <location> |
ff352cfb LC |
59 | location |
60 | location? | |
61 | location-file | |
62 | location-line | |
63 | location-column | |
64 | source-properties->location | |
f7df1e3e | 65 | location->source-properties |
ff352cfb | 66 | |
8eb80484 | 67 | nix-system->gnu-triplet |
98090557 | 68 | gnu-triplet->nix-system |
9b48fb88 | 69 | %current-system |
9c1edabd | 70 | %current-target-system |
1b846da8 | 71 | package-name->name+version |
0d1e6ce4 MW |
72 | version-compare |
73 | version>? | |
cde1e967 | 74 | version>=? |
29a7c98a ID |
75 | version-prefix |
76 | version-major+minor | |
7db3ff4a | 77 | guile-version>? |
56b943de | 78 | string-replace-substring |
16eb115e | 79 | arguments-from-environment-variable |
0fdd3bea | 80 | file-extension |
ac10e0e1 | 81 | file-sans-extension |
089b1678 | 82 | compressed-file? |
3bb168b0 | 83 | switch-symlinks |
861693f3 | 84 | call-with-temporary-output-file |
db6e5e2b | 85 | call-with-temporary-directory |
04d4c8a4 | 86 | with-atomic-file-output |
739ab68b | 87 | cache-directory |
d50cb56d | 88 | readlink* |
50a3d594 | 89 | edit-expression |
7a8024a3 LC |
90 | |
91 | filtered-port | |
92 | compressed-port | |
80dea563 | 93 | decompressed-port |
01ac19dc LC |
94 | call-with-decompressed-port |
95 | compressed-output-port | |
c8be6f0d FB |
96 | call-with-compressed-output-port |
97 | canonical-newline-port)) | |
e3deeebb | 98 | |
38b3122a | 99 | \f |
38b3122a LC |
100 | ;;; |
101 | ;;; Base 16. | |
102 | ;;; | |
103 | ||
104 | (define (bytevector->base16-string bv) | |
105 | "Return the hexadecimal representation of BV's contents." | |
106 | (define len | |
107 | (bytevector-length bv)) | |
108 | ||
109 | (let-syntax ((base16-chars (lambda (s) | |
110 | (syntax-case s () | |
111 | (_ | |
112 | (let ((v (list->vector | |
113 | (unfold (cut > <> 255) | |
114 | (lambda (n) | |
115 | (format #f "~2,'0x" n)) | |
116 | 1+ | |
117 | 0)))) | |
118 | v)))))) | |
119 | (define chars base16-chars) | |
998fb8fa | 120 | (let loop ((i len) |
38b3122a | 121 | (r '())) |
998fb8fa LC |
122 | (if (zero? i) |
123 | (string-concatenate r) | |
124 | (let ((i (- i 1))) | |
125 | (loop i | |
126 | (cons (vector-ref chars (bytevector-u8-ref bv i)) r))))))) | |
38b3122a | 127 | |
6d800a80 LC |
128 | (define base16-string->bytevector |
129 | (let ((chars->value (fold (lambda (i r) | |
130 | (vhash-consv (string-ref (number->string i 16) | |
131 | 0) | |
132 | i r)) | |
133 | vlist-null | |
134 | (iota 16)))) | |
135 | (lambda (s) | |
136 | "Return the bytevector whose hexadecimal representation is string S." | |
137 | (define bv | |
138 | (make-bytevector (quotient (string-length s) 2) 0)) | |
139 | ||
140 | (string-fold (lambda (chr i) | |
141 | (let ((j (quotient i 2)) | |
142 | (v (and=> (vhash-assv chr chars->value) cdr))) | |
143 | (if v | |
144 | (if (zero? (logand i 1)) | |
145 | (bytevector-u8-set! bv j | |
146 | (arithmetic-shift v 4)) | |
147 | (let ((w (bytevector-u8-ref bv j))) | |
148 | (bytevector-u8-set! bv j (logior v w)))) | |
149 | (error "invalid hexadecimal character" chr))) | |
150 | (+ i 1)) | |
151 | 0 | |
152 | s) | |
153 | bv))) | |
154 | ||
de4c3f26 LC |
155 | |
156 | \f | |
e0fbbc88 LC |
157 | ;;; |
158 | ;;; Filtering & pipes. | |
159 | ;;; | |
160 | ||
161 | (define (filtered-port command input) | |
162 | "Return an input port where data drained from INPUT is filtered through | |
163 | COMMAND (a list). In addition, return a list of PIDs that the caller must | |
101d9f3f LC |
164 | wait. When INPUT is a file port, it must be unbuffered; otherwise, any |
165 | buffered data is lost." | |
e0fbbc88 | 166 | (let loop ((input input) |
443eb4e9 | 167 | (pids '())) |
e0fbbc88 LC |
168 | (if (file-port? input) |
169 | (match (pipe) | |
170 | ((in . out) | |
171 | (match (primitive-fork) | |
172 | (0 | |
443eb4e9 LC |
173 | (dynamic-wind |
174 | (const #f) | |
175 | (lambda () | |
176 | (close-port in) | |
177 | (close-port (current-input-port)) | |
178 | (dup2 (fileno input) 0) | |
179 | (close-port (current-output-port)) | |
180 | (dup2 (fileno out) 1) | |
181 | (catch 'system-error | |
182 | (lambda () | |
183 | (apply execl (car command) command)) | |
184 | (lambda args | |
185 | (format (current-error-port) | |
186 | "filtered-port: failed to execute '~{~a ~}': ~a~%" | |
187 | command (strerror (system-error-errno args)))))) | |
188 | (lambda () | |
189 | (primitive-_exit 1)))) | |
e0fbbc88 LC |
190 | (child |
191 | (close-port out) | |
192 | (values in (cons child pids)))))) | |
193 | ||
194 | ;; INPUT is not a file port, so fork just for the sake of tunneling it | |
195 | ;; through a file port. | |
196 | (match (pipe) | |
197 | ((in . out) | |
198 | (match (primitive-fork) | |
199 | (0 | |
200 | (dynamic-wind | |
201 | (const #t) | |
202 | (lambda () | |
203 | (close-port in) | |
204 | (dump-port input out)) | |
205 | (lambda () | |
206 | (false-if-exception (close out)) | |
443eb4e9 | 207 | (primitive-_exit 0)))) |
e0fbbc88 LC |
208 | (child |
209 | (close-port out) | |
210 | (loop in (cons child pids))))))))) | |
211 | ||
7a8024a3 LC |
212 | (define (decompressed-port compression input) |
213 | "Return an input port where INPUT is decompressed according to COMPRESSION, | |
214 | a symbol such as 'xz." | |
215 | (match compression | |
216 | ((or #f 'none) (values input '())) | |
217 | ('bzip2 (filtered-port `(,%bzip2 "-dc") input)) | |
218 | ('xz (filtered-port `(,%xz "-dc") input)) | |
219 | ('gzip (filtered-port `(,%gzip "-dc") input)) | |
220 | (else (error "unsupported compression scheme" compression)))) | |
221 | ||
222 | (define (compressed-port compression input) | |
223 | "Return an input port where INPUT is decompressed according to COMPRESSION, | |
224 | a symbol such as 'xz." | |
225 | (match compression | |
226 | ((or #f 'none) (values input '())) | |
227 | ('bzip2 (filtered-port `(,%bzip2 "-c") input)) | |
228 | ('xz (filtered-port `(,%xz "-c") input)) | |
229 | ('gzip (filtered-port `(,%gzip "-c") input)) | |
230 | (else (error "unsupported compression scheme" compression)))) | |
231 | ||
01ac19dc LC |
232 | (define (call-with-decompressed-port compression port proc) |
233 | "Call PROC with a wrapper around PORT, a file port, that decompresses data | |
30ce8012 | 234 | read from PORT according to COMPRESSION, a symbol such as 'xz." |
01ac19dc LC |
235 | (let-values (((decompressed pids) |
236 | (decompressed-port compression port))) | |
237 | (dynamic-wind | |
238 | (const #f) | |
239 | (lambda () | |
01ac19dc LC |
240 | (proc decompressed)) |
241 | (lambda () | |
242 | (close-port decompressed) | |
243 | (unless (every (compose zero? cdr waitpid) pids) | |
244 | (error "decompressed-port failure" pids)))))) | |
245 | ||
80dea563 LC |
246 | (define (filtered-output-port command output) |
247 | "Return an output port. Data written to that port is filtered through | |
248 | COMMAND and written to OUTPUT, an output file port. In addition, return a | |
249 | list of PIDs to wait for. OUTPUT must be unbuffered; otherwise, any buffered | |
250 | data is lost." | |
251 | (match (pipe) | |
252 | ((in . out) | |
253 | (match (primitive-fork) | |
254 | (0 | |
255 | (dynamic-wind | |
256 | (const #f) | |
257 | (lambda () | |
258 | (close-port out) | |
259 | (close-port (current-input-port)) | |
260 | (dup2 (fileno in) 0) | |
261 | (close-port (current-output-port)) | |
262 | (dup2 (fileno output) 1) | |
263 | (catch 'system-error | |
264 | (lambda () | |
265 | (apply execl (car command) command)) | |
266 | (lambda args | |
267 | (format (current-error-port) | |
268 | "filtered-output-port: failed to execute '~{~a ~}': ~a~%" | |
269 | command (strerror (system-error-errno args)))))) | |
270 | (lambda () | |
271 | (primitive-_exit 1)))) | |
272 | (child | |
273 | (close-port in) | |
274 | (values out (list child))))))) | |
275 | ||
62d2b571 LC |
276 | (define* (compressed-output-port compression output |
277 | #:key (options '())) | |
80dea563 LC |
278 | "Return an output port whose input is compressed according to COMPRESSION, |
279 | a symbol such as 'xz, and then written to OUTPUT. In addition return a list | |
62d2b571 LC |
280 | of PIDs to wait for. OPTIONS is a list of strings passed to the compression |
281 | program--e.g., '(\"--fast\")." | |
80dea563 LC |
282 | (match compression |
283 | ((or #f 'none) (values output '())) | |
62d2b571 LC |
284 | ('bzip2 (filtered-output-port `(,%bzip2 "-c" ,@options) output)) |
285 | ('xz (filtered-output-port `(,%xz "-c" ,@options) output)) | |
286 | ('gzip (filtered-output-port `(,%gzip "-c" ,@options) output)) | |
80dea563 LC |
287 | (else (error "unsupported compression scheme" compression)))) |
288 | ||
62d2b571 LC |
289 | (define* (call-with-compressed-output-port compression port proc |
290 | #:key (options '())) | |
01ac19dc | 291 | "Call PROC with a wrapper around PORT, a file port, that compresses data |
62d2b571 LC |
292 | that goes to PORT according to COMPRESSION, a symbol such as 'xz. OPTIONS is |
293 | a list of command-line arguments passed to the compression program." | |
01ac19dc | 294 | (let-values (((compressed pids) |
62d2b571 LC |
295 | (compressed-output-port compression port |
296 | #:options options))) | |
01ac19dc LC |
297 | (dynamic-wind |
298 | (const #f) | |
299 | (lambda () | |
01ac19dc LC |
300 | (proc compressed)) |
301 | (lambda () | |
302 | (close-port compressed) | |
303 | (unless (every (compose zero? cdr waitpid) pids) | |
304 | (error "compressed-output-port failure" pids)))))) | |
305 | ||
50a3d594 SB |
306 | (define* (edit-expression source-properties proc #:key (encoding "UTF-8")) |
307 | "Edit the expression specified by SOURCE-PROPERTIES using PROC, which should | |
308 | be a procedure that takes the original expression in string and returns a new | |
309 | one. ENCODING will be used to interpret all port I/O, it default to UTF-8. | |
310 | This procedure returns #t on success." | |
311 | (with-fluids ((%default-port-encoding encoding)) | |
312 | (let* ((file (assq-ref source-properties 'filename)) | |
313 | (line (assq-ref source-properties 'line)) | |
314 | (column (assq-ref source-properties 'column)) | |
315 | (in (open-input-file file)) | |
316 | ;; The start byte position of the expression. | |
317 | (start (begin (while (not (and (= line (port-line in)) | |
318 | (= column (port-column in)))) | |
319 | (when (eof-object? (read-char in)) | |
320 | (error (format #f "~a: end of file~%" in)))) | |
321 | (ftell in))) | |
322 | ;; The end byte position of the expression. | |
323 | (end (begin (read in) (ftell in)))) | |
324 | (seek in 0 SEEK_SET) ; read from the beginning of the file. | |
325 | (let* ((pre-bv (get-bytevector-n in start)) | |
326 | ;; The expression in string form. | |
327 | (str (bytevector->string | |
328 | (get-bytevector-n in (- end start)) | |
329 | (port-encoding in))) | |
330 | (post-bv (get-bytevector-all in)) | |
331 | (str* (proc str))) | |
332 | ;; Verify the edited expression is still a scheme expression. | |
333 | (call-with-input-string str* read) | |
334 | ;; Update the file with edited expression. | |
335 | (with-atomic-file-output file | |
336 | (lambda (out) | |
337 | (put-bytevector out pre-bv) | |
338 | (display str* out) | |
339 | ;; post-bv maybe the end-of-file object. | |
340 | (when (not (eof-object? post-bv)) | |
341 | (put-bytevector out post-bv)) | |
342 | #t)))))) | |
343 | ||
e0fbbc88 | 344 | \f |
de4c3f26 | 345 | ;;; |
958dd3ce | 346 | ;;; Keyword arguments. |
de4c3f26 LC |
347 | ;;; |
348 | ||
5e110382 LC |
349 | (define (strip-keyword-arguments keywords args) |
350 | "Remove all of the keyword arguments listed in KEYWORDS from ARGS." | |
351 | (let loop ((args args) | |
352 | (result '())) | |
353 | (match args | |
354 | (() | |
355 | (reverse result)) | |
356 | (((? keyword? kw) arg . rest) | |
357 | (loop rest | |
358 | (if (memq kw keywords) | |
359 | result | |
360 | (cons* arg kw result)))) | |
361 | ((head . tail) | |
362 | (loop tail (cons head result)))))) | |
363 | ||
0af2c24e LC |
364 | (define (default-keyword-arguments args defaults) |
365 | "Return ARGS augmented with any keyword/value from DEFAULTS for | |
366 | keywords not already present in ARGS." | |
367 | (let loop ((defaults defaults) | |
368 | (args args)) | |
369 | (match defaults | |
370 | ((kw value rest ...) | |
371 | (loop rest | |
347df601 | 372 | (if (memq kw args) |
0af2c24e LC |
373 | args |
374 | (cons* kw value args)))) | |
375 | (() | |
376 | args)))) | |
377 | ||
378 | (define-syntax substitute-keyword-arguments | |
379 | (syntax-rules () | |
380 | "Return a new list of arguments where the value for keyword arg KW is | |
381 | replaced by EXP. EXP is evaluated in a context where VAR is boud to the | |
382 | previous value of the keyword argument." | |
383 | ((_ original-args ((kw var) exp) ...) | |
384 | (let loop ((args original-args) | |
385 | (before '())) | |
386 | (match args | |
387 | ((kw var rest (... ...)) | |
388 | (loop rest (cons* exp kw before))) | |
389 | ... | |
390 | ((x rest (... ...)) | |
391 | (loop rest (cons x before))) | |
392 | (() | |
393 | (reverse before))))))) | |
394 | ||
6071122b LC |
395 | (define (delkw kw lst) |
396 | "Remove KW and its associated value from LST, a keyword/value list such | |
397 | as '(#:foo 1 #:bar 2)." | |
398 | (let loop ((lst lst) | |
399 | (result '())) | |
400 | (match lst | |
401 | (() | |
402 | (reverse result)) | |
403 | ((kw? value rest ...) | |
404 | (if (eq? kw? kw) | |
405 | (append (reverse result) rest) | |
406 | (loop rest (cons* value kw? result))))))) | |
407 | ||
408 | (define (ensure-keyword-arguments args kw/values) | |
409 | "Force the keywords arguments KW/VALUES in the keyword argument list ARGS. | |
410 | For instance: | |
411 | ||
412 | (ensure-keyword-arguments '(#:foo 2) '(#:foo 2)) | |
413 | => (#:foo 2) | |
414 | ||
415 | (ensure-keyword-arguments '(#:foo 2) '(#:bar 3)) | |
416 | => (#:foo 2 #:bar 3) | |
417 | ||
418 | (ensure-keyword-arguments '(#:foo 2) '(#:bar 3 #:foo 42)) | |
419 | => (#:foo 42 #:bar 3) | |
420 | " | |
421 | (let loop ((args args) | |
422 | (kw/values kw/values) | |
423 | (result '())) | |
424 | (match args | |
425 | (() | |
426 | (append (reverse result) kw/values)) | |
427 | ((kw value rest ...) | |
428 | (match (memq kw kw/values) | |
429 | ((_ value . _) | |
430 | (loop rest (delkw kw kw/values) (cons* value kw result))) | |
431 | (#f | |
432 | (loop rest kw/values (cons* value kw result)))))))) | |
433 | ||
958dd3ce LC |
434 | \f |
435 | ;;; | |
436 | ;;; System strings. | |
437 | ;;; | |
438 | ||
8eb80484 MW |
439 | (define* (nix-system->gnu-triplet |
440 | #:optional (system (%current-system)) (vendor "unknown")) | |
441 | "Return a guess of the GNU triplet corresponding to Nix system | |
442 | identifier SYSTEM." | |
3f00ff8b MW |
443 | (match system |
444 | ("armhf-linux" | |
445 | (string-append "arm-" vendor "-linux-gnueabihf")) | |
446 | (_ | |
447 | (let* ((dash (string-index system #\-)) | |
448 | (arch (substring system 0 dash)) | |
449 | (os (substring system (+ 1 dash)))) | |
450 | (string-append arch | |
451 | "-" vendor "-" | |
452 | (if (string=? os "linux") | |
453 | "linux-gnu" | |
454 | os)))))) | |
8eb80484 | 455 | |
98090557 LC |
456 | (define (gnu-triplet->nix-system triplet) |
457 | "Return the Nix system type corresponding to TRIPLET, a GNU triplet as | |
458 | returned by `config.guess'." | |
459 | (let ((triplet (cond ((string-match "^i[345]86-(.*)$" triplet) | |
460 | => | |
461 | (lambda (m) | |
462 | (string-append "i686-" (match:substring m 1)))) | |
463 | (else triplet)))) | |
3f00ff8b MW |
464 | (cond ((string-match "^arm[^-]*-([^-]+-)?linux-gnueabihf" triplet) |
465 | "armhf-linux") | |
466 | ((string-match "^([^-]+)-([^-]+-)?linux-gnu.*" triplet) | |
98090557 LC |
467 | => |
468 | (lambda (m) | |
469 | ;; Nix omits `-gnu' for GNU/Linux. | |
470 | (string-append (match:substring m 1) "-linux"))) | |
471 | ((string-match "^([^-]+)-([^-]+-)?([[:alpha:]]+)([0-9]+\\.?)*$" triplet) | |
472 | => | |
473 | (lambda (m) | |
474 | ;; Nix strip the version number from names such as `gnu0.3', | |
475 | ;; `darwin10.2.0', etc., and always strips the vendor part. | |
476 | (string-append (match:substring m 1) "-" | |
477 | (match:substring m 3)))) | |
478 | (else triplet)))) | |
479 | ||
480 | (define %current-system | |
481 | ;; System type as expected by Nix, usually ARCHITECTURE-KERNEL. | |
d8eea3d2 LC |
482 | ;; By default, this is equal to (gnu-triplet->nix-system %host-type). |
483 | (make-parameter %system)) | |
ff352cfb | 484 | |
9c1edabd LC |
485 | (define %current-target-system |
486 | ;; Either #f or a GNU triplet representing the target system we are | |
487 | ;; cross-building to. | |
488 | (make-parameter #f)) | |
489 | ||
1b846da8 ML |
490 | (define (package-name->name+version spec) |
491 | "Given SPEC, a package name like \"foo@0.9.1b\", return two values: \"foo\" | |
492 | and \"0.9.1b\". When the version part is unavailable, SPEC and #f are | |
493 | returned. Both parts must not contain any '@'." | |
494 | (match (string-rindex spec #\@) | |
495 | (#f (values spec #f)) | |
496 | (idx (values (substring spec 0 idx) | |
497 | (substring spec (1+ idx)))))) | |
498 | ||
0d1e6ce4 MW |
499 | (define version-compare |
500 | (let ((strverscmp | |
501 | (let ((sym (or (dynamic-func "strverscmp" (dynamic-link)) | |
502 | (error "could not find `strverscmp' (from GNU libc)")))) | |
503 | (pointer->procedure int sym (list '* '*))))) | |
504 | (lambda (a b) | |
505 | "Return '> when A denotes a newer version than B, | |
506 | '< when A denotes a older version than B, | |
507 | or '= when they denote equal versions." | |
508 | (let ((result (strverscmp (string->pointer a) (string->pointer b)))) | |
509 | (cond ((positive? result) '>) | |
510 | ((negative? result) '<) | |
511 | (else '=)))))) | |
512 | ||
29a7c98a ID |
513 | (define (version-prefix version-string num-parts) |
514 | "Truncate version-string to the first num-parts components of the version. | |
515 | For example, (version-prefix \"2.1.47.4.23\" 3) returns \"2.1.47\"" | |
516 | (string-join (take (string-split version-string #\.) num-parts) ".")) | |
517 | ||
518 | ||
519 | (define (version-major+minor version-string) | |
520 | "Return \"<major>.<minor>\", where major and minor are the major and | |
521 | minor version numbers from version-string." | |
522 | (version-prefix version-string 2)) | |
523 | ||
0d1e6ce4 | 524 | (define (version>? a b) |
cde1e967 | 525 | "Return #t when A denotes a version strictly newer than B." |
0d1e6ce4 MW |
526 | (eq? '> (version-compare a b))) |
527 | ||
cde1e967 LC |
528 | (define (version>=? a b) |
529 | "Return #t when A denotes a version newer or equal to B." | |
530 | (case (version-compare a b) | |
531 | ((> =) #t) | |
532 | (else #f))) | |
533 | ||
7db3ff4a LC |
534 | (define (guile-version>? str) |
535 | "Return #t if the running Guile version is greater than STR." | |
536 | ;; Note: Using (version>? (version) "2.0.5") or similar doesn't work, | |
537 | ;; because the result of (version) can have a prefix, like "2.0.5-deb1". | |
538 | (version>? (string-append (major-version) "." | |
539 | (minor-version) "." | |
540 | (micro-version)) | |
541 | str)) | |
542 | ||
0fdd3bea LC |
543 | (define (file-extension file) |
544 | "Return the extension of FILE or #f if there is none." | |
545 | (let ((dot (string-rindex file #\.))) | |
546 | (and dot (substring file (+ 1 dot) (string-length file))))) | |
547 | ||
ac10e0e1 LC |
548 | (define (file-sans-extension file) |
549 | "Return the substring of FILE without its extension, if any." | |
550 | (let ((dot (string-rindex file #\.))) | |
551 | (if dot | |
552 | (substring file 0 dot) | |
553 | file))) | |
554 | ||
089b1678 LC |
555 | (define (compressed-file? file) |
556 | "Return true if FILE denotes a compressed file." | |
557 | (->bool (member (file-extension file) | |
558 | '("gz" "bz2" "xz" "lz" "tgz" "tbz2" "zip")))) | |
559 | ||
3bb168b0 LC |
560 | (define (switch-symlinks link target) |
561 | "Atomically switch LINK, a symbolic link, to point to TARGET. Works | |
562 | both when LINK already exists and when it does not." | |
563 | (let ((pivot (string-append link ".new"))) | |
564 | (symlink target pivot) | |
565 | (rename-file pivot link))) | |
566 | ||
56b943de LC |
567 | (define* (string-replace-substring str substr replacement |
568 | #:optional | |
569 | (start 0) | |
570 | (end (string-length str))) | |
571 | "Replace all occurrences of SUBSTR in the START--END range of STR by | |
572 | REPLACEMENT." | |
573 | (match (string-length substr) | |
574 | (0 | |
575 | (error "string-replace-substring: empty substring")) | |
576 | (substr-length | |
577 | (let loop ((start start) | |
578 | (pieces (list (substring str 0 start)))) | |
579 | (match (string-contains str substr start end) | |
580 | (#f | |
581 | (string-concatenate-reverse | |
582 | (cons (substring str start) pieces))) | |
583 | (index | |
584 | (loop (+ index substr-length) | |
585 | (cons* replacement | |
586 | (substring str start index) | |
587 | pieces)))))))) | |
588 | ||
16eb115e DP |
589 | (define (arguments-from-environment-variable variable) |
590 | "Retrieve value of environment variable denoted by string VARIABLE in the | |
591 | form of a list of strings (`char-set:graphic' tokens) suitable for consumption | |
592 | by `args-fold', if VARIABLE is defined, otherwise return an empty list." | |
593 | (let ((env (getenv variable))) | |
594 | (if env | |
595 | (string-tokenize env char-set:graphic) | |
596 | '()))) | |
597 | ||
861693f3 LC |
598 | (define (call-with-temporary-output-file proc) |
599 | "Call PROC with a name of a temporary file and open output port to that | |
600 | file; close the file and delete it when leaving the dynamic extent of this | |
601 | call." | |
57db49cc LC |
602 | (let* ((directory (or (getenv "TMPDIR") "/tmp")) |
603 | (template (string-append directory "/guix-file.XXXXXX")) | |
604 | (out (mkstemp! template))) | |
861693f3 LC |
605 | (dynamic-wind |
606 | (lambda () | |
607 | #t) | |
608 | (lambda () | |
609 | (proc template out)) | |
610 | (lambda () | |
611 | (false-if-exception (close out)) | |
612 | (false-if-exception (delete-file template)))))) | |
04d4c8a4 | 613 | |
db6e5e2b DT |
614 | (define (call-with-temporary-directory proc) |
615 | "Call PROC with a name of a temporary directory; close the directory and | |
616 | delete it when leaving the dynamic extent of this call." | |
617 | (let* ((directory (or (getenv "TMPDIR") "/tmp")) | |
618 | (template (string-append directory "/guix-directory.XXXXXX")) | |
619 | (tmp-dir (mkdtemp! template))) | |
620 | (dynamic-wind | |
621 | (const #t) | |
622 | (lambda () | |
623 | (proc tmp-dir)) | |
624 | (lambda () | |
625 | (false-if-exception (rmdir tmp-dir)))))) | |
626 | ||
04d4c8a4 LC |
627 | (define (with-atomic-file-output file proc) |
628 | "Call PROC with an output port for the file that is going to replace FILE. | |
629 | Upon success, FILE is atomically replaced by what has been written to the | |
630 | output port, and PROC's result is returned." | |
631 | (let* ((template (string-append file ".XXXXXX")) | |
632 | (out (mkstemp! template))) | |
633 | (with-throw-handler #t | |
634 | (lambda () | |
635 | (let ((result (proc out))) | |
1752a17a LC |
636 | (fdatasync out) |
637 | (close-port out) | |
04d4c8a4 LC |
638 | (rename-file template file) |
639 | result)) | |
640 | (lambda (key . args) | |
c25637df LC |
641 | (false-if-exception (delete-file template)) |
642 | (close-port out))))) | |
861693f3 | 643 | |
739ab68b LC |
644 | (define (cache-directory) |
645 | "Return the cache directory for Guix, by default ~/.cache/guix." | |
934c5d5b LC |
646 | (string-append (or (getenv "XDG_CACHE_HOME") |
647 | (and=> (or (getenv "HOME") | |
648 | (passwd:dir (getpwuid (getuid)))) | |
649 | (cut string-append <> "/.cache"))) | |
650 | "/guix")) | |
739ab68b | 651 | |
d50cb56d LC |
652 | (define (readlink* file) |
653 | "Call 'readlink' until the result is not a symlink." | |
654 | (define %max-symlink-depth 50) | |
655 | ||
656 | (let loop ((file file) | |
657 | (depth 0)) | |
658 | (define (absolute target) | |
659 | (if (absolute-file-name? target) | |
660 | target | |
661 | (string-append (dirname file) "/" target))) | |
662 | ||
663 | (if (>= depth %max-symlink-depth) | |
664 | file | |
665 | (call-with-values | |
666 | (lambda () | |
667 | (catch 'system-error | |
668 | (lambda () | |
669 | (values #t (readlink file))) | |
670 | (lambda args | |
671 | (let ((errno (system-error-errno args))) | |
672 | (if (or (= errno EINVAL)) | |
673 | (values #f file) | |
674 | (apply throw args)))))) | |
675 | (lambda (success? target) | |
676 | (if success? | |
677 | (loop (absolute target) (+ depth 1)) | |
678 | file)))))) | |
c8be6f0d FB |
679 | |
680 | (define (canonical-newline-port port) | |
681 | "Return an input port that wraps PORT such that all newlines consist | |
682 | of a single carriage return." | |
683 | (define (get-position) | |
684 | (if (port-has-port-position? port) (port-position port) #f)) | |
685 | (define (set-position! position) | |
686 | (if (port-has-set-port-position!? port) | |
687 | (set-port-position! position port) | |
688 | #f)) | |
689 | (define (close) (close-port port)) | |
690 | (define (read! bv start n) | |
691 | (let loop ((count 0) | |
692 | (byte (get-u8 port))) | |
693 | (cond ((eof-object? byte) count) | |
694 | ((= count (- n 1)) | |
695 | (bytevector-u8-set! bv (+ start count) byte) | |
696 | n) | |
697 | ;; XXX: consume all LFs even if not followed by CR. | |
698 | ((eqv? byte (char->integer #\return)) (loop count (get-u8 port))) | |
699 | (else | |
700 | (bytevector-u8-set! bv (+ start count) byte) | |
701 | (loop (+ count 1) (get-u8 port)))))) | |
702 | (make-custom-binary-input-port "canonical-newline-port" | |
703 | read! | |
704 | get-position | |
705 | set-position! | |
706 | close)) | |
ff352cfb LC |
707 | \f |
708 | ;;; | |
709 | ;;; Source location. | |
710 | ;;; | |
711 | ||
cbbbb7be LC |
712 | (define (absolute-dirname file) |
713 | "Return the absolute name of the directory containing FILE, or #f upon | |
714 | failure." | |
715 | (match (search-path %load-path file) | |
716 | (#f #f) | |
717 | ((? string? file) | |
718 | ;; If there are relative names in %LOAD-PATH, FILE can be relative and | |
719 | ;; needs to be canonicalized. | |
720 | (if (string-prefix? "/" file) | |
721 | (dirname file) | |
722 | (canonicalize-path (dirname file)))))) | |
723 | ||
5dbae738 LC |
724 | (define-syntax current-source-directory |
725 | (lambda (s) | |
d4dd37fc LC |
726 | "Return the absolute name of the current directory, or #f if it could not |
727 | be determined." | |
5dbae738 LC |
728 | (syntax-case s () |
729 | ((_) | |
730 | (match (assq 'filename (syntax-source s)) | |
731 | (('filename . (? string? file-name)) | |
d4dd37fc | 732 | ;; If %FILE-PORT-NAME-CANONICALIZATION is 'relative, then FILE-NAME |
cbbbb7be LC |
733 | ;; can be relative. In that case, we try to find out at run time |
734 | ;; the absolute file name by looking at %LOAD-PATH; doing this at | |
735 | ;; run time rather than expansion time is necessary to allow files | |
736 | ;; to be moved on the file system. | |
a68d0f6f LC |
737 | (cond ((not file-name) |
738 | #f) ;raising an error would upset Geiser users | |
739 | ((string-prefix? "/" file-name) | |
740 | (dirname file-name)) | |
741 | (else | |
742 | #`(absolute-dirname #,file-name)))) | |
5dbae738 LC |
743 | (_ |
744 | #f)))))) | |
07c8a98c | 745 | |
ff352cfb LC |
746 | ;; A source location. |
747 | (define-record-type <location> | |
748 | (make-location file line column) | |
749 | location? | |
750 | (file location-file) ; file name | |
751 | (line location-line) ; 1-indexed line | |
752 | (column location-column)) ; 0-indexed column | |
753 | ||
754 | (define location | |
755 | (memoize | |
756 | (lambda (file line column) | |
757 | "Return the <location> object for the given FILE, LINE, and COLUMN." | |
758 | (and line column file | |
759 | (make-location file line column))))) | |
760 | ||
761 | (define (source-properties->location loc) | |
762 | "Return a location object based on the info in LOC, an alist as returned | |
763 | by Guile's `source-properties', `frame-source', `current-source-location', | |
764 | etc." | |
765 | (let ((file (assq-ref loc 'filename)) | |
766 | (line (assq-ref loc 'line)) | |
767 | (col (assq-ref loc 'column))) | |
5fe21fbe LC |
768 | ;; In accordance with the GCS, start line and column numbers at 1. Note |
769 | ;; that unlike LINE and `port-column', COL is actually 1-indexed here... | |
770 | (location file (and line (+ line 1)) col))) | |
f7df1e3e SB |
771 | |
772 | (define (location->source-properties loc) | |
773 | "Return the source property association list based on the info in LOC, | |
774 | a location object." | |
775 | `((line . ,(and=> (location-line loc) 1-)) | |
776 | (column . ,(location-column loc)) | |
777 | (filename . ,(location-file loc)))) |