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