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