Commit | Line | Data |
---|---|---|
7adf9b84 | 1 | ;;; GNU Guix --- Functional package management for GNU |
db45712a | 2 | ;;; Copyright © 2014, 2015, 2016, 2017, 2018, 2019, 2020, 2021 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 | 22 | #:use-module (guix records) |
58bb8333 | 23 | #:use-module (guix combinators) |
7adf9b84 LC |
24 | #:use-module (guix derivations) |
25 | #:use-module ((guix utils) #:select (%current-system)) | |
aad086d8 | 26 | #:use-module (guix sets) |
7adf9b84 | 27 | #:use-module (srfi srfi-1) |
acb01e37 | 28 | #:use-module (srfi srfi-9 gnu) |
7adf9b84 | 29 | #:use-module (srfi srfi-26) |
c90cb5c9 | 30 | #:use-module (srfi srfi-34) |
4a93fb05 | 31 | #:use-module (srfi srfi-71) |
7adf9b84 | 32 | #:use-module (ice-9 match) |
c90cb5c9 | 33 | #:use-module (ice-9 vlist) |
7adf9b84 LC |
34 | #:export (graft? |
35 | graft | |
36 | graft-origin | |
37 | graft-replacement | |
38 | graft-origin-output | |
39 | graft-replacement-output | |
40 | ||
41 | graft-derivation | |
c22a1324 | 42 | graft-derivation/shallow |
7adf9b84 LC |
43 | |
44 | %graft? | |
c6080c32 LC |
45 | set-grafting |
46 | grafting?)) | |
7adf9b84 LC |
47 | |
48 | (define-record-type* <graft> graft make-graft | |
49 | graft? | |
50 | (origin graft-origin) ;derivation | store item | |
51 | (origin-output graft-origin-output ;string | #f | |
52 | (default "out")) | |
53 | (replacement graft-replacement) ;derivation | store item | |
54 | (replacement-output graft-replacement-output ;string | #f | |
55 | (default "out"))) | |
56 | ||
acb01e37 LC |
57 | (define (write-graft graft port) |
58 | "Write a concise representation of GRAFT to PORT." | |
59 | (define (->string thing output) | |
60 | (if (derivation? thing) | |
61 | (derivation->output-path thing output) | |
62 | thing)) | |
63 | ||
64 | (match graft | |
65 | (($ <graft> origin origin-output replacement replacement-output) | |
66 | (format port "#<graft ~a ==> ~a ~a>" | |
67 | (->string origin origin-output) | |
68 | (->string replacement replacement-output) | |
69 | (number->string (object-address graft) 16))))) | |
70 | ||
71 | (set-record-type-printer! <graft> write-graft) | |
72 | ||
c22a1324 LC |
73 | (define (graft-origin-file-name graft) |
74 | "Return the output file name of the origin of GRAFT." | |
75 | (match graft | |
76 | (($ <graft> (? derivation? origin) output) | |
77 | (derivation->output-path origin output)) | |
78 | (($ <graft> (? string? item)) | |
79 | item))) | |
80 | ||
81 | (define* (graft-derivation/shallow store drv grafts | |
82 | #:key | |
83 | (name (derivation-name drv)) | |
fd7d1235 | 84 | (outputs (derivation-output-names drv)) |
c22a1324 LC |
85 | (guile (%guile-for-build)) |
86 | (system (%current-system))) | |
fd7d1235 LC |
87 | "Return a derivation called NAME, which applies GRAFTS to the specified |
88 | OUTPUTS of DRV. This procedure performs \"shallow\" grafting in that GRAFTS | |
89 | are not recursively applied to dependencies of DRV." | |
7adf9b84 LC |
90 | ;; XXX: Someday rewrite using gexps. |
91 | (define mapping | |
92 | ;; List of store item pairs. | |
93 | (map (match-lambda | |
94 | (($ <graft> source source-output target target-output) | |
95 | (cons (if (derivation? source) | |
96 | (derivation->output-path source source-output) | |
97 | source) | |
98 | (if (derivation? target) | |
99 | (derivation->output-path target target-output) | |
100 | target)))) | |
101 | grafts)) | |
102 | ||
fd7d1235 LC |
103 | (define output-pairs |
104 | (map (lambda (output) | |
105 | (cons output | |
106 | (derivation-output-path | |
107 | (assoc-ref (derivation-outputs drv) output)))) | |
108 | outputs)) | |
7adf9b84 LC |
109 | |
110 | (define build | |
111 | `(begin | |
112 | (use-modules (guix build graft) | |
113 | (guix build utils) | |
114 | (ice-9 match)) | |
115 | ||
fd7d1235 | 116 | (let* ((old-outputs ',output-pairs) |
f376dc3a LC |
117 | (mapping (append ',mapping |
118 | (map (match-lambda | |
119 | ((name . file) | |
120 | (cons (assoc-ref old-outputs name) | |
121 | file))) | |
122 | %outputs)))) | |
e4297aa8 | 123 | (graft old-outputs %outputs mapping)))) |
7adf9b84 LC |
124 | |
125 | (define add-label | |
126 | (cut cons "x" <>)) | |
127 | ||
64fd1c01 LC |
128 | (define properties |
129 | `((type . graft) | |
130 | (graft (count . ,(length grafts))))) | |
131 | ||
7adf9b84 LC |
132 | (match grafts |
133 | ((($ <graft> sources source-outputs targets target-outputs) ...) | |
134 | (let ((sources (zip sources source-outputs)) | |
135 | (targets (zip targets target-outputs))) | |
136 | (build-expression->derivation store name build | |
137 | #:system system | |
138 | #:guile-for-build guile | |
139 | #:modules '((guix build graft) | |
93c33389 LC |
140 | (guix build utils) |
141 | (guix build debug-link) | |
142 | (guix elf)) | |
7adf9b84 LC |
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 |
7bc5657f LC |
149 | |
150 | ;; Grafts are computationally cheap so no | |
151 | ;; need to offload or substitute. | |
64fd1c01 | 152 | #:local-build? #t |
7bc5657f LC |
153 | #:substitutable? #f |
154 | ||
64fd1c01 | 155 | #:properties properties))))) |
c22a1324 | 156 | |
4b75a706 | 157 | (define (non-self-references store drv outputs) |
c22a1324 | 158 | "Return the list of references of the OUTPUTS of DRV, excluding self |
4b75a706 | 159 | references." |
c90cb5c9 | 160 | (define (references* items) |
71085430 | 161 | ;; Return the references of ITEMS. |
f9e8a123 | 162 | (guard (c ((store-protocol-error? c) |
71085430 | 163 | ;; ITEMS are not in store so build INPUT first. |
4b75a706 LC |
164 | (and (build-derivations store (list drv)) |
165 | (append-map (cut references/cached store <>) items)))) | |
166 | (append-map (cut references/cached store <>) items))) | |
c90cb5c9 | 167 | |
4b75a706 LC |
168 | (let ((refs (references* (map (cut derivation->output-path drv <>) |
169 | outputs))) | |
170 | (self (match (derivation->output-paths drv) | |
171 | (((names . items) ...) | |
172 | items)))) | |
173 | (remove (cut member <> self) refs))) | |
c90cb5c9 | 174 | |
0c109026 LC |
175 | (define %graft-cache |
176 | ;; Cache that maps derivation/outputs/grafts tuples to lists of grafts. | |
177 | (allocate-store-connection-cache 'grafts)) | |
178 | ||
6bd3d4fe LC |
179 | (define record-cache-lookup! |
180 | (cache-lookup-recorder "derivation-graft-cache" | |
181 | "Derivation graft cache")) | |
182 | ||
d38bc9a9 LC |
183 | (define-syntax-rule (with-cache key exp ...) |
184 | "Cache the value of monadic expression EXP under KEY." | |
6bd3d4fe LC |
185 | (mlet* %state-monad ((cache (current-state)) |
186 | (result -> (vhash-assoc key cache))) | |
187 | (record-cache-lookup! result cache) | |
188 | (match result | |
d38bc9a9 LC |
189 | ((_ . result) ;cache hit |
190 | (return result)) | |
191 | (#f ;cache miss | |
0aeed5e3 LC |
192 | (mlet %state-monad ((result (begin exp ...)) |
193 | (cache (current-state))) | |
90ad5c88 | 194 | (mbegin %state-monad |
482fda27 | 195 | (set-current-state (vhash-cons key result cache)) |
90ad5c88 | 196 | (return result))))))) |
d38bc9a9 | 197 | |
58bb8333 LC |
198 | (define (reference-origins drv items) |
199 | "Return the derivation/output pairs among the inputs of DRV, recursively, | |
200 | that produce ITEMS. Elements of ITEMS not produced by a derivation (i.e., | |
201 | it's a content-addressed \"source\"), or not produced by a dependency of DRV, | |
202 | have no corresponding element in the resulting list." | |
203 | (define (lookup-derivers drv result items) | |
204 | ;; Return RESULT augmented by all the drv/output pairs producing one of | |
205 | ;; ITEMS, and ITEMS stripped of matching items. | |
206 | (fold2 (match-lambda* | |
207 | (((output . file) result items) | |
208 | (if (member file items) | |
209 | (values (alist-cons drv output result) | |
210 | (delete file items)) | |
211 | (values result items)))) | |
212 | result items | |
213 | (derivation->output-paths drv))) | |
214 | ||
aad086d8 | 215 | ;; Perform a breadth-first traversal of the dependency graph of DRV in |
58bb8333 | 216 | ;; search of the derivations that produce ITEMS. |
aad086d8 | 217 | (let loop ((drv (list drv)) |
58bb8333 LC |
218 | (items items) |
219 | (result '()) | |
aad086d8 LC |
220 | (visited (setq))) |
221 | (match drv | |
222 | (() | |
58bb8333 | 223 | result) |
aad086d8 | 224 | ((drv . rest) |
58bb8333 LC |
225 | (cond ((null? items) |
226 | result) | |
227 | ((set-contains? visited drv) | |
228 | (loop rest items result visited)) | |
229 | (else | |
4a93fb05 LC |
230 | (let* ((inputs |
231 | (map derivation-input-derivation | |
232 | (derivation-inputs drv))) | |
233 | (result items | |
58bb8333 LC |
234 | (fold2 lookup-derivers |
235 | result items inputs))) | |
236 | (loop (append rest inputs) | |
237 | items result | |
238 | (set-insert drv visited))))))))) | |
aad086d8 | 239 | |
c22a1324 LC |
240 | (define* (cumulative-grafts store drv grafts |
241 | #:key | |
242 | (outputs (derivation-output-names drv)) | |
243 | (guile (%guile-for-build)) | |
244 | (system (%current-system))) | |
245 | "Augment GRAFTS with additional grafts resulting from the application of | |
4b75a706 | 246 | GRAFTS to the dependencies of DRV. Return the resulting list of grafts. |
d4da602e LC |
247 | |
248 | This is a monadic procedure in %STATE-MONAD where the state is a vhash mapping | |
249 | derivations to the corresponding set of grafts." | |
b013c33f LC |
250 | (define (graft-origin? drv graft) |
251 | ;; Return true if DRV corresponds to the origin of GRAFT. | |
252 | (match graft | |
253 | (($ <graft> (? derivation? origin) output) | |
254 | (match (assoc-ref (derivation->output-paths drv) output) | |
255 | ((? string? result) | |
256 | (string=? result | |
257 | (derivation->output-path origin output))) | |
258 | (_ | |
259 | #f))) | |
260 | (_ | |
261 | #f))) | |
262 | ||
58bb8333 LC |
263 | (define (dependency-grafts items) |
264 | (mapm %store-monad | |
265 | (lambda (drv+output) | |
266 | (match drv+output | |
267 | ((drv . output) | |
268 | ;; If GRAFTS already contains a graft from DRV, do not | |
269 | ;; override it. | |
270 | (if (find (cut graft-origin? drv <>) grafts) | |
271 | (state-return grafts) | |
272 | (cumulative-grafts store drv grafts | |
273 | #:outputs (list output) | |
274 | #:guile guile | |
275 | #:system system))))) | |
276 | (reference-origins drv items))) | |
d4da602e | 277 | |
0c109026 | 278 | (with-cache (list (derivation-file-name drv) outputs grafts) |
4b75a706 | 279 | (match (non-self-references store drv outputs) |
d38bc9a9 | 280 | (() ;no dependencies |
d4da602e | 281 | (return grafts)) |
d38bc9a9 | 282 | (deps ;one or more dependencies |
58bb8333 | 283 | (mlet %state-monad ((grafts (dependency-grafts deps))) |
d38bc9a9 LC |
284 | (let ((grafts (delete-duplicates (concatenate grafts) equal?))) |
285 | (match (filter (lambda (graft) | |
286 | (member (graft-origin-file-name graft) deps)) | |
287 | grafts) | |
288 | (() | |
289 | (return grafts)) | |
290 | ((applicable ..1) | |
291 | ;; Use APPLICABLE, the subset of GRAFTS that is really | |
292 | ;; applicable to DRV, to avoid creating several identical | |
293 | ;; grafted variants of DRV. | |
294 | (let* ((new (graft-derivation/shallow store drv applicable | |
482fda27 | 295 | #:outputs outputs |
d38bc9a9 LC |
296 | #:guile guile |
297 | #:system system)) | |
d38bc9a9 LC |
298 | (grafts (append (map (lambda (output) |
299 | (graft | |
300 | (origin drv) | |
301 | (origin-output output) | |
302 | (replacement new) | |
303 | (replacement-output output))) | |
482fda27 | 304 | outputs) |
d38bc9a9 LC |
305 | grafts))) |
306 | (return grafts)))))))))) | |
c22a1324 LC |
307 | |
308 | (define* (graft-derivation store drv grafts | |
482fda27 LC |
309 | #:key |
310 | (guile (%guile-for-build)) | |
311 | (outputs (derivation-output-names drv)) | |
c22a1324 | 312 | (system (%current-system))) |
482fda27 LC |
313 | "Apply GRAFTS to the OUTPUTS of DRV and all their dependencies, recursively. |
314 | That is, if GRAFTS apply only indirectly to DRV, graft the dependencies of | |
315 | DRV, and graft DRV itself to refer to those grafted dependencies." | |
0c109026 LC |
316 | (let ((grafts cache |
317 | (run-with-state | |
318 | (cumulative-grafts store drv grafts | |
319 | #:outputs outputs | |
320 | #:guile guile #:system system) | |
321 | (store-connection-cache store %graft-cache)))) | |
322 | ||
323 | ;; Save CACHE in STORE to benefit from it on the next call. | |
324 | ;; XXX: Ideally we'd use %STORE-MONAD and 'mcached' and avoid mutating | |
325 | ;; STORE. | |
326 | (set-store-connection-cache! store %graft-cache cache) | |
327 | ||
328 | (match grafts | |
329 | ((first . rest) | |
330 | ;; If FIRST is not a graft for DRV, it means that GRAFTS are not | |
331 | ;; applicable to DRV and nothing needs to be done. | |
332 | (if (equal? drv (graft-origin first)) | |
333 | (graft-replacement first) | |
334 | drv))))) | |
7adf9b84 LC |
335 | |
336 | \f | |
337 | ;; The following might feel more at home in (guix packages) but since (guix | |
338 | ;; gexp), which is a lower level, needs them, we put them here. | |
339 | ||
340 | (define %graft? | |
341 | ;; Whether to honor package grafts by default. | |
342 | (make-parameter #t)) | |
343 | ||
db45712a LC |
344 | (define-inlinable (set-grafting enable?) |
345 | ;; This monadic procedure enables grafting when ENABLE? is true, and | |
346 | ;; disables it otherwise. It returns the previous setting. | |
7adf9b84 LC |
347 | (lambda (store) |
348 | (values (%graft? enable?) store))) | |
349 | ||
db45712a LC |
350 | (define-inlinable (grafting?) |
351 | ;; Return a Boolean indicating whether grafting is enabled. | |
c6080c32 LC |
352 | (lambda (store) |
353 | (values (%graft?) store))) | |
354 | ||
d38bc9a9 LC |
355 | ;; Local Variables: |
356 | ;; eval: (put 'with-cache 'scheme-indent-function 1) | |
357 | ;; End: | |
358 | ||
7adf9b84 | 359 | ;;; grafts.scm ends here |