grafts: Memoize intermediate results in 'cumulative-grafts'.
[jackhill/guix/guix.git] / guix / grafts.scm
1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2014, 2015, 2016 Ludovic Courtès <ludo@gnu.org>
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)
20 #:use-module (guix store)
21 #:use-module (guix monads)
22 #:use-module (guix records)
23 #:use-module (guix derivations)
24 #:use-module ((guix utils) #:select (%current-system))
25 #:use-module (srfi srfi-1)
26 #:use-module (srfi srfi-9 gnu)
27 #:use-module (srfi srfi-11)
28 #:use-module (srfi srfi-26)
29 #:use-module (srfi srfi-34)
30 #:use-module (ice-9 match)
31 #:use-module (ice-9 vlist)
32 #:export (graft?
33 graft
34 graft-origin
35 graft-replacement
36 graft-origin-output
37 graft-replacement-output
38
39 graft-derivation
40 graft-derivation/shallow
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
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
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)))
83 "Return a derivation called NAME, based on DRV but with all the GRAFTS
84 applied. This procedure performs \"shallow\" grafting in that GRAFTS are not
85 recursively applied to dependencies of DRV."
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
100 (map (match-lambda
101 ((name . output)
102 (cons name (derivation-output-path output))))
103 (derivation-outputs drv)))
104
105 (define output-names
106 (derivation-output-names drv))
107
108 (define build
109 `(begin
110 (use-modules (guix build graft)
111 (guix build utils)
112 (ice-9 match))
113
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))))
121 (for-each (lambda (input output)
122 (format #t "grafting '~a' -> '~a'...~%" input output)
123 (force-output)
124 (rewrite-directory input output mapping))
125 (match old-outputs
126 (((names . files) ...)
127 files))
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)))))
151 (define (item->deriver store item)
152 "Return two values: the derivation that led to ITEM (a store item), and the
153 name 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
167 (define (non-self-references references drv outputs)
168 "Return the list of references of the OUTPUTS of DRV, excluding self
169 references. Call REFERENCES to get the list of references."
170 (let ((refs (append-map (compose references
171 (cut derivation->output-path drv <>))
172 outputs))
173 (self (match (derivation->output-paths drv)
174 (((names . items) ...)
175 items))))
176 (remove (cut member <> self) refs)))
177
178 (define (references-oracle store drv)
179 "Return a one-argument procedure that, when passed the file name of DRV's
180 outputs or their dependencies, returns the list of references of that item.
181 Use either local info or substitute info; build DRV if no information is
182 available."
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.
192 (and (build-derivations store (list drv))
193 (map (cut references store <>) items))))
194 (references/substitutes store items)))
195
196 (let loop ((items (output-paths drv))
197 (result vlist-null))
198 (match items
199 (()
200 (lambda (item)
201 (match (vhash-assoc item result)
202 ((_ . refs) refs)
203 (#f #f))))
204 (_
205 (let* ((refs (references* items))
206 (result (fold vhash-cons result items refs)))
207 (loop (remove (cut vhash-assoc <> result)
208 (delete-duplicates (concatenate refs) string=?))
209 result))))))
210
211 (define* (cumulative-grafts store drv grafts
212 references
213 #:key
214 (outputs (derivation-output-names drv))
215 (guile (%guile-for-build))
216 (system (%current-system)))
217 "Augment GRAFTS with additional grafts resulting from the application of
218 GRAFTS to the dependencies of DRV; REFERENCES must be a one-argument procedure
219 that returns the list of references of the store item it is given. Return the
220 resulting list of grafts.
221
222 This is a monadic procedure in %STATE-MONAD where the state is a vhash mapping
223 derivations to the corresponding set of grafts."
224 (define (dependency-grafts item)
225 (let-values (((drv output) (item->deriver store item)))
226 (if drv
227 (cumulative-grafts store drv grafts references
228 #:outputs (list output)
229 #:guile guile
230 #:system system)
231 (state-return grafts))))
232
233 (define (return/cache cache value)
234 (mbegin %store-monad
235 (set-current-state (vhash-consq drv value cache))
236 (return value)))
237
238 (mlet %state-monad ((cache (current-state)))
239 (match (vhash-assq drv cache)
240 ((_ . grafts) ;hit
241 (return grafts))
242 (#f ;miss
243 (match (non-self-references references drv outputs)
244 (() ;no dependencies
245 (return/cache cache grafts))
246 (deps ;one or more dependencies
247 (mlet %state-monad ((grafts (mapm %state-monad dependency-grafts deps))
248 (cache (current-state)))
249 (let* ((grafts (delete-duplicates (concatenate grafts) equal?))
250 (origins (map graft-origin-file-name grafts)))
251 (if (find (cut member <> deps) origins)
252 (let* ((new (graft-derivation/shallow store drv grafts
253 #:guile guile
254 #:system system))
255 (grafts (cons (graft (origin drv) (replacement new))
256 grafts)))
257 (return/cache cache grafts))
258 (return/cache cache grafts))))))))))
259
260 (define* (graft-derivation store drv grafts
261 #:key (guile (%guile-for-build))
262 (system (%current-system)))
263 "Applied GRAFTS to DRV and all its dependencies, recursively. That is, if
264 GRAFTS apply only indirectly to DRV, graft the dependencies of DRV, and graft
265 DRV itself to refer to those grafted dependencies."
266
267 ;; First, pre-compute the dependency tree of the outputs of DRV. Do this
268 ;; upfront to have as much parallelism as possible when querying substitute
269 ;; info or when building DRV.
270 (define references
271 (references-oracle store drv))
272
273 (match (run-with-state
274 (cumulative-grafts store drv grafts references
275 #:guile guile #:system system)
276 vlist-null) ;the initial cache
277 ((first . rest)
278 ;; If FIRST is not a graft for DRV, it means that GRAFTS are not
279 ;; applicable to DRV and nothing needs to be done.
280 (if (equal? drv (graft-origin first))
281 (graft-replacement first)
282 drv))))
283
284 \f
285 ;; The following might feel more at home in (guix packages) but since (guix
286 ;; gexp), which is a lower level, needs them, we put them here.
287
288 (define %graft?
289 ;; Whether to honor package grafts by default.
290 (make-parameter #t))
291
292 (define (set-grafting enable?)
293 "This monadic procedure enables grafting when ENABLE? is true, and disables
294 it otherwise. It returns the previous setting."
295 (lambda (store)
296 (values (%graft? enable?) store)))
297
298 ;;; grafts.scm ends here