1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2014, 2015, 2016, 2017, 2018, 2019, 2020 Ludovic Courtès <ludo@gnu.org>
4 ;;; This file is part of GNU Guix.
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.
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.
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/>.
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 (guix sets)
26 #:use-module (srfi srfi-1)
27 #:use-module (srfi srfi-9 gnu)
28 #:use-module (srfi srfi-26)
29 #:use-module (srfi srfi-34)
30 #:use-module (ice-9 match)
31 #:use-module (ice-9 vlist)
37 graft-replacement-output
40 graft-derivation/shallow
46 (define-record-type* <graft> graft make-graft
48 (origin graft-origin) ;derivation | store item
49 (origin-output graft-origin-output ;string | #f
51 (replacement graft-replacement) ;derivation | store item
52 (replacement-output graft-replacement-output ;string | #f
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)
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)))))
69 (set-record-type-printer! <graft> write-graft)
71 (define (graft-origin-file-name graft)
72 "Return the output file name of the origin of GRAFT."
74 (($ <graft> (? derivation? origin) output)
75 (derivation->output-path origin output))
76 (($ <graft> (? string? item))
79 (define* (graft-derivation/shallow store drv grafts
81 (name (derivation-name drv))
82 (outputs (derivation-output-names drv))
83 (guile (%guile-for-build))
84 (system (%current-system)))
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."
88 ;; XXX: Someday rewrite using gexps.
90 ;; List of store item pairs.
92 (($ <graft> source source-output target target-output)
93 (cons (if (derivation? source)
94 (derivation->output-path source source-output)
96 (if (derivation? target)
97 (derivation->output-path target target-output)
102 (map (lambda (output)
104 (derivation-output-path
105 (assoc-ref (derivation-outputs drv) output))))
110 (use-modules (guix build graft)
114 (let* ((old-outputs ',output-pairs)
115 (mapping (append ',mapping
118 (cons (assoc-ref old-outputs name)
121 (graft old-outputs %outputs mapping))))
128 (graft (count . ,(length 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
136 #:guile-for-build guile
137 #:modules '((guix build graft)
139 (guix build debug-link)
141 #:inputs `(,@(map (lambda (out)
144 ,@(append (map add-label sources)
145 (map add-label targets)))
148 ;; Grafts are computationally cheap so no
149 ;; need to offload or substitute.
153 #:properties properties)))))
155 (define (non-self-references references drv outputs)
156 "Return the list of references of the OUTPUTS of DRV, excluding self
157 references. Call REFERENCES to get the list of references."
158 (let ((refs (append-map (compose references
159 (cut derivation->output-path drv <>))
161 (self (match (derivation->output-paths drv)
162 (((names . items) ...)
164 (remove (cut member <> self) refs)))
166 (define (references-oracle store input)
167 "Return a one-argument procedure that, when passed the output file names of
168 INPUT, a derivation input, or their dependencies, returns the list of
169 references of that item. Build INPUT if it's not available."
170 (define (references* items)
171 ;; Return the references of ITEMS.
172 (guard (c ((store-protocol-error? c)
173 ;; ITEMS are not in store so build INPUT first.
174 (and (build-derivations store (list input))
175 (map (cut references/cached store <>) items))))
176 (map (cut references/cached store <>) items)))
178 (let loop ((items (derivation-input-output-paths input))
183 (match (vhash-assoc item result)
187 (let* ((refs (references* items))
188 (result (fold vhash-cons result items refs)))
189 (loop (remove (cut vhash-assoc <> result)
190 (delete-duplicates (concatenate refs) string=?))
193 (define-syntax-rule (with-cache key exp ...)
194 "Cache the value of monadic expression EXP under KEY."
195 (mlet %state-monad ((cache (current-state)))
196 (match (vhash-assoc key cache)
197 ((_ . result) ;cache hit
200 (mlet %state-monad ((result (begin exp ...))
201 (cache (current-state)))
203 (set-current-state (vhash-cons key result cache))
204 (return result)))))))
206 (define (reference-origin drv item)
207 "Return the derivation/output pair among the inputs of DRV, recursively,
208 that produces ITEM. Return #f if ITEM is not produced by a derivation (i.e.,
209 it's a content-addressed \"source\"), or if it's not produced by a dependency
211 ;; Perform a breadth-first traversal of the dependency graph of DRV in
212 ;; search of the derivation that produces ITEM.
213 (let loop ((drv (list drv))
219 (if (set-contains? visited drv)
221 (let ((inputs (derivation-inputs drv)))
222 (or (any (lambda (input)
223 (let ((drv (derivation-input-derivation input)))
226 (and (string=? file item)
228 (derivation->output-paths drv))))
230 (loop (append rest (map derivation-input-derivation inputs))
231 (set-insert drv visited)))))))))
233 (define* (cumulative-grafts store drv grafts
236 (outputs (derivation-output-names drv))
237 (guile (%guile-for-build))
238 (system (%current-system)))
239 "Augment GRAFTS with additional grafts resulting from the application of
240 GRAFTS to the dependencies of DRV; REFERENCES must be a one-argument procedure
241 that returns the list of references of the store item it is given. Return the
242 resulting list of grafts.
244 This is a monadic procedure in %STATE-MONAD where the state is a vhash mapping
245 derivations to the corresponding set of grafts."
246 (define (graft-origin? drv graft)
247 ;; Return true if DRV corresponds to the origin of GRAFT.
249 (($ <graft> (? derivation? origin) output)
250 (match (assoc-ref (derivation->output-paths drv) output)
253 (derivation->output-path origin output)))
259 (define (dependency-grafts item)
260 (match (reference-origin drv item)
262 ;; If GRAFTS already contains a graft from DRV, do not override it.
263 (if (find (cut graft-origin? drv <>) grafts)
264 (state-return grafts)
265 (cumulative-grafts store drv grafts references
266 #:outputs (list output)
270 (state-return grafts))))
272 (with-cache (cons (derivation-file-name drv) outputs)
273 (match (non-self-references references drv outputs)
276 (deps ;one or more dependencies
277 (mlet %state-monad ((grafts (mapm %state-monad dependency-grafts deps)))
278 (let ((grafts (delete-duplicates (concatenate grafts) equal?)))
279 (match (filter (lambda (graft)
280 (member (graft-origin-file-name graft) deps))
285 ;; Use APPLICABLE, the subset of GRAFTS that is really
286 ;; applicable to DRV, to avoid creating several identical
287 ;; grafted variants of DRV.
288 (let* ((new (graft-derivation/shallow store drv applicable
292 (grafts (append (map (lambda (output)
295 (origin-output output)
297 (replacement-output output)))
300 (return grafts))))))))))
302 (define* (graft-derivation store drv grafts
304 (guile (%guile-for-build))
305 (outputs (derivation-output-names drv))
306 (system (%current-system)))
307 "Apply GRAFTS to the OUTPUTS of DRV and all their dependencies, recursively.
308 That is, if GRAFTS apply only indirectly to DRV, graft the dependencies of
309 DRV, and graft DRV itself to refer to those grafted dependencies."
311 ;; First, pre-compute the dependency tree of the outputs of DRV. Do this
312 ;; upfront to have as much parallelism as possible when querying substitute
313 ;; info or when building DRV.
315 (references-oracle store (derivation-input drv outputs)))
317 (match (run-with-state
318 (cumulative-grafts store drv grafts references
320 #:guile guile #:system system)
321 vlist-null) ;the initial cache
323 ;; If FIRST is not a graft for DRV, it means that GRAFTS are not
324 ;; applicable to DRV and nothing needs to be done.
325 (if (equal? drv (graft-origin first))
326 (graft-replacement first)
330 ;; The following might feel more at home in (guix packages) but since (guix
331 ;; gexp), which is a lower level, needs them, we put them here.
334 ;; Whether to honor package grafts by default.
337 (define (set-grafting enable?)
338 "This monadic procedure enables grafting when ENABLE? is true, and disables
339 it otherwise. It returns the previous setting."
341 (values (%graft? enable?) store)))
344 "Return a Boolean indicating whether grafting is enabled."
346 (values (%graft?) store)))
349 ;; eval: (put 'with-cache 'scheme-indent-function 1)
352 ;;; grafts.scm ends here