From 4862a98be473ac6bb0bfd8a6cdbc81119cb44355 Mon Sep 17 00:00:00 2001 From: Ludovic Courtès Date: Thu, 25 May 2017 14:11:22 +0200 Subject: base64: Turn into a regular Guile module. * guix/base64.scm: Replace 'library' form with 'define-module'. --- guix/base64.scm | 377 ++++++++++++++++++++++++++++---------------------------- 1 file changed, 189 insertions(+), 188 deletions(-) (limited to 'guix') diff --git a/guix/base64.scm b/guix/base64.scm index 4bd5dc5e1b..0fa501eca0 100644 --- a/guix/base64.scm +++ b/guix/base64.scm @@ -5,6 +5,7 @@ ;; February 12, 2014. ;; ;; Some optimizations made by Ludovic Courtès , 2015. +;; Turned into a Guile module (instead of R6RS). ;; ;; This program is free software: you can redistribute it and/or modify ;; it under the terms of the GNU General Public License as published by @@ -42,211 +43,211 @@ ;; FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER ;; DEALINGS IN THE SOFTWARE. -#!r6rs - ;; RFC 4648 Base-N Encodings -(library (guix base64) - (export base64-encode - base64-decode - base64-alphabet - base64url-alphabet - get-delimited-base64 - put-delimited-base64) - (import (rnrs) - (only (srfi :13 strings) - string-index - string-prefix? string-suffix? - string-concatenate string-trim-both) - (only (guile) ash logior)) +(define-module (guix base64) + #:export (base64-encode + base64-decode + base64-alphabet + base64url-alphabet + get-delimited-base64 + put-delimited-base64) + #:use-module (rnrs) + #:use-module ((srfi srfi-13) + #:select (string-index + string-prefix? string-suffix? + string-concatenate string-trim-both))) - (define-syntax define-alias - (syntax-rules () - ((_ new old) - (define-syntax new (identifier-syntax old))))) +(define-syntax define-alias + (syntax-rules () + ((_ new old) + (define-syntax new (identifier-syntax old))))) - ;; Force the use of Guile's own primitives to avoid the overhead of its 'fx' - ;; procedures. - (define-alias fxbit-field bitwise-bit-field) - (define-alias fxarithmetic-shift ash) - (define-alias fxarithmetic-shift-left ash) - (define-alias fxand logand) - (define-alias fxior logior) - (define-alias fxxor logxor) +;; Force the use of Guile's own primitives to avoid the overhead of its 'fx' +;; procedures. - (define base64-alphabet - "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/") +(define-alias fxbit-field bitwise-bit-field) +(define-alias fxarithmetic-shift ash) +(define-alias fxarithmetic-shift-left ash) +(define-alias fxand logand) +(define-alias fxior logior) +(define-alias fxxor logxor) - (define base64url-alphabet - "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_") +(define base64-alphabet + "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/") - (define base64-encode - (case-lambda - ;; Simple interface. Returns a string containing the canonical - ;; base64 representation of the given bytevector. - ((bv) - (base64-encode bv 0 (bytevector-length bv) #f #f base64-alphabet #f)) - ((bv start) - (base64-encode bv start (bytevector-length bv) #f #f base64-alphabet #f)) - ((bv start end) - (base64-encode bv start end #f #f base64-alphabet #f)) - ((bv start end line-length) - (base64-encode bv start end line-length #f base64-alphabet #f)) - ((bv start end line-length no-padding) - (base64-encode bv start end line-length no-padding base64-alphabet #f)) - ((bv start end line-length no-padding alphabet) - (base64-encode bv start end line-length no-padding alphabet #f)) - ;; Base64 encodes the bytes [start,end[ in the given bytevector. - ;; Lines are limited to line-length characters (unless #f), - ;; which must be a multiple of four. To omit the padding - ;; characters (#\=) set no-padding to a true value. If port is - ;; #f, returns a string. - ((bv start end line-length no-padding alphabet port) - (assert (or (not line-length) (zero? (mod line-length 4)))) - (let-values (((p extract) (if port - (values port (lambda () (values))) - (open-string-output-port)))) - (letrec ((put (if line-length - (let ((chars 0)) - (lambda (p c) - (when (fx=? chars line-length) - (set! chars 0) - (put-char p #\linefeed)) - (set! chars (fx+ chars 1)) - (put-char p c))) - put-char))) - (let lp ((i start)) - (cond ((= i end)) - ((<= (+ i 3) end) - (let ((x (bytevector-uint-ref bv i (endianness big) 3))) - (put p (string-ref alphabet (fxbit-field x 18 24))) - (put p (string-ref alphabet (fxbit-field x 12 18))) - (put p (string-ref alphabet (fxbit-field x 6 12))) - (put p (string-ref alphabet (fxbit-field x 0 6))) - (lp (+ i 3)))) - ((<= (+ i 2) end) - (let ((x (fxarithmetic-shift-left (bytevector-u16-ref bv i (endianness big)) 8))) - (put p (string-ref alphabet (fxbit-field x 18 24))) - (put p (string-ref alphabet (fxbit-field x 12 18))) - (put p (string-ref alphabet (fxbit-field x 6 12))) - (unless no-padding - (put p #\=)))) - (else - (let ((x (fxarithmetic-shift-left (bytevector-u8-ref bv i) 16))) - (put p (string-ref alphabet (fxbit-field x 18 24))) - (put p (string-ref alphabet (fxbit-field x 12 18))) - (unless no-padding - (put p #\=) - (put p #\=))))))) - (extract))))) +(define base64url-alphabet + "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_") + +(define base64-encode + (case-lambda + ;; Simple interface. Returns a string containing the canonical + ;; base64 representation of the given bytevector. + ((bv) + (base64-encode bv 0 (bytevector-length bv) #f #f base64-alphabet #f)) + ((bv start) + (base64-encode bv start (bytevector-length bv) #f #f base64-alphabet #f)) + ((bv start end) + (base64-encode bv start end #f #f base64-alphabet #f)) + ((bv start end line-length) + (base64-encode bv start end line-length #f base64-alphabet #f)) + ((bv start end line-length no-padding) + (base64-encode bv start end line-length no-padding base64-alphabet #f)) + ((bv start end line-length no-padding alphabet) + (base64-encode bv start end line-length no-padding alphabet #f)) + ;; Base64 encodes the bytes [start,end[ in the given bytevector. + ;; Lines are limited to line-length characters (unless #f), + ;; which must be a multiple of four. To omit the padding + ;; characters (#\=) set no-padding to a true value. If port is + ;; #f, returns a string. + ((bv start end line-length no-padding alphabet port) + (assert (or (not line-length) (zero? (mod line-length 4)))) + (let-values (((p extract) (if port + (values port (lambda () (values))) + (open-string-output-port)))) + (letrec ((put (if line-length + (let ((chars 0)) + (lambda (p c) + (when (fx=? chars line-length) + (set! chars 0) + (put-char p #\linefeed)) + (set! chars (fx+ chars 1)) + (put-char p c))) + put-char))) + (let lp ((i start)) + (cond ((= i end)) + ((<= (+ i 3) end) + (let ((x (bytevector-uint-ref bv i (endianness big) 3))) + (put p (string-ref alphabet (fxbit-field x 18 24))) + (put p (string-ref alphabet (fxbit-field x 12 18))) + (put p (string-ref alphabet (fxbit-field x 6 12))) + (put p (string-ref alphabet (fxbit-field x 0 6))) + (lp (+ i 3)))) + ((<= (+ i 2) end) + (let ((x (fxarithmetic-shift-left (bytevector-u16-ref bv i (endianness big)) 8))) + (put p (string-ref alphabet (fxbit-field x 18 24))) + (put p (string-ref alphabet (fxbit-field x 12 18))) + (put p (string-ref alphabet (fxbit-field x 6 12))) + (unless no-padding + (put p #\=)))) + (else + (let ((x (fxarithmetic-shift-left (bytevector-u8-ref bv i) 16))) + (put p (string-ref alphabet (fxbit-field x 18 24))) + (put p (string-ref alphabet (fxbit-field x 12 18))) + (unless no-padding + (put p #\=) + (put p #\=))))))) + (extract))))) ;; Decodes a base64 string. The string must contain only pure ;; unpadded base64 data. - (define base64-decode - (case-lambda - ((str) - (base64-decode str base64-alphabet #f)) - ((str alphabet) - (base64-decode str alphabet #f)) - ((str alphabet port) - (unless (zero? (mod (string-length str) 4)) - (error 'base64-decode - "input string must be a multiple of four characters")) - (let-values (((p extract) (if port - (values port (lambda () (values))) - (open-bytevector-output-port)))) - (do ((i 0 (+ i 4))) - ((= i (string-length str)) - (extract)) - (let ((c1 (string-ref str i)) - (c2 (string-ref str (+ i 1))) - (c3 (string-ref str (+ i 2))) - (c4 (string-ref str (+ i 3)))) - ;; TODO: be more clever than string-index - (let ((i1 (string-index alphabet c1)) - (i2 (string-index alphabet c2)) - (i3 (string-index alphabet c3)) - (i4 (string-index alphabet c4))) - (cond ((and i1 i2 i3 i4) - (let ((x (fxior (fxarithmetic-shift-left i1 18) - (fxarithmetic-shift-left i2 12) - (fxarithmetic-shift-left i3 6) - i4))) - (put-u8 p (fxbit-field x 16 24)) - (put-u8 p (fxbit-field x 8 16)) - (put-u8 p (fxbit-field x 0 8)))) - ((and i1 i2 i3 (char=? c4 #\=) - (= i (- (string-length str) 4))) - (let ((x (fxior (fxarithmetic-shift-left i1 18) - (fxarithmetic-shift-left i2 12) - (fxarithmetic-shift-left i3 6)))) - (put-u8 p (fxbit-field x 16 24)) - (put-u8 p (fxbit-field x 8 16)))) - ((and i1 i2 (char=? c3 #\=) (char=? c4 #\=) - (= i (- (string-length str) 4))) - (let ((x (fxior (fxarithmetic-shift-left i1 18) - (fxarithmetic-shift-left i2 12)))) - (put-u8 p (fxbit-field x 16 24)))) - (else - (error 'base64-decode "invalid input" - (list c1 c2 c3 c4))))))))))) + +(define base64-decode + (case-lambda + ((str) + (base64-decode str base64-alphabet #f)) + ((str alphabet) + (base64-decode str alphabet #f)) + ((str alphabet port) + (unless (zero? (mod (string-length str) 4)) + (error 'base64-decode + "input string must be a multiple of four characters")) + (let-values (((p extract) (if port + (values port (lambda () (values))) + (open-bytevector-output-port)))) + (do ((i 0 (+ i 4))) + ((= i (string-length str)) + (extract)) + (let ((c1 (string-ref str i)) + (c2 (string-ref str (+ i 1))) + (c3 (string-ref str (+ i 2))) + (c4 (string-ref str (+ i 3)))) + ;; TODO: be more clever than string-index + (let ((i1 (string-index alphabet c1)) + (i2 (string-index alphabet c2)) + (i3 (string-index alphabet c3)) + (i4 (string-index alphabet c4))) + (cond ((and i1 i2 i3 i4) + (let ((x (fxior (fxarithmetic-shift-left i1 18) + (fxarithmetic-shift-left i2 12) + (fxarithmetic-shift-left i3 6) + i4))) + (put-u8 p (fxbit-field x 16 24)) + (put-u8 p (fxbit-field x 8 16)) + (put-u8 p (fxbit-field x 0 8)))) + ((and i1 i2 i3 (char=? c4 #\=) + (= i (- (string-length str) 4))) + (let ((x (fxior (fxarithmetic-shift-left i1 18) + (fxarithmetic-shift-left i2 12) + (fxarithmetic-shift-left i3 6)))) + (put-u8 p (fxbit-field x 16 24)) + (put-u8 p (fxbit-field x 8 16)))) + ((and i1 i2 (char=? c3 #\=) (char=? c4 #\=) + (= i (- (string-length str) 4))) + (let ((x (fxior (fxarithmetic-shift-left i1 18) + (fxarithmetic-shift-left i2 12)))) + (put-u8 p (fxbit-field x 16 24)))) + (else + (error 'base64-decode "invalid input" + (list c1 c2 c3 c4))))))))))) - (define (get-line-comp f port) - (if (port-eof? port) - (eof-object) - (f (get-line port)))) +(define (get-line-comp f port) + (if (port-eof? port) + (eof-object) + (f (get-line port)))) ;; Reads the common -----BEGIN/END type----- delimited format from ;; the given port. Returns two values: a string with the type and a ;; bytevector containing the base64 decoded data. The second value ;; is the eof object if there is an eof before the BEGIN delimiter. - (define (get-delimited-base64 port) - (define (get-first-data-line port) - ;; Some MIME data has header fields in the same format as mail - ;; or http. These are ignored. - (let ((line (get-line-comp string-trim-both port))) - (cond ((eof-object? line) line) - ((string-index line #\:) - (let lp () ;read until empty line - (let ((line (get-line-comp string-trim-both port))) - (if (string=? line "") - (get-line-comp string-trim-both port) - (lp))))) - (else line)))) + +(define (get-delimited-base64 port) + (define (get-first-data-line port) + ;; Some MIME data has header fields in the same format as mail + ;; or http. These are ignored. (let ((line (get-line-comp string-trim-both port))) - (cond ((eof-object? line) - (values "" (eof-object))) - ((string=? line "") - (get-delimited-base64 port)) - ((and (string-prefix? "-----BEGIN " line) - (string-suffix? "-----" line)) - (let* ((type (substring line 11 (- (string-length line) 5))) - (endline (string-append "-----END " type "-----"))) - (let-values (((outp extract) (open-bytevector-output-port))) - (let lp ((line (get-first-data-line port))) - (cond ((eof-object? line) + (cond ((eof-object? line) line) + ((string-index line #\:) + (let lp () ;read until empty line + (let ((line (get-line-comp string-trim-both port))) + (if (string=? line "") + (get-line-comp string-trim-both port) + (lp))))) + (else line)))) + (let ((line (get-line-comp string-trim-both port))) + (cond ((eof-object? line) + (values "" (eof-object))) + ((string=? line "") + (get-delimited-base64 port)) + ((and (string-prefix? "-----BEGIN " line) + (string-suffix? "-----" line)) + (let* ((type (substring line 11 (- (string-length line) 5))) + (endline (string-append "-----END " type "-----"))) + (let-values (((outp extract) (open-bytevector-output-port))) + (let lp ((line (get-first-data-line port))) + (cond ((eof-object? line) + (error 'get-delimited-base64 + "unexpected end of file")) + ((string-prefix? "-" line) + (unless (string=? line endline) (error 'get-delimited-base64 - "unexpected end of file")) - ((string-prefix? "-" line) - (unless (string=? line endline) - (error 'get-delimited-base64 - "bad end delimiter" type line)) - (values type (extract))) - (else - (unless (and (= (string-length line) 5) - (string-prefix? "=" line)) ;Skip Radix-64 checksum - (base64-decode line base64-alphabet outp)) - (lp (get-line-comp string-trim-both port)))))))) - (else ;skip garbage (like in openssl x509 -in foo -text output). - (get-delimited-base64 port))))) + "bad end delimiter" type line)) + (values type (extract))) + (else + (unless (and (= (string-length line) 5) + (string-prefix? "=" line)) ;Skip Radix-64 checksum + (base64-decode line base64-alphabet outp)) + (lp (get-line-comp string-trim-both port)))))))) + (else ;skip garbage (like in openssl x509 -in foo -text output). + (get-delimited-base64 port))))) - (define put-delimited-base64 - (case-lambda - ((port type bv line-length) - (display (string-append "-----BEGIN " type "-----\n") port) - (base64-encode bv 0 (bytevector-length bv) - line-length #f base64-alphabet port) - (display (string-append "\n-----END " type "-----\n") port)) - ((port type bv) - (put-delimited-base64 port type bv 76))))) +(define put-delimited-base64 + (case-lambda + ((port type bv line-length) + (display (string-append "-----BEGIN " type "-----\n") port) + (base64-encode bv 0 (bytevector-length bv) + line-length #f base64-alphabet port) + (display (string-append "\n-----END " type "-----\n") port)) + ((port type bv) + (put-delimited-base64 port type bv 76)))) -- cgit v1.2.3 From bfe5264aa10c1af64adc5c24d0cf091562e3e09c Mon Sep 17 00:00:00 2001 From: Ludovic Courtès Date: Thu, 25 May 2017 14:19:51 +0200 Subject: modules: Raise an error when a dependency could not be found. * guix/modules.scm (&missing-dependency-error): New error condition. (source-module-dependencies): Raise it when 'search-path' returns #f. * tests/modules.scm ("&missing-dependency-error"): New test. --- guix/modules.scm | 21 +++++++++++++++++---- tests/modules.scm | 25 ++++++++++++++++++++++++- 2 files changed, 41 insertions(+), 5 deletions(-) (limited to 'guix') diff --git a/guix/modules.scm b/guix/modules.scm index 24b5903579..19a4acd76c 100644 --- a/guix/modules.scm +++ b/guix/modules.scm @@ -20,8 +20,13 @@ #:use-module (guix memoization) #:use-module (guix sets) #:use-module (srfi srfi-26) + #:use-module (srfi srfi-34) + #:use-module (srfi srfi-35) #:use-module (ice-9 match) - #:export (source-module-closure + #:export (missing-dependency-error? + missing-dependency-module + + source-module-closure live-module-closure guix-module-name?)) @@ -35,6 +40,11 @@ ;;; ;;; Code: +;; The error corresponding to a missing module. +(define-condition-type &missing-dependency-error &error + missing-dependency-error? + (module missing-dependency-module)) + (define (colon-symbol? obj) "Return true if OBJ is a symbol that starts with a colon." (and (symbol? obj) @@ -106,9 +116,12 @@ depends on." "Return the modules used by MODULE by looking at its source code." (if (member module %source-less-modules) '() - (module-file-dependencies - (search-path load-path - (module-name->file-name module))))) + (match (search-path load-path (module-name->file-name module)) + ((? string? file) + (module-file-dependencies file)) + (#f + (raise (condition (&missing-dependency-error + (module module)))))))) (define* (module-closure modules #:key diff --git a/tests/modules.scm b/tests/modules.scm index 04945e531b..57019c600c 100644 --- a/tests/modules.scm +++ b/tests/modules.scm @@ -1,5 +1,5 @@ ;;; GNU Guix --- Functional package management for GNU -;;; Copyright © 2016 Ludovic Courtès +;;; Copyright © 2016, 2017 Ludovic Courtès ;;; ;;; This file is part of GNU Guix. ;;; @@ -19,7 +19,9 @@ (define-module (test-modules) #:use-module (guix modules) #:use-module ((guix build-system gnu) #:select (%gnu-build-system-modules)) + #:use-module ((guix utils) #:select (call-with-temporary-directory)) #:use-module (srfi srfi-1) + #:use-module (srfi srfi-34) #:use-module (srfi srfi-64)) (test-begin "modules") @@ -42,4 +44,25 @@ (live-module-closure '((gnu build vm))) (source-module-closure '((gnu build vm))))) +(test-equal "&missing-dependency-error" + '(something that does not exist) + (call-with-temporary-directory + (lambda (directory) + (call-with-output-file (string-append directory "/foobar.scm") + (lambda (port) + (write '(define-module (foobar) + #:use-module (something that does not exist)) + port))) + + (call-with-output-file (string-append directory "/baz.scm") + (lambda (port) + (write '(define-module (baz) + #:use-module (foobar)) + port))) + + (guard (c ((missing-dependency-error? c) + (missing-dependency-module c))) + (source-module-closure '((baz)) (list directory) + #:select? (const #t)))))) + (test-end) -- cgit v1.2.3 From 20ed093977cc80ba1729c38e05ae7955a38069a6 Mon Sep 17 00:00:00 2001 From: Ludovic Courtès Date: Thu, 25 May 2017 14:21:04 +0200 Subject: pull: Catch '&missing-dependency-error' raised by 'source-module-closure'. Fixes . Reported by Mathieu Othacehe . * guix/build/pull.scm (depends-on-guile-ssh?): Remove. (has-all-its-dependencies?): New procedure. (build-guix): Use it to filter source files. --- guix/build/pull.scm | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) (limited to 'guix') diff --git a/guix/build/pull.scm b/guix/build/pull.scm index d2e0404b14..03b0f925a7 100644 --- a/guix/build/pull.scm +++ b/guix/build/pull.scm @@ -28,6 +28,7 @@ #:use-module (srfi srfi-1) #:use-module (srfi srfi-11) #:use-module (srfi srfi-26) + #:use-module (srfi srfi-34) #:export (build-guix)) ;;; Commentary: @@ -36,13 +37,18 @@ ;;; ;;; Code: -(define (depends-on-guile-ssh? file) - "Return true if FILE is a Scheme source file that depends, directly or -indirectly, on Guile-SSH." - (find (match-lambda - (('ssh _ ...) #t) - (_ #f)) - (source-module-closure file #:select? (const #t)))) +(define (has-all-its-dependencies? file) + "Return true if the dependencies of the module defined in FILE are +available, false otherwise." + (let ((module (call-with-input-file file + (lambda (port) + (match (read port) + (('define-module name _ ...) + name)))))) + ;; If one of the dependencies of MODULE is missing, we get a + ;; '&missing-dependency-error'. + (guard (c ((missing-dependency-error? c) #f)) + (source-module-closure (list module) #:select? (const #t))))) (define (all-scheme-files directory) "Return a sorted list of Scheme files found in DIRECTORY." @@ -145,10 +151,7 @@ containing the source code. Write any debugging output to DEBUG-PORT." ;; Compile the .scm files. Load all the files before compiling them to ;; work around (FIXME). ;; Filter out files depending on Guile-SSH when Guile-SSH is missing. - (let* ((files (remove (if (false-if-exception - (resolve-interface '(ssh session))) - (const #f) - depends-on-guile-ssh?) + (let* ((files (filter has-all-its-dependencies? (all-scheme-files out))) (total (length files))) (let loop ((files files) -- cgit v1.2.3 From 3a62dd6b22c054f2e8d6df38d493def52020e714 Mon Sep 17 00:00:00 2001 From: Ricardo Wurmus Date: Thu, 25 May 2017 16:58:15 +0200 Subject: build-system: gnu: Fix cross-gcc call. This is a follow-up to 7b3318e34f4e2743254a88b908859901db960e9a. * guix/build-system/gnu.scm (standard-cross-packages): Use keyword arguments in cross-gcc call. --- guix/build-system/gnu.scm | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'guix') diff --git a/guix/build-system/gnu.scm b/guix/build-system/gnu.scm index 730e638c89..7cf0cafc0f 100644 --- a/guix/build-system/gnu.scm +++ b/guix/build-system/gnu.scm @@ -403,8 +403,8 @@ is one of `host' or `target'." (case kind ((host) `(("cross-gcc" ,(gcc target - (binutils target) - (libc target))) + #:xbinutils (binutils target) + #:libc (libc target))) ("cross-binutils" ,(binutils target)))) ((target) `(("cross-libc" ,(libc target)))))))) -- cgit v1.2.3