2013-01-06 00:47:50 +01:00
|
|
|
|
;;; GNU Guix --- Functional package management for GNU
|
2016-01-06 22:42:09 +01:00
|
|
|
|
;;; Copyright © 2012, 2013, 2014, 2015, 2016 Ludovic Courtès <ludo@gnu.org>
|
2015-01-07 21:55:23 +01:00
|
|
|
|
;;; Copyright © 2013, 2014, 2015 Mark H Weaver <mhw@netris.org>
|
2014-07-20 18:22:46 +02:00
|
|
|
|
;;; Copyright © 2014 Eric Bavier <bavier@member.fsf.org>
|
2014-10-19 04:03:42 +02:00
|
|
|
|
;;; Copyright © 2014 Ian Denhardt <ian@zenhack.net>
|
2016-02-28 23:11:36 +01:00
|
|
|
|
;;; Copyright © 2016 Mathieu Lirzin <mthl@gnu.org>
|
2015-10-09 18:10:47 +02:00
|
|
|
|
;;; Copyright © 2015 David Thompson <davet@gnu.org>
|
2012-06-01 23:08:32 +02:00
|
|
|
|
;;;
|
2013-01-06 00:47:50 +01:00
|
|
|
|
;;; This file is part of GNU Guix.
|
2012-06-01 23:08:32 +02:00
|
|
|
|
;;;
|
2013-01-06 00:47:50 +01:00
|
|
|
|
;;; GNU Guix is free software; you can redistribute it and/or modify it
|
2012-06-01 23:08:32 +02:00
|
|
|
|
;;; under the terms of the GNU General Public License as published by
|
|
|
|
|
;;; the Free Software Foundation; either version 3 of the License, or (at
|
|
|
|
|
;;; your option) any later version.
|
|
|
|
|
;;;
|
2013-01-06 00:47:50 +01:00
|
|
|
|
;;; GNU Guix is distributed in the hope that it will be useful, but
|
2012-06-01 23:08:32 +02:00
|
|
|
|
;;; WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
;;; GNU General Public License for more details.
|
|
|
|
|
;;;
|
|
|
|
|
;;; You should have received a copy of the GNU General Public License
|
2013-01-06 00:47:50 +01:00
|
|
|
|
;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
|
2012-06-01 23:08:32 +02:00
|
|
|
|
|
|
|
|
|
(define-module (guix utils)
|
2012-11-03 21:07:52 +01:00
|
|
|
|
#:use-module (guix config)
|
2012-06-01 23:29:55 +02:00
|
|
|
|
#:use-module (srfi srfi-1)
|
2012-06-27 23:40:12 +02:00
|
|
|
|
#:use-module (srfi srfi-9)
|
2014-03-24 22:15:29 +01:00
|
|
|
|
#:use-module (srfi srfi-11)
|
2012-06-01 23:29:55 +02:00
|
|
|
|
#:use-module (srfi srfi-26)
|
2012-06-07 23:15:00 +02:00
|
|
|
|
#:use-module (srfi srfi-39)
|
2012-06-01 23:08:32 +02:00
|
|
|
|
#:use-module (srfi srfi-60)
|
|
|
|
|
#:use-module (rnrs bytevectors)
|
2015-11-14 15:00:36 +01:00
|
|
|
|
#:use-module (rnrs io ports)
|
|
|
|
|
#:use-module ((rnrs bytevectors) #:select (bytevector-u8-set!))
|
2016-02-28 23:11:36 +01:00
|
|
|
|
#:use-module ((guix build utils) #:select (dump-port))
|
2015-06-25 03:16:11 +02:00
|
|
|
|
#:use-module ((guix build syscalls) #:select (errno mkdtemp!))
|
2012-06-09 15:25:51 +02:00
|
|
|
|
#:use-module (ice-9 vlist)
|
2012-06-01 23:29:55 +02:00
|
|
|
|
#:use-module (ice-9 format)
|
2012-06-07 23:15:00 +02:00
|
|
|
|
#:autoload (ice-9 popen) (open-pipe*)
|
|
|
|
|
#:autoload (ice-9 rdelim) (read-line)
|
2012-06-11 21:50:17 +02:00
|
|
|
|
#:use-module (ice-9 regex)
|
2012-06-27 23:40:12 +02:00
|
|
|
|
#:use-module (ice-9 match)
|
2012-07-01 17:32:03 +02:00
|
|
|
|
#:use-module (ice-9 format)
|
2014-01-10 23:27:39 +01:00
|
|
|
|
#:use-module (system foreign)
|
Move base32 code to (guix base32).
* guix/utils.scm (bytevector-quintet-ref, bytevector-quintet-ref-right,
bytevector-quintet-length, bytevector-quintet-fold,
bytevector-quintet-fold-right, make-bytevector->base32-string,
%nix-base32-chars, %rfc4648-base32-chars, bytevector->base32-string,
bytevector->nix-base32-string, bytevector-quintet-set!,
bytevector-quintet-set-right!, base32-string-unfold,
base32-string-unfold-right, make-base32-string->bytevector,
base32-string->bytevector, nix-base32-string->bytevector): Move to...
* guix/base32.scm: ... here. New file.
* tests/utils.scm (%nix-hash, "bytevector->base32-string",
"base32-string->bytevector", "nix-base32-string->bytevector", "sha256
& bytevector->base32-string"): Move to...
* tests/base32.scm: ... here. New file
* guix-download.in, guix/derivations.scm, guix/packages.scm,
guix/snix.scm, tests/builders.scm, tests/derivations.scm: Adjust
accordingly.
* guix.scm (%public-modules): Add `base32'.
2012-11-11 22:33:28 +01:00
|
|
|
|
#:export (bytevector->base16-string
|
2012-06-09 16:34:18 +02:00
|
|
|
|
base16-string->bytevector
|
2012-06-07 23:15:00 +02:00
|
|
|
|
|
2012-06-28 22:38:09 +02:00
|
|
|
|
compile-time-value
|
2014-01-10 23:27:39 +01:00
|
|
|
|
fcntl-flock
|
2012-06-11 21:50:17 +02:00
|
|
|
|
memoize
|
2014-10-01 10:19:14 +02:00
|
|
|
|
strip-keyword-arguments
|
2012-11-05 23:46:55 +01:00
|
|
|
|
default-keyword-arguments
|
|
|
|
|
substitute-keyword-arguments
|
2016-01-06 22:42:09 +01:00
|
|
|
|
ensure-keyword-arguments
|
2012-09-01 19:21:06 +02:00
|
|
|
|
|
2012-11-19 23:02:59 +01:00
|
|
|
|
<location>
|
2012-09-01 19:21:06 +02:00
|
|
|
|
location
|
|
|
|
|
location?
|
|
|
|
|
location-file
|
|
|
|
|
location-line
|
|
|
|
|
location-column
|
|
|
|
|
source-properties->location
|
|
|
|
|
|
2014-12-31 09:47:40 +01:00
|
|
|
|
nix-system->gnu-triplet
|
2012-06-11 21:50:17 +02:00
|
|
|
|
gnu-triplet->nix-system
|
2012-11-04 01:29:18 +01:00
|
|
|
|
%current-system
|
2013-05-24 22:21:24 +02:00
|
|
|
|
%current-target-system
|
2016-02-28 23:11:36 +01:00
|
|
|
|
package-name->name+version
|
2013-02-12 18:02:15 +01:00
|
|
|
|
version-compare
|
|
|
|
|
version>?
|
2015-02-09 17:17:07 +01:00
|
|
|
|
version>=?
|
2014-10-19 04:03:42 +02:00
|
|
|
|
version-prefix
|
|
|
|
|
version-major+minor
|
2013-08-23 15:51:36 +02:00
|
|
|
|
guile-version>?
|
2013-11-12 22:56:13 +01:00
|
|
|
|
string-replace-substring
|
2014-11-30 17:47:22 +01:00
|
|
|
|
arguments-from-environment-variable
|
2013-04-24 23:48:36 +02:00
|
|
|
|
file-extension
|
2013-10-09 00:04:45 +02:00
|
|
|
|
file-sans-extension
|
2015-10-26 22:17:25 +01:00
|
|
|
|
switch-symlinks
|
2013-04-21 22:40:23 +02:00
|
|
|
|
call-with-temporary-output-file
|
2015-06-25 03:16:11 +02:00
|
|
|
|
call-with-temporary-directory
|
2013-12-29 15:51:07 +01:00
|
|
|
|
with-atomic-file-output
|
2013-04-29 23:25:19 +02:00
|
|
|
|
fold2
|
2014-07-20 18:22:46 +02:00
|
|
|
|
fold-tree
|
|
|
|
|
fold-tree-leaves
|
2015-10-09 18:10:47 +02:00
|
|
|
|
split
|
2015-10-17 13:02:53 +02:00
|
|
|
|
cache-directory
|
2015-10-26 20:01:45 +01:00
|
|
|
|
readlink*
|
2014-03-22 21:58:23 +01:00
|
|
|
|
|
|
|
|
|
filtered-port
|
|
|
|
|
compressed-port
|
2014-03-24 21:09:15 +01:00
|
|
|
|
decompressed-port
|
2014-03-24 22:15:29 +01:00
|
|
|
|
call-with-decompressed-port
|
|
|
|
|
compressed-output-port
|
2015-11-14 15:00:36 +01:00
|
|
|
|
call-with-compressed-output-port
|
|
|
|
|
canonical-newline-port))
|
2012-06-01 23:08:32 +02:00
|
|
|
|
|
2012-06-28 22:57:40 +02:00
|
|
|
|
|
|
|
|
|
;;;
|
|
|
|
|
;;; Compile-time computations.
|
|
|
|
|
;;;
|
|
|
|
|
|
|
|
|
|
(define-syntax compile-time-value
|
|
|
|
|
(syntax-rules ()
|
|
|
|
|
"Evaluate the given expression at compile time. The expression must
|
|
|
|
|
evaluate to a simple datum."
|
|
|
|
|
((_ exp)
|
|
|
|
|
(let-syntax ((v (lambda (s)
|
|
|
|
|
(let ((val exp))
|
|
|
|
|
(syntax-case s ()
|
|
|
|
|
(_ #`'#,(datum->syntax s val)))))))
|
|
|
|
|
v))))
|
|
|
|
|
|
2012-06-01 23:29:55 +02:00
|
|
|
|
|
|
|
|
|
;;;
|
|
|
|
|
;;; Base 16.
|
|
|
|
|
;;;
|
|
|
|
|
|
|
|
|
|
(define (bytevector->base16-string bv)
|
|
|
|
|
"Return the hexadecimal representation of BV's contents."
|
|
|
|
|
(define len
|
|
|
|
|
(bytevector-length bv))
|
|
|
|
|
|
|
|
|
|
(let-syntax ((base16-chars (lambda (s)
|
|
|
|
|
(syntax-case s ()
|
|
|
|
|
(_
|
|
|
|
|
(let ((v (list->vector
|
|
|
|
|
(unfold (cut > <> 255)
|
|
|
|
|
(lambda (n)
|
|
|
|
|
(format #f "~2,'0x" n))
|
|
|
|
|
1+
|
|
|
|
|
0))))
|
|
|
|
|
v))))))
|
|
|
|
|
(define chars base16-chars)
|
2014-10-13 23:07:47 +02:00
|
|
|
|
(let loop ((i len)
|
2012-06-01 23:29:55 +02:00
|
|
|
|
(r '()))
|
2014-10-13 23:07:47 +02:00
|
|
|
|
(if (zero? i)
|
|
|
|
|
(string-concatenate r)
|
|
|
|
|
(let ((i (- i 1)))
|
|
|
|
|
(loop i
|
|
|
|
|
(cons (vector-ref chars (bytevector-u8-ref bv i)) r)))))))
|
2012-06-01 23:29:55 +02:00
|
|
|
|
|
2012-06-09 16:34:18 +02:00
|
|
|
|
(define base16-string->bytevector
|
|
|
|
|
(let ((chars->value (fold (lambda (i r)
|
|
|
|
|
(vhash-consv (string-ref (number->string i 16)
|
|
|
|
|
0)
|
|
|
|
|
i r))
|
|
|
|
|
vlist-null
|
|
|
|
|
(iota 16))))
|
|
|
|
|
(lambda (s)
|
|
|
|
|
"Return the bytevector whose hexadecimal representation is string S."
|
|
|
|
|
(define bv
|
|
|
|
|
(make-bytevector (quotient (string-length s) 2) 0))
|
|
|
|
|
|
|
|
|
|
(string-fold (lambda (chr i)
|
|
|
|
|
(let ((j (quotient i 2))
|
|
|
|
|
(v (and=> (vhash-assv chr chars->value) cdr)))
|
|
|
|
|
(if v
|
|
|
|
|
(if (zero? (logand i 1))
|
|
|
|
|
(bytevector-u8-set! bv j
|
|
|
|
|
(arithmetic-shift v 4))
|
|
|
|
|
(let ((w (bytevector-u8-ref bv j)))
|
|
|
|
|
(bytevector-u8-set! bv j (logior v w))))
|
|
|
|
|
(error "invalid hexadecimal character" chr)))
|
|
|
|
|
(+ i 1))
|
|
|
|
|
0
|
|
|
|
|
s)
|
|
|
|
|
bv)))
|
|
|
|
|
|
2012-06-07 23:15:00 +02:00
|
|
|
|
|
2013-04-29 23:25:19 +02:00
|
|
|
|
|
|
|
|
|
;;;
|
|
|
|
|
;;; Filtering & pipes.
|
|
|
|
|
;;;
|
|
|
|
|
|
|
|
|
|
(define (filtered-port command input)
|
|
|
|
|
"Return an input port where data drained from INPUT is filtered through
|
|
|
|
|
COMMAND (a list). In addition, return a list of PIDs that the caller must
|
2013-05-15 23:40:09 +02:00
|
|
|
|
wait. When INPUT is a file port, it must be unbuffered; otherwise, any
|
|
|
|
|
buffered data is lost."
|
2013-04-29 23:25:19 +02:00
|
|
|
|
(let loop ((input input)
|
2014-03-22 21:50:12 +01:00
|
|
|
|
(pids '()))
|
2013-04-29 23:25:19 +02:00
|
|
|
|
(if (file-port? input)
|
|
|
|
|
(match (pipe)
|
|
|
|
|
((in . out)
|
|
|
|
|
(match (primitive-fork)
|
|
|
|
|
(0
|
2014-03-22 21:50:12 +01:00
|
|
|
|
(dynamic-wind
|
|
|
|
|
(const #f)
|
|
|
|
|
(lambda ()
|
|
|
|
|
(close-port in)
|
|
|
|
|
(close-port (current-input-port))
|
|
|
|
|
(dup2 (fileno input) 0)
|
|
|
|
|
(close-port (current-output-port))
|
|
|
|
|
(dup2 (fileno out) 1)
|
|
|
|
|
(catch 'system-error
|
|
|
|
|
(lambda ()
|
|
|
|
|
(apply execl (car command) command))
|
|
|
|
|
(lambda args
|
|
|
|
|
(format (current-error-port)
|
|
|
|
|
"filtered-port: failed to execute '~{~a ~}': ~a~%"
|
|
|
|
|
command (strerror (system-error-errno args))))))
|
|
|
|
|
(lambda ()
|
|
|
|
|
(primitive-_exit 1))))
|
2013-04-29 23:25:19 +02:00
|
|
|
|
(child
|
|
|
|
|
(close-port out)
|
|
|
|
|
(values in (cons child pids))))))
|
|
|
|
|
|
|
|
|
|
;; INPUT is not a file port, so fork just for the sake of tunneling it
|
|
|
|
|
;; through a file port.
|
|
|
|
|
(match (pipe)
|
|
|
|
|
((in . out)
|
|
|
|
|
(match (primitive-fork)
|
|
|
|
|
(0
|
|
|
|
|
(dynamic-wind
|
|
|
|
|
(const #t)
|
|
|
|
|
(lambda ()
|
|
|
|
|
(close-port in)
|
|
|
|
|
(dump-port input out))
|
|
|
|
|
(lambda ()
|
|
|
|
|
(false-if-exception (close out))
|
2014-03-22 21:50:12 +01:00
|
|
|
|
(primitive-_exit 0))))
|
2013-04-29 23:25:19 +02:00
|
|
|
|
(child
|
|
|
|
|
(close-port out)
|
|
|
|
|
(loop in (cons child pids)))))))))
|
|
|
|
|
|
2014-03-22 21:58:23 +01:00
|
|
|
|
(define (decompressed-port compression input)
|
|
|
|
|
"Return an input port where INPUT is decompressed according to COMPRESSION,
|
|
|
|
|
a symbol such as 'xz."
|
|
|
|
|
(match compression
|
|
|
|
|
((or #f 'none) (values input '()))
|
|
|
|
|
('bzip2 (filtered-port `(,%bzip2 "-dc") input))
|
|
|
|
|
('xz (filtered-port `(,%xz "-dc") input))
|
|
|
|
|
('gzip (filtered-port `(,%gzip "-dc") input))
|
|
|
|
|
(else (error "unsupported compression scheme" compression))))
|
|
|
|
|
|
|
|
|
|
(define (compressed-port compression input)
|
|
|
|
|
"Return an input port where INPUT is decompressed according to COMPRESSION,
|
|
|
|
|
a symbol such as 'xz."
|
|
|
|
|
(match compression
|
|
|
|
|
((or #f 'none) (values input '()))
|
|
|
|
|
('bzip2 (filtered-port `(,%bzip2 "-c") input))
|
|
|
|
|
('xz (filtered-port `(,%xz "-c") input))
|
|
|
|
|
('gzip (filtered-port `(,%gzip "-c") input))
|
|
|
|
|
(else (error "unsupported compression scheme" compression))))
|
|
|
|
|
|
2014-03-24 22:15:29 +01:00
|
|
|
|
(define (call-with-decompressed-port compression port proc)
|
|
|
|
|
"Call PROC with a wrapper around PORT, a file port, that decompresses data
|
2014-04-14 00:24:24 +02:00
|
|
|
|
read from PORT according to COMPRESSION, a symbol such as 'xz."
|
2014-03-24 22:15:29 +01:00
|
|
|
|
(let-values (((decompressed pids)
|
|
|
|
|
(decompressed-port compression port)))
|
|
|
|
|
(dynamic-wind
|
|
|
|
|
(const #f)
|
|
|
|
|
(lambda ()
|
|
|
|
|
(proc decompressed))
|
|
|
|
|
(lambda ()
|
|
|
|
|
(close-port decompressed)
|
|
|
|
|
(unless (every (compose zero? cdr waitpid) pids)
|
|
|
|
|
(error "decompressed-port failure" pids))))))
|
|
|
|
|
|
2014-03-24 21:09:15 +01:00
|
|
|
|
(define (filtered-output-port command output)
|
|
|
|
|
"Return an output port. Data written to that port is filtered through
|
|
|
|
|
COMMAND and written to OUTPUT, an output file port. In addition, return a
|
|
|
|
|
list of PIDs to wait for. OUTPUT must be unbuffered; otherwise, any buffered
|
|
|
|
|
data is lost."
|
|
|
|
|
(match (pipe)
|
|
|
|
|
((in . out)
|
|
|
|
|
(match (primitive-fork)
|
|
|
|
|
(0
|
|
|
|
|
(dynamic-wind
|
|
|
|
|
(const #f)
|
|
|
|
|
(lambda ()
|
|
|
|
|
(close-port out)
|
|
|
|
|
(close-port (current-input-port))
|
|
|
|
|
(dup2 (fileno in) 0)
|
|
|
|
|
(close-port (current-output-port))
|
|
|
|
|
(dup2 (fileno output) 1)
|
|
|
|
|
(catch 'system-error
|
|
|
|
|
(lambda ()
|
|
|
|
|
(apply execl (car command) command))
|
|
|
|
|
(lambda args
|
|
|
|
|
(format (current-error-port)
|
|
|
|
|
"filtered-output-port: failed to execute '~{~a ~}': ~a~%"
|
|
|
|
|
command (strerror (system-error-errno args))))))
|
|
|
|
|
(lambda ()
|
|
|
|
|
(primitive-_exit 1))))
|
|
|
|
|
(child
|
|
|
|
|
(close-port in)
|
|
|
|
|
(values out (list child)))))))
|
|
|
|
|
|
2015-09-25 22:34:16 +02:00
|
|
|
|
(define* (compressed-output-port compression output
|
|
|
|
|
#:key (options '()))
|
2014-03-24 21:09:15 +01:00
|
|
|
|
"Return an output port whose input is compressed according to COMPRESSION,
|
|
|
|
|
a symbol such as 'xz, and then written to OUTPUT. In addition return a list
|
2015-09-25 22:34:16 +02:00
|
|
|
|
of PIDs to wait for. OPTIONS is a list of strings passed to the compression
|
|
|
|
|
program--e.g., '(\"--fast\")."
|
2014-03-24 21:09:15 +01:00
|
|
|
|
(match compression
|
|
|
|
|
((or #f 'none) (values output '()))
|
2015-09-25 22:34:16 +02:00
|
|
|
|
('bzip2 (filtered-output-port `(,%bzip2 "-c" ,@options) output))
|
|
|
|
|
('xz (filtered-output-port `(,%xz "-c" ,@options) output))
|
|
|
|
|
('gzip (filtered-output-port `(,%gzip "-c" ,@options) output))
|
2014-03-24 21:09:15 +01:00
|
|
|
|
(else (error "unsupported compression scheme" compression))))
|
|
|
|
|
|
2015-09-25 22:34:16 +02:00
|
|
|
|
(define* (call-with-compressed-output-port compression port proc
|
|
|
|
|
#:key (options '()))
|
2014-03-24 22:15:29 +01:00
|
|
|
|
"Call PROC with a wrapper around PORT, a file port, that compresses data
|
2015-09-25 22:34:16 +02:00
|
|
|
|
that goes to PORT according to COMPRESSION, a symbol such as 'xz. OPTIONS is
|
|
|
|
|
a list of command-line arguments passed to the compression program."
|
2014-03-24 22:15:29 +01:00
|
|
|
|
(let-values (((compressed pids)
|
2015-09-25 22:34:16 +02:00
|
|
|
|
(compressed-output-port compression port
|
|
|
|
|
#:options options)))
|
2014-03-24 22:15:29 +01:00
|
|
|
|
(dynamic-wind
|
|
|
|
|
(const #f)
|
|
|
|
|
(lambda ()
|
|
|
|
|
(proc compressed))
|
|
|
|
|
(lambda ()
|
|
|
|
|
(close-port compressed)
|
|
|
|
|
(unless (every (compose zero? cdr waitpid) pids)
|
|
|
|
|
(error "compressed-output-port failure" pids))))))
|
|
|
|
|
|
2014-01-10 23:27:39 +01:00
|
|
|
|
|
|
|
|
|
;;;
|
|
|
|
|
;;; Advisory file locking.
|
|
|
|
|
;;;
|
|
|
|
|
|
|
|
|
|
(define %struct-flock
|
|
|
|
|
;; 'struct flock' from <fcntl.h>.
|
|
|
|
|
(list short ; l_type
|
|
|
|
|
short ; l_whence
|
|
|
|
|
size_t ; l_start
|
|
|
|
|
size_t ; l_len
|
|
|
|
|
int)) ; l_pid
|
|
|
|
|
|
|
|
|
|
(define F_SETLKW
|
|
|
|
|
;; On Linux-based systems, this is usually 7, but not always
|
|
|
|
|
;; (exceptions include SPARC.) On GNU/Hurd, it's 9.
|
|
|
|
|
(compile-time-value
|
|
|
|
|
(cond ((string-contains %host-type "sparc") 9) ; sparc-*-linux-gnu
|
|
|
|
|
((string-contains %host-type "linux") 7) ; *-linux-gnu
|
|
|
|
|
(else 9)))) ; *-gnu*
|
|
|
|
|
|
2014-03-07 16:46:09 +01:00
|
|
|
|
(define F_SETLK
|
|
|
|
|
;; Likewise: GNU/Hurd and SPARC use 8, while the others typically use 6.
|
|
|
|
|
(compile-time-value
|
|
|
|
|
(cond ((string-contains %host-type "sparc") 8) ; sparc-*-linux-gnu
|
|
|
|
|
((string-contains %host-type "linux") 6) ; *-linux-gnu
|
|
|
|
|
(else 8)))) ; *-gnu*
|
|
|
|
|
|
2014-01-10 23:27:39 +01:00
|
|
|
|
(define F_xxLCK
|
|
|
|
|
;; The F_RDLCK, F_WRLCK, and F_UNLCK constants.
|
|
|
|
|
(compile-time-value
|
|
|
|
|
(cond ((string-contains %host-type "sparc") #(1 2 3)) ; sparc-*-linux-gnu
|
|
|
|
|
((string-contains %host-type "hppa") #(1 2 3)) ; hppa-*-linux-gnu
|
|
|
|
|
((string-contains %host-type "linux") #(0 1 2)) ; *-linux-gnu
|
|
|
|
|
(else #(1 2 3))))) ; *-gnu*
|
|
|
|
|
|
|
|
|
|
(define fcntl-flock
|
|
|
|
|
(let* ((ptr (dynamic-func "fcntl" (dynamic-link)))
|
|
|
|
|
(proc (pointer->procedure int ptr `(,int ,int *))))
|
2014-03-07 16:46:09 +01:00
|
|
|
|
(lambda* (fd-or-port operation #:key (wait? #t))
|
2014-01-10 23:27:39 +01:00
|
|
|
|
"Perform locking OPERATION on the file beneath FD-OR-PORT. OPERATION
|
2014-03-07 16:46:09 +01:00
|
|
|
|
must be a symbol, one of 'read-lock, 'write-lock, or 'unlock. When WAIT? is
|
|
|
|
|
true, block until the lock is acquired; otherwise, thrown an 'flock-error'
|
|
|
|
|
exception if it's already taken."
|
2014-01-10 23:27:39 +01:00
|
|
|
|
(define (operation->int op)
|
|
|
|
|
(case op
|
|
|
|
|
((read-lock) (vector-ref F_xxLCK 0))
|
|
|
|
|
((write-lock) (vector-ref F_xxLCK 1))
|
|
|
|
|
((unlock) (vector-ref F_xxLCK 2))
|
|
|
|
|
(else (error "invalid fcntl-flock operation" op))))
|
|
|
|
|
|
|
|
|
|
(define fd
|
|
|
|
|
(if (port? fd-or-port)
|
|
|
|
|
(fileno fd-or-port)
|
|
|
|
|
fd-or-port))
|
|
|
|
|
|
|
|
|
|
;; XXX: 'fcntl' is a vararg function, but here we happily use the
|
|
|
|
|
;; standard ABI; crossing fingers.
|
|
|
|
|
(let ((err (proc fd
|
2014-03-07 16:46:09 +01:00
|
|
|
|
(if wait?
|
|
|
|
|
F_SETLKW ; lock & wait
|
|
|
|
|
F_SETLK) ; non-blocking attempt
|
2014-01-10 23:27:39 +01:00
|
|
|
|
(make-c-struct %struct-flock
|
|
|
|
|
(list (operation->int operation)
|
|
|
|
|
SEEK_SET
|
|
|
|
|
0 0 ; whole file
|
|
|
|
|
0)))))
|
|
|
|
|
(or (zero? err)
|
|
|
|
|
|
|
|
|
|
;; Presumably we got EAGAIN or so.
|
2014-03-06 21:41:51 +01:00
|
|
|
|
(throw 'flock-error (errno)))))))
|
2014-01-10 23:27:39 +01:00
|
|
|
|
|
2012-06-07 23:15:00 +02:00
|
|
|
|
|
|
|
|
|
;;;
|
|
|
|
|
;;; Miscellaneous.
|
|
|
|
|
;;;
|
|
|
|
|
|
|
|
|
|
(define (memoize proc)
|
|
|
|
|
"Return a memoizing version of PROC."
|
|
|
|
|
(let ((cache (make-hash-table)))
|
|
|
|
|
(lambda args
|
|
|
|
|
(let ((results (hash-ref cache args)))
|
|
|
|
|
(if results
|
|
|
|
|
(apply values results)
|
|
|
|
|
(let ((results (call-with-values (lambda ()
|
|
|
|
|
(apply proc args))
|
|
|
|
|
list)))
|
|
|
|
|
(hash-set! cache args results)
|
|
|
|
|
(apply values results)))))))
|
2012-06-11 21:50:17 +02:00
|
|
|
|
|
2014-10-01 10:19:14 +02:00
|
|
|
|
(define (strip-keyword-arguments keywords args)
|
|
|
|
|
"Remove all of the keyword arguments listed in KEYWORDS from ARGS."
|
|
|
|
|
(let loop ((args args)
|
|
|
|
|
(result '()))
|
|
|
|
|
(match args
|
|
|
|
|
(()
|
|
|
|
|
(reverse result))
|
|
|
|
|
(((? keyword? kw) arg . rest)
|
|
|
|
|
(loop rest
|
|
|
|
|
(if (memq kw keywords)
|
|
|
|
|
result
|
|
|
|
|
(cons* arg kw result))))
|
|
|
|
|
((head . tail)
|
|
|
|
|
(loop tail (cons head result))))))
|
|
|
|
|
|
2012-11-05 23:46:55 +01:00
|
|
|
|
(define (default-keyword-arguments args defaults)
|
|
|
|
|
"Return ARGS augmented with any keyword/value from DEFAULTS for
|
|
|
|
|
keywords not already present in ARGS."
|
|
|
|
|
(let loop ((defaults defaults)
|
|
|
|
|
(args args))
|
|
|
|
|
(match defaults
|
|
|
|
|
((kw value rest ...)
|
|
|
|
|
(loop rest
|
|
|
|
|
(if (assoc-ref kw args)
|
|
|
|
|
args
|
|
|
|
|
(cons* kw value args))))
|
|
|
|
|
(()
|
|
|
|
|
args))))
|
|
|
|
|
|
|
|
|
|
(define-syntax substitute-keyword-arguments
|
|
|
|
|
(syntax-rules ()
|
|
|
|
|
"Return a new list of arguments where the value for keyword arg KW is
|
|
|
|
|
replaced by EXP. EXP is evaluated in a context where VAR is boud to the
|
|
|
|
|
previous value of the keyword argument."
|
|
|
|
|
((_ original-args ((kw var) exp) ...)
|
|
|
|
|
(let loop ((args original-args)
|
|
|
|
|
(before '()))
|
|
|
|
|
(match args
|
|
|
|
|
((kw var rest (... ...))
|
|
|
|
|
(loop rest (cons* exp kw before)))
|
|
|
|
|
...
|
|
|
|
|
((x rest (... ...))
|
|
|
|
|
(loop rest (cons x before)))
|
|
|
|
|
(()
|
|
|
|
|
(reverse before)))))))
|
|
|
|
|
|
2016-01-06 22:42:09 +01:00
|
|
|
|
(define (delkw kw lst)
|
|
|
|
|
"Remove KW and its associated value from LST, a keyword/value list such
|
|
|
|
|
as '(#:foo 1 #:bar 2)."
|
|
|
|
|
(let loop ((lst lst)
|
|
|
|
|
(result '()))
|
|
|
|
|
(match lst
|
|
|
|
|
(()
|
|
|
|
|
(reverse result))
|
|
|
|
|
((kw? value rest ...)
|
|
|
|
|
(if (eq? kw? kw)
|
|
|
|
|
(append (reverse result) rest)
|
|
|
|
|
(loop rest (cons* value kw? result)))))))
|
|
|
|
|
|
|
|
|
|
(define (ensure-keyword-arguments args kw/values)
|
|
|
|
|
"Force the keywords arguments KW/VALUES in the keyword argument list ARGS.
|
|
|
|
|
For instance:
|
|
|
|
|
|
|
|
|
|
(ensure-keyword-arguments '(#:foo 2) '(#:foo 2))
|
|
|
|
|
=> (#:foo 2)
|
|
|
|
|
|
|
|
|
|
(ensure-keyword-arguments '(#:foo 2) '(#:bar 3))
|
|
|
|
|
=> (#:foo 2 #:bar 3)
|
|
|
|
|
|
|
|
|
|
(ensure-keyword-arguments '(#:foo 2) '(#:bar 3 #:foo 42))
|
|
|
|
|
=> (#:foo 42 #:bar 3)
|
|
|
|
|
"
|
|
|
|
|
(let loop ((args args)
|
|
|
|
|
(kw/values kw/values)
|
|
|
|
|
(result '()))
|
|
|
|
|
(match args
|
|
|
|
|
(()
|
|
|
|
|
(append (reverse result) kw/values))
|
|
|
|
|
((kw value rest ...)
|
|
|
|
|
(match (memq kw kw/values)
|
|
|
|
|
((_ value . _)
|
|
|
|
|
(loop rest (delkw kw kw/values) (cons* value kw result)))
|
|
|
|
|
(#f
|
|
|
|
|
(loop rest kw/values (cons* value kw result))))))))
|
|
|
|
|
|
2014-12-31 09:47:40 +01:00
|
|
|
|
(define* (nix-system->gnu-triplet
|
|
|
|
|
#:optional (system (%current-system)) (vendor "unknown"))
|
|
|
|
|
"Return a guess of the GNU triplet corresponding to Nix system
|
|
|
|
|
identifier SYSTEM."
|
2015-01-07 21:55:23 +01:00
|
|
|
|
(match system
|
|
|
|
|
("armhf-linux"
|
|
|
|
|
(string-append "arm-" vendor "-linux-gnueabihf"))
|
|
|
|
|
(_
|
|
|
|
|
(let* ((dash (string-index system #\-))
|
|
|
|
|
(arch (substring system 0 dash))
|
|
|
|
|
(os (substring system (+ 1 dash))))
|
|
|
|
|
(string-append arch
|
|
|
|
|
"-" vendor "-"
|
|
|
|
|
(if (string=? os "linux")
|
|
|
|
|
"linux-gnu"
|
|
|
|
|
os))))))
|
2014-12-31 09:47:40 +01:00
|
|
|
|
|
2012-06-11 21:50:17 +02:00
|
|
|
|
(define (gnu-triplet->nix-system triplet)
|
|
|
|
|
"Return the Nix system type corresponding to TRIPLET, a GNU triplet as
|
|
|
|
|
returned by `config.guess'."
|
|
|
|
|
(let ((triplet (cond ((string-match "^i[345]86-(.*)$" triplet)
|
|
|
|
|
=>
|
|
|
|
|
(lambda (m)
|
|
|
|
|
(string-append "i686-" (match:substring m 1))))
|
|
|
|
|
(else triplet))))
|
2015-01-07 21:55:23 +01:00
|
|
|
|
(cond ((string-match "^arm[^-]*-([^-]+-)?linux-gnueabihf" triplet)
|
|
|
|
|
"armhf-linux")
|
|
|
|
|
((string-match "^([^-]+)-([^-]+-)?linux-gnu.*" triplet)
|
2012-06-11 21:50:17 +02:00
|
|
|
|
=>
|
|
|
|
|
(lambda (m)
|
|
|
|
|
;; Nix omits `-gnu' for GNU/Linux.
|
|
|
|
|
(string-append (match:substring m 1) "-linux")))
|
|
|
|
|
((string-match "^([^-]+)-([^-]+-)?([[:alpha:]]+)([0-9]+\\.?)*$" triplet)
|
|
|
|
|
=>
|
|
|
|
|
(lambda (m)
|
|
|
|
|
;; Nix strip the version number from names such as `gnu0.3',
|
|
|
|
|
;; `darwin10.2.0', etc., and always strips the vendor part.
|
|
|
|
|
(string-append (match:substring m 1) "-"
|
|
|
|
|
(match:substring m 3))))
|
|
|
|
|
(else triplet))))
|
|
|
|
|
|
|
|
|
|
(define %current-system
|
|
|
|
|
;; System type as expected by Nix, usually ARCHITECTURE-KERNEL.
|
2012-12-05 23:02:47 +01:00
|
|
|
|
;; By default, this is equal to (gnu-triplet->nix-system %host-type).
|
|
|
|
|
(make-parameter %system))
|
2012-09-01 19:21:06 +02:00
|
|
|
|
|
2013-05-24 22:21:24 +02:00
|
|
|
|
(define %current-target-system
|
|
|
|
|
;; Either #f or a GNU triplet representing the target system we are
|
|
|
|
|
;; cross-building to.
|
|
|
|
|
(make-parameter #f))
|
|
|
|
|
|
2016-02-28 23:11:36 +01:00
|
|
|
|
(define (package-name->name+version spec)
|
|
|
|
|
"Given SPEC, a package name like \"foo@0.9.1b\", return two values: \"foo\"
|
|
|
|
|
and \"0.9.1b\". When the version part is unavailable, SPEC and #f are
|
|
|
|
|
returned. Both parts must not contain any '@'."
|
|
|
|
|
(match (string-rindex spec #\@)
|
|
|
|
|
(#f (values spec #f))
|
|
|
|
|
(idx (values (substring spec 0 idx)
|
|
|
|
|
(substring spec (1+ idx))))))
|
|
|
|
|
|
2013-02-12 18:02:15 +01:00
|
|
|
|
(define version-compare
|
|
|
|
|
(let ((strverscmp
|
|
|
|
|
(let ((sym (or (dynamic-func "strverscmp" (dynamic-link))
|
|
|
|
|
(error "could not find `strverscmp' (from GNU libc)"))))
|
|
|
|
|
(pointer->procedure int sym (list '* '*)))))
|
|
|
|
|
(lambda (a b)
|
|
|
|
|
"Return '> when A denotes a newer version than B,
|
|
|
|
|
'< when A denotes a older version than B,
|
|
|
|
|
or '= when they denote equal versions."
|
|
|
|
|
(let ((result (strverscmp (string->pointer a) (string->pointer b))))
|
|
|
|
|
(cond ((positive? result) '>)
|
|
|
|
|
((negative? result) '<)
|
|
|
|
|
(else '=))))))
|
|
|
|
|
|
2014-10-19 04:03:42 +02:00
|
|
|
|
(define (version-prefix version-string num-parts)
|
|
|
|
|
"Truncate version-string to the first num-parts components of the version.
|
|
|
|
|
For example, (version-prefix \"2.1.47.4.23\" 3) returns \"2.1.47\""
|
|
|
|
|
(string-join (take (string-split version-string #\.) num-parts) "."))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(define (version-major+minor version-string)
|
|
|
|
|
"Return \"<major>.<minor>\", where major and minor are the major and
|
|
|
|
|
minor version numbers from version-string."
|
|
|
|
|
(version-prefix version-string 2))
|
|
|
|
|
|
2013-02-12 18:02:15 +01:00
|
|
|
|
(define (version>? a b)
|
2015-02-09 17:17:07 +01:00
|
|
|
|
"Return #t when A denotes a version strictly newer than B."
|
2013-02-12 18:02:15 +01:00
|
|
|
|
(eq? '> (version-compare a b)))
|
|
|
|
|
|
2015-02-09 17:17:07 +01:00
|
|
|
|
(define (version>=? a b)
|
|
|
|
|
"Return #t when A denotes a version newer or equal to B."
|
|
|
|
|
(case (version-compare a b)
|
|
|
|
|
((> =) #t)
|
|
|
|
|
(else #f)))
|
|
|
|
|
|
2013-08-23 15:51:36 +02:00
|
|
|
|
(define (guile-version>? str)
|
|
|
|
|
"Return #t if the running Guile version is greater than STR."
|
|
|
|
|
;; Note: Using (version>? (version) "2.0.5") or similar doesn't work,
|
|
|
|
|
;; because the result of (version) can have a prefix, like "2.0.5-deb1".
|
|
|
|
|
(version>? (string-append (major-version) "."
|
|
|
|
|
(minor-version) "."
|
|
|
|
|
(micro-version))
|
|
|
|
|
str))
|
|
|
|
|
|
2013-04-24 23:48:36 +02:00
|
|
|
|
(define (file-extension file)
|
|
|
|
|
"Return the extension of FILE or #f if there is none."
|
|
|
|
|
(let ((dot (string-rindex file #\.)))
|
|
|
|
|
(and dot (substring file (+ 1 dot) (string-length file)))))
|
|
|
|
|
|
2013-10-09 00:04:45 +02:00
|
|
|
|
(define (file-sans-extension file)
|
|
|
|
|
"Return the substring of FILE without its extension, if any."
|
|
|
|
|
(let ((dot (string-rindex file #\.)))
|
|
|
|
|
(if dot
|
|
|
|
|
(substring file 0 dot)
|
|
|
|
|
file)))
|
|
|
|
|
|
2015-10-26 22:17:25 +01:00
|
|
|
|
(define (switch-symlinks link target)
|
|
|
|
|
"Atomically switch LINK, a symbolic link, to point to TARGET. Works
|
|
|
|
|
both when LINK already exists and when it does not."
|
|
|
|
|
(let ((pivot (string-append link ".new")))
|
|
|
|
|
(symlink target pivot)
|
|
|
|
|
(rename-file pivot link)))
|
|
|
|
|
|
2013-11-12 22:56:13 +01:00
|
|
|
|
(define* (string-replace-substring str substr replacement
|
|
|
|
|
#:optional
|
|
|
|
|
(start 0)
|
|
|
|
|
(end (string-length str)))
|
|
|
|
|
"Replace all occurrences of SUBSTR in the START--END range of STR by
|
|
|
|
|
REPLACEMENT."
|
|
|
|
|
(match (string-length substr)
|
|
|
|
|
(0
|
|
|
|
|
(error "string-replace-substring: empty substring"))
|
|
|
|
|
(substr-length
|
|
|
|
|
(let loop ((start start)
|
|
|
|
|
(pieces (list (substring str 0 start))))
|
|
|
|
|
(match (string-contains str substr start end)
|
|
|
|
|
(#f
|
|
|
|
|
(string-concatenate-reverse
|
|
|
|
|
(cons (substring str start) pieces)))
|
|
|
|
|
(index
|
|
|
|
|
(loop (+ index substr-length)
|
|
|
|
|
(cons* replacement
|
|
|
|
|
(substring str start index)
|
|
|
|
|
pieces))))))))
|
|
|
|
|
|
2014-11-30 17:47:22 +01:00
|
|
|
|
(define (arguments-from-environment-variable variable)
|
|
|
|
|
"Retrieve value of environment variable denoted by string VARIABLE in the
|
|
|
|
|
form of a list of strings (`char-set:graphic' tokens) suitable for consumption
|
|
|
|
|
by `args-fold', if VARIABLE is defined, otherwise return an empty list."
|
|
|
|
|
(let ((env (getenv variable)))
|
|
|
|
|
(if env
|
|
|
|
|
(string-tokenize env char-set:graphic)
|
|
|
|
|
'())))
|
|
|
|
|
|
2013-04-21 22:40:23 +02:00
|
|
|
|
(define (call-with-temporary-output-file proc)
|
|
|
|
|
"Call PROC with a name of a temporary file and open output port to that
|
|
|
|
|
file; close the file and delete it when leaving the dynamic extent of this
|
|
|
|
|
call."
|
2014-09-17 07:18:46 +02:00
|
|
|
|
(let* ((directory (or (getenv "TMPDIR") "/tmp"))
|
|
|
|
|
(template (string-append directory "/guix-file.XXXXXX"))
|
|
|
|
|
(out (mkstemp! template)))
|
2013-04-21 22:40:23 +02:00
|
|
|
|
(dynamic-wind
|
|
|
|
|
(lambda ()
|
|
|
|
|
#t)
|
|
|
|
|
(lambda ()
|
|
|
|
|
(proc template out))
|
|
|
|
|
(lambda ()
|
|
|
|
|
(false-if-exception (close out))
|
|
|
|
|
(false-if-exception (delete-file template))))))
|
2013-12-29 15:51:07 +01:00
|
|
|
|
|
2015-06-25 03:16:11 +02:00
|
|
|
|
(define (call-with-temporary-directory proc)
|
|
|
|
|
"Call PROC with a name of a temporary directory; close the directory and
|
|
|
|
|
delete it when leaving the dynamic extent of this call."
|
|
|
|
|
(let* ((directory (or (getenv "TMPDIR") "/tmp"))
|
|
|
|
|
(template (string-append directory "/guix-directory.XXXXXX"))
|
|
|
|
|
(tmp-dir (mkdtemp! template)))
|
|
|
|
|
(dynamic-wind
|
|
|
|
|
(const #t)
|
|
|
|
|
(lambda ()
|
|
|
|
|
(proc tmp-dir))
|
|
|
|
|
(lambda ()
|
|
|
|
|
(false-if-exception (rmdir tmp-dir))))))
|
|
|
|
|
|
2013-12-29 15:51:07 +01:00
|
|
|
|
(define (with-atomic-file-output file proc)
|
|
|
|
|
"Call PROC with an output port for the file that is going to replace FILE.
|
|
|
|
|
Upon success, FILE is atomically replaced by what has been written to the
|
|
|
|
|
output port, and PROC's result is returned."
|
|
|
|
|
(let* ((template (string-append file ".XXXXXX"))
|
|
|
|
|
(out (mkstemp! template)))
|
|
|
|
|
(with-throw-handler #t
|
|
|
|
|
(lambda ()
|
|
|
|
|
(let ((result (proc out)))
|
|
|
|
|
(close out)
|
|
|
|
|
(rename-file template file)
|
|
|
|
|
result))
|
|
|
|
|
(lambda (key . args)
|
|
|
|
|
(false-if-exception (delete-file template))))))
|
2013-04-21 22:40:23 +02:00
|
|
|
|
|
2013-04-14 16:56:08 +02:00
|
|
|
|
(define fold2
|
|
|
|
|
(case-lambda
|
|
|
|
|
((proc seed1 seed2 lst)
|
|
|
|
|
"Like `fold', but with a single list and two seeds."
|
|
|
|
|
(let loop ((result1 seed1)
|
|
|
|
|
(result2 seed2)
|
|
|
|
|
(lst lst))
|
|
|
|
|
(if (null? lst)
|
|
|
|
|
(values result1 result2)
|
|
|
|
|
(call-with-values
|
|
|
|
|
(lambda () (proc (car lst) result1 result2))
|
|
|
|
|
(lambda (result1 result2)
|
|
|
|
|
(loop result1 result2 (cdr lst)))))))
|
|
|
|
|
((proc seed1 seed2 lst1 lst2)
|
|
|
|
|
"Like `fold', but with a two lists and two seeds."
|
|
|
|
|
(let loop ((result1 seed1)
|
|
|
|
|
(result2 seed2)
|
|
|
|
|
(lst1 lst1)
|
|
|
|
|
(lst2 lst2))
|
|
|
|
|
(if (or (null? lst1) (null? lst2))
|
|
|
|
|
(values result1 result2)
|
|
|
|
|
(call-with-values
|
|
|
|
|
(lambda () (proc (car lst1) (car lst2) result1 result2))
|
|
|
|
|
(lambda (result1 result2)
|
|
|
|
|
(fold2 proc result1 result2 (cdr lst1) (cdr lst2)))))))))
|
|
|
|
|
|
2014-07-20 18:22:46 +02:00
|
|
|
|
(define (fold-tree proc init children roots)
|
|
|
|
|
"Call (PROC NODE RESULT) for each node in the tree that is reachable from
|
|
|
|
|
ROOTS, using INIT as the initial value of RESULT. The order in which nodes
|
|
|
|
|
are traversed is not specified, however, each node is visited only once, based
|
|
|
|
|
on an eq? check. Children of a node to be visited are generated by
|
|
|
|
|
calling (CHILDREN NODE), the result of which should be a list of nodes that
|
|
|
|
|
are connected to NODE in the tree, or '() or #f if NODE is a leaf node."
|
|
|
|
|
(let loop ((result init)
|
|
|
|
|
(seen vlist-null)
|
|
|
|
|
(lst roots))
|
|
|
|
|
(match lst
|
|
|
|
|
(() result)
|
|
|
|
|
((head . tail)
|
|
|
|
|
(if (not (vhash-assq head seen))
|
|
|
|
|
(loop (proc head result)
|
|
|
|
|
(vhash-consq head #t seen)
|
|
|
|
|
(match (children head)
|
|
|
|
|
((or () #f) tail)
|
|
|
|
|
(children (append tail children))))
|
|
|
|
|
(loop result seen tail))))))
|
|
|
|
|
|
|
|
|
|
(define (fold-tree-leaves proc init children roots)
|
|
|
|
|
"Like fold-tree, but call (PROC NODE RESULT) only for leaf nodes."
|
|
|
|
|
(fold-tree
|
|
|
|
|
(lambda (node result)
|
|
|
|
|
(match (children node)
|
|
|
|
|
((or () #f) (proc node result))
|
|
|
|
|
(else result)))
|
|
|
|
|
init children roots))
|
|
|
|
|
|
2015-10-09 18:10:47 +02:00
|
|
|
|
(define (split lst e)
|
|
|
|
|
"Return two values, a list containing the elements of the list LST that
|
|
|
|
|
appear before the first occurence of the object E and a list containing the
|
|
|
|
|
elements after E."
|
|
|
|
|
(define (same? x)
|
|
|
|
|
(equal? e x))
|
|
|
|
|
|
|
|
|
|
(let loop ((rest lst)
|
|
|
|
|
(acc '()))
|
|
|
|
|
(match rest
|
|
|
|
|
(()
|
|
|
|
|
(values lst '()))
|
|
|
|
|
(((? same?) . tail)
|
|
|
|
|
(values (reverse acc) tail))
|
|
|
|
|
((head . tail)
|
|
|
|
|
(loop tail (cons head acc))))))
|
|
|
|
|
|
2015-10-17 13:02:53 +02:00
|
|
|
|
(define (cache-directory)
|
|
|
|
|
"Return the cache directory for Guix, by default ~/.cache/guix."
|
|
|
|
|
(or (getenv "XDG_CONFIG_HOME")
|
|
|
|
|
(and=> (getenv "HOME")
|
|
|
|
|
(cut string-append <> "/.cache/guix"))))
|
|
|
|
|
|
2015-10-26 20:01:45 +01:00
|
|
|
|
(define (readlink* file)
|
|
|
|
|
"Call 'readlink' until the result is not a symlink."
|
|
|
|
|
(define %max-symlink-depth 50)
|
|
|
|
|
|
|
|
|
|
(let loop ((file file)
|
|
|
|
|
(depth 0))
|
|
|
|
|
(define (absolute target)
|
|
|
|
|
(if (absolute-file-name? target)
|
|
|
|
|
target
|
|
|
|
|
(string-append (dirname file) "/" target)))
|
|
|
|
|
|
|
|
|
|
(if (>= depth %max-symlink-depth)
|
|
|
|
|
file
|
|
|
|
|
(call-with-values
|
|
|
|
|
(lambda ()
|
|
|
|
|
(catch 'system-error
|
|
|
|
|
(lambda ()
|
|
|
|
|
(values #t (readlink file)))
|
|
|
|
|
(lambda args
|
|
|
|
|
(let ((errno (system-error-errno args)))
|
|
|
|
|
(if (or (= errno EINVAL))
|
|
|
|
|
(values #f file)
|
|
|
|
|
(apply throw args))))))
|
|
|
|
|
(lambda (success? target)
|
|
|
|
|
(if success?
|
|
|
|
|
(loop (absolute target) (+ depth 1))
|
|
|
|
|
file))))))
|
2015-11-14 15:00:36 +01:00
|
|
|
|
|
|
|
|
|
(define (canonical-newline-port port)
|
|
|
|
|
"Return an input port that wraps PORT such that all newlines consist
|
|
|
|
|
of a single carriage return."
|
|
|
|
|
(define (get-position)
|
|
|
|
|
(if (port-has-port-position? port) (port-position port) #f))
|
|
|
|
|
(define (set-position! position)
|
|
|
|
|
(if (port-has-set-port-position!? port)
|
|
|
|
|
(set-port-position! position port)
|
|
|
|
|
#f))
|
|
|
|
|
(define (close) (close-port port))
|
|
|
|
|
(define (read! bv start n)
|
|
|
|
|
(let loop ((count 0)
|
|
|
|
|
(byte (get-u8 port)))
|
|
|
|
|
(cond ((eof-object? byte) count)
|
|
|
|
|
((= count (- n 1))
|
|
|
|
|
(bytevector-u8-set! bv (+ start count) byte)
|
|
|
|
|
n)
|
|
|
|
|
;; XXX: consume all LFs even if not followed by CR.
|
|
|
|
|
((eqv? byte (char->integer #\return)) (loop count (get-u8 port)))
|
|
|
|
|
(else
|
|
|
|
|
(bytevector-u8-set! bv (+ start count) byte)
|
|
|
|
|
(loop (+ count 1) (get-u8 port))))))
|
|
|
|
|
(make-custom-binary-input-port "canonical-newline-port"
|
|
|
|
|
read!
|
|
|
|
|
get-position
|
|
|
|
|
set-position!
|
|
|
|
|
close))
|
2012-09-01 19:21:06 +02:00
|
|
|
|
|
|
|
|
|
;;;
|
|
|
|
|
;;; Source location.
|
|
|
|
|
;;;
|
|
|
|
|
|
|
|
|
|
;; A source location.
|
|
|
|
|
(define-record-type <location>
|
|
|
|
|
(make-location file line column)
|
|
|
|
|
location?
|
|
|
|
|
(file location-file) ; file name
|
|
|
|
|
(line location-line) ; 1-indexed line
|
|
|
|
|
(column location-column)) ; 0-indexed column
|
|
|
|
|
|
|
|
|
|
(define location
|
|
|
|
|
(memoize
|
|
|
|
|
(lambda (file line column)
|
|
|
|
|
"Return the <location> object for the given FILE, LINE, and COLUMN."
|
|
|
|
|
(and line column file
|
|
|
|
|
(make-location file line column)))))
|
|
|
|
|
|
|
|
|
|
(define (source-properties->location loc)
|
|
|
|
|
"Return a location object based on the info in LOC, an alist as returned
|
|
|
|
|
by Guile's `source-properties', `frame-source', `current-source-location',
|
|
|
|
|
etc."
|
|
|
|
|
(let ((file (assq-ref loc 'filename))
|
|
|
|
|
(line (assq-ref loc 'line))
|
|
|
|
|
(col (assq-ref loc 'column)))
|
2013-04-24 14:44:33 +02:00
|
|
|
|
;; In accordance with the GCS, start line and column numbers at 1. Note
|
|
|
|
|
;; that unlike LINE and `port-column', COL is actually 1-indexed here...
|
|
|
|
|
(location file (and line (+ line 1)) col)))
|