1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2014, 2015, 2016, 2017, 2018, 2019 Ludovic Courtès <ludo@gnu.org>
3 ;;; Copyright © 2018 Clément Lassieur <clement@lassieur.org>
4 ;;; Copyright © 2018 Jan Nieuwenhuizen <janneke@gnu.org>
6 ;;; This file is part of GNU Guix.
8 ;;; GNU Guix is free software; you can redistribute it and/or modify it
9 ;;; under the terms of the GNU General Public License as published by
10 ;;; the Free Software Foundation; either version 3 of the License, or (at
11 ;;; your option) any later version.
13 ;;; GNU Guix is distributed in the hope that it will be useful, but
14 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
15 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 ;;; GNU General Public License for more details.
18 ;;; You should have received a copy of the GNU General Public License
19 ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
21 (define-module (guix gexp)
22 #:use-module (guix store)
23 #:use-module (guix monads)
24 #:use-module (guix derivations)
25 #:use-module (guix grafts)
26 #:use-module (guix utils)
27 #:use-module (rnrs bytevectors)
28 #:use-module (srfi srfi-1)
29 #:use-module (srfi srfi-9)
30 #:use-module (srfi srfi-9 gnu)
31 #:use-module (srfi srfi-26)
32 #:use-module (srfi srfi-34)
33 #:use-module (srfi srfi-35)
34 #:use-module (ice-9 match)
49 local-file-absolute-file-name
69 program-file-module-path
90 lowered-gexp-load-path
91 lowered-gexp-load-compiled-path
115 gexp-error-invalid-input))
119 ;;; This module implements "G-expressions", or "gexps". Gexps are like
120 ;;; S-expressions (sexps), with two differences:
122 ;;; 1. References (un-quotations) to derivations or packages in a gexp are
123 ;;; replaced by the corresponding output file name; in addition, the
124 ;;; 'ungexp-native' unquote-like form allows code to explicitly refer to
125 ;;; the native code of a given package, in case of cross-compilation;
127 ;;; 2. Gexps embed information about the derivations they refer to.
129 ;;; Gexps make it easy to write to files Scheme code that refers to store
130 ;;; items, or to write Scheme code to build derivations.
135 (define-record-type <gexp>
136 (make-gexp references modules extensions proc)
138 (references gexp-references) ;list of <gexp-input>
139 (modules gexp-self-modules) ;list of module names
140 (extensions gexp-self-extensions) ;list of lowerable things
141 (proc gexp-proc)) ;procedure
143 (define (write-gexp gexp port)
144 "Write GEXP on PORT."
145 (display "#<gexp " port)
147 ;; Try to write the underlying sexp. Now, this trick doesn't work when
148 ;; doing things like (ungexp-splicing (gexp ())) because GEXP's procedure
149 ;; tries to use 'append' on that, which fails with wrong-type-arg.
151 (write (apply (gexp-proc gexp)
152 (gexp-references gexp))
155 (number->string (object-address gexp) 16)))
157 (set-record-type-printer! <gexp> write-gexp)
164 ;; Compiler for a type of objects that may be introduced in a gexp.
165 (define-record-type <gexp-compiler>
166 (gexp-compiler type lower expand)
168 (type gexp-compiler-type) ;record type descriptor
169 (lower gexp-compiler-lower)
170 (expand gexp-compiler-expand)) ;#f | DRV -> sexp
172 (define-condition-type &gexp-error &error
175 (define-condition-type &gexp-input-error &gexp-error
177 (input gexp-error-invalid-input))
180 (define %gexp-compilers
181 ;; 'eq?' mapping of record type descriptor to <gexp-compiler>.
182 (make-hash-table 20))
184 (define (default-expander thing obj output)
185 "This is the default expander for \"things\" that appear in gexps. It
186 returns its output file name of OBJ's OUTPUT."
189 (derivation->output-path drv output))
193 (define (register-compiler! compiler)
194 "Register COMPILER as a gexp compiler."
195 (hashq-set! %gexp-compilers
196 (gexp-compiler-type compiler) compiler))
198 (define (lookup-compiler object)
199 "Search for a compiler for OBJECT. Upon success, return the three argument
200 procedure to lower it; otherwise return #f."
201 (and=> (hashq-ref %gexp-compilers (struct-vtable object))
202 gexp-compiler-lower))
204 (define (file-like? object)
205 "Return #t if OBJECT leads to a file in the store once unquoted in a
206 G-expression; otherwise return #f."
207 (and (struct? object) (->bool (lookup-compiler object))))
209 (define (lookup-expander object)
210 "Search for an expander for OBJECT. Upon success, return the three argument
211 procedure to expand it; otherwise return #f."
212 (and=> (hashq-ref %gexp-compilers (struct-vtable object))
213 gexp-compiler-expand))
215 (define* (lower-object obj
216 #:optional (system (%current-system))
218 "Return as a value in %STORE-MONAD the derivation or store item
219 corresponding to OBJ for SYSTEM, cross-compiling for TARGET if TARGET is true.
220 OBJ must be an object that has an associated gexp compiler, such as a
222 (match (lookup-compiler obj)
224 (raise (condition (&gexp-input-error (input obj)))))
226 ;; Cache in STORE the result of lowering OBJ.
227 (mlet %store-monad ((graft? (grafting?)))
228 (mcached (let ((lower (lookup-compiler obj)))
229 (lower obj system target))
231 system target graft?)))))
233 (define-syntax define-gexp-compiler
234 (syntax-rules (=> compiler expander)
235 "Define NAME as a compiler for objects matching PREDICATE encountered in
238 In the simplest form of the macro, BODY must return a derivation for PARAM, an
239 object that matches PREDICATE, for SYSTEM and TARGET (the latter of which is
240 #f except when cross-compiling.)
242 The more elaborate form allows you to specify an expander:
244 (define-gexp-compiler something something?
245 compiler => (lambda (param system target) ...)
246 expander => (lambda (param drv output) ...))
248 The expander specifies how an object is converted to its sexp representation."
249 ((_ (name (param record-type) system target) body ...)
250 (define-gexp-compiler name record-type
251 compiler => (lambda (param system target) body ...)
252 expander => default-expander))
258 (gexp-compiler record-type compile expand))
259 (register-compiler! name)))))
261 (define-gexp-compiler (derivation-compiler (drv <derivation>) system target)
262 ;; Derivations are the lowest-level representation, so this is the identity
264 (with-monad %store-monad
269 ;;; File declarations.
272 ;; A local file name. FILE is the file name the user entered, which can be a
273 ;; relative file name, and ABSOLUTE is a promise that computes its canonical
274 ;; absolute file name. We keep it in a promise to compute it lazily and avoid
275 ;; repeated 'stat' calls.
276 (define-record-type <local-file>
277 (%%local-file file absolute name recursive? select?)
279 (file local-file-file) ;string
280 (absolute %local-file-absolute-file-name) ;promise string
281 (name local-file-name) ;string
282 (recursive? local-file-recursive?) ;Boolean
283 (select? local-file-select?)) ;string stat -> Boolean
285 (define (true file stat) #t)
287 (define* (%local-file file promise #:optional (name (basename file))
288 #:key recursive? (select? true))
289 ;; This intermediate procedure is part of our ABI, but the underlying
290 ;; %%LOCAL-FILE is not.
291 (%%local-file file promise name recursive? select?))
293 (define (absolute-file-name file directory)
294 "Return the canonical absolute file name for FILE, which lives in the
295 vicinity of DIRECTORY."
297 (cond ((string-prefix? "/" file) file)
298 ((not directory) file)
299 ((string-prefix? "/" directory)
300 (string-append directory "/" file))
303 (define-syntax local-file
305 "Return an object representing local file FILE to add to the store; this
306 object can be used in a gexp. If FILE is a relative file name, it is looked
307 up relative to the source file where this form appears. FILE will be added to
308 the store under NAME--by default the base name of FILE.
310 When RECURSIVE? is true, the contents of FILE are added recursively; if FILE
311 designates a flat file and RECURSIVE? is true, its contents are added, and its
312 permission bits are kept.
314 When RECURSIVE? is true, call (SELECT? FILE STAT) for each directory entry,
315 where FILE is the entry's absolute file name and STAT is the result of
316 'lstat'; exclude entries for which SELECT? does not return true.
318 This is the declarative counterpart of the 'interned-file' monadic procedure.
319 It is implemented as a macro to capture the current source directory where it
324 (delay (absolute-file-name file (current-source-directory)))
327 #'(syntax-error "missing file name"))
330 ;; XXX: We could return #'(lambda (file . rest) ...). However,
331 ;; (syntax-source #'id) is #f so (current-source-directory) would not
332 ;; work. Thus, simply forbid this form.
334 "'local-file' is a macro and cannot be used like this")))))
336 (define (local-file-absolute-file-name file)
337 "Return the absolute file name for FILE, a <local-file> instance. A
338 'system-error' exception is raised if FILE could not be found."
339 (force (%local-file-absolute-file-name file)))
341 (define-gexp-compiler (local-file-compiler (file <local-file>) system target)
342 ;; "Compile" FILE by adding it to the store.
344 (($ <local-file> file (= force absolute) name recursive? select?)
345 ;; Canonicalize FILE so that if it's a symlink, it is resolved. Failing
346 ;; to do that, when RECURSIVE? is #t, we could end up creating a dangling
347 ;; symlink in the store, and when RECURSIVE? is #f 'add-to-store' would
348 ;; just throw an error, both of which are inconvenient.
349 (interned-file absolute name
350 #:recursive? recursive? #:select? select?))))
352 (define-record-type <plain-file>
353 (%plain-file name content references)
355 (name plain-file-name) ;string
356 (content plain-file-content) ;string or bytevector
357 (references plain-file-references)) ;list (currently unused)
359 (define (plain-file name content)
360 "Return an object representing a text file called NAME with the given
361 CONTENT (a string) to be added to the store.
363 This is the declarative counterpart of 'text-file'."
364 ;; XXX: For now just ignore 'references' because it's not clear how to use
365 ;; them in a declarative context.
366 (%plain-file name content '()))
368 (define-gexp-compiler (plain-file-compiler (file <plain-file>) system target)
369 ;; "Compile" FILE by adding it to the store.
371 (($ <plain-file> name (and (? string?) content) references)
372 (text-file name content references))
373 (($ <plain-file> name (and (? bytevector?) content) references)
374 (binary-file name content references))))
376 (define-record-type <computed-file>
377 (%computed-file name gexp guile options)
379 (name computed-file-name) ;string
380 (gexp computed-file-gexp) ;gexp
381 (guile computed-file-guile) ;<package>
382 (options computed-file-options)) ;list of arguments
384 (define* (computed-file name gexp
385 #:key guile (options '(#:local-build? #t)))
386 "Return an object representing the store item NAME, a file or directory
387 computed by GEXP. OPTIONS is a list of additional arguments to pass
388 to 'gexp->derivation'.
390 This is the declarative counterpart of 'gexp->derivation'."
391 (%computed-file name gexp guile options))
393 (define-gexp-compiler (computed-file-compiler (file <computed-file>)
395 ;; Compile FILE by returning a derivation whose build expression is its
398 (($ <computed-file> name gexp guile options)
400 (mlet %store-monad ((guile (lower-object guile system
402 (apply gexp->derivation name gexp #:guile-for-build guile
403 #:system system #:target target options))
404 (apply gexp->derivation name gexp
405 #:system system #:target target options)))))
407 (define-record-type <program-file>
408 (%program-file name gexp guile path)
410 (name program-file-name) ;string
411 (gexp program-file-gexp) ;gexp
412 (guile program-file-guile) ;package
413 (path program-file-module-path)) ;list of strings
415 (define* (program-file name gexp #:key (guile #f) (module-path %load-path))
416 "Return an object representing the executable store item NAME that runs
417 GEXP. GUILE is the Guile package used to execute that script. Imported
418 modules of GEXP are looked up in MODULE-PATH.
420 This is the declarative counterpart of 'gexp->script'."
421 (%program-file name gexp guile module-path))
423 (define-gexp-compiler (program-file-compiler (file <program-file>)
425 ;; Compile FILE by returning a derivation that builds the script.
427 (($ <program-file> name gexp guile module-path)
428 (gexp->script name gexp
429 #:module-path module-path
430 #:guile (or guile (default-guile))
434 (define-record-type <scheme-file>
435 (%scheme-file name gexp splice?)
437 (name scheme-file-name) ;string
438 (gexp scheme-file-gexp) ;gexp
439 (splice? scheme-file-splice?)) ;Boolean
441 (define* (scheme-file name gexp #:key splice?)
442 "Return an object representing the Scheme file NAME that contains GEXP.
444 This is the declarative counterpart of 'gexp->file'."
445 (%scheme-file name gexp splice?))
447 (define-gexp-compiler (scheme-file-compiler (file <scheme-file>)
449 ;; Compile FILE by returning a derivation that builds the file.
451 (($ <scheme-file> name gexp splice?)
452 (gexp->file name gexp #:splice? splice?))))
454 ;; Appending SUFFIX to BASE's output file name.
455 (define-record-type <file-append>
456 (%file-append base suffix)
458 (base file-append-base) ;<package> | <derivation> | ...
459 (suffix file-append-suffix)) ;list of strings
461 (define (write-file-append file port)
463 (($ <file-append> base suffix)
464 (format port "#<file-append ~s ~s>" base
465 (string-join suffix)))))
467 (set-record-type-printer! <file-append> write-file-append)
469 (define (file-append base . suffix)
470 "Return a <file-append> object that expands to the concatenation of BASE and
472 (%file-append base suffix))
474 (define-gexp-compiler file-append-compiler <file-append>
475 compiler => (lambda (obj system target)
477 (($ <file-append> base _)
478 (lower-object base system #:target target))))
479 expander => (lambda (obj lowered output)
481 (($ <file-append> base suffix)
482 (let* ((expand (lookup-expander base))
483 (base (expand base lowered output)))
484 (string-append base (string-concatenate suffix)))))))
488 ;;; Inputs & outputs.
491 ;; The input of a gexp.
492 (define-record-type <gexp-input>
493 (%gexp-input thing output native?)
495 (thing gexp-input-thing) ;<package> | <origin> | <derivation> | ...
496 (output gexp-input-output) ;string
497 (native? gexp-input-native?)) ;Boolean
499 (define (write-gexp-input input port)
501 (($ <gexp-input> thing output #f)
502 (format port "#<gexp-input ~s:~a>" thing output))
503 (($ <gexp-input> thing output #t)
504 (format port "#<gexp-input native ~s:~a>" thing output))))
506 (set-record-type-printer! <gexp-input> write-gexp-input)
508 (define* (gexp-input thing ;convenience procedure
509 #:optional (output "out")
511 "Return a new <gexp-input> for the OUTPUT of THING; NATIVE? determines
512 whether this should be considered a \"native\" input or not."
513 (%gexp-input thing output native?))
515 ;; Reference to one of the derivation's outputs, for gexps used in
517 (define-record-type <gexp-output>
520 (name gexp-output-name))
522 (define (write-gexp-output output port)
524 (($ <gexp-output> name)
525 (format port "#<gexp-output ~a>" name))))
527 (set-record-type-printer! <gexp-output> write-gexp-output)
529 (define* (gexp-attribute gexp self-attribute #:optional (equal? equal?))
530 "Recurse on GEXP and the expressions it refers to, summing the items
531 returned by SELF-ATTRIBUTE, a procedure that takes a gexp. Use EQUAL? as the
532 second argument to 'delete-duplicates'."
535 (append (self-attribute gexp)
536 (append-map (match-lambda
537 (($ <gexp-input> (? gexp? exp))
538 (gexp-attribute exp self-attribute))
539 (($ <gexp-input> (lst ...))
540 (append-map (lambda (item)
548 (gexp-references gexp)))
550 '())) ;plain Scheme data type
552 (define (gexp-modules gexp)
553 "Return the list of Guile module names GEXP relies on. If (gexp? GEXP) is
554 false, meaning that GEXP is a plain Scheme object, return the empty list."
555 (define (module=? m1 m2)
556 ;; Return #t when M1 equals M2. Special-case '=>' specs because their
557 ;; right-hand side may not be comparable with 'equal?': it's typically a
558 ;; file-like object that embeds a gexp, which in turn embeds closure;
559 ;; those closures may be 'eq?' when running compiled code but are unlikely
560 ;; to be 'eq?' when running on 'eval'. Ignore the right-hand side to
561 ;; avoid this discrepancy.
565 (((name2 ...) '=> _) (equal? name1 name2))
570 (gexp-attribute gexp gexp-self-modules module=?))
572 (define (gexp-extensions gexp)
573 "Return the list of Guile extensions (packages) GEXP relies on. If (gexp?
574 GEXP) is false, meaning that GEXP is a plain Scheme object, return the empty
576 (gexp-attribute gexp gexp-self-extensions))
578 (define* (lower-inputs inputs
580 "Turn any object from INPUTS into a derivation input for SYSTEM or a store
581 item (a \"source\"); return the corresponding input list as a monadic value.
582 When TARGET is true, use it as the cross-compilation target triplet."
583 (define (store-item? obj)
584 (and (string? obj) (store-path? obj)))
586 (with-monad %store-monad
589 (((? struct? thing) sub-drv ...)
590 (mlet %store-monad ((obj (lower-object
591 thing system #:target target)))
594 (let ((outputs (if (null? sub-drv)
597 (derivation-input drv outputs)))
598 ((? store-item? item)
600 (((? store-item? item))
604 (define* (lower-reference-graphs graphs #:key system target)
605 "Given GRAPHS, a list of (FILE-NAME INPUT ...) lists for use as a
606 #:reference-graphs argument, lower it such that each INPUT is replaced by the
607 corresponding <derivation-input> or store item."
609 (((file-names . inputs) ...)
610 (mlet %store-monad ((inputs (lower-inputs inputs
613 (return (map cons file-names inputs))))))
615 (define* (lower-references lst #:key system target)
616 "Based on LST, a list of output names and packages, return a list of output
617 names and file names suitable for the #:allowed-references argument to
619 (with-monad %store-monad
624 (($ <gexp-input> thing output native?)
625 (mlet %store-monad ((drv (lower-object thing system
628 (return (derivation->output-path drv output))))
630 (mlet %store-monad ((drv (lower-object thing system
632 (return (derivation->output-path drv))))))
634 (mapm %store-monad lower lst)))
636 (define default-guile-derivation
637 ;; Here we break the abstraction by talking to the higher-level layer.
638 ;; Thus, do the resolution lazily to hide the circular dependency.
640 (let ((iface (resolve-interface '(guix packages))))
641 (module-ref iface 'default-guile-derivation)))))
643 ((force proc) system))))
645 ;; Representation of a gexp instantiated for a given target and system.
646 ;; It's an intermediate representation between <gexp> and <derivation>.
647 (define-record-type <lowered-gexp>
648 (lowered-gexp sexp inputs sources guile load-path load-compiled-path)
650 (sexp lowered-gexp-sexp) ;sexp
651 (inputs lowered-gexp-inputs) ;list of <derivation-input>
652 (sources lowered-gexp-sources) ;list of store items
653 (guile lowered-gexp-guile) ;<derivation-input> | #f
654 (load-path lowered-gexp-load-path) ;list of store items
655 (load-compiled-path lowered-gexp-load-compiled-path)) ;list of store items
657 (define* (lower-gexp exp
659 (module-path %load-path)
660 (system (%current-system))
663 (guile-for-build (%guile-for-build))
664 (effective-version "2.2")
666 deprecation-warnings)
667 "*Note: This API is subject to change; use at your own risk!*
669 Lower EXP, a gexp, instantiating it for SYSTEM and TARGET. Return a
670 <lowered-gexp> ready to be used.
672 Lowered gexps are an intermediate representation that's useful for
673 applications that deal with gexps outside in a way that is disconnected from
674 derivations--e.g., code evaluated for its side effects."
676 (delete-duplicates (gexp-modules exp)))
678 (define (search-path modules extensions suffix)
679 (append (match modules
681 (list (derivation->output-path drv)))
684 ((? store-path? item)
686 (map (lambda (extension)
687 (string-append (match extension
689 (derivation->output-path drv))
690 ((? store-path? item)
695 (mlet* %store-monad ( ;; The following binding forces '%current-system' and
696 ;; '%current-target-system' to be looked up at >>=
698 (graft? (set-grafting graft?))
700 (system -> (or system (%current-system)))
701 (target -> (if (eq? target 'current)
702 (%current-target-system)
704 (guile (if guile-for-build
705 (return guile-for-build)
706 (default-guile-derivation system)))
707 (normals (lower-inputs (gexp-inputs exp)
710 (natives (lower-inputs (gexp-native-inputs exp)
713 (inputs -> (append normals natives))
714 (sexp (gexp->sexp exp
717 (extensions -> (gexp-extensions exp))
718 (exts (mapm %store-monad
720 (lower-object obj system))
722 (modules (if (pair? %modules)
723 (imported-modules %modules
725 #:module-path module-path)
727 (compiled (if (pair? %modules)
728 (compiled-modules %modules
730 #:module-path module-path
731 #:extensions extensions
733 #:deprecation-warnings
734 deprecation-warnings)
737 (search-path modules exts
738 (string-append "/share/guile/site/" effective-version)))
740 (define load-compiled-path
741 (search-path compiled exts
742 (string-append "/lib/guile/" effective-version
746 (set-grafting graft?) ;restore the initial setting
747 (return (lowered-gexp sexp
748 `(,@(if (derivation? modules)
749 (list (derivation-input modules))
752 (list (derivation-input compiled))
754 ,@(map derivation-input exts)
755 ,@(filter derivation-input? inputs))
756 (filter string? (cons modules inputs))
757 (derivation-input guile '("out"))
759 load-compiled-path)))))
761 (define* (gexp->derivation name exp
763 system (target 'current)
764 hash hash-algo recursive?
767 (module-path %load-path)
768 (guile-for-build (%guile-for-build))
769 (effective-version "2.2")
772 allowed-references disallowed-references
774 local-build? (substitutable? #t)
777 (script-name (string-append name "-builder")))
778 "Return a derivation NAME that runs EXP (a gexp) with GUILE-FOR-BUILD (a
779 derivation) on SYSTEM; EXP is stored in a file called SCRIPT-NAME. When
780 TARGET is true, it is used as the cross-compilation target triplet for
781 packages referred to by EXP.
783 MODULES is deprecated in favor of 'with-imported-modules'. Its meaning is to
784 make MODULES available in the evaluation context of EXP; MODULES is a list of
785 names of Guile modules searched in MODULE-PATH to be copied in the store,
786 compiled, and made available in the load path during the execution of
787 EXP---e.g., '((guix build utils) (guix build gnu-build-system)).
789 EFFECTIVE-VERSION determines the string to use when adding extensions of
790 EXP (see 'with-extensions') to the search path---e.g., \"2.2\".
792 GRAFT? determines whether packages referred to by EXP should be grafted when
795 When REFERENCES-GRAPHS is true, it must be a list of tuples of one of the
799 (FILE-NAME PACKAGE OUTPUT)
800 (FILE-NAME DERIVATION)
801 (FILE-NAME DERIVATION OUTPUT)
802 (FILE-NAME STORE-ITEM)
804 The right-hand-side of each element of REFERENCES-GRAPHS is automatically made
805 an input of the build process of EXP. In the build environment, each
806 FILE-NAME contains the reference graph of the corresponding item, in a simple
809 ALLOWED-REFERENCES must be either #f or a list of output names and packages.
810 In the latter case, the list denotes store items that the result is allowed to
811 refer to. Any reference to another store item will lead to a build error.
812 Similarly for DISALLOWED-REFERENCES, which can list items that must not be
813 referenced by the outputs.
815 DEPRECATION-WARNINGS determines whether to show deprecation warnings while
816 compiling modules. It can be #f, #t, or 'detailed.
818 The other arguments are as for 'derivation'."
819 (define outputs (gexp-outputs exp))
820 (define requested-graft? graft?)
822 (define (graphs-file-names graphs)
823 ;; Return a list of (FILE-NAME . STORE-PATH) pairs made from GRAPHS.
825 ((file-name . (? derivation-input? input))
826 (cons file-name (first (derivation-input-output-paths input))))
827 ((file-name . (? string? item))
828 (cons file-name item)))
831 (define (add-modules exp modules)
834 (make-gexp (gexp-references exp)
835 (append modules (gexp-self-modules exp))
836 (gexp-self-extensions exp)
839 (mlet* %store-monad ( ;; The following binding forces '%current-system' and
840 ;; '%current-target-system' to be looked up at >>=
842 (graft? (set-grafting graft?))
844 (system -> (or system (%current-system)))
845 (target -> (if (eq? target 'current)
846 (%current-target-system)
848 (exp -> (add-modules exp modules))
849 (lowered (lower-gexp exp
850 #:module-path module-path
853 #:graft? requested-graft?
858 #:deprecation-warnings
859 deprecation-warnings))
861 (graphs (if references-graphs
862 (lower-reference-graphs references-graphs
866 (allowed (if allowed-references
867 (lower-references allowed-references
871 (disallowed (if disallowed-references
872 (lower-references disallowed-references
876 (guile -> (lowered-gexp-guile lowered))
877 (builder (text-file script-name
879 (lowered-gexp-sexp lowered)))))
881 (set-grafting graft?) ;restore the initial setting
883 (string-append (derivation-input-output-path guile)
885 `("--no-auto-compile"
886 ,@(append-map (lambda (directory)
888 (lowered-gexp-load-path lowered))
889 ,@(append-map (lambda (directory)
891 (lowered-gexp-load-compiled-path lowered))
897 ,@(lowered-gexp-inputs lowered)
900 (filter derivation-input? inputs))
903 ,@(if (and (string? modules)
904 (store-path? modules))
907 ,@(lowered-gexp-sources lowered)
910 (filter string? inputs))
913 #:hash hash #:hash-algo hash-algo #:recursive? recursive?
914 #:references-graphs (and=> graphs graphs-file-names)
915 #:allowed-references allowed
916 #:disallowed-references disallowed
917 #:leaked-env-vars leaked-env-vars
918 #:local-build? local-build?
919 #:substitutable? substitutable?
920 #:properties properties))))
922 (define* (gexp-inputs exp #:key native?)
923 "Return the input list for EXP. When NATIVE? is true, return only native
924 references; otherwise, return only non-native references."
925 ;; TODO: Return <gexp-input> records instead of tuples.
926 (define (add-reference-inputs ref result)
928 (($ <gexp-input> (? gexp? exp) _ #t)
930 (append (gexp-inputs exp)
931 (gexp-inputs exp #:native? #t)
934 (($ <gexp-input> (? gexp? exp) _ #f)
935 (append (gexp-inputs exp #:native? native?)
937 (($ <gexp-input> (? string? str))
938 (if (direct-store-path? str)
939 (cons `(,str) result)
941 (($ <gexp-input> (? struct? thing) output n?)
942 (if (and (eqv? n? native?) (lookup-compiler thing))
943 ;; THING is a derivation, or a package, or an origin, etc.
944 (cons `(,thing ,output) result)
946 (($ <gexp-input> (lst ...) output n?)
947 (fold-right add-reference-inputs result
948 ;; XXX: For now, automatically convert LST to a list of
949 ;; gexp-inputs. Inherit N?.
952 (%gexp-input (gexp-input-thing x)
953 (gexp-input-output x)
956 (%gexp-input x "out" n?)))
959 ;; Ignore references to other kinds of objects.
962 (fold-right add-reference-inputs
964 (gexp-references exp)))
966 (define gexp-native-inputs
967 (cut gexp-inputs <> #:native? #t))
969 (define (gexp-outputs exp)
970 "Return the outputs referred to by EXP as a list of strings."
971 (define (add-reference-output ref result)
973 (($ <gexp-output> name)
975 (($ <gexp-input> (? gexp? exp))
976 (append (gexp-outputs exp) result))
977 (($ <gexp-input> (lst ...) output native?)
978 ;; XXX: Automatically convert LST.
979 (add-reference-output (map (match-lambda
980 ((? gexp-input? x) x)
981 (x (%gexp-input x "out" native?)))
985 (fold-right add-reference-output result lst))
990 (add-reference-output (gexp-references exp) '())))
992 (define* (gexp->sexp exp #:key
993 (system (%current-system))
994 (target (%current-target-system)))
995 "Return (monadically) the sexp corresponding to EXP for the given OUTPUT,
996 and in the current monad setting (system type, etc.)"
997 (define (self-quoting? x)
998 (letrec-syntax ((one-of (syntax-rules ()
1002 (one-of rest ...))))))
1003 (one-of symbol? string? keyword? pair? null? array?
1006 (define* (reference->sexp ref #:optional native?)
1007 (with-monad %store-monad
1009 (($ <gexp-output> output)
1010 ;; Output file names are not known in advance but the daemon defines
1011 ;; an environment variable for each of them at build time, so use
1013 (return `((@ (guile) getenv) ,output)))
1014 (($ <gexp-input> (? gexp? exp) output n?)
1017 #:target (if (or n? native?) #f target)))
1018 (($ <gexp-input> (refs ...) output n?)
1021 ;; XXX: Automatically convert REF to an gexp-input.
1023 (if (gexp-input? ref)
1025 (%gexp-input ref "out" n?))
1028 (($ <gexp-input> (? struct? thing) output n?)
1029 (let ((target (if (or n? native?) #f target))
1030 (expand (lookup-expander thing)))
1031 (mlet %store-monad ((obj (lower-object thing system
1033 ;; OBJ must be either a derivation or a store file name.
1034 (return (expand thing obj output)))))
1035 (($ <gexp-input> (? self-quoting? x))
1038 (raise (condition (&gexp-input-error (input x)))))
1043 ((args (mapm %store-monad
1044 reference->sexp (gexp-references exp))))
1045 (return (apply (gexp-proc exp) args))))
1047 (define-syntax-rule (define-syntax-parameter-once name proc)
1048 ;; Like 'define-syntax-parameter' but ensure the top-level binding for NAME
1049 ;; does not get redefined. This works around a race condition in a
1050 ;; multi-threaded context with Guile <= 2.2.4: <https://bugs.gnu.org/27476>.
1051 (eval-when (load eval expand compile)
1053 (if (module-locally-bound? (current-module) 'name)
1054 (module-ref (current-module) 'name)
1055 (make-syntax-transformer 'name 'syntax-parameter
1058 (define-syntax-parameter-once current-imported-modules
1059 ;; Current list of imported modules.
1060 (identifier-syntax '()))
1062 (define-syntax-rule (with-imported-modules modules body ...)
1063 "Mark the gexps defined in BODY... as requiring MODULES in their execution
1065 (syntax-parameterize ((current-imported-modules
1066 (identifier-syntax modules)))
1069 (define-syntax-parameter-once current-imported-extensions
1070 ;; Current list of extensions.
1071 (identifier-syntax '()))
1073 (define-syntax-rule (with-extensions extensions body ...)
1074 "Mark the gexps defined in BODY... as requiring EXTENSIONS in their
1075 execution environment."
1076 (syntax-parameterize ((current-imported-extensions
1077 (identifier-syntax extensions)))
1082 (define (collect-escapes exp)
1083 ;; Return all the 'ungexp' present in EXP.
1084 (let loop ((exp exp)
1086 (syntax-case exp (ungexp
1089 ungexp-native-splicing)
1094 ((ungexp-splicing _ ...)
1096 ((ungexp-native _ ...)
1098 ((ungexp-native-splicing _ ...)
1101 (let ((result (loop #'exp0 result)))
1102 (loop #'exp result)))
1106 (define (escape->ref exp)
1107 ;; Turn 'ungexp' form EXP into a "reference".
1108 (syntax-case exp (ungexp ungexp-splicing
1109 ungexp-native ungexp-native-splicing
1112 #'(gexp-output "out"))
1113 ((ungexp output name)
1114 #'(gexp-output name))
1116 #'(%gexp-input thing "out" #f))
1117 ((ungexp drv-or-pkg out)
1118 #'(%gexp-input drv-or-pkg out #f))
1119 ((ungexp-splicing lst)
1120 #'(%gexp-input lst "out" #f))
1121 ((ungexp-native thing)
1122 #'(%gexp-input thing "out" #t))
1123 ((ungexp-native drv-or-pkg out)
1124 #'(%gexp-input drv-or-pkg out #t))
1125 ((ungexp-native-splicing lst)
1126 #'(%gexp-input lst "out" #t))))
1128 (define (substitute-ungexp exp substs)
1129 ;; Given EXP, an 'ungexp' or 'ungexp-native' form, substitute it with
1130 ;; the corresponding form in SUBSTS.
1131 (match (assoc exp substs)
1135 (with-syntax ((exp exp))
1136 #'(syntax-error "error: no 'ungexp' substitution" exp)))))
1138 (define (substitute-ungexp-splicing exp substs)
1141 (match (assoc #'exp substs)
1143 (with-syntax ((id id))
1145 #,(substitute-references #'(rest ...) substs))))
1147 #'(syntax-error "error: no 'ungexp-splicing' substitution"
1150 (define (substitute-references exp substs)
1151 ;; Return a variant of EXP where all the cars of SUBSTS have been
1152 ;; replaced by the corresponding cdr.
1153 (syntax-case exp (ungexp ungexp-native
1154 ungexp-splicing ungexp-native-splicing)
1156 (substitute-ungexp exp substs))
1157 ((ungexp-native _ ...)
1158 (substitute-ungexp exp substs))
1159 (((ungexp-splicing _ ...) rest ...)
1160 (substitute-ungexp-splicing exp substs))
1161 (((ungexp-native-splicing _ ...) rest ...)
1162 (substitute-ungexp-splicing exp substs))
1164 #`(cons #,(substitute-references #'exp0 substs)
1165 #,(substitute-references #'exp substs)))
1168 (syntax-case s (ungexp output)
1170 (let* ((escapes (delete-duplicates (collect-escapes #'exp)))
1171 (formals (generate-temporaries escapes))
1172 (sexp (substitute-references #'exp (zip escapes formals)))
1173 (refs (map escape->ref escapes)))
1174 #`(make-gexp (list #,@refs)
1175 current-imported-modules
1176 current-imported-extensions
1182 ;;; Module handling.
1186 (char-set-complement (char-set #\/)))
1188 (define (file-mapping->tree mapping)
1189 "Convert MAPPING, an alist like:
1191 ((\"guix/build/utils.scm\" . \"…/utils.scm\"))
1193 to a tree suitable for 'interned-file-tree'."
1194 (let ((mapping (map (match-lambda
1195 ((destination . source)
1196 (cons (string-tokenize destination
1200 (fold (lambda (pair result)
1202 ((destination . source)
1203 (let loop ((destination destination)
1207 (let* ((mode (stat:mode (stat source)))
1208 (type (if (zero? (logand mode #o100))
1212 `(,type (file ,source))
1215 (let ((directory (assoc-ref result file)))
1220 (('directory . entries) entries)
1223 (alist-delete file result)
1228 (define %utils-module
1229 ;; This file provides 'mkdir-p', needed to implement 'imported-files' and
1230 ;; other primitives below. Note: We give the file name relative to this
1231 ;; file you are currently reading; 'search-path' could return a file name
1232 ;; relative to the current working directory.
1233 (local-file "build/utils.scm"
1236 (define* (imported-files/derivation files
1237 #:key (name "file-import")
1239 (system (%current-system))
1240 (guile (%guile-for-build)))
1241 "Return a derivation that imports FILES into STORE. FILES must be a list
1242 of (FINAL-PATH . FILE) pairs. Each FILE is mapped to FINAL-PATH in the
1243 resulting store path. FILE can be either a file name, or a file-like object,
1244 as returned by 'local-file' for example. If SYMLINK? is true, create symlinks
1245 to the source files instead of copying them."
1248 ((final-path . (? string? file-name))
1249 (mlet %store-monad ((file (interned-file file-name
1250 (basename final-path))))
1251 (return (list final-path file))))
1252 ((final-path . file-like)
1253 (mlet %store-monad ((file (lower-object file-like system)))
1254 (return (list final-path file))))))
1256 (mlet %store-monad ((files (mapm %store-monad file-pair files)))
1260 (primitive-load (ungexp %utils-module)) ;for 'mkdir-p'
1261 (use-modules (ice-9 match))
1263 (mkdir (ungexp output)) (chdir (ungexp output))
1264 (for-each (match-lambda
1265 ((final-path store-path)
1266 (mkdir-p (dirname final-path))
1267 ((ungexp (if symlink? 'symlink 'copy-file))
1268 store-path final-path)))
1271 ;; TODO: Pass FILES as an environment variable so that BUILD remains
1272 ;; exactly the same regardless of FILES: less disk space, and fewer
1273 ;; 'add-to-store' RPCs.
1274 (gexp->derivation name build
1276 #:guile-for-build guile
1279 ;; Avoid deprecation warnings about the use of the _IO*
1280 ;; constants in (guix build utils).
1282 '(("GUILE_WARN_DEPRECATED" . "no")))))
1284 (define* (imported-files files
1285 #:key (name "file-import")
1286 ;; The following parameters make sense when creating
1287 ;; an actual derivation.
1288 (system (%current-system))
1289 (guile (%guile-for-build)))
1290 "Import FILES into the store and return the resulting derivation or store
1291 file name (a derivation is created if and only if some elements of FILES are
1292 file-like objects and not local file names.) FILES must be a list
1293 of (FINAL-PATH . FILE) pairs. Each FILE is mapped to FINAL-PATH in the
1294 resulting store path. FILE can be either a file name, or a file-like object,
1295 as returned by 'local-file' for example."
1296 (if (any (match-lambda
1297 ((_ . (? struct? source)) #t)
1300 (imported-files/derivation files #:name name
1301 #:symlink? derivation?
1302 #:system system #:guile guile)
1303 (interned-file-tree `(,name directory
1304 ,@(file-mapping->tree files)))))
1306 (define* (imported-modules modules
1307 #:key (name "module-import")
1308 (system (%current-system))
1309 (guile (%guile-for-build))
1310 (module-path %load-path))
1311 "Return a derivation that contains the source files of MODULES, a list of
1312 module names such as `(ice-9 q)'. All of MODULES must be either names of
1313 modules to be found in the MODULE-PATH search path, or a module name followed
1314 by an arrow followed by a file-like object. For example:
1316 (imported-modules `((guix build utils)
1318 ((guix config) => ,(scheme-file …))))
1320 In this example, the first two modules are taken from MODULE-PATH, and the
1321 last one is created from the given <scheme-file> object."
1322 (let ((files (map (match-lambda
1323 (((module ...) '=> file)
1324 (cons (module->source-file-name module)
1327 (let ((f (module->source-file-name module)))
1328 (cons f (search-path* module-path f)))))
1330 (imported-files files #:name name
1334 (define* (compiled-modules modules
1335 #:key (name "module-import-compiled")
1336 (system (%current-system))
1338 (guile (%guile-for-build))
1339 (module-path %load-path)
1341 (deprecation-warnings #f))
1342 "Return a derivation that builds a tree containing the `.go' files
1343 corresponding to MODULES. All the MODULES are built in a context where
1344 they can refer to each other. When TARGET is true, cross-compile MODULES for
1345 TARGET, a GNU triplet."
1346 (define total (length modules))
1348 (mlet %store-monad ((modules (imported-modules modules
1356 (primitive-load (ungexp %utils-module)) ;for 'mkdir-p'
1358 (use-modules (ice-9 ftw)
1362 (system base compile))
1364 ;; TODO: Inline this on the next rebuild cycle.
1367 (gexp ((use-modules (system base target))))
1370 (define (regular? file)
1371 (not (member file '("." ".."))))
1373 (define (process-entry entry output processed)
1374 (if (file-is-directory? entry)
1375 (let ((output (string-append output "/" (basename entry))))
1377 (process-directory entry output processed))
1378 (let* ((base (basename entry ".scm"))
1379 (output (string-append output "/" base ".go")))
1380 (format #t "[~2@a/~2@a] Compiling '~a'...~%"
1381 (+ 1 processed (ungexp total))
1382 (ungexp (* total 2))
1387 (gexp ((with-target (ungexp target)
1390 #:output-file output
1392 %auto-compilation-options)))))
1393 (gexp ((compile-file entry
1394 #:output-file output
1395 #:opts %auto-compilation-options)))))
1399 (define (process-directory directory output processed)
1400 (let ((entries (map (cut string-append directory "/" <>)
1401 (scandir directory regular?))))
1402 (fold (cut process-entry <> output <>)
1406 (define* (load-from-directory directory
1407 #:optional (loaded 0))
1408 "Load all the source files found in DIRECTORY."
1409 ;; XXX: This works around <https://bugs.gnu.org/15602>.
1410 (let ((entries (map (cut string-append directory "/" <>)
1411 (scandir directory regular?))))
1412 (fold (lambda (file loaded)
1413 (if (file-is-directory? file)
1414 (load-from-directory file loaded)
1416 (format #t "[~2@a/~2@a] Loading '~a'...~%"
1417 (+ 1 loaded) (ungexp (* 2 total))
1419 (save-module-excursion
1421 (primitive-load file)))
1426 (setvbuf (current-output-port)
1427 (cond-expand (guile-2.2 'line) (else _IOLBF)))
1430 ;; Capture 'mkdir-p'.
1431 (@ (guix build utils) mkdir-p))
1433 ;; Add EXTENSIONS to the search path.
1435 (append (map (lambda (extension)
1436 (string-append extension
1437 "/share/guile/site/"
1438 (effective-version)))
1439 '((ungexp-native-splicing extensions)))
1441 (set! %load-compiled-path
1442 (append (map (lambda (extension)
1443 (string-append extension "/lib/guile/"
1446 '((ungexp-native-splicing extensions)))
1447 %load-compiled-path))
1449 (set! %load-path (cons (ungexp modules) %load-path))
1451 ;; Above we loaded our own (guix build utils) but now we may need to
1452 ;; load a compile a different one. Thus, force a reload.
1453 (let ((utils (string-append (ungexp modules)
1454 "/guix/build/utils.scm")))
1455 (when (file-exists? utils)
1458 (mkdir (ungexp output))
1459 (chdir (ungexp modules))
1461 (load-from-directory ".")
1462 (process-directory "." (ungexp output) 0))))
1464 ;; TODO: Pass MODULES as an environment variable.
1465 (gexp->derivation name build
1467 #:guile-for-build guile
1470 (case deprecation-warnings
1472 '(("GUILE_WARN_DEPRECATED" . "no")))
1474 '(("GUILE_WARN_DEPRECATED" . "detailed")))
1480 ;;; Convenience procedures.
1483 (define (default-guile)
1484 ;; Lazily resolve 'guile-2.2' (not 'guile-final' because this is for
1485 ;; programs returned by 'program-file' and we don't want to keep references
1486 ;; to several Guile packages). This module must not refer to (gnu …)
1487 ;; modules directly, to avoid circular dependencies, hence this hack.
1488 (module-ref (resolve-interface '(gnu packages guile))
1491 (define* (load-path-expression modules #:optional (path %load-path)
1492 #:key (extensions '()) system target)
1493 "Return as a monadic value a gexp that sets '%load-path' and
1494 '%load-compiled-path' to point to MODULES, a list of module names. MODULES
1495 are searched for in PATH. Return #f when MODULES and EXTENSIONS are empty."
1496 (if (and (null? modules) (null? extensions))
1497 (with-monad %store-monad
1499 (mlet %store-monad ((modules (imported-modules modules
1502 (compiled (compiled-modules modules
1503 #:extensions extensions
1507 (return (gexp (eval-when (expand load eval)
1509 (cons (ungexp modules)
1510 (append (map (lambda (extension)
1511 (string-append extension
1512 "/share/guile/site/"
1513 (effective-version)))
1514 '((ungexp-native-splicing extensions)))
1516 (set! %load-compiled-path
1517 (cons (ungexp compiled)
1518 (append (map (lambda (extension)
1519 (string-append extension
1523 '((ungexp-native-splicing extensions)))
1524 %load-compiled-path)))))))))
1526 (define* (gexp->script name exp
1527 #:key (guile (default-guile))
1528 (module-path %load-path)
1529 (system (%current-system))
1531 "Return an executable script NAME that runs EXP using GUILE, with EXP's
1532 imported modules in its search path. Look up EXP's modules in MODULE-PATH."
1533 (mlet %store-monad ((set-load-path
1534 (load-path-expression (gexp-modules exp)
1537 (gexp-extensions exp)
1540 (gexp->derivation name
1542 (call-with-output-file (ungexp output)
1544 ;; Note: that makes a long shebang. When the store
1545 ;; is /gnu/store, that fits within the 128-byte
1546 ;; limit imposed by Linux, but that may go beyond
1547 ;; when running tests.
1549 "#!~a/bin/guile --no-auto-compile~%!#~%"
1554 (gexp ((write '(ungexp set-load-path) port)))
1557 (write '(ungexp exp) port)
1558 (chmod port #o555))))
1561 #:module-path module-path)))
1563 (define* (gexp->file name exp #:key
1565 (module-path %load-path)
1567 "Return a derivation that builds a file NAME containing EXP. When SPLICE?
1568 is true, EXP is considered to be a list of expressions that will be spliced in
1571 When SET-LOAD-PATH? is true, emit code in the resulting file to set
1572 '%load-path' and '%load-compiled-path' to honor EXP's imported modules.
1573 Lookup EXP's modules in MODULE-PATH."
1574 (define modules (gexp-modules exp))
1575 (define extensions (gexp-extensions exp))
1577 (if (or (not set-load-path?)
1578 (and (null? modules) (null? extensions)))
1579 (gexp->derivation name
1581 (call-with-output-file (ungexp output)
1583 (for-each (lambda (exp)
1585 '(ungexp (if splice?
1587 (gexp ((ungexp exp)))))))))
1589 #:substitutable? #f)
1590 (mlet %store-monad ((set-load-path
1591 (load-path-expression modules module-path
1592 #:extensions extensions)))
1593 (gexp->derivation name
1595 (call-with-output-file (ungexp output)
1597 (write '(ungexp set-load-path) port)
1598 (for-each (lambda (exp)
1600 '(ungexp (if splice?
1602 (gexp ((ungexp exp)))))))))
1603 #:module-path module-path
1605 #:substitutable? #f))))
1607 (define* (text-file* name #:rest text)
1608 "Return as a monadic value a derivation that builds a text file containing
1609 all of TEXT. TEXT may list, in addition to strings, objects of any type that
1610 can be used in a gexp: packages, derivations, local file objects, etc. The
1611 resulting store file holds references to all these."
1613 (gexp (call-with-output-file (ungexp output "out")
1615 (display (string-append (ungexp-splicing text)) port)))))
1617 (gexp->derivation name builder
1619 #:substitutable? #f))
1621 (define* (mixed-text-file name #:rest text)
1622 "Return an object representing store file NAME containing TEXT. TEXT is a
1623 sequence of strings and file-like objects, as in:
1625 (mixed-text-file \"profile\"
1626 \"export PATH=\" coreutils \"/bin:\" grep \"/bin\")
1628 This is the declarative counterpart of 'text-file*'."
1630 (gexp (call-with-output-file (ungexp output "out")
1632 (display (string-append (ungexp-splicing text)) port)))))
1634 (computed-file name build))
1636 (define (file-union name files)
1637 "Return a <computed-file> that builds a directory containing all of FILES.
1638 Each item in FILES must be a two-element list where the first element is the
1639 file name to use in the new directory, and the second element is a gexp
1640 denoting the target file. Here's an example:
1643 `((\"hosts\" ,(plain-file \"hosts\"
1644 \"127.0.0.1 localhost\"))
1645 (\"bashrc\" ,(plain-file \"bashrc\"
1646 \"alias ls='ls --color'\"))
1647 (\"libvirt/qemu.conf\" ,(plain-file \"qemu.conf\" \"\"))))
1649 This yields an 'etc' directory containing these two files."
1651 (with-imported-modules '((guix build utils))
1654 (use-modules (guix build utils))
1656 (mkdir (ungexp output))
1657 (chdir (ungexp output))
1663 ;; Stat the source to abort early if it does
1665 (stat (ungexp source))
1667 (mkdir-p (dirname (ungexp target)))
1668 (symlink (ungexp source)
1669 (ungexp target))))))
1672 (define* (directory-union name things
1673 #:key (copy? #f) (quiet? #f)
1674 (resolve-collision 'warn-about-collision))
1675 "Return a directory that is the union of THINGS, where THINGS is a list of
1676 file-like objects denoting directories. For example:
1678 (directory-union \"guile+emacs\" (list guile emacs))
1680 yields a directory that is the union of the 'guile' and 'emacs' packages.
1682 Call RESOLVE-COLLISION when several files collide, passing it the list of
1683 colliding files. RESOLVE-COLLISION must return the chosen file or #f, in
1684 which case the colliding entry is skipped altogether.
1686 When HARD-LINKS? is true, create hard links instead of symlinks. When QUIET?
1687 is true, the derivation will not print anything."
1690 (gexp (lambda (old new)
1691 (if (file-is-directory? old)
1693 (copy-file old new))))
1698 (gexp (%make-void-port "w"))
1699 (gexp (current-error-port))))
1703 ;; Only one thing; return it.
1707 (with-imported-modules '((guix build union))
1709 (use-modules (guix build union)
1710 (srfi srfi-1)) ;for 'first' and 'last'
1712 (union-build (ungexp output)
1715 #:log-port (ungexp log-port)
1716 #:symlink (ungexp symlink)
1718 (ungexp resolve-collision)))))))))
1722 ;;; Syntactic sugar.
1725 (eval-when (expand load eval)
1726 (define* (read-ungexp chr port #:optional native?)
1727 "Read an 'ungexp' or 'ungexp-splicing' form from PORT. When NATIVE? is
1728 true, use 'ungexp-native' and 'ungexp-native-splicing' instead."
1729 (define unquote-symbol
1730 (match (peek-char port)
1734 'ungexp-native-splicing
1743 (let ((str (symbol->string symbol)))
1744 (match (string-index-right str #\:)
1746 `(,unquote-symbol ,symbol))
1748 (let ((name (string->symbol (substring str 0 colon)))
1749 (output (substring str (+ colon 1))))
1750 `(,unquote-symbol ,name ,output))))))
1752 `(,unquote-symbol ,x))))
1754 (define (read-gexp chr port)
1755 "Read a 'gexp' form from PORT."
1756 `(gexp ,(read port)))
1758 ;; Extend the reader
1759 (read-hash-extend #\~ read-gexp)
1760 (read-hash-extend #\$ read-ungexp)
1761 (read-hash-extend #\+ (cut read-ungexp <> <> #t)))
1763 ;;; gexp.scm ends here