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