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