2013-01-06 00:47:50 +01:00
|
|
|
|
;;; GNU Guix --- Functional package management for GNU
|
2022-01-08 17:29:12 +01:00
|
|
|
|
;;; Copyright © 2012-2022 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>
|
2017-11-29 15:10:12 +01:00
|
|
|
|
;;; Copyright © 2017 Mathieu Othacehe <m.othacehe@gmail.com>
|
2020-05-28 18:53:13 +02:00
|
|
|
|
;;; Copyright © 2018, 2020 Marius Bakke <marius@gnu.org>
|
2021-08-17 08:39:46 +02:00
|
|
|
|
;;; Copyright © 2020, 2021 Efraim Flashner <efraim@flashner.co.il>
|
2020-09-19 06:20:47 +02:00
|
|
|
|
;;; Copyright © 2020 Maxim Cournoyer <maxim.cournoyer@gmail.com>
|
2021-01-19 22:28:08 +01:00
|
|
|
|
;;; Copyright © 2021 Simon Tournier <zimon.toutoune@gmail.com>
|
2021-02-02 09:00:57 +01:00
|
|
|
|
;;; Copyright © 2021 Chris Marusich <cmmarusich@gmail.com>
|
2021-07-14 13:12:46 +02:00
|
|
|
|
;;; Copyright © 2021 Maxime Devos <maximedevos@telenet.be>
|
2018-01-09 23:00:11 +01:00
|
|
|
|
;;; Copyright © 2018 Steve Sprang <scs@stevesprang.com>
|
|
|
|
|
;;; Copyright © 2021 Maxim Cournoyer <maxim.cournoyer@gmail.com>
|
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)
|
2021-06-21 16:13:35 +02:00
|
|
|
|
#:use-module (srfi srfi-71)
|
2018-07-12 02:24:29 +02:00
|
|
|
|
#:use-module (ice-9 ftw)
|
2020-01-06 15:14:09 +01:00
|
|
|
|
#:use-module (rnrs io ports) ;need 'port-position' etc.
|
2015-11-14 15:00:36 +01:00
|
|
|
|
#:use-module ((rnrs bytevectors) #:select (bytevector-u8-set!))
|
Add (guix memoization).
* guix/combinators.scm (memoize): Remove.
* guix/memoization.scm: New file.
* Makefile.am (MODULES): Add it.
* gnu/packages.scm, gnu/packages/bootstrap.scm,
guix/build-system/gnu.scm, guix/build-system/python.scm,
guix/derivations.scm, guix/gnu-maintenance.scm,
guix/import/cran.scm, guix/import/elpa.scm,
guix/modules.scm, guix/scripts/build.scm,
guix/scripts/graph.scm, guix/scripts/lint.scm,
guix/store.scm, guix/utils.scm: Adjust imports accordingly.
2017-01-28 16:33:57 +01:00
|
|
|
|
#:use-module (guix memoization)
|
2020-09-19 14:11:47 +02:00
|
|
|
|
#:use-module ((guix build utils)
|
|
|
|
|
#:select (dump-port mkdir-p delete-file-recursively
|
2020-09-19 06:20:47 +02:00
|
|
|
|
call-with-temporary-output-file %xz-parallel-args))
|
2016-06-13 17:52:08 +02:00
|
|
|
|
#:use-module ((guix build syscalls) #:select (mkdtemp! fdatasync))
|
2021-01-19 22:28:08 +01:00
|
|
|
|
#:use-module ((guix combinators) #:select (fold2))
|
2020-07-24 22:58:08 +02:00
|
|
|
|
#:use-module (guix diagnostics) ;<location>, &error-location, etc.
|
2012-06-01 23:29:55 +02:00
|
|
|
|
#:use-module (ice-9 format)
|
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)
|
2017-03-09 09:48:33 +01:00
|
|
|
|
#:use-module ((ice-9 iconv) #:prefix iconv:)
|
2021-06-21 12:21:19 +02:00
|
|
|
|
#:use-module (ice-9 vlist)
|
2021-03-12 14:38:10 +01:00
|
|
|
|
#:autoload (zlib) (make-zlib-input-port make-zlib-output-port)
|
2014-01-10 23:27:39 +01:00
|
|
|
|
#:use-module (system foreign)
|
2020-07-24 23:10:32 +02:00
|
|
|
|
#:re-export (<location> ;for backwards compatibility
|
2020-07-24 22:58:08 +02:00
|
|
|
|
location
|
|
|
|
|
location?
|
|
|
|
|
location-file
|
|
|
|
|
location-line
|
|
|
|
|
location-column
|
|
|
|
|
source-properties->location
|
|
|
|
|
location->source-properties
|
|
|
|
|
|
|
|
|
|
&error-location
|
|
|
|
|
error-location?
|
2020-07-24 23:28:11 +02:00
|
|
|
|
error-location
|
|
|
|
|
|
|
|
|
|
&fix-hint
|
|
|
|
|
fix-hint?
|
2020-09-19 14:11:47 +02:00
|
|
|
|
condition-fix-hint
|
|
|
|
|
|
|
|
|
|
call-with-temporary-output-file)
|
utils: Move base16 procedures to (guix base16).
* guix/utils.scm (bytevector->base16-string, base16-string->bytevector):
Move to...
* guix/base16.scm: ... here. New file.
* tests/utils.scm ("bytevector->base16-string->bytevector"): Move to...
* tests/base16.scm: ... here. New file.
* Makefile.am (MODULES): Add guix/base16.scm.
(SCM_TESTS): Add tests/base16.scm.
* build-aux/download.scm, guix/derivations.scm,
guix/docker.scm, guix/import/snix.scm, guix/pk-crypto.scm,
guix/scripts/authenticate.scm, guix/scripts/download.scm,
guix/scripts/hash.scm, guix/store.scm, tests/hash.scm,
tests/pk-crypto.scm: Adjust imports accordingly.
2017-03-15 21:54:34 +01:00
|
|
|
|
#:export (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
|
|
|
|
|
2016-06-15 23:08:05 +02:00
|
|
|
|
current-source-directory
|
|
|
|
|
|
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
|
2021-07-14 13:12:46 +02:00
|
|
|
|
target-linux?
|
2021-07-14 13:12:47 +02:00
|
|
|
|
target-hurd?
|
2016-05-28 17:32:04 +02:00
|
|
|
|
target-mingw?
|
2021-07-14 13:12:49 +02:00
|
|
|
|
target-x86-32?
|
|
|
|
|
target-x86-64?
|
2017-11-29 15:10:12 +01:00
|
|
|
|
target-arm32?
|
2019-08-20 17:45:50 +02:00
|
|
|
|
target-aarch64?
|
|
|
|
|
target-arm?
|
2021-08-17 08:39:46 +02:00
|
|
|
|
target-ppc32?
|
2021-08-17 11:41:44 +02:00
|
|
|
|
target-ppc64le?
|
2021-02-02 09:00:57 +01:00
|
|
|
|
target-powerpc?
|
2021-08-01 10:01:41 +02:00
|
|
|
|
target-riscv64?
|
2018-06-12 19:51:23 +02:00
|
|
|
|
target-64bit?
|
2022-03-08 14:46:56 +01:00
|
|
|
|
ar-for-target
|
2020-05-28 18:53:13 +02:00
|
|
|
|
cc-for-target
|
2020-11-16 10:26:46 +01:00
|
|
|
|
cxx-for-target
|
2022-03-08 14:46:56 +01:00
|
|
|
|
ld-for-target
|
2020-12-28 18:00:30 +01:00
|
|
|
|
pkg-config-for-target
|
2020-05-28 18:53:13 +02:00
|
|
|
|
|
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
|
2020-08-13 13:47:27 +02:00
|
|
|
|
version-major+minor+point
|
2014-10-19 04:03:42 +02:00
|
|
|
|
version-major+minor
|
2017-10-09 23:02:05 +02:00
|
|
|
|
version-major
|
2021-01-10 11:53:43 +01:00
|
|
|
|
version-unique-prefix
|
2013-08-23 15:51:36 +02:00
|
|
|
|
guile-version>?
|
2018-05-13 12:16:08 +02:00
|
|
|
|
version-prefix?
|
2013-11-12 22:56:13 +01:00
|
|
|
|
string-replace-substring
|
2013-04-24 23:48:36 +02:00
|
|
|
|
file-extension
|
2013-10-09 00:04:45 +02:00
|
|
|
|
file-sans-extension
|
2019-08-27 18:20:16 +02:00
|
|
|
|
tarball-sans-extension
|
2016-08-01 17:42:09 +02:00
|
|
|
|
compressed-file?
|
2015-10-26 22:17:25 +01:00
|
|
|
|
switch-symlinks
|
2015-06-25 03:16:11 +02:00
|
|
|
|
call-with-temporary-directory
|
2013-12-29 15:51:07 +01:00
|
|
|
|
with-atomic-file-output
|
2017-07-28 14:51:44 +02:00
|
|
|
|
|
2020-06-06 23:00:05 +02:00
|
|
|
|
with-environment-variables
|
|
|
|
|
arguments-from-environment-variable
|
|
|
|
|
|
2017-07-28 14:51:44 +02:00
|
|
|
|
config-directory
|
2015-10-17 13:02:53 +02:00
|
|
|
|
cache-directory
|
2017-07-28 14:51:44 +02:00
|
|
|
|
|
2015-10-26 20:01:45 +01:00
|
|
|
|
readlink*
|
2021-06-21 12:21:19 +02:00
|
|
|
|
go-to-location
|
2016-04-06 11:35:13 +02:00
|
|
|
|
edit-expression
|
2014-03-22 21:58:23 +01:00
|
|
|
|
|
|
|
|
|
filtered-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
|
2021-01-19 22:28:08 +01:00
|
|
|
|
canonical-newline-port
|
|
|
|
|
|
|
|
|
|
string-distance
|
2018-01-09 23:00:11 +01:00
|
|
|
|
string-closest
|
|
|
|
|
|
|
|
|
|
pretty-print-table))
|
2012-06-01 23:08:32 +02:00
|
|
|
|
|
2020-06-06 23:00:05 +02:00
|
|
|
|
|
|
|
|
|
;;;
|
|
|
|
|
;;; Environment variables.
|
|
|
|
|
;;;
|
|
|
|
|
|
|
|
|
|
(define (call-with-environment-variables variables thunk)
|
|
|
|
|
"Call THUNK with the environment VARIABLES set."
|
|
|
|
|
(let ((environment (environ)))
|
|
|
|
|
(dynamic-wind
|
|
|
|
|
(lambda ()
|
|
|
|
|
(for-each (match-lambda
|
|
|
|
|
((variable value)
|
|
|
|
|
(setenv variable value)))
|
|
|
|
|
variables))
|
|
|
|
|
thunk
|
|
|
|
|
(lambda ()
|
|
|
|
|
(environ environment)))))
|
|
|
|
|
|
|
|
|
|
(define-syntax-rule (with-environment-variables variables exp ...)
|
|
|
|
|
"Evaluate EXP with the given environment VARIABLES set."
|
|
|
|
|
(call-with-environment-variables variables
|
|
|
|
|
(lambda () exp ...)))
|
|
|
|
|
|
|
|
|
|
(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-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 ()
|
2017-10-14 16:45:55 +02:00
|
|
|
|
(close-port input)
|
2013-04-29 23:25:19 +02:00
|
|
|
|
(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
|
2017-10-14 16:45:55 +02:00
|
|
|
|
(close-port input)
|
2013-04-29 23:25:19 +02:00
|
|
|
|
(close-port out)
|
|
|
|
|
(loop in (cons child pids)))))))))
|
|
|
|
|
|
2019-05-23 22:14:53 +02:00
|
|
|
|
(define (lzip-port proc port . args)
|
|
|
|
|
"Return the lzip port produced by calling PROC (a symbol) on PORT and ARGS.
|
|
|
|
|
Raise an error if lzlib support is missing."
|
Use "guile-zlib" and "guile-lzlib" instead of (guix config).
* Makefile.am (MODULES): Remove guix/zlib.scm and guix/lzlib.scm,
(SCM_TESTS): remove tests/zlib.scm, tests/lzlib.scm.
* build-aux/build-self.scm (make-config.scm): Remove unused %libz variable.
* configure.ac: Remove LIBZ and LIBLZ variables and check instead for
Guile-zlib and Guile-lzlib.
* doc/guix.texi ("Requirements"): Remove zlib requirement and add Guile-zlib
and Guile-lzlib instead.
* gnu/packages/package-management.scm (guix)[native-inputs]: Add "guile-zlib"
and "guile-lzlib",
[inputs]: remove "zlib" and "lzlib",
[propagated-inputs]: ditto,
[arguments]: add "guile-zlib" and "guile-lzlib" to Guile load path.
* guix/config.scm.in (%libz, %liblz): Remove them.
* guix/lzlib.scm: Remove it.
* guix/man-db.scm: Use (zlib) instead of (guix zlib).
* guix/profiles.scm (manual-database): Do not stub (guix config) in imported
modules list, instead add "guile-zlib" to the extension list.
* guix/scripts/publish.scm: Use (zlib) instead of (guix zlib) and (lzlib)
instead of (guix lzlib),
(string->compression-type, effective-compression): do not check for zlib and
lzlib availability.
* guix/scripts/substitute.scm (%compression-methods): Do not check for lzlib
availability.
* guix/self.scm (specification->package): Add "guile-zlib" and "guile-lzlib"
and remove "zlib" and "lzlib",
(compiled-guix): remove "zlib" and "lzlib" arguments and add guile-zlib and
guile-lzlib to the dependencies, also do not pass "zlib" and "lzlib" to
"make-config.scm" procedure,
(make-config.scm): remove "zlib" and "lzlib" arguments as well as %libz and
%liblz variables.
* guix/utils.scm (lzip-port): Use (lzlib) instead of (guix lzlib) and do not
check for lzlib availability.
* guix/zlib.scm: Remove it.
* m4/guix.m4 (GUIX_LIBZ_LIBDIR, GUIX_LIBLZ_FILE_NAME): Remove them.
* tests/lzlib.scm: Use (zlib) instead of (guix zlib) and (lzlib)
instead of (guix lzlib), and do not check for zlib and lzlib availability.
* tests/publish.scm: Ditto.
* tests/substitute.scm: Do not check for lzlib availability.
* tests/utils.scm: Ditto.
* tests/zlib.scm: Remove it.
2020-07-27 16:36:39 +02:00
|
|
|
|
(let ((make-port (module-ref (resolve-interface '(lzlib)) proc)))
|
2020-12-26 23:01:05 +01:00
|
|
|
|
(make-port port)))
|
|
|
|
|
|
|
|
|
|
(define (zstd-port proc port . args)
|
|
|
|
|
"Return the zstd port produced by calling PROC (a symbol) on PORT and ARGS.
|
|
|
|
|
Raise an error if zstd support is missing."
|
|
|
|
|
(let ((make-port (module-ref (resolve-interface '(zstd)) proc)))
|
|
|
|
|
(make-port port)))
|
2019-05-23 22:14:53 +02:00
|
|
|
|
|
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))
|
2018-07-22 18:01:35 +02:00
|
|
|
|
('xz (filtered-port `(,%xz "-dc") input))
|
2021-03-12 14:38:10 +01:00
|
|
|
|
('gzip (values (make-zlib-input-port input #:format 'gzip)
|
|
|
|
|
'()))
|
2019-05-23 22:14:53 +02:00
|
|
|
|
('lzip (values (lzip-port 'make-lzip-input-port input)
|
|
|
|
|
'()))
|
2020-12-26 23:01:05 +01:00
|
|
|
|
('zstd (values (zstd-port 'make-zstd-input-port input)
|
|
|
|
|
'()))
|
2019-05-23 22:14:53 +02:00
|
|
|
|
(_ (error "unsupported compression scheme" compression))))
|
2014-03-22 21:58:23 +01:00
|
|
|
|
|
|
|
|
|
(define (compressed-port compression input)
|
2019-05-23 22:14:53 +02:00
|
|
|
|
"Return an input port where INPUT is compressed according to COMPRESSION,
|
2014-03-22 21:58:23 +01:00
|
|
|
|
a symbol such as 'xz."
|
|
|
|
|
(match compression
|
|
|
|
|
((or #f 'none) (values input '()))
|
|
|
|
|
('bzip2 (filtered-port `(,%bzip2 "-c") input))
|
2020-09-19 06:20:47 +02:00
|
|
|
|
('xz (filtered-port `(,%xz "-c" ,@(%xz-parallel-args)) input))
|
2014-03-22 21:58:23 +01:00
|
|
|
|
('gzip (filtered-port `(,%gzip "-c") input))
|
2019-05-23 22:14:53 +02:00
|
|
|
|
('lzip (values (lzip-port 'make-lzip-input-port/compressed input)
|
|
|
|
|
'()))
|
|
|
|
|
(_ (error "unsupported compression scheme" compression))))
|
2014-03-22 21:58:23 +01:00
|
|
|
|
|
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))
|
2018-07-22 18:01:35 +02:00
|
|
|
|
('xz (filtered-output-port `(,%xz "-c" ,@options) output))
|
2021-03-12 14:38:10 +01:00
|
|
|
|
('gzip (values (make-zlib-output-port output #:format 'gzip)
|
|
|
|
|
'()))
|
2019-05-23 22:14:53 +02:00
|
|
|
|
('lzip (values (lzip-port 'make-lzip-output-port output)
|
|
|
|
|
'()))
|
2020-12-26 23:01:05 +01:00
|
|
|
|
('zstd (values (zstd-port 'make-zstd-output-port output)
|
|
|
|
|
'()))
|
2019-05-23 22:14:53 +02:00
|
|
|
|
(_ (error "unsupported compression scheme" compression))))
|
2014-03-24 21:09:15 +01:00
|
|
|
|
|
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))))))
|
|
|
|
|
|
2021-06-21 12:21:19 +02:00
|
|
|
|
(define %source-location-map
|
|
|
|
|
;; Maps inode/device tuples to "source location maps" used by
|
|
|
|
|
;; 'go-to-location'.
|
|
|
|
|
(make-hash-table))
|
|
|
|
|
|
2021-06-21 16:13:35 +02:00
|
|
|
|
(define (source-location-key/stamp stat)
|
|
|
|
|
"Return two values: the key for STAT in %SOURCE-LOCATION-MAP, and a stamp
|
|
|
|
|
used to invalidate corresponding entries."
|
|
|
|
|
(let ((key (list (stat:ino stat) (stat:dev stat)))
|
|
|
|
|
(stamp (list (stat:mtime stat) (stat:mtimensec stat)
|
|
|
|
|
(stat:size stat))))
|
|
|
|
|
(values key stamp)))
|
|
|
|
|
|
|
|
|
|
(define* (go-to-location port line column)
|
2021-06-21 12:21:19 +02:00
|
|
|
|
"Jump to LINE and COLUMN (both one-indexed) in PORT. Maintain a source
|
|
|
|
|
location map such that this can boil down to seek(2) and a few read(2) calls,
|
|
|
|
|
which can drastically speed up repetitive operations on large files."
|
|
|
|
|
(let* ((stat (stat port))
|
2021-06-21 16:13:35 +02:00
|
|
|
|
(key stamp (source-location-key/stamp stat))
|
2021-06-21 12:21:19 +02:00
|
|
|
|
|
|
|
|
|
;; Look for an up-to-date source map for KEY. The map is a vlist
|
|
|
|
|
;; where each entry gives the byte offset of the beginning of a line:
|
|
|
|
|
;; element 0 is the offset of the first line, element 1 the offset of
|
|
|
|
|
;; the second line, etc. The map is filled lazily.
|
|
|
|
|
(source-map (match (hash-ref %source-location-map key)
|
|
|
|
|
(#f
|
|
|
|
|
(vlist-cons 0 vlist-null))
|
|
|
|
|
((cache-stamp ... map)
|
|
|
|
|
(if (equal? cache-stamp stamp) ;invalidate?
|
|
|
|
|
map
|
|
|
|
|
(vlist-cons 0 vlist-null)))))
|
|
|
|
|
(last (vlist-length source-map)))
|
|
|
|
|
;; Jump to LINE, ideally via SOURCE-MAP.
|
|
|
|
|
(if (<= line last)
|
|
|
|
|
(seek port (vlist-ref source-map (- line 1)) SEEK_SET)
|
|
|
|
|
(let ((target line)
|
|
|
|
|
(offset (vlist-ref source-map (- last 1))))
|
|
|
|
|
(seek port offset SEEK_SET)
|
|
|
|
|
(let loop ((source-map (vlist-reverse source-map))
|
|
|
|
|
(line last))
|
|
|
|
|
(if (< line target)
|
|
|
|
|
(match (read-char port)
|
|
|
|
|
(#\newline
|
|
|
|
|
(loop (vlist-cons (ftell port) source-map)
|
|
|
|
|
(+ 1 line)))
|
|
|
|
|
((? eof-object?)
|
|
|
|
|
(error "unexpected end of file" port line))
|
|
|
|
|
(chr (loop source-map line)))
|
|
|
|
|
(hash-set! %source-location-map key
|
|
|
|
|
`(,@stamp
|
|
|
|
|
,(vlist-reverse source-map)))))))
|
|
|
|
|
|
|
|
|
|
;; Read up to COLUMN.
|
|
|
|
|
(let ((target column))
|
|
|
|
|
(let loop ((column 1))
|
|
|
|
|
(when (< column target)
|
|
|
|
|
(match (read-char port)
|
|
|
|
|
(#\newline (error "unexpected end of line" port))
|
|
|
|
|
(#\tab (loop (+ 8 column)))
|
|
|
|
|
(chr (loop (+ 1 column)))))))
|
|
|
|
|
|
|
|
|
|
;; Update PORT's position info.
|
|
|
|
|
(set-port-line! port (- line 1))
|
|
|
|
|
(set-port-column! port (- column 1))))
|
|
|
|
|
|
2021-06-21 16:13:35 +02:00
|
|
|
|
(define (move-source-location-map! source target line)
|
|
|
|
|
"Move the source location map from SOURCE up to LINE to TARGET. SOURCE and
|
|
|
|
|
TARGET must be stat buffers as returned by 'stat'."
|
|
|
|
|
(let* ((source-key (source-location-key/stamp source))
|
|
|
|
|
(target-key target-stamp (source-location-key/stamp target)))
|
|
|
|
|
(match (hash-ref %source-location-map source-key)
|
|
|
|
|
(#f #t)
|
|
|
|
|
((_ ... source-map)
|
|
|
|
|
;; Strip the source map and update the associated stamp.
|
|
|
|
|
(let ((source-map (vlist-take source-map (max line 1))))
|
|
|
|
|
(hash-remove! %source-location-map source-key)
|
|
|
|
|
(hash-set! %source-location-map target-key
|
|
|
|
|
`(,@target-stamp ,source-map)))))))
|
|
|
|
|
|
2016-04-06 11:35:13 +02:00
|
|
|
|
(define* (edit-expression source-properties proc #:key (encoding "UTF-8"))
|
|
|
|
|
"Edit the expression specified by SOURCE-PROPERTIES using PROC, which should
|
|
|
|
|
be a procedure that takes the original expression in string and returns a new
|
|
|
|
|
one. ENCODING will be used to interpret all port I/O, it default to UTF-8.
|
|
|
|
|
This procedure returns #t on success."
|
2021-06-19 22:00:44 +02:00
|
|
|
|
(define file (assq-ref source-properties 'filename))
|
|
|
|
|
(define line (assq-ref source-properties 'line))
|
|
|
|
|
(define column (assq-ref source-properties 'column))
|
|
|
|
|
|
2016-04-06 11:35:13 +02:00
|
|
|
|
(with-fluids ((%default-port-encoding encoding))
|
2021-06-19 22:00:44 +02:00
|
|
|
|
(call-with-input-file file
|
|
|
|
|
(lambda (in)
|
|
|
|
|
(let* ( ;; The start byte position of the expression.
|
2021-06-21 12:21:19 +02:00
|
|
|
|
(start (begin (go-to-location in (+ 1 line) (+ 1 column))
|
2021-06-19 22:00:44 +02:00
|
|
|
|
(ftell in)))
|
|
|
|
|
;; The end byte position of the expression.
|
|
|
|
|
(end (begin (read in) (ftell in))))
|
|
|
|
|
(seek in 0 SEEK_SET) ; read from the beginning of the file.
|
|
|
|
|
(let* ((pre-bv (get-bytevector-n in start))
|
|
|
|
|
;; The expression in string form.
|
|
|
|
|
(str (iconv:bytevector->string
|
|
|
|
|
(get-bytevector-n in (- end start))
|
|
|
|
|
(port-encoding in)))
|
|
|
|
|
(str* (proc str)))
|
2021-06-21 13:56:59 +02:00
|
|
|
|
;; Modify FILE only if there are changes.
|
|
|
|
|
(unless (string=? str* str)
|
|
|
|
|
;; Verify the edited expression is still a scheme expression.
|
|
|
|
|
(call-with-input-string str* read)
|
2022-01-08 17:29:12 +01:00
|
|
|
|
|
|
|
|
|
(let ((post-bv (get-bytevector-all in)))
|
|
|
|
|
;; Update the file with edited expression.
|
|
|
|
|
(with-atomic-file-output file
|
|
|
|
|
(lambda (out)
|
|
|
|
|
(put-bytevector out pre-bv)
|
|
|
|
|
(display str* out)
|
|
|
|
|
(unless (eof-object? post-bv)
|
|
|
|
|
;; Copy everything that came after STR.
|
|
|
|
|
(put-bytevector out post-bv))))
|
|
|
|
|
|
|
|
|
|
;; Due to 'with-atomic-file-output', IN and FILE no longer
|
|
|
|
|
;; share the same inode, but we can reassign the source map up
|
|
|
|
|
;; to LINE to the new file.
|
|
|
|
|
(move-source-location-map! (stat in) (stat file)
|
|
|
|
|
(+ 1 line))))))))))
|
2016-04-06 11:35:13 +02:00
|
|
|
|
|
2012-06-07 23:15:00 +02:00
|
|
|
|
|
|
|
|
|
;;;
|
utils: Move combinators to (guix combinators).
* guix/utils.scm (compile-time-value, memoize, fold2)
(fold-tree, fold-tree-leaves): Move to...
* guix/combinators: ... here. New file.
* tests/utils.scm ("fold2, 1 list", "fold2, 2 lists")
(fold-tree tests): Move to...
* tests/combinators.scm: ... here. New file.
* Makefile.am (MODULES, SCM_TESTS): Add them.
* gnu/packages.scm, gnu/packages/bootstrap.scm,
gnu/services/herd.scm, guix/build-system/gnu.scm,
guix/build-system/python.scm, guix/derivations.scm,
guix/gnu-maintenance.scm, guix/import/elpa.scm,
guix/scripts/archive.scm, guix/scripts/build.scm,
guix/scripts/graph.scm, guix/scripts/lint.scm,
guix/scripts/size.scm, guix/scripts/substitute.scm,
guix/serialization.scm, guix/store.scm, guix/ui.scm: Adjust imports
accordingly.
2016-05-04 17:35:47 +02:00
|
|
|
|
;;; Keyword arguments.
|
2012-06-07 23:15:00 +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
|
2016-09-01 15:20:25 +02:00
|
|
|
|
(if (memq kw args)
|
2012-11-05 23:46:55 +01:00
|
|
|
|
args
|
|
|
|
|
(cons* kw value args))))
|
|
|
|
|
(()
|
|
|
|
|
args))))
|
|
|
|
|
|
2016-09-20 22:41:31 +02:00
|
|
|
|
(define-syntax collect-default-args
|
|
|
|
|
(syntax-rules ()
|
|
|
|
|
((_)
|
|
|
|
|
'())
|
|
|
|
|
((_ (_ _) rest ...)
|
|
|
|
|
(collect-default-args rest ...))
|
|
|
|
|
((_ (kw _ dflt) rest ...)
|
|
|
|
|
(cons* kw dflt (collect-default-args rest ...)))))
|
|
|
|
|
|
2012-11-05 23:46:55 +01:00
|
|
|
|
(define-syntax substitute-keyword-arguments
|
|
|
|
|
(syntax-rules ()
|
|
|
|
|
"Return a new list of arguments where the value for keyword arg KW is
|
2016-09-20 22:41:31 +02:00
|
|
|
|
replaced by EXP. EXP is evaluated in a context where VAR is bound to the
|
|
|
|
|
previous value of the keyword argument, or DFLT if given."
|
|
|
|
|
((_ original-args ((kw var dflt ...) exp) ...)
|
|
|
|
|
(let loop ((args (default-keyword-arguments
|
|
|
|
|
original-args
|
|
|
|
|
(collect-default-args (kw var dflt ...) ...)))
|
2012-11-05 23:46:55 +01:00
|
|
|
|
(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))))))))
|
|
|
|
|
|
utils: Move combinators to (guix combinators).
* guix/utils.scm (compile-time-value, memoize, fold2)
(fold-tree, fold-tree-leaves): Move to...
* guix/combinators: ... here. New file.
* tests/utils.scm ("fold2, 1 list", "fold2, 2 lists")
(fold-tree tests): Move to...
* tests/combinators.scm: ... here. New file.
* Makefile.am (MODULES, SCM_TESTS): Add them.
* gnu/packages.scm, gnu/packages/bootstrap.scm,
gnu/services/herd.scm, guix/build-system/gnu.scm,
guix/build-system/python.scm, guix/derivations.scm,
guix/gnu-maintenance.scm, guix/import/elpa.scm,
guix/scripts/archive.scm, guix/scripts/build.scm,
guix/scripts/graph.scm, guix/scripts/lint.scm,
guix/scripts/size.scm, guix/scripts/substitute.scm,
guix/serialization.scm, guix/store.scm, guix/ui.scm: Adjust imports
accordingly.
2016-05-04 17:35:47 +02:00
|
|
|
|
|
|
|
|
|
;;;
|
|
|
|
|
;;; System strings.
|
|
|
|
|
;;;
|
|
|
|
|
|
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))
|
|
|
|
|
|
2017-01-11 11:55:51 +01:00
|
|
|
|
(define* (package-name->name+version spec
|
|
|
|
|
#:optional (delimiter #\@))
|
2016-02-28 23:11:36 +01:00
|
|
|
|
"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
|
2017-01-11 11:55:51 +01:00
|
|
|
|
returned. Both parts must not contain any '@'. Optionally, DELIMITER can be
|
|
|
|
|
a character other than '@'."
|
|
|
|
|
(match (string-rindex spec delimiter)
|
2016-02-28 23:11:36 +01:00
|
|
|
|
(#f (values spec #f))
|
|
|
|
|
(idx (values (substring spec 0 idx)
|
|
|
|
|
(substring spec (1+ idx))))))
|
|
|
|
|
|
2021-07-14 13:12:46 +02:00
|
|
|
|
(define* (target-linux? #:optional (target (or (%current-target-system)
|
|
|
|
|
(%current-system))))
|
|
|
|
|
"Does the operating system of TARGET use the Linux kernel?"
|
|
|
|
|
(->bool (string-contains target "linux")))
|
|
|
|
|
|
2021-07-14 13:12:47 +02:00
|
|
|
|
(define* (target-hurd? #:optional (target (or (%current-target-system)
|
|
|
|
|
(%current-system))))
|
|
|
|
|
"Does TARGET represent the GNU(/Hurd) system?"
|
|
|
|
|
(and (string-suffix? "-gnu" target)
|
|
|
|
|
(not (string-contains target "linux"))))
|
|
|
|
|
|
2016-05-28 17:32:04 +02:00
|
|
|
|
(define* (target-mingw? #:optional (target (%current-target-system)))
|
2021-07-14 13:12:48 +02:00
|
|
|
|
"Is the operating system of TARGET Windows?"
|
2016-05-28 17:32:04 +02:00
|
|
|
|
(and target
|
2021-07-14 13:12:48 +02:00
|
|
|
|
;; The "-32" doesn't mean TARGET is 32-bit, as "x86_64-w64-mingw32"
|
|
|
|
|
;; is a valid triplet (see the (gnu ci) module) and 'w64' and 'x86_64'
|
|
|
|
|
;; are 64-bit.
|
2016-05-28 17:32:04 +02:00
|
|
|
|
(string-suffix? "-mingw32" target)))
|
|
|
|
|
|
2021-07-14 13:12:49 +02:00
|
|
|
|
(define* (target-x86-32? #:optional (target (or (%current-target-system)
|
|
|
|
|
(%current-system))))
|
|
|
|
|
"Is the architecture of TARGET a variant of Intel's 32-bit architecture
|
|
|
|
|
(IA32)?"
|
|
|
|
|
;; Intel also has a 16-bit architecture in the iN86 series, i286
|
2021-12-29 22:55:55 +01:00
|
|
|
|
;; (see, e.g., https://en.wikipedia.org/wiki/Intel_80286) so this
|
2021-07-14 13:12:49 +02:00
|
|
|
|
;; procedure is not named target-x86?.
|
|
|
|
|
(or (string-prefix? "i386-" target)
|
|
|
|
|
(string-prefix? "i486-" target)
|
|
|
|
|
(string-prefix? "i586-" target)
|
|
|
|
|
(string-prefix? "i686-" target)))
|
|
|
|
|
|
|
|
|
|
(define* (target-x86-64? #:optional (target (or (%current-target-system)
|
|
|
|
|
(%current-system))))
|
|
|
|
|
"Is the architecture of TARGET a variant of Intel/AMD's 64-bit
|
|
|
|
|
architecture (x86_64)?"
|
|
|
|
|
(string-prefix? "x86_64-" target))
|
|
|
|
|
|
2020-05-15 22:22:03 +02:00
|
|
|
|
(define* (target-arm32? #:optional (target (or (%current-target-system)
|
|
|
|
|
(%current-system))))
|
|
|
|
|
(string-prefix? "arm" target))
|
2017-11-29 15:10:12 +01:00
|
|
|
|
|
2020-05-15 22:22:03 +02:00
|
|
|
|
(define* (target-aarch64? #:optional (target (or (%current-target-system)
|
|
|
|
|
(%current-system))))
|
|
|
|
|
(string-prefix? "aarch64" target))
|
2019-08-20 17:45:50 +02:00
|
|
|
|
|
2020-05-15 22:22:03 +02:00
|
|
|
|
(define* (target-arm? #:optional (target (or (%current-target-system)
|
|
|
|
|
(%current-system))))
|
|
|
|
|
(or (target-arm32? target) (target-aarch64? target)))
|
2019-08-20 17:45:50 +02:00
|
|
|
|
|
2021-08-17 08:39:46 +02:00
|
|
|
|
(define* (target-ppc32? #:optional (target (or (%current-target-system)
|
|
|
|
|
(%current-system))))
|
|
|
|
|
(string-prefix? "powerpc-" target))
|
|
|
|
|
|
2021-08-17 11:41:44 +02:00
|
|
|
|
(define* (target-ppc64le? #:optional (target (or (%current-target-system)
|
|
|
|
|
(%current-system))))
|
|
|
|
|
(string-prefix? "powerpc64le-" target))
|
|
|
|
|
|
2021-02-02 09:00:57 +01:00
|
|
|
|
(define* (target-powerpc? #:optional (target (or (%current-target-system)
|
|
|
|
|
(%current-system))))
|
|
|
|
|
(string-prefix? "powerpc" target))
|
|
|
|
|
|
2021-08-01 10:01:41 +02:00
|
|
|
|
(define* (target-riscv64? #:optional (target (or (%current-target-system)
|
|
|
|
|
(%current-system))))
|
|
|
|
|
"Is the architecture of TARGET a 'riscv64' machine?"
|
|
|
|
|
(string-prefix? "riscv64" target))
|
|
|
|
|
|
2020-05-15 22:22:03 +02:00
|
|
|
|
(define* (target-64bit? #:optional (system (or (%current-target-system)
|
|
|
|
|
(%current-system))))
|
2021-06-12 22:23:43 +02:00
|
|
|
|
(any (cut string-prefix? <> system) '("x86_64" "aarch64" "mips64"
|
|
|
|
|
"powerpc64" "riscv64")))
|
2018-06-12 19:51:23 +02:00
|
|
|
|
|
2022-03-08 14:46:56 +01:00
|
|
|
|
(define* (ar-for-target #:optional (target (%current-target-system)))
|
|
|
|
|
(if target
|
|
|
|
|
(string-append target "-ar")
|
|
|
|
|
"ar"))
|
|
|
|
|
|
2020-05-28 18:53:13 +02:00
|
|
|
|
(define* (cc-for-target #:optional (target (%current-target-system)))
|
|
|
|
|
(if target
|
|
|
|
|
(string-append target "-gcc")
|
|
|
|
|
"gcc"))
|
|
|
|
|
|
2020-11-16 10:26:46 +01:00
|
|
|
|
(define* (cxx-for-target #:optional (target (%current-target-system)))
|
|
|
|
|
(if target
|
|
|
|
|
(string-append target "-g++")
|
|
|
|
|
"g++"))
|
|
|
|
|
|
2022-03-08 14:46:56 +01:00
|
|
|
|
(define* (ld-for-target #:optional (target (%current-target-system)))
|
|
|
|
|
(if target
|
|
|
|
|
(string-append target "-ld")
|
|
|
|
|
"ld"))
|
|
|
|
|
|
2020-12-28 18:00:30 +01:00
|
|
|
|
(define* (pkg-config-for-target #:optional (target (%current-target-system)))
|
|
|
|
|
(if target
|
|
|
|
|
(string-append target "-pkg-config")
|
|
|
|
|
"pkg-config"))
|
|
|
|
|
|
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) "."))
|
|
|
|
|
|
2020-08-13 13:47:27 +02:00
|
|
|
|
(define (version-major+minor+point version-string)
|
|
|
|
|
"Return \"major>.<minor>.<point>\", where major, minor and point are the
|
|
|
|
|
major, minor and point version numbers from the version-string. For example,
|
|
|
|
|
(version-major+minor+point \"6.4.5.2\") returns \"6.4.5\" or
|
|
|
|
|
(version-major+minor+point \"1.19.2-2581-324ca14c3003\") returns \"1.19.2\"."
|
|
|
|
|
(let* ((3-dot (version-prefix version-string 3))
|
|
|
|
|
(index (string-index 3-dot #\-)))
|
|
|
|
|
(or (false-if-exception (substring 3-dot 0 index))
|
|
|
|
|
3-dot)))
|
2014-10-19 04:03:42 +02:00
|
|
|
|
|
|
|
|
|
(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))
|
|
|
|
|
|
2017-10-09 23:02:05 +02:00
|
|
|
|
(define (version-major version-string)
|
|
|
|
|
"Return the major version number as string from the version-string."
|
|
|
|
|
(version-prefix version-string 1))
|
|
|
|
|
|
2021-01-10 11:53:43 +01:00
|
|
|
|
(define (version-unique-prefix version versions)
|
|
|
|
|
"Return the shortest version prefix to unambiguously identify VERSION among
|
|
|
|
|
VERSIONS. For example:
|
|
|
|
|
|
|
|
|
|
(version-unique-prefix \"2.0\" '(\"3.0\" \"2.0\"))
|
|
|
|
|
=> \"2\"
|
|
|
|
|
|
|
|
|
|
(version-unique-prefix \"2.2\" '(\"3.0.5\" \"2.0.9\" \"2.2.7\"))
|
|
|
|
|
=> \"2.2\"
|
|
|
|
|
|
|
|
|
|
(version-unique-prefix \"27.1\" '(\"27.1\"))
|
|
|
|
|
=> \"\"
|
|
|
|
|
"
|
|
|
|
|
(define not-dot
|
|
|
|
|
(char-set-complement (char-set #\.)))
|
|
|
|
|
|
|
|
|
|
(define other-versions
|
|
|
|
|
(delete version versions))
|
|
|
|
|
|
|
|
|
|
(let loop ((prefix '())
|
|
|
|
|
(components (string-tokenize version not-dot)))
|
|
|
|
|
(define prefix-str
|
|
|
|
|
(string-join prefix "."))
|
|
|
|
|
|
|
|
|
|
(if (any (cut string-prefix? prefix-str <>) other-versions)
|
|
|
|
|
(match components
|
|
|
|
|
((head . tail)
|
|
|
|
|
(loop `(,@prefix ,head) tail))
|
|
|
|
|
(()
|
|
|
|
|
version))
|
|
|
|
|
prefix-str)))
|
|
|
|
|
|
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))
|
|
|
|
|
|
2018-05-13 12:16:08 +02:00
|
|
|
|
(define version-prefix?
|
|
|
|
|
(let ((not-dot (char-set-complement (char-set #\.))))
|
|
|
|
|
(lambda (v1 v2)
|
|
|
|
|
"Return true if V1 is a version prefix of V2:
|
|
|
|
|
|
|
|
|
|
(version-prefix? \"4.1\" \"4.16.2\") => #f
|
|
|
|
|
(version-prefix? \"4.1\" \"4.1.2\") => #t
|
|
|
|
|
"
|
|
|
|
|
(define (list-prefix? lst1 lst2)
|
|
|
|
|
(match lst1
|
|
|
|
|
(() #t)
|
|
|
|
|
((head1 tail1 ...)
|
|
|
|
|
(match lst2
|
|
|
|
|
(() #f)
|
|
|
|
|
((head2 tail2 ...)
|
|
|
|
|
(and (equal? head1 head2)
|
|
|
|
|
(list-prefix? tail1 tail2)))))))
|
|
|
|
|
|
|
|
|
|
(list-prefix? (string-tokenize v1 not-dot)
|
|
|
|
|
(string-tokenize v2 not-dot)))))
|
|
|
|
|
|
2020-06-06 23:00:05 +02:00
|
|
|
|
|
|
|
|
|
;;;
|
|
|
|
|
;;; Files.
|
|
|
|
|
;;;
|
|
|
|
|
|
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)))
|
|
|
|
|
|
2019-08-27 18:20:16 +02:00
|
|
|
|
(define (tarball-sans-extension tarball)
|
|
|
|
|
"Return TARBALL without its .tar.* or .zip extension."
|
|
|
|
|
(let ((end (or (string-contains tarball ".tar")
|
2021-04-04 22:24:42 +02:00
|
|
|
|
(string-contains tarball ".tgz")
|
2019-08-27 18:20:16 +02:00
|
|
|
|
(string-contains tarball ".zip"))))
|
|
|
|
|
(substring tarball 0 end)))
|
|
|
|
|
|
2016-08-01 17:42:09 +02:00
|
|
|
|
(define (compressed-file? file)
|
|
|
|
|
"Return true if FILE denotes a compressed file."
|
|
|
|
|
(->bool (member (file-extension file)
|
2017-05-15 00:31:14 +02:00
|
|
|
|
'("gz" "bz2" "xz" "lz" "lzma" "tgz" "tbz2" "zip"))))
|
2016-08-01 17:42:09 +02:00
|
|
|
|
|
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))))))))
|
|
|
|
|
|
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 ()
|
2018-07-12 02:24:29 +02:00
|
|
|
|
(false-if-exception (delete-file-recursively tmp-dir))))))
|
2015-06-25 03:16:11 +02:00
|
|
|
|
|
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)))
|
2016-06-13 17:52:08 +02:00
|
|
|
|
(fdatasync out)
|
|
|
|
|
(close-port out)
|
2013-12-29 15:51:07 +01:00
|
|
|
|
(rename-file template file)
|
|
|
|
|
result))
|
|
|
|
|
(lambda (key . args)
|
2016-06-13 18:00:29 +02:00
|
|
|
|
(false-if-exception (delete-file template))
|
|
|
|
|
(close-port out)))))
|
2013-04-21 22:40:23 +02:00
|
|
|
|
|
2017-07-28 14:51:44 +02:00
|
|
|
|
(define* (xdg-directory variable suffix #:key (ensure? #t))
|
|
|
|
|
"Return the name of the XDG directory that matches VARIABLE and SUFFIX,
|
|
|
|
|
after making sure that it exists if ENSURE? is true. VARIABLE is an
|
|
|
|
|
environment variable name like \"XDG_CONFIG_HOME\"; SUFFIX is a suffix like
|
|
|
|
|
\"/.config\". Honor the XDG specs,
|
|
|
|
|
<http://standards.freedesktop.org/basedir-spec/basedir-spec-latest.html>."
|
|
|
|
|
(let ((dir (and=> (or (getenv variable)
|
|
|
|
|
(and=> (or (getenv "HOME")
|
|
|
|
|
(passwd:dir (getpwuid (getuid))))
|
|
|
|
|
(cut string-append <> suffix)))
|
|
|
|
|
(cut string-append <> "/guix"))))
|
|
|
|
|
(when ensure?
|
|
|
|
|
(mkdir-p dir))
|
|
|
|
|
dir))
|
|
|
|
|
|
|
|
|
|
(define config-directory
|
|
|
|
|
(cut xdg-directory "XDG_CONFIG_HOME" "/.config" <...>))
|
|
|
|
|
|
|
|
|
|
(define cache-directory
|
|
|
|
|
(cut xdg-directory "XDG_CACHE_HOME" "/.cache" <...>))
|
2015-10-17 13:02:53 +02:00
|
|
|
|
|
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
|
2019-06-16 16:18:29 +02:00
|
|
|
|
of a single linefeed."
|
2015-11-14 15:00:36 +01:00
|
|
|
|
(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)
|
2019-06-16 16:18:29 +02:00
|
|
|
|
;; XXX: consume all CRs even if not followed by LF.
|
|
|
|
|
((eqv? byte (char->integer #\return)) (loop count (get-u8 port)))
|
2015-11-14 15:00:36 +01:00
|
|
|
|
((= count (- n 1))
|
|
|
|
|
(bytevector-u8-set! bv (+ start count) byte)
|
|
|
|
|
n)
|
|
|
|
|
(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.
|
|
|
|
|
;;;
|
|
|
|
|
|
2017-06-26 17:48:28 +02:00
|
|
|
|
(define absolute-dirname
|
|
|
|
|
;; Memoize to avoid repeated 'stat' storms from 'search-path'.
|
|
|
|
|
(mlambda (file)
|
|
|
|
|
"Return the absolute name of the directory containing FILE, or #f upon
|
2016-06-19 22:15:15 +02:00
|
|
|
|
failure."
|
2017-06-26 17:48:28 +02:00
|
|
|
|
(match (search-path %load-path file)
|
|
|
|
|
(#f #f)
|
|
|
|
|
((? string? file)
|
|
|
|
|
;; If there are relative names in %LOAD-PATH, FILE can be relative and
|
|
|
|
|
;; needs to be canonicalized.
|
|
|
|
|
(if (string-prefix? "/" file)
|
|
|
|
|
(dirname file)
|
|
|
|
|
(canonicalize-path (dirname file)))))))
|
2016-06-19 22:15:15 +02:00
|
|
|
|
|
2016-06-16 22:05:10 +02:00
|
|
|
|
(define-syntax current-source-directory
|
|
|
|
|
(lambda (s)
|
2016-06-16 22:24:14 +02:00
|
|
|
|
"Return the absolute name of the current directory, or #f if it could not
|
|
|
|
|
be determined."
|
2016-06-16 22:05:10 +02:00
|
|
|
|
(syntax-case s ()
|
|
|
|
|
((_)
|
2017-09-18 17:00:17 +02:00
|
|
|
|
(match (assq 'filename (or (syntax-source s) '()))
|
2016-06-16 22:05:10 +02:00
|
|
|
|
(('filename . (? string? file-name))
|
2016-06-16 22:24:14 +02:00
|
|
|
|
;; If %FILE-PORT-NAME-CANONICALIZATION is 'relative, then FILE-NAME
|
2016-06-19 22:15:15 +02:00
|
|
|
|
;; can be relative. In that case, we try to find out at run time
|
|
|
|
|
;; the absolute file name by looking at %LOAD-PATH; doing this at
|
|
|
|
|
;; run time rather than expansion time is necessary to allow files
|
|
|
|
|
;; to be moved on the file system.
|
2019-04-08 16:18:23 +02:00
|
|
|
|
(if (string-prefix? "/" file-name)
|
|
|
|
|
(dirname file-name)
|
|
|
|
|
#`(absolute-dirname #,file-name)))
|
|
|
|
|
((or ('filename . #f) #f)
|
|
|
|
|
;; raising an error would upset Geiser users
|
2016-06-16 22:05:10 +02:00
|
|
|
|
#f))))))
|
2016-06-15 23:08:05 +02:00
|
|
|
|
|
2021-01-19 22:28:08 +01:00
|
|
|
|
|
|
|
|
|
;;;
|
|
|
|
|
;;; String comparison.
|
|
|
|
|
;;;
|
|
|
|
|
|
|
|
|
|
(define (string-distance s1 s2)
|
|
|
|
|
"Compute the Levenshtein distance between two strings."
|
|
|
|
|
;; Naive implemenation
|
|
|
|
|
(define loop
|
|
|
|
|
(mlambda (as bt)
|
|
|
|
|
(match as
|
|
|
|
|
(() (length bt))
|
|
|
|
|
((a s ...)
|
|
|
|
|
(match bt
|
|
|
|
|
(() (length as))
|
|
|
|
|
((b t ...)
|
|
|
|
|
(if (char=? a b)
|
|
|
|
|
(loop s t)
|
|
|
|
|
(1+ (min
|
|
|
|
|
(loop as t)
|
|
|
|
|
(loop s bt)
|
|
|
|
|
(loop s t))))))))))
|
|
|
|
|
|
|
|
|
|
(let ((c1 (string->list s1))
|
|
|
|
|
(c2 (string->list s2)))
|
|
|
|
|
(loop c1 c2)))
|
|
|
|
|
|
|
|
|
|
(define* (string-closest trial tests #:key (threshold 3))
|
|
|
|
|
"Return the string from TESTS that is the closest from the TRIAL,
|
|
|
|
|
according to 'string-distance'. If the TESTS are too far from TRIAL,
|
|
|
|
|
according to THRESHOLD, then #f is returned."
|
|
|
|
|
(identity ;discard second return value
|
|
|
|
|
(fold2 (lambda (test closest minimal)
|
|
|
|
|
(let ((dist (string-distance trial test)))
|
|
|
|
|
(if (and (< dist minimal) (< dist threshold))
|
|
|
|
|
(values test dist)
|
|
|
|
|
(values closest minimal))))
|
|
|
|
|
#f +inf.0
|
|
|
|
|
tests)))
|
|
|
|
|
|
2018-01-09 23:00:11 +01:00
|
|
|
|
|
|
|
|
|
;;;
|
|
|
|
|
;;; Prettified output.
|
|
|
|
|
;;;
|
|
|
|
|
|
|
|
|
|
(define* (pretty-print-table rows #:key (max-column-width 20))
|
|
|
|
|
"Print ROWS in neat columns. All rows should be lists of strings and each
|
|
|
|
|
row should have the same length. The columns are separated by a tab
|
|
|
|
|
character, and aligned using spaces. The maximum width of each column is
|
|
|
|
|
bound by MAX-COLUMN-WIDTH."
|
|
|
|
|
(let* ((number-of-columns-to-pad (if (null? rows)
|
|
|
|
|
0
|
|
|
|
|
(1- (length (first rows)))))
|
|
|
|
|
;; Ignore the last column as it is left aligned and doesn't need
|
|
|
|
|
;; padding; this prevents printing extraneous trailing spaces.
|
|
|
|
|
(column-widths (fold (lambda (row maximums)
|
|
|
|
|
(map max (map string-length row) maximums))
|
|
|
|
|
;; Initial max width is 0 for each column.
|
|
|
|
|
(make-list number-of-columns-to-pad 0)
|
|
|
|
|
(map (cut drop-right <> 1) rows)))
|
|
|
|
|
(column-formats (map (cut format #f "~~~da" <>)
|
|
|
|
|
(map (cut min <> max-column-width)
|
|
|
|
|
column-widths)))
|
|
|
|
|
(fmt (string-append (string-join column-formats "\t") "\t~a")))
|
|
|
|
|
(for-each (cut format #t "~?~%" fmt <>) rows)))
|
|
|
|
|
|
2017-09-16 22:10:18 +02:00
|
|
|
|
;;; Local Variables:
|
|
|
|
|
;;; eval: (put 'call-with-progress-reporter 'scheme-indent-function 1)
|
|
|
|
|
;;; End:
|