grafts: Actually cache grafts during the derivation DAG traversal.
[jackhill/guix/guix.git] / guix / grafts.scm
CommitLineData
7adf9b84 1;;; GNU Guix --- Functional package management for GNU
d38bc9a9 2;;; Copyright © 2014, 2015, 2016, 2017 Ludovic Courtès <ludo@gnu.org>
7adf9b84
LC
3;;;
4;;; This file is part of GNU Guix.
5;;;
6;;; GNU Guix is free software; you can redistribute it and/or modify it
7;;; under the terms of the GNU General Public License as published by
8;;; the Free Software Foundation; either version 3 of the License, or (at
9;;; your option) any later version.
10;;;
11;;; GNU Guix is distributed in the hope that it will be useful, but
12;;; WITHOUT ANY WARRANTY; without even the implied warranty of
13;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14;;; GNU General Public License for more details.
15;;;
16;;; You should have received a copy of the GNU General Public License
17;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
18
19(define-module (guix grafts)
c22a1324
LC
20 #:use-module (guix store)
21 #:use-module (guix monads)
7adf9b84
LC
22 #:use-module (guix records)
23 #:use-module (guix derivations)
24 #:use-module ((guix utils) #:select (%current-system))
25 #:use-module (srfi srfi-1)
acb01e37 26 #:use-module (srfi srfi-9 gnu)
c22a1324 27 #:use-module (srfi srfi-11)
7adf9b84 28 #:use-module (srfi srfi-26)
c90cb5c9 29 #:use-module (srfi srfi-34)
7adf9b84 30 #:use-module (ice-9 match)
c90cb5c9 31 #:use-module (ice-9 vlist)
7adf9b84
LC
32 #:export (graft?
33 graft
34 graft-origin
35 graft-replacement
36 graft-origin-output
37 graft-replacement-output
38
39 graft-derivation
c22a1324 40 graft-derivation/shallow
7adf9b84
LC
41
42 %graft?
43 set-grafting))
44
45(define-record-type* <graft> graft make-graft
46 graft?
47 (origin graft-origin) ;derivation | store item
48 (origin-output graft-origin-output ;string | #f
49 (default "out"))
50 (replacement graft-replacement) ;derivation | store item
51 (replacement-output graft-replacement-output ;string | #f
52 (default "out")))
53
acb01e37
LC
54(define (write-graft graft port)
55 "Write a concise representation of GRAFT to PORT."
56 (define (->string thing output)
57 (if (derivation? thing)
58 (derivation->output-path thing output)
59 thing))
60
61 (match graft
62 (($ <graft> origin origin-output replacement replacement-output)
63 (format port "#<graft ~a ==> ~a ~a>"
64 (->string origin origin-output)
65 (->string replacement replacement-output)
66 (number->string (object-address graft) 16)))))
67
68(set-record-type-printer! <graft> write-graft)
69
c22a1324
LC
70(define (graft-origin-file-name graft)
71 "Return the output file name of the origin of GRAFT."
72 (match graft
73 (($ <graft> (? derivation? origin) output)
74 (derivation->output-path origin output))
75 (($ <graft> (? string? item))
76 item)))
77
78(define* (graft-derivation/shallow store drv grafts
79 #:key
80 (name (derivation-name drv))
81 (guile (%guile-for-build))
82 (system (%current-system)))
7adf9b84 83 "Return a derivation called NAME, based on DRV but with all the GRAFTS
c22a1324
LC
84applied. This procedure performs \"shallow\" grafting in that GRAFTS are not
85recursively applied to dependencies of DRV."
7adf9b84
LC
86 ;; XXX: Someday rewrite using gexps.
87 (define mapping
88 ;; List of store item pairs.
89 (map (match-lambda
90 (($ <graft> source source-output target target-output)
91 (cons (if (derivation? source)
92 (derivation->output-path source source-output)
93 source)
94 (if (derivation? target)
95 (derivation->output-path target target-output)
96 target))))
97 grafts))
98
99 (define outputs
f376dc3a
LC
100 (map (match-lambda
101 ((name . output)
102 (cons name (derivation-output-path output))))
103 (derivation-outputs drv)))
7adf9b84
LC
104
105 (define output-names
cd05d388 106 (derivation-output-names drv))
7adf9b84
LC
107
108 (define build
109 `(begin
110 (use-modules (guix build graft)
111 (guix build utils)
112 (ice-9 match))
113
f376dc3a
LC
114 (let* ((old-outputs ',outputs)
115 (mapping (append ',mapping
116 (map (match-lambda
117 ((name . file)
118 (cons (assoc-ref old-outputs name)
119 file)))
120 %outputs))))
7adf9b84
LC
121 (for-each (lambda (input output)
122 (format #t "grafting '~a' -> '~a'...~%" input output)
123 (force-output)
f376dc3a
LC
124 (rewrite-directory input output mapping))
125 (match old-outputs
126 (((names . files) ...)
127 files))
7adf9b84
LC
128 (match %outputs
129 (((names . files) ...)
130 files))))))
131
132 (define add-label
133 (cut cons "x" <>))
134
135 (match grafts
136 ((($ <graft> sources source-outputs targets target-outputs) ...)
137 (let ((sources (zip sources source-outputs))
138 (targets (zip targets target-outputs)))
139 (build-expression->derivation store name build
140 #:system system
141 #:guile-for-build guile
142 #:modules '((guix build graft)
143 (guix build utils))
144 #:inputs `(,@(map (lambda (out)
145 `("x" ,drv ,out))
146 output-names)
147 ,@(append (map add-label sources)
148 (map add-label targets)))
149 #:outputs output-names
150 #:local-build? #t)))))
c22a1324
LC
151(define (item->deriver store item)
152 "Return two values: the derivation that led to ITEM (a store item), and the
153name of the output of that derivation ITEM corresponds to (for example
154\"out\"). When ITEM has no deriver, for instance because it is a plain file,
155#f and #f are returned."
156 (match (valid-derivers store item)
157 (() ;ITEM is a plain file
158 (values #f #f))
159 ((drv-file _ ...)
160 (let ((drv (call-with-input-file drv-file read-derivation)))
161 (values drv
162 (any (match-lambda
163 ((name . path)
164 (and (string=? item path) name)))
165 (derivation->output-paths drv)))))))
166
c90cb5c9 167(define (non-self-references references drv outputs)
c22a1324 168 "Return the list of references of the OUTPUTS of DRV, excluding self
c90cb5c9
LC
169references. Call REFERENCES to get the list of references."
170 (let ((refs (append-map (compose references
171 (cut derivation->output-path drv <>))
c22a1324
LC
172 outputs))
173 (self (match (derivation->output-paths drv)
174 (((names . items) ...)
175 items))))
176 (remove (cut member <> self) refs)))
177
c90cb5c9
LC
178(define (references-oracle store drv)
179 "Return a one-argument procedure that, when passed the file name of DRV's
180outputs or their dependencies, returns the list of references of that item.
181Use either local info or substitute info; build DRV if no information is
182available."
183 (define (output-paths drv)
184 (match (derivation->output-paths drv)
185 (((names . items) ...)
186 items)))
187
188 (define (references* items)
189 (guard (c ((nix-protocol-error? c)
190 ;; As a last resort, build DRV and query the references of the
191 ;; build result.
264fdedb
LC
192
193 ;; Warm up the narinfo cache, otherwise each derivation build
194 ;; will result in one HTTP request to get one narinfo, which is
195 ;; much less efficient than fetching them all upfront.
196 (substitution-oracle store (list drv))
197
c90cb5c9
LC
198 (and (build-derivations store (list drv))
199 (map (cut references store <>) items))))
200 (references/substitutes store items)))
201
202 (let loop ((items (output-paths drv))
203 (result vlist-null))
204 (match items
205 (()
206 (lambda (item)
207 (match (vhash-assoc item result)
208 ((_ . refs) refs)
209 (#f #f))))
210 (_
211 (let* ((refs (references* items))
212 (result (fold vhash-cons result items refs)))
213 (loop (remove (cut vhash-assoc <> result)
214 (delete-duplicates (concatenate refs) string=?))
215 result))))))
216
d38bc9a9
LC
217(define-syntax-rule (with-cache key exp ...)
218 "Cache the value of monadic expression EXP under KEY."
219 (mlet %state-monad ((cache (current-state)))
220 (match (vhash-assq key cache)
221 ((_ . result) ;cache hit
222 (return result))
223 (#f ;cache miss
224 (mlet %state-monad ((result (begin exp ...)))
90ad5c88
LC
225 (mbegin %state-monad
226 (set-current-state (vhash-consq key result cache))
227 (return result)))))))
d38bc9a9 228
c22a1324 229(define* (cumulative-grafts store drv grafts
c90cb5c9 230 references
c22a1324
LC
231 #:key
232 (outputs (derivation-output-names drv))
233 (guile (%guile-for-build))
234 (system (%current-system)))
235 "Augment GRAFTS with additional grafts resulting from the application of
c90cb5c9
LC
236GRAFTS to the dependencies of DRV; REFERENCES must be a one-argument procedure
237that returns the list of references of the store item it is given. Return the
d4da602e
LC
238resulting list of grafts.
239
240This is a monadic procedure in %STATE-MONAD where the state is a vhash mapping
241derivations to the corresponding set of grafts."
b013c33f
LC
242 (define (graft-origin? drv graft)
243 ;; Return true if DRV corresponds to the origin of GRAFT.
244 (match graft
245 (($ <graft> (? derivation? origin) output)
246 (match (assoc-ref (derivation->output-paths drv) output)
247 ((? string? result)
248 (string=? result
249 (derivation->output-path origin output)))
250 (_
251 #f)))
252 (_
253 #f)))
254
c22a1324
LC
255 (define (dependency-grafts item)
256 (let-values (((drv output) (item->deriver store item)))
257 (if drv
b013c33f
LC
258 ;; If GRAFTS already contains a graft from DRV, do not override it.
259 (if (find (cut graft-origin? drv <>) grafts)
260 (state-return grafts)
261 (cumulative-grafts store drv grafts references
262 #:outputs (list output)
263 #:guile guile
264 #:system system))
d4da602e
LC
265 (state-return grafts))))
266
d38bc9a9
LC
267 (with-cache drv
268 (match (non-self-references references drv outputs)
269 (() ;no dependencies
d4da602e 270 (return grafts))
d38bc9a9
LC
271 (deps ;one or more dependencies
272 (mlet %state-monad ((grafts (mapm %state-monad dependency-grafts deps)))
273 (let ((grafts (delete-duplicates (concatenate grafts) equal?)))
274 (match (filter (lambda (graft)
275 (member (graft-origin-file-name graft) deps))
276 grafts)
277 (()
278 (return grafts))
279 ((applicable ..1)
280 ;; Use APPLICABLE, the subset of GRAFTS that is really
281 ;; applicable to DRV, to avoid creating several identical
282 ;; grafted variants of DRV.
283 (let* ((new (graft-derivation/shallow store drv applicable
284 #:guile guile
285 #:system system))
286
287 ;; Replace references to any of the outputs of DRV,
288 ;; even if that's more than needed. This is so that
289 ;; the result refers only to the outputs of NEW and
290 ;; not to those of DRV.
291 (grafts (append (map (lambda (output)
292 (graft
293 (origin drv)
294 (origin-output output)
295 (replacement new)
296 (replacement-output output)))
297 (derivation-output-names drv))
298 grafts)))
299 (return grafts))))))))))
c22a1324
LC
300
301(define* (graft-derivation store drv grafts
302 #:key (guile (%guile-for-build))
303 (system (%current-system)))
304 "Applied GRAFTS to DRV and all its dependencies, recursively. That is, if
305GRAFTS apply only indirectly to DRV, graft the dependencies of DRV, and graft
306DRV itself to refer to those grafted dependencies."
307
c90cb5c9
LC
308 ;; First, pre-compute the dependency tree of the outputs of DRV. Do this
309 ;; upfront to have as much parallelism as possible when querying substitute
310 ;; info or when building DRV.
311 (define references
312 (references-oracle store drv))
c22a1324 313
d4da602e
LC
314 (match (run-with-state
315 (cumulative-grafts store drv grafts references
316 #:guile guile #:system system)
317 vlist-null) ;the initial cache
c22a1324
LC
318 ((first . rest)
319 ;; If FIRST is not a graft for DRV, it means that GRAFTS are not
320 ;; applicable to DRV and nothing needs to be done.
321 (if (equal? drv (graft-origin first))
322 (graft-replacement first)
323 drv))))
7adf9b84
LC
324
325\f
326;; The following might feel more at home in (guix packages) but since (guix
327;; gexp), which is a lower level, needs them, we put them here.
328
329(define %graft?
330 ;; Whether to honor package grafts by default.
331 (make-parameter #t))
332
333(define (set-grafting enable?)
334 "This monadic procedure enables grafting when ENABLE? is true, and disables
335it otherwise. It returns the previous setting."
336 (lambda (store)
337 (values (%graft? enable?) store)))
338
d38bc9a9
LC
339;; Local Variables:
340;; eval: (put 'with-cache 'scheme-indent-function 1)
341;; End:
342
7adf9b84 343;;; grafts.scm ends here