gnu: emacs: Strip double wrap.
[jackhill/guix/guix.git] / guix / gexp.scm
CommitLineData
21b679f6 1;;; GNU Guix --- Functional package management for GNU
52207b39 2;;; Copyright © 2014, 2015, 2016, 2017, 2018, 2019, 2020 Ludovic Courtès <ludo@gnu.org>
bdcf0e6f 3;;; Copyright © 2018 Clément Lassieur <clement@lassieur.org>
e8e1f295 4;;; Copyright © 2018 Jan Nieuwenhuizen <janneke@gnu.org>
a6bf7a97 5;;; Copyright © 2019, 2020 Mathieu Othacehe <m.othacehe@gmail.com>
21b679f6
LC
6;;;
7;;; This file is part of GNU Guix.
8;;;
9;;; GNU Guix is free software; you can redistribute it and/or modify it
10;;; under the terms of the GNU General Public License as published by
11;;; the Free Software Foundation; either version 3 of the License, or (at
12;;; your option) any later version.
13;;;
14;;; GNU Guix is distributed in the hope that it will be useful, but
15;;; WITHOUT ANY WARRANTY; without even the implied warranty of
16;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17;;; GNU General Public License for more details.
18;;;
19;;; You should have received a copy of the GNU General Public License
20;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
21
22(define-module (guix gexp)
e87f0591 23 #:use-module (guix store)
21b679f6 24 #:use-module (guix monads)
e87f0591 25 #:use-module (guix derivations)
7adf9b84 26 #:use-module (guix grafts)
aa72d9af 27 #:use-module (guix utils)
e8e1f295 28 #:use-module (rnrs bytevectors)
21b679f6
LC
29 #:use-module (srfi srfi-1)
30 #:use-module (srfi srfi-9)
7560b00b 31 #:use-module (srfi srfi-9 gnu)
21b679f6 32 #:use-module (srfi srfi-26)
3e43166f
LC
33 #:use-module (srfi srfi-34)
34 #:use-module (srfi srfi-35)
21b679f6
LC
35 #:use-module (ice-9 match)
36 #:export (gexp
37 gexp?
0bb9929e 38 with-imported-modules
838e17d8 39 with-extensions
644cb40c 40 let-system
0dbea56b
LC
41
42 gexp-input
43 gexp-input?
2ca41030
LC
44 gexp-input-thing
45 gexp-input-output
46 gexp-input-native?
558e8b11 47
d9ae938f
LC
48 local-file
49 local-file?
74d441ab 50 local-file-file
9d3994f7 51 local-file-absolute-file-name
74d441ab
LC
52 local-file-name
53 local-file-recursive?
f408d8d6 54 local-file-select?
0dbea56b 55
558e8b11
LC
56 plain-file
57 plain-file?
58 plain-file-name
59 plain-file-content
60
91937029
LC
61 computed-file
62 computed-file?
63 computed-file-name
64 computed-file-gexp
91937029
LC
65 computed-file-options
66
15a01c72
LC
67 program-file
68 program-file?
69 program-file-name
70 program-file-gexp
15a01c72 71 program-file-guile
427ec19e 72 program-file-module-path
15a01c72 73
e1c153e0
LC
74 scheme-file
75 scheme-file?
76 scheme-file-name
77 scheme-file-gexp
78
a9e5e92f
LC
79 file-append
80 file-append?
81 file-append-base
82 file-append-suffix
83
d63ee94d
LC
84 raw-derivation-file
85 raw-derivation-file?
86
cf2ac04f
LC
87 with-parameters
88 parameterized?
89
64fc9f65
RJ
90 load-path-expression
91 gexp-modules
92
2ca41030
LC
93 lower-gexp
94 lowered-gexp?
95 lowered-gexp-sexp
96 lowered-gexp-inputs
38685774 97 lowered-gexp-sources
2ca41030
LC
98 lowered-gexp-guile
99 lowered-gexp-load-path
100 lowered-gexp-load-compiled-path
101
21b679f6
LC
102 gexp->derivation
103 gexp->file
462a3fa3 104 gexp->script
aa72d9af 105 text-file*
b751cde3 106 mixed-text-file
dedb512f 107 file-union
d298c815 108 directory-union
aa72d9af
LC
109 imported-files
110 imported-modules
ff40e9b7
LC
111 compiled-modules
112
113 define-gexp-compiler
6b6298ae 114 gexp-compiler?
bdcf0e6f 115 file-like?
c2b84676 116 lower-object
6b6298ae 117
3e43166f
LC
118 lower-inputs
119
120 &gexp-error
121 gexp-error?
122 &gexp-input-error
123 gexp-input-error?
124 gexp-error-invalid-input))
21b679f6
LC
125
126;;; Commentary:
127;;;
128;;; This module implements "G-expressions", or "gexps". Gexps are like
129;;; S-expressions (sexps), with two differences:
130;;;
131;;; 1. References (un-quotations) to derivations or packages in a gexp are
667b2508
LC
132;;; replaced by the corresponding output file name; in addition, the
133;;; 'ungexp-native' unquote-like form allows code to explicitly refer to
134;;; the native code of a given package, in case of cross-compilation;
21b679f6
LC
135;;;
136;;; 2. Gexps embed information about the derivations they refer to.
137;;;
138;;; Gexps make it easy to write to files Scheme code that refers to store
139;;; items, or to write Scheme code to build derivations.
140;;;
141;;; Code:
142
143;; "G expressions".
144(define-record-type <gexp>
838e17d8 145 (make-gexp references modules extensions proc)
21b679f6 146 gexp?
affd7761 147 (references gexp-references) ;list of <gexp-input>
0bb9929e 148 (modules gexp-self-modules) ;list of module names
838e17d8 149 (extensions gexp-self-extensions) ;list of lowerable things
affd7761 150 (proc gexp-proc)) ;procedure
21b679f6 151
7560b00b
LC
152(define (write-gexp gexp port)
153 "Write GEXP on PORT."
154 (display "#<gexp " port)
2cf0ea0d
LC
155
156 ;; Try to write the underlying sexp. Now, this trick doesn't work when
157 ;; doing things like (ungexp-splicing (gexp ())) because GEXP's procedure
158 ;; tries to use 'append' on that, which fails with wrong-type-arg.
159 (false-if-exception
667b2508 160 (write (apply (gexp-proc gexp)
affd7761 161 (gexp-references gexp))
667b2508 162 port))
7560b00b
LC
163 (format port " ~a>"
164 (number->string (object-address gexp) 16)))
165
166(set-record-type-printer! <gexp> write-gexp)
167
bcb13287
LC
168\f
169;;;
170;;; Methods.
171;;;
172
173;; Compiler for a type of objects that may be introduced in a gexp.
174(define-record-type <gexp-compiler>
1cdecf24 175 (gexp-compiler type lower expand)
bcb13287 176 gexp-compiler?
1cdecf24 177 (type gexp-compiler-type) ;record type descriptor
ebdfd776 178 (lower gexp-compiler-lower)
1cdecf24 179 (expand gexp-compiler-expand)) ;#f | DRV -> sexp
bcb13287 180
3e43166f
LC
181(define-condition-type &gexp-error &error
182 gexp-error?)
183
184(define-condition-type &gexp-input-error &gexp-error
185 gexp-input-error?
186 (input gexp-error-invalid-input))
187
188
bcb13287 189(define %gexp-compilers
1cdecf24
LC
190 ;; 'eq?' mapping of record type descriptor to <gexp-compiler>.
191 (make-hash-table 20))
bcb13287 192
ebdfd776
LC
193(define (default-expander thing obj output)
194 "This is the default expander for \"things\" that appear in gexps. It
195returns its output file name of OBJ's OUTPUT."
196 (match obj
197 ((? derivation? drv)
198 (derivation->output-path drv output))
199 ((? string? file)
644cb40c
LC
200 file)
201 ((? self-quoting? obj)
202 obj)))
ebdfd776 203
bcb13287
LC
204(define (register-compiler! compiler)
205 "Register COMPILER as a gexp compiler."
1cdecf24
LC
206 (hashq-set! %gexp-compilers
207 (gexp-compiler-type compiler) compiler))
bcb13287
LC
208
209(define (lookup-compiler object)
ebdfd776 210 "Search for a compiler for OBJECT. Upon success, return the three argument
bcb13287 211procedure to lower it; otherwise return #f."
1cdecf24
LC
212 (and=> (hashq-ref %gexp-compilers (struct-vtable object))
213 gexp-compiler-lower))
bcb13287 214
bdcf0e6f
CL
215(define (file-like? object)
216 "Return #t if OBJECT leads to a file in the store once unquoted in a
217G-expression; otherwise return #f."
218 (and (struct? object) (->bool (lookup-compiler object))))
219
ebdfd776
LC
220(define (lookup-expander object)
221 "Search for an expander for OBJECT. Upon success, return the three argument
222procedure to expand it; otherwise return #f."
1cdecf24
LC
223 (and=> (hashq-ref %gexp-compilers (struct-vtable object))
224 gexp-compiler-expand))
ebdfd776 225
c2b84676
LC
226(define* (lower-object obj
227 #:optional (system (%current-system))
a6bf7a97 228 #:key (target 'current))
c2b84676
LC
229 "Return as a value in %STORE-MONAD the derivation or store item
230corresponding to OBJ for SYSTEM, cross-compiling for TARGET if TARGET is true.
231OBJ must be an object that has an associated gexp compiler, such as a
232<package>."
d03001a3
LC
233 (mlet %store-monad ((target (if (eq? target 'current)
234 (current-target-system)
235 (return target)))
236 (graft? (grafting?)))
237 (let loop ((obj obj))
238 (match (lookup-compiler obj)
239 (#f
240 (raise (condition (&gexp-input-error (input obj)))))
241 (lower
242 ;; Cache in STORE the result of lowering OBJ.
243 (mcached (mlet %store-monad ((lowered (lower obj system target)))
244 (if (and (struct? lowered)
245 (not (derivation? lowered)))
246 (loop lowered)
247 (return lowered)))
248 obj
249 system target graft?))))))
250
251(define* (lower+expand-object obj
252 #:optional (system (%current-system))
253 #:key target (output "out"))
254 "Return as a value in %STORE-MONAD the output of object OBJ expands to for
255SYSTEM and TARGET. Object such as <package>, <file-append>, or <plain-file>
256expand to file names, but it's possible to expand to a plain data type."
257 (let loop ((obj obj)
258 (expand (and (struct? obj) (lookup-expander obj))))
259 (match (lookup-compiler obj)
260 (#f
261 (raise (condition (&gexp-input-error (input obj)))))
262 (lower
263 (mlet* %store-monad ((graft? (grafting?))
264 (lowered (mcached (lower obj system target)
265 obj
266 system target graft?)))
267 ;; LOWER might return something that needs to be further
268 ;; lowered.
269 (if (struct? lowered)
270 ;; If we lack an expander, delegate to that of LOWERED.
271 (if (not expand)
272 (loop lowered (lookup-expander lowered))
273 (return (expand obj lowered output)))
abf43d45
LC
274 (if (not expand) ;self-quoting
275 (return lowered)
276 (return (expand obj lowered output)))))))))
c2b84676 277
ebdfd776
LC
278(define-syntax define-gexp-compiler
279 (syntax-rules (=> compiler expander)
280 "Define NAME as a compiler for objects matching PREDICATE encountered in
281gexps.
282
d03001a3
LC
283In the simplest form of the macro, BODY must return (1) a derivation for
284a record of the specified type, for SYSTEM and TARGET (the latter of which is
285#f except when cross-compiling), (2) another record that can itself be
286compiled down to a derivation, or (3) an object of a primitive data type.
ebdfd776
LC
287
288The more elaborate form allows you to specify an expander:
289
d03001a3 290 (define-gexp-compiler something-compiler <something>
ebdfd776
LC
291 compiler => (lambda (param system target) ...)
292 expander => (lambda (param drv output) ...))
293
294The expander specifies how an object is converted to its sexp representation."
1cdecf24
LC
295 ((_ (name (param record-type) system target) body ...)
296 (define-gexp-compiler name record-type
ebdfd776
LC
297 compiler => (lambda (param system target) body ...)
298 expander => default-expander))
1cdecf24 299 ((_ name record-type
ebdfd776
LC
300 compiler => compile
301 expander => expand)
302 (begin
303 (define name
1cdecf24 304 (gexp-compiler record-type compile expand))
ebdfd776 305 (register-compiler! name)))))
bcb13287 306
1cdecf24 307(define-gexp-compiler (derivation-compiler (drv <derivation>) system target)
2924f0d6
LC
308 ;; Derivations are the lowest-level representation, so this is the identity
309 ;; compiler.
310 (with-monad %store-monad
311 (return drv)))
312
d63ee94d
LC
313;; Expand to a raw ".drv" file for the lowerable object it wraps. In other
314;; words, this gives the raw ".drv" file instead of its build result.
315(define-record-type <raw-derivation-file>
316 (raw-derivation-file obj)
317 raw-derivation-file?
318 (obj raw-derivation-file-object)) ;lowerable object
319
320(define-gexp-compiler raw-derivation-file-compiler <raw-derivation-file>
321 compiler => (lambda (obj system target)
322 (mlet %store-monad ((obj (lower-object
323 (raw-derivation-file-object obj)
324 system #:target target)))
325 ;; Returning the .drv file name instead of the <derivation>
326 ;; record ensures that 'lower-gexp' will classify it as a
327 ;; "source" and not as an "input".
328 (return (if (derivation? obj)
329 (derivation-file-name obj)
330 obj))))
331 expander => (lambda (obj lowered output)
332 (if (derivation? lowered)
333 (derivation-file-name lowered)
334 lowered)))
335
bcb13287 336\f
644cb40c
LC
337;;;
338;;; System dependencies.
339;;;
340
341;; Binding form for the current system and cross-compilation target.
342(define-record-type <system-binding>
343 (system-binding proc)
344 system-binding?
345 (proc system-binding-proc))
346
347(define-syntax let-system
348 (syntax-rules ()
349 "Introduce a system binding in a gexp. The simplest form is:
350
351 (let-system system
352 (cond ((string=? system \"x86_64-linux\") ...)
353 (else ...)))
354
355which binds SYSTEM to the currently targeted system. The second form is
356similar, but it also shows the cross-compilation target:
357
358 (let-system (system target)
359 ...)
360
361Here TARGET is bound to the cross-compilation triplet or #f."
362 ((_ (system target) exp0 exp ...)
363 (system-binding (lambda (system target)
364 exp0 exp ...)))
365 ((_ system exp0 exp ...)
366 (system-binding (lambda (system target)
367 exp0 exp ...)))))
368
369(define-gexp-compiler system-binding-compiler <system-binding>
370 compiler => (lambda (binding system target)
371 (match binding
372 (($ <system-binding> proc)
373 (with-monad %store-monad
374 ;; PROC is expected to return a lowerable object.
375 ;; 'lower-object' takes care of residualizing it to a
376 ;; derivation or similar.
377 (return (proc system target))))))
378
379 ;; Delegate to the expander of the object returned by PROC.
380 expander => #f)
381
382\f
d9ae938f 383;;;
558e8b11 384;;; File declarations.
d9ae938f
LC
385;;;
386
9d3994f7
LC
387;; A local file name. FILE is the file name the user entered, which can be a
388;; relative file name, and ABSOLUTE is a promise that computes its canonical
389;; absolute file name. We keep it in a promise to compute it lazily and avoid
390;; repeated 'stat' calls.
d9ae938f 391(define-record-type <local-file>
0687fc9c 392 (%%local-file file absolute name recursive? select?)
d9ae938f
LC
393 local-file?
394 (file local-file-file) ;string
9d3994f7 395 (absolute %local-file-absolute-file-name) ;promise string
d9ae938f 396 (name local-file-name) ;string
0687fc9c
LC
397 (recursive? local-file-recursive?) ;Boolean
398 (select? local-file-select?)) ;string stat -> Boolean
399
400(define (true file stat) #t)
d9ae938f 401
9d3994f7 402(define* (%local-file file promise #:optional (name (basename file))
0687fc9c 403 #:key recursive? (select? true))
9d3994f7
LC
404 ;; This intermediate procedure is part of our ABI, but the underlying
405 ;; %%LOCAL-FILE is not.
0687fc9c 406 (%%local-file file promise name recursive? select?))
9d3994f7 407
9d3994f7
LC
408(define (absolute-file-name file directory)
409 "Return the canonical absolute file name for FILE, which lives in the
410vicinity of DIRECTORY."
411 (canonicalize-path
412 (cond ((string-prefix? "/" file) file)
413 ((not directory) file)
414 ((string-prefix? "/" directory)
415 (string-append directory "/" file))
416 (else file))))
417
302d46e6
LC
418(define-syntax local-file
419 (lambda (s)
420 "Return an object representing local file FILE to add to the store; this
9d3994f7
LC
421object can be used in a gexp. If FILE is a relative file name, it is looked
422up relative to the source file where this form appears. FILE will be added to
423the store under NAME--by default the base name of FILE.
d9ae938f
LC
424
425When RECURSIVE? is true, the contents of FILE are added recursively; if FILE
426designates a flat file and RECURSIVE? is true, its contents are added, and its
427permission bits are kept.
428
0687fc9c
LC
429When RECURSIVE? is true, call (SELECT? FILE STAT) for each directory entry,
430where FILE is the entry's absolute file name and STAT is the result of
431'lstat'; exclude entries for which SELECT? does not return true.
432
302d46e6
LC
433This is the declarative counterpart of the 'interned-file' monadic procedure.
434It is implemented as a macro to capture the current source directory where it
435appears."
436 (syntax-case s ()
437 ((_ file rest ...)
99c45877
LC
438 (string? (syntax->datum #'file))
439 ;; FILE is a literal, so resolve it relative to the source directory.
302d46e6
LC
440 #'(%local-file file
441 (delay (absolute-file-name file (current-source-directory)))
442 rest ...))
99c45877
LC
443 ((_ file rest ...)
444 ;; Resolve FILE relative to the current directory.
445 #'(%local-file file
446 (delay (absolute-file-name file (getcwd)))
447 rest ...))
302d46e6
LC
448 ((_)
449 #'(syntax-error "missing file name"))
450 (id
451 (identifier? #'id)
452 ;; XXX: We could return #'(lambda (file . rest) ...). However,
453 ;; (syntax-source #'id) is #f so (current-source-directory) would not
454 ;; work. Thus, simply forbid this form.
455 #'(syntax-error
456 "'local-file' is a macro and cannot be used like this")))))
9d3994f7
LC
457
458(define (local-file-absolute-file-name file)
459 "Return the absolute file name for FILE, a <local-file> instance. A
460'system-error' exception is raised if FILE could not be found."
461 (force (%local-file-absolute-file-name file)))
d9ae938f 462
1cdecf24 463(define-gexp-compiler (local-file-compiler (file <local-file>) system target)
d9ae938f
LC
464 ;; "Compile" FILE by adding it to the store.
465 (match file
0687fc9c 466 (($ <local-file> file (= force absolute) name recursive? select?)
9d3994f7
LC
467 ;; Canonicalize FILE so that if it's a symlink, it is resolved. Failing
468 ;; to do that, when RECURSIVE? is #t, we could end up creating a dangling
469 ;; symlink in the store, and when RECURSIVE? is #f 'add-to-store' would
470 ;; just throw an error, both of which are inconvenient.
0687fc9c
LC
471 (interned-file absolute name
472 #:recursive? recursive? #:select? select?))))
d9ae938f 473
558e8b11
LC
474(define-record-type <plain-file>
475 (%plain-file name content references)
476 plain-file?
477 (name plain-file-name) ;string
e8e1f295 478 (content plain-file-content) ;string or bytevector
558e8b11
LC
479 (references plain-file-references)) ;list (currently unused)
480
481(define (plain-file name content)
482 "Return an object representing a text file called NAME with the given
483CONTENT (a string) to be added to the store.
484
485This is the declarative counterpart of 'text-file'."
486 ;; XXX: For now just ignore 'references' because it's not clear how to use
487 ;; them in a declarative context.
488 (%plain-file name content '()))
489
1cdecf24 490(define-gexp-compiler (plain-file-compiler (file <plain-file>) system target)
558e8b11
LC
491 ;; "Compile" FILE by adding it to the store.
492 (match file
e8e1f295
JN
493 (($ <plain-file> name (and (? string?) content) references)
494 (text-file name content references))
495 (($ <plain-file> name (and (? bytevector?) content) references)
496 (binary-file name content references))))
558e8b11 497
91937029 498(define-record-type <computed-file>
ab25eb7c 499 (%computed-file name gexp guile options)
91937029
LC
500 computed-file?
501 (name computed-file-name) ;string
502 (gexp computed-file-gexp) ;gexp
ab25eb7c 503 (guile computed-file-guile) ;<package>
91937029
LC
504 (options computed-file-options)) ;list of arguments
505
506(define* (computed-file name gexp
ab25eb7c 507 #:key guile (options '(#:local-build? #t)))
91937029 508 "Return an object representing the store item NAME, a file or directory
a769bffb 509computed by GEXP. OPTIONS is a list of additional arguments to pass
91937029
LC
510to 'gexp->derivation'.
511
512This is the declarative counterpart of 'gexp->derivation'."
ab25eb7c 513 (%computed-file name gexp guile options))
91937029 514
1cdecf24 515(define-gexp-compiler (computed-file-compiler (file <computed-file>)
91937029
LC
516 system target)
517 ;; Compile FILE by returning a derivation whose build expression is its
518 ;; gexp.
519 (match file
ab25eb7c
LC
520 (($ <computed-file> name gexp guile options)
521 (if guile
522 (mlet %store-monad ((guile (lower-object guile system
523 #:target target)))
524 (apply gexp->derivation name gexp #:guile-for-build guile
9ec154f5
LC
525 #:system system #:target target options))
526 (apply gexp->derivation name gexp
527 #:system system #:target target options)))))
91937029 528
15a01c72 529(define-record-type <program-file>
427ec19e 530 (%program-file name gexp guile path)
15a01c72
LC
531 program-file?
532 (name program-file-name) ;string
533 (gexp program-file-gexp) ;gexp
427ec19e
LC
534 (guile program-file-guile) ;package
535 (path program-file-module-path)) ;list of strings
15a01c72 536
427ec19e 537(define* (program-file name gexp #:key (guile #f) (module-path %load-path))
15a01c72 538 "Return an object representing the executable store item NAME that runs
427ec19e
LC
539GEXP. GUILE is the Guile package used to execute that script. Imported
540modules of GEXP are looked up in MODULE-PATH.
15a01c72
LC
541
542This is the declarative counterpart of 'gexp->script'."
427ec19e 543 (%program-file name gexp guile module-path))
15a01c72 544
1cdecf24 545(define-gexp-compiler (program-file-compiler (file <program-file>)
15a01c72
LC
546 system target)
547 ;; Compile FILE by returning a derivation that builds the script.
548 (match file
427ec19e 549 (($ <program-file> name gexp guile module-path)
15a01c72 550 (gexp->script name gexp
427ec19e 551 #:module-path module-path
2e8cabb8
LC
552 #:guile (or guile (default-guile))
553 #:system system
554 #:target target))))
15a01c72 555
e1c153e0 556(define-record-type <scheme-file>
34faf63e 557 (%scheme-file name gexp splice? load-path?)
e1c153e0
LC
558 scheme-file?
559 (name scheme-file-name) ;string
4fbd1a2b 560 (gexp scheme-file-gexp) ;gexp
34faf63e
LC
561 (splice? scheme-file-splice?) ;Boolean
562 (load-path? scheme-file-set-load-path?)) ;Boolean
e1c153e0 563
34faf63e 564(define* (scheme-file name gexp #:key splice? (set-load-path? #t))
e1c153e0
LC
565 "Return an object representing the Scheme file NAME that contains GEXP.
566
567This is the declarative counterpart of 'gexp->file'."
34faf63e 568 (%scheme-file name gexp splice? set-load-path?))
e1c153e0 569
1cdecf24 570(define-gexp-compiler (scheme-file-compiler (file <scheme-file>)
e1c153e0
LC
571 system target)
572 ;; Compile FILE by returning a derivation that builds the file.
573 (match file
34faf63e 574 (($ <scheme-file> name gexp splice? set-load-path?)
3cd1444d 575 (gexp->file name gexp
34faf63e 576 #:set-load-path? set-load-path?
3cd1444d
MO
577 #:splice? splice?
578 #:system system
579 #:target target))))
e1c153e0 580
a9e5e92f
LC
581;; Appending SUFFIX to BASE's output file name.
582(define-record-type <file-append>
583 (%file-append base suffix)
584 file-append?
585 (base file-append-base) ;<package> | <derivation> | ...
586 (suffix file-append-suffix)) ;list of strings
587
39d7fdce
LC
588(define (write-file-append file port)
589 (match file
590 (($ <file-append> base suffix)
591 (format port "#<file-append ~s ~s>" base
592 (string-join suffix)))))
593
594(set-record-type-printer! <file-append> write-file-append)
595
a9e5e92f
LC
596(define (file-append base . suffix)
597 "Return a <file-append> object that expands to the concatenation of BASE and
598SUFFIX."
599 (%file-append base suffix))
600
1cdecf24 601(define-gexp-compiler file-append-compiler <file-append>
a9e5e92f
LC
602 compiler => (lambda (obj system target)
603 (match obj
604 (($ <file-append> base _)
605 (lower-object base system #:target target))))
606 expander => (lambda (obj lowered output)
607 (match obj
608 (($ <file-append> base suffix)
609 (let* ((expand (lookup-expander base))
610 (base (expand base lowered output)))
611 (string-append base (string-concatenate suffix)))))))
612
cf2ac04f
LC
613;; Representation of SRFI-39 parameter settings in the dynamic scope of an
614;; object lowering.
615(define-record-type <parameterized>
616 (parameterized bindings thunk)
617 parameterized?
618 (bindings parameterized-bindings) ;list of parameter/value pairs
619 (thunk parameterized-thunk)) ;thunk
620
621(define-syntax-rule (with-parameters ((param value) ...) body ...)
622 "Bind each PARAM to the corresponding VALUE for the extent during which BODY
623is lowered. Consider this example:
624
625 (with-parameters ((%current-system \"x86_64-linux\"))
626 coreutils)
627
628It returns a <parameterized> object that ensures %CURRENT-SYSTEM is set to
629x86_64-linux when COREUTILS is lowered."
630 (parameterized (list (list param (lambda () value)) ...)
631 (lambda ()
632 body ...)))
633
634(define-gexp-compiler compile-parameterized <parameterized>
635 compiler =>
636 (lambda (parameterized system target)
637 (match (parameterized-bindings parameterized)
638 (((parameters values) ...)
639 (let ((fluids (map parameter-fluid parameters))
640 (thunk (parameterized-thunk parameterized)))
641 ;; Install the PARAMETERS for the dynamic extent of THUNK.
642 (with-fluids* fluids
643 (map (lambda (thunk) (thunk)) values)
644 (lambda ()
645 ;; Special-case '%current-system' and '%current-target-system' to
646 ;; make sure we get the desired effect.
647 (let ((system (if (memq %current-system parameters)
648 (%current-system)
649 system))
650 (target (if (memq %current-target-system parameters)
651 (%current-target-system)
652 target)))
653 (lower-object (thunk) system #:target target))))))))
654
655 expander => (lambda (parameterized lowered output)
656 (match (parameterized-bindings parameterized)
657 (((parameters values) ...)
658 (let ((fluids (map parameter-fluid parameters))
659 (thunk (parameterized-thunk parameterized)))
660 ;; Install the PARAMETERS for the dynamic extent of THUNK.
661 (with-fluids* fluids
662 (map (lambda (thunk) (thunk)) values)
663 (lambda ()
664 ;; Delegate to the expander of the wrapped object.
665 (let* ((base (thunk))
666 (expand (lookup-expander base)))
667 (expand base lowered output)))))))))
a9e5e92f 668
d9ae938f 669\f
bcb13287
LC
670;;;
671;;; Inputs & outputs.
672;;;
673
e39d1461
LC
674;; The input of a gexp.
675(define-record-type <gexp-input>
0dbea56b 676 (%gexp-input thing output native?)
e39d1461
LC
677 gexp-input?
678 (thing gexp-input-thing) ;<package> | <origin> | <derivation> | ...
679 (output gexp-input-output) ;string
680 (native? gexp-input-native?)) ;Boolean
681
f7328634
LC
682(define (write-gexp-input input port)
683 (match input
684 (($ <gexp-input> thing output #f)
685 (format port "#<gexp-input ~s:~a>" thing output))
686 (($ <gexp-input> thing output #t)
687 (format port "#<gexp-input native ~s:~a>" thing output))))
688
689(set-record-type-printer! <gexp-input> write-gexp-input)
690
0dbea56b
LC
691(define* (gexp-input thing ;convenience procedure
692 #:optional (output "out")
693 #:key native?)
694 "Return a new <gexp-input> for the OUTPUT of THING; NATIVE? determines
695whether this should be considered a \"native\" input or not."
696 (%gexp-input thing output native?))
697
21b679f6
LC
698;; Reference to one of the derivation's outputs, for gexps used in
699;; derivations.
1e87da58
LC
700(define-record-type <gexp-output>
701 (gexp-output name)
702 gexp-output?
703 (name gexp-output-name))
21b679f6 704
f7328634
LC
705(define (write-gexp-output output port)
706 (match output
707 (($ <gexp-output> name)
708 (format port "#<gexp-output ~a>" name))))
709
710(set-record-type-printer! <gexp-output> write-gexp-output)
711
932d1600 712(define* (gexp-attribute gexp self-attribute #:optional (equal? equal?))
838e17d8 713 "Recurse on GEXP and the expressions it refers to, summing the items
932d1600
LC
714returned by SELF-ATTRIBUTE, a procedure that takes a gexp. Use EQUAL? as the
715second argument to 'delete-duplicates'."
2363bdd7
LC
716 (if (gexp? gexp)
717 (delete-duplicates
838e17d8 718 (append (self-attribute gexp)
2363bdd7
LC
719 (append-map (match-lambda
720 (($ <gexp-input> (? gexp? exp))
838e17d8 721 (gexp-attribute exp self-attribute))
2363bdd7
LC
722 (($ <gexp-input> (lst ...))
723 (append-map (lambda (item)
724 (if (gexp? item)
838e17d8
LC
725 (gexp-attribute item
726 self-attribute)
2363bdd7
LC
727 '()))
728 lst))
729 (_
730 '()))
932d1600
LC
731 (gexp-references gexp)))
732 equal?)
2363bdd7 733 '())) ;plain Scheme data type
0bb9929e 734
838e17d8
LC
735(define (gexp-modules gexp)
736 "Return the list of Guile module names GEXP relies on. If (gexp? GEXP) is
737false, meaning that GEXP is a plain Scheme object, return the empty list."
932d1600
LC
738 (define (module=? m1 m2)
739 ;; Return #t when M1 equals M2. Special-case '=>' specs because their
740 ;; right-hand side may not be comparable with 'equal?': it's typically a
741 ;; file-like object that embeds a gexp, which in turn embeds closure;
742 ;; those closures may be 'eq?' when running compiled code but are unlikely
743 ;; to be 'eq?' when running on 'eval'. Ignore the right-hand side to
744 ;; avoid this discrepancy.
745 (match m1
746 (((name1 ...) '=> _)
747 (match m2
748 (((name2 ...) '=> _) (equal? name1 name2))
749 (_ #f)))
750 (_
751 (equal? m1 m2))))
752
753 (gexp-attribute gexp gexp-self-modules module=?))
838e17d8
LC
754
755(define (gexp-extensions gexp)
756 "Return the list of Guile extensions (packages) GEXP relies on. If (gexp?
757GEXP) is false, meaning that GEXP is a plain Scheme object, return the empty
758list."
759 (gexp-attribute gexp gexp-self-extensions))
760
644cb40c
LC
761(define (self-quoting? x)
762 (letrec-syntax ((one-of (syntax-rules ()
763 ((_) #f)
764 ((_ pred rest ...)
765 (or (pred x)
766 (one-of rest ...))))))
767 (one-of symbol? string? keyword? pair? null? array?
768 number? boolean? char?)))
769
68a61e9f
LC
770(define* (lower-inputs inputs
771 #:key system target)
38685774
LC
772 "Turn any object from INPUTS into a derivation input for SYSTEM or a store
773item (a \"source\"); return the corresponding input list as a monadic value.
774When TARGET is true, use it as the cross-compilation target triplet."
2ca41030
LC
775 (define (store-item? obj)
776 (and (string? obj) (store-path? obj)))
777
644cb40c
LC
778 (define filterm
779 (lift1 (cut filter ->bool <>) %store-monad))
780
21b679f6 781 (with-monad %store-monad
644cb40c
LC
782 (>>= (mapm/accumulate-builds
783 (match-lambda
784 (((? struct? thing) sub-drv ...)
785 (mlet %store-monad ((obj (lower-object
786 thing system #:target target)))
787 (return (match obj
788 ((? derivation? drv)
789 (let ((outputs (if (null? sub-drv)
790 '("out")
791 sub-drv)))
792 (derivation-input drv outputs)))
793 ((? store-item? item)
794 item)
795 ((? self-quoting?)
796 ;; Some inputs such as <system-binding> can lower to
797 ;; a self-quoting object that FILTERM will filter
798 ;; out.
799 #f)))))
800 (((? store-item? item))
801 (return item)))
802 inputs)
803 filterm)))
21b679f6 804
b53833b2
LC
805(define* (lower-reference-graphs graphs #:key system target)
806 "Given GRAPHS, a list of (FILE-NAME INPUT ...) lists for use as a
807#:reference-graphs argument, lower it such that each INPUT is replaced by the
38685774 808corresponding <derivation-input> or store item."
b53833b2
LC
809 (match graphs
810 (((file-names . inputs) ...)
811 (mlet %store-monad ((inputs (lower-inputs inputs
812 #:system system
813 #:target target)))
38685774 814 (return (map cons file-names inputs))))))
b53833b2 815
c8351d9a
LC
816(define* (lower-references lst #:key system target)
817 "Based on LST, a list of output names and packages, return a list of output
818names and file names suitable for the #:allowed-references argument to
819'derivation'."
c8351d9a
LC
820 (with-monad %store-monad
821 (define lower
822 (match-lambda
823 ((? string? output)
824 (return output))
accb682c 825 (($ <gexp-input> thing output native?)
c2b84676
LC
826 (mlet %store-monad ((drv (lower-object thing system
827 #:target (if native?
828 #f target))))
accb682c 829 (return (derivation->output-path drv output))))
bcb13287 830 (thing
c2b84676
LC
831 (mlet %store-monad ((drv (lower-object thing system
832 #:target target)))
c8351d9a
LC
833 (return (derivation->output-path drv))))))
834
b34ead48 835 (mapm/accumulate-builds lower lst)))
c8351d9a 836
ff40e9b7
LC
837(define default-guile-derivation
838 ;; Here we break the abstraction by talking to the higher-level layer.
839 ;; Thus, do the resolution lazily to hide the circular dependency.
840 (let ((proc (delay
841 (let ((iface (resolve-interface '(guix packages))))
842 (module-ref iface 'default-guile-derivation)))))
843 (lambda (system)
844 ((force proc) system))))
845
2ca41030 846;; Representation of a gexp instantiated for a given target and system.
38685774 847;; It's an intermediate representation between <gexp> and <derivation>.
2ca41030 848(define-record-type <lowered-gexp>
38685774 849 (lowered-gexp sexp inputs sources guile load-path load-compiled-path)
2ca41030
LC
850 lowered-gexp?
851 (sexp lowered-gexp-sexp) ;sexp
38685774
LC
852 (inputs lowered-gexp-inputs) ;list of <derivation-input>
853 (sources lowered-gexp-sources) ;list of store items
b9373e26 854 (guile lowered-gexp-guile) ;<derivation-input> | #f
2ca41030
LC
855 (load-path lowered-gexp-load-path) ;list of store items
856 (load-compiled-path lowered-gexp-load-compiled-path)) ;list of store items
857
f58b4535
LC
858(define* (imported+compiled-modules modules system
859 #:key (extensions '())
860 deprecation-warnings guile
861 (module-path %load-path))
862 "Return a pair where the first element is the imported MODULES and the
863second element is the derivation to compile them."
f5fca9a8
LC
864 (mcached equal?
865 (mlet %store-monad ((modules (if (pair? modules)
866 (imported-modules modules
867 #:system system
868 #:module-path module-path)
869 (return #f)))
870 (compiled (if (pair? modules)
871 (compiled-modules modules
872 #:system system
873 #:module-path module-path
874 #:extensions extensions
875 #:guile guile
876 #:deprecation-warnings
877 deprecation-warnings)
878 (return #f))))
879 (return (cons modules compiled)))
880 modules
881 system extensions guile deprecation-warnings module-path))
f58b4535 882
2ca41030
LC
883(define* (lower-gexp exp
884 #:key
885 (module-path %load-path)
886 (system (%current-system))
887 (target 'current)
888 (graft? (%graft?))
889 (guile-for-build (%guile-for-build))
2bc1a400 890 (effective-version "3.0")
2ca41030 891
fb9a23a3 892 deprecation-warnings)
2ca41030
LC
893 "*Note: This API is subject to change; use at your own risk!*
894
895Lower EXP, a gexp, instantiating it for SYSTEM and TARGET. Return a
896<lowered-gexp> ready to be used.
897
898Lowered gexps are an intermediate representation that's useful for
899applications that deal with gexps outside in a way that is disconnected from
900derivations--e.g., code evaluated for its side effects."
901 (define %modules
902 (delete-duplicates (gexp-modules exp)))
903
904 (define (search-path modules extensions suffix)
905 (append (match modules
906 ((? derivation? drv)
907 (list (derivation->output-path drv)))
908 (#f
909 '())
910 ((? store-path? item)
911 (list item)))
912 (map (lambda (extension)
913 (string-append (match extension
914 ((? derivation? drv)
915 (derivation->output-path drv))
916 ((? store-path? item)
917 item))
918 suffix))
919 extensions)))
920
921 (mlet* %store-monad ( ;; The following binding forces '%current-system' and
922 ;; '%current-target-system' to be looked up at >>=
923 ;; time.
924 (graft? (set-grafting graft?))
925
926 (system -> (or system (%current-system)))
927 (target -> (if (eq? target 'current)
928 (%current-target-system)
929 target))
930 (guile (if guile-for-build
931 (return guile-for-build)
932 (default-guile-derivation system)))
933 (normals (lower-inputs (gexp-inputs exp)
934 #:system system
935 #:target target))
936 (natives (lower-inputs (gexp-native-inputs exp)
937 #:system system
938 #:target #f))
939 (inputs -> (append normals natives))
940 (sexp (gexp->sexp exp
941 #:system system
942 #:target target))
943 (extensions -> (gexp-extensions exp))
944 (exts (mapm %store-monad
945 (lambda (obj)
a6bf7a97
MO
946 (lower-object obj system
947 #:target #f))
2ca41030 948 extensions))
f58b4535
LC
949 (modules+compiled (imported+compiled-modules
950 %modules system
951 #:extensions extensions
952 #:deprecation-warnings
953 deprecation-warnings
954 #:guile guile
955 #:module-path module-path))
956 (modules -> (car modules+compiled))
957 (compiled -> (cdr modules+compiled)))
2ca41030
LC
958 (define load-path
959 (search-path modules exts
960 (string-append "/share/guile/site/" effective-version)))
961
962 (define load-compiled-path
963 (search-path compiled exts
964 (string-append "/lib/guile/" effective-version
965 "/site-ccache")))
966
967 (mbegin %store-monad
968 (set-grafting graft?) ;restore the initial setting
969 (return (lowered-gexp sexp
38685774
LC
970 `(,@(if (derivation? modules)
971 (list (derivation-input modules))
2ca41030
LC
972 '())
973 ,@(if compiled
38685774 974 (list (derivation-input compiled))
2ca41030 975 '())
38685774
LC
976 ,@(map derivation-input exts)
977 ,@(filter derivation-input? inputs))
978 (filter string? (cons modules inputs))
b9373e26 979 (derivation-input guile '("out"))
2ca41030
LC
980 load-path
981 load-compiled-path)))))
982
21b679f6
LC
983(define* (gexp->derivation name exp
984 #:key
68a61e9f 985 system (target 'current)
21b679f6
LC
986 hash hash-algo recursive?
987 (env-vars '())
988 (modules '())
4684f301 989 (module-path %load-path)
21b679f6 990 (guile-for-build (%guile-for-build))
2bc1a400 991 (effective-version "3.0")
ce45eb4c 992 (graft? (%graft?))
21b679f6 993 references-graphs
3f4ecf32 994 allowed-references disallowed-references
c0468155 995 leaked-env-vars
0309e1b0 996 local-build? (substitutable? #t)
8856f409 997 (properties '())
a912c723 998 deprecation-warnings
0309e1b0 999 (script-name (string-append name "-builder")))
21b679f6 1000 "Return a derivation NAME that runs EXP (a gexp) with GUILE-FOR-BUILD (a
0309e1b0
LC
1001derivation) on SYSTEM; EXP is stored in a file called SCRIPT-NAME. When
1002TARGET is true, it is used as the cross-compilation target triplet for
1003packages referred to by EXP.
21b679f6 1004
0bb9929e
LC
1005MODULES is deprecated in favor of 'with-imported-modules'. Its meaning is to
1006make MODULES available in the evaluation context of EXP; MODULES is a list of
4684f301 1007names of Guile modules searched in MODULE-PATH to be copied in the store,
21b679f6
LC
1008compiled, and made available in the load path during the execution of
1009EXP---e.g., '((guix build utils) (guix build gnu-build-system)).
1010
838e17d8
LC
1011EFFECTIVE-VERSION determines the string to use when adding extensions of
1012EXP (see 'with-extensions') to the search path---e.g., \"2.2\".
1013
ce45eb4c
LC
1014GRAFT? determines whether packages referred to by EXP should be grafted when
1015applicable.
1016
b53833b2
LC
1017When REFERENCES-GRAPHS is true, it must be a list of tuples of one of the
1018following forms:
1019
1020 (FILE-NAME PACKAGE)
1021 (FILE-NAME PACKAGE OUTPUT)
1022 (FILE-NAME DERIVATION)
1023 (FILE-NAME DERIVATION OUTPUT)
1024 (FILE-NAME STORE-ITEM)
1025
1026The right-hand-side of each element of REFERENCES-GRAPHS is automatically made
1027an input of the build process of EXP. In the build environment, each
1028FILE-NAME contains the reference graph of the corresponding item, in a simple
1029text format.
1030
c8351d9a
LC
1031ALLOWED-REFERENCES must be either #f or a list of output names and packages.
1032In the latter case, the list denotes store items that the result is allowed to
1033refer to. Any reference to another store item will lead to a build error.
3f4ecf32
LC
1034Similarly for DISALLOWED-REFERENCES, which can list items that must not be
1035referenced by the outputs.
b53833b2 1036
a912c723
LC
1037DEPRECATION-WARNINGS determines whether to show deprecation warnings while
1038compiling modules. It can be #f, #t, or 'detailed.
1039
21b679f6 1040The other arguments are as for 'derivation'."
21b679f6 1041 (define outputs (gexp-outputs exp))
2ca41030 1042 (define requested-graft? graft?)
21b679f6 1043
b53833b2
LC
1044 (define (graphs-file-names graphs)
1045 ;; Return a list of (FILE-NAME . STORE-PATH) pairs made from GRAPHS.
1046 (map (match-lambda
38685774
LC
1047 ((file-name . (? derivation-input? input))
1048 (cons file-name (first (derivation-input-output-paths input))))
1049 ((file-name . (? string? item))
1050 (cons file-name item)))
b53833b2
LC
1051 graphs))
1052
2ca41030
LC
1053 (define (add-modules exp modules)
1054 (if (null? modules)
1055 exp
1056 (make-gexp (gexp-references exp)
1057 (append modules (gexp-self-modules exp))
1058 (gexp-self-extensions exp)
1059 (gexp-proc exp))))
838e17d8
LC
1060
1061 (mlet* %store-monad ( ;; The following binding forces '%current-system' and
ce45eb4c
LC
1062 ;; '%current-target-system' to be looked up at >>=
1063 ;; time.
1064 (graft? (set-grafting graft?))
68a61e9f 1065
5d098459 1066 (system -> (or system (%current-system)))
68a61e9f
LC
1067 (target -> (if (eq? target 'current)
1068 (%current-target-system)
1069 target))
2ca41030
LC
1070 (exp -> (add-modules exp modules))
1071 (lowered (lower-gexp exp
1072 #:module-path module-path
1073 #:system system
1074 #:target target
1075 #:graft? requested-graft?
1076 #:guile-for-build
1077 guile-for-build
1078 #:effective-version
1079 effective-version
1080 #:deprecation-warnings
fb9a23a3 1081 deprecation-warnings))
2ca41030 1082
b53833b2
LC
1083 (graphs (if references-graphs
1084 (lower-reference-graphs references-graphs
1085 #:system system
1086 #:target target)
1087 (return #f)))
c8351d9a
LC
1088 (allowed (if allowed-references
1089 (lower-references allowed-references
1090 #:system system
1091 #:target target)
1092 (return #f)))
3f4ecf32
LC
1093 (disallowed (if disallowed-references
1094 (lower-references disallowed-references
1095 #:system system
1096 #:target target)
1097 (return #f)))
2ca41030
LC
1098 (guile -> (lowered-gexp-guile lowered))
1099 (builder (text-file script-name
1100 (object->string
1101 (lowered-gexp-sexp lowered)))))
ce45eb4c
LC
1102 (mbegin %store-monad
1103 (set-grafting graft?) ;restore the initial setting
1104 (raw-derivation name
b9373e26 1105 (string-append (derivation-input-output-path guile)
ce45eb4c
LC
1106 "/bin/guile")
1107 `("--no-auto-compile"
2ca41030
LC
1108 ,@(append-map (lambda (directory)
1109 `("-L" ,directory))
1110 (lowered-gexp-load-path lowered))
1111 ,@(append-map (lambda (directory)
1112 `("-C" ,directory))
1113 (lowered-gexp-load-compiled-path lowered))
ce45eb4c
LC
1114 ,builder)
1115 #:outputs outputs
1116 #:env-vars env-vars
1117 #:system system
b9373e26 1118 #:inputs `(,guile
38685774 1119 ,@(lowered-gexp-inputs lowered)
ce45eb4c 1120 ,@(match graphs
38685774
LC
1121 (((_ . inputs) ...)
1122 (filter derivation-input? inputs))
1123 (#f '())))
1124 #:sources `(,builder
1125 ,@(if (and (string? modules)
1126 (store-path? modules))
1127 (list modules)
1128 '())
1129 ,@(lowered-gexp-sources lowered)
1130 ,@(match graphs
1131 (((_ . inputs) ...)
1132 (filter string? inputs))
1133 (#f '())))
1134
ce45eb4c
LC
1135 #:hash hash #:hash-algo hash-algo #:recursive? recursive?
1136 #:references-graphs (and=> graphs graphs-file-names)
1137 #:allowed-references allowed
3f4ecf32 1138 #:disallowed-references disallowed
c0468155 1139 #:leaked-env-vars leaked-env-vars
4a6aeb67 1140 #:local-build? local-build?
8856f409
LC
1141 #:substitutable? substitutable?
1142 #:properties properties))))
21b679f6 1143
1123759b
LC
1144(define* (gexp-inputs exp #:key native?)
1145 "Return the input list for EXP. When NATIVE? is true, return only native
1146references; otherwise, return only non-native references."
2ca41030 1147 ;; TODO: Return <gexp-input> records instead of tuples.
21b679f6
LC
1148 (define (add-reference-inputs ref result)
1149 (match ref
1123759b
LC
1150 (($ <gexp-input> (? gexp? exp) _ #t)
1151 (if native?
1152 (append (gexp-inputs exp)
1153 (gexp-inputs exp #:native? #t)
1154 result)
1155 result))
1156 (($ <gexp-input> (? gexp? exp) _ #f)
d343a60f
LC
1157 (append (gexp-inputs exp #:native? native?)
1158 result))
e39d1461
LC
1159 (($ <gexp-input> (? string? str))
1160 (if (direct-store-path? str)
1161 (cons `(,str) result)
21b679f6 1162 result))
5b14a790
LC
1163 (($ <gexp-input> (? struct? thing) output n?)
1164 (if (and (eqv? n? native?) (lookup-compiler thing))
bcb13287
LC
1165 ;; THING is a derivation, or a package, or an origin, etc.
1166 (cons `(,thing ,output) result)
1167 result))
1123759b 1168 (($ <gexp-input> (lst ...) output n?)
578dfbe0
LC
1169 (fold-right add-reference-inputs result
1170 ;; XXX: For now, automatically convert LST to a list of
1171 ;; gexp-inputs. Inherit N?.
1172 (map (match-lambda
1173 ((? gexp-input? x)
1174 (%gexp-input (gexp-input-thing x)
1175 (gexp-input-output x)
1176 n?))
1177 (x
1178 (%gexp-input x "out" n?)))
1179 lst)))
21b679f6
LC
1180 (_
1181 ;; Ignore references to other kinds of objects.
1182 result)))
1183
1184 (fold-right add-reference-inputs
1185 '()
5b14a790 1186 (gexp-references exp)))
667b2508
LC
1187
1188(define gexp-native-inputs
1123759b 1189 (cut gexp-inputs <> #:native? #t))
21b679f6
LC
1190
1191(define (gexp-outputs exp)
1192 "Return the outputs referred to by EXP as a list of strings."
1193 (define (add-reference-output ref result)
1194 (match ref
1e87da58 1195 (($ <gexp-output> name)
21b679f6 1196 (cons name result))
e39d1461 1197 (($ <gexp-input> (? gexp? exp))
21b679f6 1198 (append (gexp-outputs exp) result))
e39d1461
LC
1199 (($ <gexp-input> (lst ...) output native?)
1200 ;; XXX: Automatically convert LST.
0dbea56b
LC
1201 (add-reference-output (map (match-lambda
1202 ((? gexp-input? x) x)
1203 (x (%gexp-input x "out" native?)))
1204 lst)
e39d1461 1205 result))
f9efe568
LC
1206 ((lst ...)
1207 (fold-right add-reference-output result lst))
21b679f6
LC
1208 (_
1209 result)))
1210
7e75a673
LC
1211 (delete-duplicates
1212 (add-reference-output (gexp-references exp) '())))
21b679f6 1213
68a61e9f
LC
1214(define* (gexp->sexp exp #:key
1215 (system (%current-system))
1216 (target (%current-target-system)))
21b679f6
LC
1217 "Return (monadically) the sexp corresponding to EXP for the given OUTPUT,
1218and in the current monad setting (system type, etc.)"
667b2508 1219 (define* (reference->sexp ref #:optional native?)
21b679f6
LC
1220 (with-monad %store-monad
1221 (match ref
1e87da58 1222 (($ <gexp-output> output)
bfd9eed9
LC
1223 ;; Output file names are not known in advance but the daemon defines
1224 ;; an environment variable for each of them at build time, so use
1225 ;; that trick.
1226 (return `((@ (guile) getenv) ,output)))
e39d1461 1227 (($ <gexp-input> (? gexp? exp) output n?)
667b2508
LC
1228 (gexp->sexp exp
1229 #:system system
e39d1461
LC
1230 #:target (if (or n? native?) #f target)))
1231 (($ <gexp-input> (refs ...) output n?)
b334674f
LC
1232 (mapm %store-monad
1233 (lambda (ref)
1234 ;; XXX: Automatically convert REF to an gexp-input.
1235 (reference->sexp
1236 (if (gexp-input? ref)
1237 ref
1238 (%gexp-input ref "out" n?))
1239 (or n? native?)))
1240 refs))
bcb13287 1241 (($ <gexp-input> (? struct? thing) output n?)
d03001a3
LC
1242 (let ((target (if (or n? native?) #f target)))
1243 (lower+expand-object thing system
1244 #:target target
1245 #:output output)))
24ab804c 1246 (($ <gexp-input> (? self-quoting? x))
e39d1461 1247 (return x))
24ab804c
LC
1248 (($ <gexp-input> x)
1249 (raise (condition (&gexp-input-error (input x)))))
21b679f6
LC
1250 (x
1251 (return x)))))
1252
1253 (mlet %store-monad
b334674f
LC
1254 ((args (mapm %store-monad
1255 reference->sexp (gexp-references exp))))
21b679f6
LC
1256 (return (apply (gexp-proc exp) args))))
1257
8245bb74
LC
1258(define-syntax-rule (define-syntax-parameter-once name proc)
1259 ;; Like 'define-syntax-parameter' but ensure the top-level binding for NAME
1260 ;; does not get redefined. This works around a race condition in a
1261 ;; multi-threaded context with Guile <= 2.2.4: <https://bugs.gnu.org/27476>.
1262 (eval-when (load eval expand compile)
1263 (define name
1264 (if (module-locally-bound? (current-module) 'name)
1265 (module-ref (current-module) 'name)
1266 (make-syntax-transformer 'name 'syntax-parameter
1267 (list proc))))))
1268
1269(define-syntax-parameter-once current-imported-modules
0bb9929e
LC
1270 ;; Current list of imported modules.
1271 (identifier-syntax '()))
1272
1273(define-syntax-rule (with-imported-modules modules body ...)
1274 "Mark the gexps defined in BODY... as requiring MODULES in their execution
1275environment."
1276 (syntax-parameterize ((current-imported-modules
1277 (identifier-syntax modules)))
1278 body ...))
1279
8245bb74 1280(define-syntax-parameter-once current-imported-extensions
838e17d8
LC
1281 ;; Current list of extensions.
1282 (identifier-syntax '()))
1283
1284(define-syntax-rule (with-extensions extensions body ...)
1285 "Mark the gexps defined in BODY... as requiring EXTENSIONS in their
1286execution environment."
1287 (syntax-parameterize ((current-imported-extensions
1288 (identifier-syntax extensions)))
1289 body ...))
1290
21b679f6
LC
1291(define-syntax gexp
1292 (lambda (s)
1293 (define (collect-escapes exp)
1294 ;; Return all the 'ungexp' present in EXP.
1295 (let loop ((exp exp)
1296 (result '()))
607e1b51
LC
1297 (syntax-case exp (ungexp
1298 ungexp-splicing
1299 ungexp-native
1300 ungexp-native-splicing)
21b679f6
LC
1301 ((ungexp _)
1302 (cons exp result))
1303 ((ungexp _ _)
1304 (cons exp result))
1305 ((ungexp-splicing _ ...)
1306 (cons exp result))
607e1b51 1307 ((ungexp-native _ ...)
667b2508
LC
1308 (cons exp result))
1309 ((ungexp-native-splicing _ ...)
1310 (cons exp result))
5e2e4a51 1311 ((exp0 . exp)
667b2508 1312 (let ((result (loop #'exp0 result)))
5e2e4a51 1313 (loop #'exp result)))
667b2508
LC
1314 (_
1315 result))))
1316
21b679f6
LC
1317 (define (escape->ref exp)
1318 ;; Turn 'ungexp' form EXP into a "reference".
667b2508
LC
1319 (syntax-case exp (ungexp ungexp-splicing
1320 ungexp-native ungexp-native-splicing
1321 output)
21b679f6 1322 ((ungexp output)
1e87da58 1323 #'(gexp-output "out"))
21b679f6 1324 ((ungexp output name)
1e87da58 1325 #'(gexp-output name))
21b679f6 1326 ((ungexp thing)
0dbea56b 1327 #'(%gexp-input thing "out" #f))
21b679f6 1328 ((ungexp drv-or-pkg out)
0dbea56b 1329 #'(%gexp-input drv-or-pkg out #f))
21b679f6 1330 ((ungexp-splicing lst)
0dbea56b 1331 #'(%gexp-input lst "out" #f))
667b2508 1332 ((ungexp-native thing)
0dbea56b 1333 #'(%gexp-input thing "out" #t))
667b2508 1334 ((ungexp-native drv-or-pkg out)
0dbea56b 1335 #'(%gexp-input drv-or-pkg out #t))
667b2508 1336 ((ungexp-native-splicing lst)
0dbea56b 1337 #'(%gexp-input lst "out" #t))))
21b679f6 1338
667b2508
LC
1339 (define (substitute-ungexp exp substs)
1340 ;; Given EXP, an 'ungexp' or 'ungexp-native' form, substitute it with
1341 ;; the corresponding form in SUBSTS.
1342 (match (assoc exp substs)
1343 ((_ id)
1344 id)
4a6e889f
LC
1345 (_ ;internal error
1346 (with-syntax ((exp exp))
1347 #'(syntax-error "error: no 'ungexp' substitution" exp)))))
667b2508
LC
1348
1349 (define (substitute-ungexp-splicing exp substs)
1350 (syntax-case exp ()
1351 ((exp rest ...)
1352 (match (assoc #'exp substs)
1353 ((_ id)
1354 (with-syntax ((id id))
1355 #`(append id
1356 #,(substitute-references #'(rest ...) substs))))
1357 (_
1358 #'(syntax-error "error: no 'ungexp-splicing' substitution"
4a6e889f 1359 exp))))))
667b2508 1360
21b679f6
LC
1361 (define (substitute-references exp substs)
1362 ;; Return a variant of EXP where all the cars of SUBSTS have been
1363 ;; replaced by the corresponding cdr.
667b2508
LC
1364 (syntax-case exp (ungexp ungexp-native
1365 ungexp-splicing ungexp-native-splicing)
21b679f6 1366 ((ungexp _ ...)
667b2508
LC
1367 (substitute-ungexp exp substs))
1368 ((ungexp-native _ ...)
1369 (substitute-ungexp exp substs))
21b679f6 1370 (((ungexp-splicing _ ...) rest ...)
667b2508
LC
1371 (substitute-ungexp-splicing exp substs))
1372 (((ungexp-native-splicing _ ...) rest ...)
1373 (substitute-ungexp-splicing exp substs))
5e2e4a51 1374 ((exp0 . exp)
21b679f6 1375 #`(cons #,(substitute-references #'exp0 substs)
5e2e4a51 1376 #,(substitute-references #'exp substs)))
21b679f6
LC
1377 (x #''x)))
1378
1379 (syntax-case s (ungexp output)
1380 ((_ exp)
affd7761 1381 (let* ((escapes (delete-duplicates (collect-escapes #'exp)))
21b679f6
LC
1382 (formals (generate-temporaries escapes))
1383 (sexp (substitute-references #'exp (zip escapes formals)))
affd7761
LC
1384 (refs (map escape->ref escapes)))
1385 #`(make-gexp (list #,@refs)
0bb9929e 1386 current-imported-modules
838e17d8 1387 current-imported-extensions
21b679f6
LC
1388 (lambda #,formals
1389 #,sexp)))))))
1390
1391\f
aa72d9af
LC
1392;;;
1393;;; Module handling.
1394;;;
1395
df2d51f0
LC
1396(define %utils-module
1397 ;; This file provides 'mkdir-p', needed to implement 'imported-files' and
a9601e23
LC
1398 ;; other primitives below. Note: We give the file name relative to this
1399 ;; file you are currently reading; 'search-path' could return a file name
1400 ;; relative to the current working directory.
1401 (local-file "build/utils.scm"
df2d51f0 1402 "build-utils.scm"))
aa72d9af 1403
8df2eca6
LC
1404(define* (imported-files/derivation files
1405 #:key (name "file-import")
e529d468 1406 (symlink? #f)
8df2eca6 1407 (system (%current-system))
8afa18d6 1408 (guile (%guile-for-build)))
aa72d9af 1409 "Return a derivation that imports FILES into STORE. FILES must be a list
d938a58b
LC
1410of (FINAL-PATH . FILE) pairs. Each FILE is mapped to FINAL-PATH in the
1411resulting store path. FILE can be either a file name, or a file-like object,
e529d468
LC
1412as returned by 'local-file' for example. If SYMLINK? is true, create symlinks
1413to the source files instead of copying them."
aa72d9af
LC
1414 (define file-pair
1415 (match-lambda
d938a58b 1416 ((final-path . (? string? file-name))
aa72d9af
LC
1417 (mlet %store-monad ((file (interned-file file-name
1418 (basename final-path))))
d938a58b
LC
1419 (return (list final-path file))))
1420 ((final-path . file-like)
1421 (mlet %store-monad ((file (lower-object file-like system)))
aa72d9af
LC
1422 (return (list final-path file))))))
1423
b334674f 1424 (mlet %store-monad ((files (mapm %store-monad file-pair files)))
aa72d9af
LC
1425 (define build
1426 (gexp
1427 (begin
df2d51f0 1428 (primitive-load (ungexp %utils-module)) ;for 'mkdir-p'
aa72d9af
LC
1429 (use-modules (ice-9 match))
1430
aa72d9af
LC
1431 (mkdir (ungexp output)) (chdir (ungexp output))
1432 (for-each (match-lambda
1433 ((final-path store-path)
1434 (mkdir-p (dirname final-path))
e529d468
LC
1435 ((ungexp (if symlink? 'symlink 'copy-file))
1436 store-path final-path)))
aa72d9af
LC
1437 '(ungexp files)))))
1438
1439 ;; TODO: Pass FILES as an environment variable so that BUILD remains
1440 ;; exactly the same regardless of FILES: less disk space, and fewer
1441 ;; 'add-to-store' RPCs.
1442 (gexp->derivation name build
1443 #:system system
1444 #:guile-for-build guile
30d722c3 1445 #:local-build? #t
2c402b1a 1446 #:substitutable? #f
30d722c3 1447
8afa18d6
LC
1448 ;; Avoid deprecation warnings about the use of the _IO*
1449 ;; constants in (guix build utils).
30d722c3 1450 #:env-vars
8afa18d6 1451 '(("GUILE_WARN_DEPRECATED" . "no")))))
aa72d9af 1452
8df2eca6
LC
1453(define* (imported-files files
1454 #:key (name "file-import")
8df2eca6
LC
1455 ;; The following parameters make sense when creating
1456 ;; an actual derivation.
1457 (system (%current-system))
8afa18d6 1458 (guile (%guile-for-build)))
8df2eca6
LC
1459 "Import FILES into the store and return the resulting derivation or store
1460file name (a derivation is created if and only if some elements of FILES are
1461file-like objects and not local file names.) FILES must be a list
1462of (FINAL-PATH . FILE) pairs. Each FILE is mapped to FINAL-PATH in the
1463resulting store path. FILE can be either a file name, or a file-like object,
1464as returned by 'local-file' for example."
8c7bebd6
LC
1465 (if (any (match-lambda
1466 ((_ . (? struct? source)) #t)
1467 (_ #f))
1468 files)
8df2eca6 1469 (imported-files/derivation files #:name name
e529d468 1470 #:symlink? derivation?
8afa18d6 1471 #:system system #:guile guile)
8df2eca6
LC
1472 (interned-file-tree `(,name directory
1473 ,@(file-mapping->tree files)))))
1474
aa72d9af
LC
1475(define* (imported-modules modules
1476 #:key (name "module-import")
1477 (system (%current-system))
1478 (guile (%guile-for-build))
8afa18d6 1479 (module-path %load-path))
aa72d9af 1480 "Return a derivation that contains the source files of MODULES, a list of
d938a58b
LC
1481module names such as `(ice-9 q)'. All of MODULES must be either names of
1482modules to be found in the MODULE-PATH search path, or a module name followed
1483by an arrow followed by a file-like object. For example:
1484
1485 (imported-modules `((guix build utils)
1486 (guix gcrypt)
1487 ((guix config) => ,(scheme-file …))))
1488
1489In this example, the first two modules are taken from MODULE-PATH, and the
1490last one is created from the given <scheme-file> object."
4d20d87b
LC
1491 (let ((files (map (match-lambda
1492 (((module ...) '=> file)
1493 (cons (module->source-file-name module)
1494 file))
1495 ((module ...)
1496 (let ((f (module->source-file-name module)))
1497 (cons f (search-path* module-path f)))))
1498 modules)))
8df2eca6 1499 (imported-files files #:name name
8df2eca6 1500 #:system system
8afa18d6 1501 #:guile guile)))
aa72d9af
LC
1502
1503(define* (compiled-modules modules
1504 #:key (name "module-import-compiled")
1505 (system (%current-system))
2cc5ec7f 1506 target
aa72d9af 1507 (guile (%guile-for-build))
a912c723 1508 (module-path %load-path)
838e17d8 1509 (extensions '())
3c6b9fb5 1510 (deprecation-warnings #f))
aa72d9af
LC
1511 "Return a derivation that builds a tree containing the `.go' files
1512corresponding to MODULES. All the MODULES are built in a context where
2cc5ec7f
LC
1513they can refer to each other. When TARGET is true, cross-compile MODULES for
1514TARGET, a GNU triplet."
d3292275
LC
1515 (define total (length modules))
1516
aa72d9af
LC
1517 (mlet %store-monad ((modules (imported-modules modules
1518 #:system system
1519 #:guile guile
1520 #:module-path
8afa18d6 1521 module-path)))
aa72d9af
LC
1522 (define build
1523 (gexp
1524 (begin
df2d51f0
LC
1525 (primitive-load (ungexp %utils-module)) ;for 'mkdir-p'
1526
aa72d9af 1527 (use-modules (ice-9 ftw)
d3292275
LC
1528 (ice-9 format)
1529 (srfi srfi-1)
aa72d9af 1530 (srfi srfi-26)
326dc630 1531 (system base target)
aa72d9af
LC
1532 (system base compile))
1533
aa72d9af
LC
1534 (define (regular? file)
1535 (not (member file '("." ".."))))
1536
d3292275 1537 (define (process-entry entry output processed)
e640c9e6
LC
1538 (if (file-is-directory? entry)
1539 (let ((output (string-append output "/" (basename entry))))
1540 (mkdir-p output)
d3292275 1541 (process-directory entry output processed))
e640c9e6
LC
1542 (let* ((base (basename entry ".scm"))
1543 (output (string-append output "/" base ".go")))
d3292275 1544 (format #t "[~2@a/~2@a] Compiling '~a'...~%"
3c6b9fb5
LC
1545 (+ 1 processed (ungexp total))
1546 (ungexp (* total 2))
a31174e8 1547 entry)
2cc5ec7f
LC
1548
1549 (ungexp-splicing
1550 (if target
1551 (gexp ((with-target (ungexp target)
1552 (lambda ()
1553 (compile-file entry
1554 #:output-file output
1555 #:opts
1556 %auto-compilation-options)))))
1557 (gexp ((compile-file entry
1558 #:output-file output
1559 #:opts %auto-compilation-options)))))
1560
d3292275 1561 (+ 1 processed))))
e640c9e6 1562
d3292275 1563 (define (process-directory directory output processed)
aa72d9af
LC
1564 (let ((entries (map (cut string-append directory "/" <>)
1565 (scandir directory regular?))))
d3292275
LC
1566 (fold (cut process-entry <> output <>)
1567 processed
1568 entries)))
1569
3c6b9fb5
LC
1570 (define* (load-from-directory directory
1571 #:optional (loaded 0))
1572 "Load all the source files found in DIRECTORY."
1573 ;; XXX: This works around <https://bugs.gnu.org/15602>.
1574 (let ((entries (map (cut string-append directory "/" <>)
1575 (scandir directory regular?))))
1576 (fold (lambda (file loaded)
1577 (if (file-is-directory? file)
1578 (load-from-directory file loaded)
1579 (begin
1580 (format #t "[~2@a/~2@a] Loading '~a'...~%"
1581 (+ 1 loaded) (ungexp (* 2 total))
1582 file)
1583 (save-module-excursion
1584 (lambda ()
1585 (primitive-load file)))
1586 (+ 1 loaded))))
1587 loaded
1588 entries)))
1589
d3292275
LC
1590 (setvbuf (current-output-port)
1591 (cond-expand (guile-2.2 'line) (else _IOLBF)))
aa72d9af 1592
4a42abc5
LC
1593 (define mkdir-p
1594 ;; Capture 'mkdir-p'.
1595 (@ (guix build utils) mkdir-p))
5d669883 1596
838e17d8 1597 ;; Add EXTENSIONS to the search path.
4a42abc5
LC
1598 (set! %load-path
1599 (append (map (lambda (extension)
1600 (string-append extension
1601 "/share/guile/site/"
1602 (effective-version)))
1603 '((ungexp-native-splicing extensions)))
1604 %load-path))
1605 (set! %load-compiled-path
1606 (append (map (lambda (extension)
1607 (string-append extension "/lib/guile/"
1608 (effective-version)
1609 "/site-ccache"))
1610 '((ungexp-native-splicing extensions)))
1611 %load-compiled-path))
838e17d8 1612
aa72d9af 1613 (set! %load-path (cons (ungexp modules) %load-path))
5d669883 1614
4a42abc5
LC
1615 ;; Above we loaded our own (guix build utils) but now we may need to
1616 ;; load a compile a different one. Thus, force a reload.
1617 (let ((utils (string-append (ungexp modules)
1618 "/guix/build/utils.scm")))
1619 (when (file-exists? utils)
1620 (load utils)))
5d669883 1621
aa72d9af
LC
1622 (mkdir (ungexp output))
1623 (chdir (ungexp modules))
a31174e8 1624
3c6b9fb5 1625 (load-from-directory ".")
d3292275 1626 (process-directory "." (ungexp output) 0))))
aa72d9af
LC
1627
1628 ;; TODO: Pass MODULES as an environment variable.
1629 (gexp->derivation name build
1630 #:system system
1631 #:guile-for-build guile
a912c723
LC
1632 #:local-build? #t
1633 #:env-vars
1634 (case deprecation-warnings
1635 ((#f)
1636 '(("GUILE_WARN_DEPRECATED" . "no")))
1637 ((detailed)
1638 '(("GUILE_WARN_DEPRECATED" . "detailed")))
1639 (else
1640 '())))))
aa72d9af
LC
1641
1642\f
21b679f6
LC
1643;;;
1644;;; Convenience procedures.
1645;;;
1646
53e89b17 1647(define (default-guile)
b6bee63b 1648 ;; Lazily resolve 'guile-3.0' (not 'guile-final' because this is for
6ee797f3
LC
1649 ;; programs returned by 'program-file' and we don't want to keep references
1650 ;; to several Guile packages). This module must not refer to (gnu …)
53e89b17 1651 ;; modules directly, to avoid circular dependencies, hence this hack.
6ee797f3 1652 (module-ref (resolve-interface '(gnu packages guile))
b6bee63b 1653 'guile-3.0))
53e89b17 1654
838e17d8 1655(define* (load-path-expression modules #:optional (path %load-path)
2e8cabb8 1656 #:key (extensions '()) system target)
dd8d1a30 1657 "Return as a monadic value a gexp that sets '%load-path' and
1ae16033 1658'%load-compiled-path' to point to MODULES, a list of module names. MODULES
efff3245
LC
1659are searched for in PATH. Return #f when MODULES and EXTENSIONS are empty."
1660 (if (and (null? modules) (null? extensions))
1661 (with-monad %store-monad
1662 (return #f))
1663 (mlet %store-monad ((modules (imported-modules modules
2e8cabb8
LC
1664 #:module-path path
1665 #:system system))
efff3245
LC
1666 (compiled (compiled-modules modules
1667 #:extensions extensions
2e8cabb8
LC
1668 #:module-path path
1669 #:system system
1670 #:target target)))
cdf9811d
LC
1671 (return
1672 (gexp (eval-when (expand load eval)
1673 ;; Augment the load paths and delete duplicates. Do that
1674 ;; without loading (srfi srfi-1) or anything.
396b05f0 1675 (let ((extensions '((ungexp-splicing extensions)))
cdf9811d
LC
1676 (prepend (lambda (items lst)
1677 ;; This is O(N²) but N is typically small.
1678 (let loop ((items items)
1679 (lst lst))
1680 (if (null? items)
1681 lst
1682 (loop (cdr items)
1683 (cons (car items)
1684 (delete (car items) lst))))))))
1685 (set! %load-path
1686 (prepend (cons (ungexp modules)
1687 (map (lambda (extension)
1688 (string-append extension
1689 "/share/guile/site/"
1690 (effective-version)))
1691 extensions))
1692 %load-path))
1693 (set! %load-compiled-path
1694 (prepend (cons (ungexp compiled)
1695 (map (lambda (extension)
1696 (string-append extension
1697 "/lib/guile/"
1698 (effective-version)
1699 "/site-ccache"))
1700 extensions))
1701 %load-compiled-path)))))))))
dd8d1a30 1702
21b679f6 1703(define* (gexp->script name exp
1ae16033 1704 #:key (guile (default-guile))
2e8cabb8
LC
1705 (module-path %load-path)
1706 (system (%current-system))
a6bf7a97 1707 (target 'current))
9c14a487 1708 "Return an executable script NAME that runs EXP using GUILE, with EXP's
1ae16033 1709imported modules in its search path. Look up EXP's modules in MODULE-PATH."
a6bf7a97
MO
1710 (mlet* %store-monad ((target (if (eq? target 'current)
1711 (current-target-system)
1712 (return target)))
1713 (set-load-path
1714 (load-path-expression (gexp-modules exp)
1715 module-path
1716 #:extensions
1717 (gexp-extensions exp)
1718 #:system system
1719 #:target target)))
21b679f6
LC
1720 (gexp->derivation name
1721 (gexp
1722 (call-with-output-file (ungexp output)
1723 (lambda (port)
c17b5ab4
LC
1724 ;; Note: that makes a long shebang. When the store
1725 ;; is /gnu/store, that fits within the 128-byte
1726 ;; limit imposed by Linux, but that may go beyond
1727 ;; when running tests.
21b679f6
LC
1728 (format port
1729 "#!~a/bin/guile --no-auto-compile~%!#~%"
1730 (ungexp guile))
4a4cbd0b 1731
efff3245
LC
1732 (ungexp-splicing
1733 (if set-load-path
1734 (gexp ((write '(ungexp set-load-path) port)))
1735 (gexp ())))
1736
21b679f6 1737 (write '(ungexp exp) port)
1ae16033 1738 (chmod port #o555))))
2e8cabb8
LC
1739 #:system system
1740 #:target target
52207b39
LC
1741 #:module-path module-path
1742
1743 ;; These derivations are not worth offloading or
1744 ;; substituting.
1745 #:local-build? #t
1746 #:substitutable? #f)))
21b679f6 1747
1ae16033
LC
1748(define* (gexp->file name exp #:key
1749 (set-load-path? #t)
4fbd1a2b 1750 (module-path %load-path)
3cd1444d
MO
1751 (splice? #f)
1752 (system (%current-system))
a6bf7a97 1753 (target 'current))
4fbd1a2b
LC
1754 "Return a derivation that builds a file NAME containing EXP. When SPLICE?
1755is true, EXP is considered to be a list of expressions that will be spliced in
1756the resulting file.
1757
1758When SET-LOAD-PATH? is true, emit code in the resulting file to set
1759'%load-path' and '%load-compiled-path' to honor EXP's imported modules.
1760Lookup EXP's modules in MODULE-PATH."
838e17d8
LC
1761 (define modules (gexp-modules exp))
1762 (define extensions (gexp-extensions exp))
1763
a6bf7a97
MO
1764 (mlet* %store-monad
1765 ((target (if (eq? target 'current)
1766 (current-target-system)
1767 (return target)))
1768 (no-load-path? -> (or (not set-load-path?)
1769 (and (null? modules)
1770 (null? extensions))))
1771 (set-load-path
1772 (load-path-expression modules module-path
1773 #:extensions extensions
1774 #:system system
1775 #:target target)))
1776 (if no-load-path?
1777 (gexp->derivation name
1778 (gexp
1779 (call-with-output-file (ungexp output)
1780 (lambda (port)
1781 (for-each
1782 (lambda (exp)
1783 (write exp port))
1784 '(ungexp (if splice?
1785 exp
1786 (gexp ((ungexp exp)))))))))
1787 #:local-build? #t
1788 #:substitutable? #f
1789 #:system system
1790 #:target target)
838e17d8
LC
1791 (gexp->derivation name
1792 (gexp
1793 (call-with-output-file (ungexp output)
1794 (lambda (port)
1795 (write '(ungexp set-load-path) port)
a6bf7a97
MO
1796 (for-each
1797 (lambda (exp)
1798 (write exp port))
1799 '(ungexp (if splice?
1800 exp
1801 (gexp ((ungexp exp)))))))))
838e17d8
LC
1802 #:module-path module-path
1803 #:local-build? #t
3cd1444d
MO
1804 #:substitutable? #f
1805 #:system system
1806 #:target target))))
21b679f6 1807
462a3fa3
LC
1808(define* (text-file* name #:rest text)
1809 "Return as a monadic value a derivation that builds a text file containing
d9ae938f
LC
1810all of TEXT. TEXT may list, in addition to strings, objects of any type that
1811can be used in a gexp: packages, derivations, local file objects, etc. The
1812resulting store file holds references to all these."
462a3fa3
LC
1813 (define builder
1814 (gexp (call-with-output-file (ungexp output "out")
1815 (lambda (port)
1816 (display (string-append (ungexp-splicing text)) port)))))
1817
851b6f62
LC
1818 (gexp->derivation name builder
1819 #:local-build? #t
1820 #:substitutable? #f))
462a3fa3 1821
b751cde3
LC
1822(define* (mixed-text-file name #:rest text)
1823 "Return an object representing store file NAME containing TEXT. TEXT is a
1824sequence of strings and file-like objects, as in:
1825
1826 (mixed-text-file \"profile\"
1827 \"export PATH=\" coreutils \"/bin:\" grep \"/bin\")
1828
1829This is the declarative counterpart of 'text-file*'."
1830 (define build
1831 (gexp (call-with-output-file (ungexp output "out")
1832 (lambda (port)
1833 (display (string-append (ungexp-splicing text)) port)))))
1834
1835 (computed-file name build))
1836
dedb512f
LC
1837(define (file-union name files)
1838 "Return a <computed-file> that builds a directory containing all of FILES.
1839Each item in FILES must be a two-element list where the first element is the
1840file name to use in the new directory, and the second element is a gexp
1841denoting the target file. Here's an example:
1842
1843 (file-union \"etc\"
1844 `((\"hosts\" ,(plain-file \"hosts\"
1845 \"127.0.0.1 localhost\"))
1846 (\"bashrc\" ,(plain-file \"bashrc\"
5dec93bb
LC
1847 \"alias ls='ls --color'\"))
1848 (\"libvirt/qemu.conf\" ,(plain-file \"qemu.conf\" \"\"))))
dedb512f
LC
1849
1850This yields an 'etc' directory containing these two files."
1851 (computed-file name
5dec93bb
LC
1852 (with-imported-modules '((guix build utils))
1853 (gexp
1854 (begin
1855 (use-modules (guix build utils))
1856
1857 (mkdir (ungexp output))
1858 (chdir (ungexp output))
1859 (ungexp-splicing
1860 (map (match-lambda
1861 ((target source)
1862 (gexp
1863 (begin
1864 ;; Stat the source to abort early if it does
1865 ;; not exist.
1866 (stat (ungexp source))
1867
1868 (mkdir-p (dirname (ungexp target)))
1869 (symlink (ungexp source)
1870 (ungexp target))))))
1871 files)))))))
dedb512f 1872
59523429 1873(define* (directory-union name things
b244ae25
LC
1874 #:key (copy? #f) (quiet? #f)
1875 (resolve-collision 'warn-about-collision))
d298c815
LC
1876 "Return a directory that is the union of THINGS, where THINGS is a list of
1877file-like objects denoting directories. For example:
1878
1879 (directory-union \"guile+emacs\" (list guile emacs))
1880
59523429
LC
1881yields a directory that is the union of the 'guile' and 'emacs' packages.
1882
b244ae25
LC
1883Call RESOLVE-COLLISION when several files collide, passing it the list of
1884colliding files. RESOLVE-COLLISION must return the chosen file or #f, in
1885which case the colliding entry is skipped altogether.
1886
de98b302
LC
1887When HARD-LINKS? is true, create hard links instead of symlinks. When QUIET?
1888is true, the derivation will not print anything."
59523429
LC
1889 (define symlink
1890 (if copy?
1891 (gexp (lambda (old new)
1892 (if (file-is-directory? old)
1893 (symlink old new)
1894 (copy-file old new))))
1895 (gexp symlink)))
1896
de98b302
LC
1897 (define log-port
1898 (if quiet?
1899 (gexp (%make-void-port "w"))
1900 (gexp (current-error-port))))
1901
d298c815
LC
1902 (match things
1903 ((one)
1904 ;; Only one thing; return it.
1905 one)
1906 (_
1907 (computed-file name
1908 (with-imported-modules '((guix build union))
1909 (gexp (begin
b244ae25
LC
1910 (use-modules (guix build union)
1911 (srfi srfi-1)) ;for 'first' and 'last'
1912
d298c815 1913 (union-build (ungexp output)
59523429
LC
1914 '(ungexp things)
1915
de98b302 1916 #:log-port (ungexp log-port)
b244ae25
LC
1917 #:symlink (ungexp symlink)
1918 #:resolve-collision
1919 (ungexp resolve-collision)))))))))
d298c815 1920
21b679f6
LC
1921\f
1922;;;
1923;;; Syntactic sugar.
1924;;;
1925
1926(eval-when (expand load eval)
667b2508
LC
1927 (define* (read-ungexp chr port #:optional native?)
1928 "Read an 'ungexp' or 'ungexp-splicing' form from PORT. When NATIVE? is
1929true, use 'ungexp-native' and 'ungexp-native-splicing' instead."
21b679f6
LC
1930 (define unquote-symbol
1931 (match (peek-char port)
1932 (#\@
1933 (read-char port)
667b2508
LC
1934 (if native?
1935 'ungexp-native-splicing
1936 'ungexp-splicing))
21b679f6 1937 (_
667b2508
LC
1938 (if native?
1939 'ungexp-native
1940 'ungexp))))
21b679f6
LC
1941
1942 (match (read port)
1943 ((? symbol? symbol)
1944 (let ((str (symbol->string symbol)))
1945 (match (string-index-right str #\:)
1946 (#f
1947 `(,unquote-symbol ,symbol))
1948 (colon
1949 (let ((name (string->symbol (substring str 0 colon)))
1950 (output (substring str (+ colon 1))))
1951 `(,unquote-symbol ,name ,output))))))
1952 (x
1953 `(,unquote-symbol ,x))))
1954
1955 (define (read-gexp chr port)
1956 "Read a 'gexp' form from PORT."
1957 `(gexp ,(read port)))
1958
1959 ;; Extend the reader
1960 (read-hash-extend #\~ read-gexp)
667b2508
LC
1961 (read-hash-extend #\$ read-ungexp)
1962 (read-hash-extend #\+ (cut read-ungexp <> <> #t)))
21b679f6
LC
1963
1964;;; gexp.scm ends here