Commit | Line | Data |
---|---|---|
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)) | |
fd7d1235 | 81 | (outputs (derivation-output-names drv)) |
c22a1324 LC |
82 | (guile (%guile-for-build)) |
83 | (system (%current-system))) | |
fd7d1235 LC |
84 | "Return a derivation called NAME, which applies GRAFTS to the specified |
85 | OUTPUTS of DRV. This procedure performs \"shallow\" grafting in that GRAFTS | |
86 | are not recursively applied to dependencies of DRV." | |
7adf9b84 LC |
87 | ;; XXX: Someday rewrite using gexps. |
88 | (define mapping | |
89 | ;; List of store item pairs. | |
90 | (map (match-lambda | |
91 | (($ <graft> source source-output target target-output) | |
92 | (cons (if (derivation? source) | |
93 | (derivation->output-path source source-output) | |
94 | source) | |
95 | (if (derivation? target) | |
96 | (derivation->output-path target target-output) | |
97 | target)))) | |
98 | grafts)) | |
99 | ||
fd7d1235 LC |
100 | (define output-pairs |
101 | (map (lambda (output) | |
102 | (cons output | |
103 | (derivation-output-path | |
104 | (assoc-ref (derivation-outputs drv) output)))) | |
105 | outputs)) | |
7adf9b84 LC |
106 | |
107 | (define build | |
108 | `(begin | |
109 | (use-modules (guix build graft) | |
110 | (guix build utils) | |
111 | (ice-9 match)) | |
112 | ||
fd7d1235 | 113 | (let* ((old-outputs ',output-pairs) |
f376dc3a LC |
114 | (mapping (append ',mapping |
115 | (map (match-lambda | |
116 | ((name . file) | |
117 | (cons (assoc-ref old-outputs name) | |
118 | file))) | |
119 | %outputs)))) | |
7adf9b84 LC |
120 | (for-each (lambda (input output) |
121 | (format #t "grafting '~a' -> '~a'...~%" input output) | |
122 | (force-output) | |
f376dc3a LC |
123 | (rewrite-directory input output mapping)) |
124 | (match old-outputs | |
125 | (((names . files) ...) | |
126 | files)) | |
7adf9b84 LC |
127 | (match %outputs |
128 | (((names . files) ...) | |
129 | files)))))) | |
130 | ||
131 | (define add-label | |
132 | (cut cons "x" <>)) | |
133 | ||
134 | (match grafts | |
135 | ((($ <graft> sources source-outputs targets target-outputs) ...) | |
136 | (let ((sources (zip sources source-outputs)) | |
137 | (targets (zip targets target-outputs))) | |
138 | (build-expression->derivation store name build | |
139 | #:system system | |
140 | #:guile-for-build guile | |
141 | #:modules '((guix build graft) | |
142 | (guix build utils)) | |
143 | #:inputs `(,@(map (lambda (out) | |
144 | `("x" ,drv ,out)) | |
fd7d1235 | 145 | outputs) |
7adf9b84 LC |
146 | ,@(append (map add-label sources) |
147 | (map add-label targets))) | |
fd7d1235 | 148 | #:outputs outputs |
7adf9b84 | 149 | #:local-build? #t))))) |
c22a1324 LC |
150 | (define (item->deriver store item) |
151 | "Return two values: the derivation that led to ITEM (a store item), and the | |
152 | name of the output of that derivation ITEM corresponds to (for example | |
153 | \"out\"). When ITEM has no deriver, for instance because it is a plain file, | |
154 | #f and #f are returned." | |
155 | (match (valid-derivers store item) | |
156 | (() ;ITEM is a plain file | |
157 | (values #f #f)) | |
158 | ((drv-file _ ...) | |
015f17e8 | 159 | (let ((drv (read-derivation-from-file drv-file))) |
c22a1324 LC |
160 | (values drv |
161 | (any (match-lambda | |
162 | ((name . path) | |
163 | (and (string=? item path) name))) | |
164 | (derivation->output-paths drv))))))) | |
165 | ||
c90cb5c9 | 166 | (define (non-self-references references drv outputs) |
c22a1324 | 167 | "Return the list of references of the OUTPUTS of DRV, excluding self |
c90cb5c9 LC |
168 | references. Call REFERENCES to get the list of references." |
169 | (let ((refs (append-map (compose references | |
170 | (cut derivation->output-path drv <>)) | |
c22a1324 LC |
171 | outputs)) |
172 | (self (match (derivation->output-paths drv) | |
173 | (((names . items) ...) | |
174 | items)))) | |
175 | (remove (cut member <> self) refs))) | |
176 | ||
c90cb5c9 LC |
177 | (define (references-oracle store drv) |
178 | "Return a one-argument procedure that, when passed the file name of DRV's | |
179 | outputs or their dependencies, returns the list of references of that item. | |
180 | Use either local info or substitute info; build DRV if no information is | |
181 | available." | |
182 | (define (output-paths drv) | |
183 | (match (derivation->output-paths drv) | |
184 | (((names . items) ...) | |
185 | items))) | |
186 | ||
187 | (define (references* items) | |
188 | (guard (c ((nix-protocol-error? c) | |
189 | ;; As a last resort, build DRV and query the references of the | |
190 | ;; build result. | |
264fdedb LC |
191 | |
192 | ;; Warm up the narinfo cache, otherwise each derivation build | |
193 | ;; will result in one HTTP request to get one narinfo, which is | |
194 | ;; much less efficient than fetching them all upfront. | |
195 | (substitution-oracle store (list drv)) | |
196 | ||
c90cb5c9 LC |
197 | (and (build-derivations store (list drv)) |
198 | (map (cut references store <>) items)))) | |
199 | (references/substitutes store items))) | |
200 | ||
201 | (let loop ((items (output-paths drv)) | |
202 | (result vlist-null)) | |
203 | (match items | |
204 | (() | |
205 | (lambda (item) | |
206 | (match (vhash-assoc item result) | |
207 | ((_ . refs) refs) | |
208 | (#f #f)))) | |
209 | (_ | |
210 | (let* ((refs (references* items)) | |
211 | (result (fold vhash-cons result items refs))) | |
212 | (loop (remove (cut vhash-assoc <> result) | |
213 | (delete-duplicates (concatenate refs) string=?)) | |
214 | result)))))) | |
215 | ||
d38bc9a9 LC |
216 | (define-syntax-rule (with-cache key exp ...) |
217 | "Cache the value of monadic expression EXP under KEY." | |
218 | (mlet %state-monad ((cache (current-state))) | |
482fda27 | 219 | (match (vhash-assoc key cache) |
d38bc9a9 LC |
220 | ((_ . result) ;cache hit |
221 | (return result)) | |
222 | (#f ;cache miss | |
0aeed5e3 LC |
223 | (mlet %state-monad ((result (begin exp ...)) |
224 | (cache (current-state))) | |
90ad5c88 | 225 | (mbegin %state-monad |
482fda27 | 226 | (set-current-state (vhash-cons key result cache)) |
90ad5c88 | 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 |
236 | GRAFTS to the dependencies of DRV; REFERENCES must be a one-argument procedure |
237 | that returns the list of references of the store item it is given. Return the | |
d4da602e LC |
238 | resulting list of grafts. |
239 | ||
240 | This is a monadic procedure in %STATE-MONAD where the state is a vhash mapping | |
241 | derivations 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 | ||
482fda27 | 267 | (with-cache (cons (derivation-file-name drv) outputs) |
d38bc9a9 LC |
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 | |
482fda27 | 284 | #:outputs outputs |
d38bc9a9 LC |
285 | #:guile guile |
286 | #:system system)) | |
d38bc9a9 LC |
287 | (grafts (append (map (lambda (output) |
288 | (graft | |
289 | (origin drv) | |
290 | (origin-output output) | |
291 | (replacement new) | |
292 | (replacement-output output))) | |
482fda27 | 293 | outputs) |
d38bc9a9 LC |
294 | grafts))) |
295 | (return grafts)))))))))) | |
c22a1324 LC |
296 | |
297 | (define* (graft-derivation store drv grafts | |
482fda27 LC |
298 | #:key |
299 | (guile (%guile-for-build)) | |
300 | (outputs (derivation-output-names drv)) | |
c22a1324 | 301 | (system (%current-system))) |
482fda27 LC |
302 | "Apply GRAFTS to the OUTPUTS of DRV and all their dependencies, recursively. |
303 | That is, if GRAFTS apply only indirectly to DRV, graft the dependencies of | |
304 | DRV, and graft DRV itself to refer to those grafted dependencies." | |
c22a1324 | 305 | |
c90cb5c9 LC |
306 | ;; First, pre-compute the dependency tree of the outputs of DRV. Do this |
307 | ;; upfront to have as much parallelism as possible when querying substitute | |
308 | ;; info or when building DRV. | |
309 | (define references | |
310 | (references-oracle store drv)) | |
c22a1324 | 311 | |
d4da602e LC |
312 | (match (run-with-state |
313 | (cumulative-grafts store drv grafts references | |
482fda27 | 314 | #:outputs outputs |
d4da602e LC |
315 | #:guile guile #:system system) |
316 | vlist-null) ;the initial cache | |
c22a1324 LC |
317 | ((first . rest) |
318 | ;; If FIRST is not a graft for DRV, it means that GRAFTS are not | |
319 | ;; applicable to DRV and nothing needs to be done. | |
320 | (if (equal? drv (graft-origin first)) | |
321 | (graft-replacement first) | |
322 | drv)))) | |
7adf9b84 LC |
323 | |
324 | \f | |
325 | ;; The following might feel more at home in (guix packages) but since (guix | |
326 | ;; gexp), which is a lower level, needs them, we put them here. | |
327 | ||
328 | (define %graft? | |
329 | ;; Whether to honor package grafts by default. | |
330 | (make-parameter #t)) | |
331 | ||
332 | (define (set-grafting enable?) | |
333 | "This monadic procedure enables grafting when ENABLE? is true, and disables | |
334 | it otherwise. It returns the previous setting." | |
335 | (lambda (store) | |
336 | (values (%graft? enable?) store))) | |
337 | ||
d38bc9a9 LC |
338 | ;; Local Variables: |
339 | ;; eval: (put 'with-cache 'scheme-indent-function 1) | |
340 | ;; End: | |
341 | ||
7adf9b84 | 342 | ;;; grafts.scm ends here |