gnu: lxqt.scm: Add prefix to licenses imports.
[guix.git] / guix / grafts.scm
blobf303e925f1f192d6b70cb0bf9e8842c7e274cabb
1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2014, 2015, 2016, 2017, 2018 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/>.
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
39             graft-derivation
40             graft-derivation/shallow
42             %graft?
43             set-grafting))
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")))
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))
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)))))
68 (set-record-type-printer! <graft> write-graft)
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)))
78 (define* (graft-derivation/shallow store drv grafts
79                                    #:key
80                                    (name (derivation-name drv))
81                                    (outputs (derivation-output-names drv))
82                                    (guile (%guile-for-build))
83                                    (system (%current-system)))
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."
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))
100   (define output-pairs
101     (map (lambda (output)
102            (cons output
103                  (derivation-output-path
104                   (assoc-ref (derivation-outputs drv) output))))
105          outputs))
107   (define build
108     `(begin
109        (use-modules (guix build graft)
110                     (guix build utils)
111                     (ice-9 match))
113        (let* ((old-outputs ',output-pairs)
114               (mapping (append ',mapping
115                                (map (match-lambda
116                                       ((name . file)
117                                        (cons (assoc-ref old-outputs name)
118                                              file)))
119                                     %outputs))))
120          (graft old-outputs %outputs mapping))))
122   (define add-label
123     (cut cons "x" <>))
125   (match grafts
126     ((($ <graft> sources source-outputs targets target-outputs) ...)
127      (let ((sources (zip sources source-outputs))
128            (targets (zip targets target-outputs)))
129        (build-expression->derivation store name build
130                                      #:system system
131                                      #:guile-for-build guile
132                                      #:modules '((guix build graft)
133                                                  (guix build utils)
134                                                  (guix build debug-link)
135                                                  (guix elf))
136                                      #:inputs `(,@(map (lambda (out)
137                                                          `("x" ,drv ,out))
138                                                        outputs)
139                                                 ,@(append (map add-label sources)
140                                                           (map add-label targets)))
141                                      #:outputs outputs
142                                      #:local-build? #t)))))
143 (define (item->deriver store item)
144   "Return two values: the derivation that led to ITEM (a store item), and the
145 name of the output of that derivation ITEM corresponds to (for example
146 \"out\").  When ITEM has no deriver, for instance because it is a plain file,
147 #f and #f are returned."
148   (match (valid-derivers store item)
149     (()                                           ;ITEM is a plain file
150      (values #f #f))
151     ((drv-file _ ...)
152      (let ((drv (read-derivation-from-file drv-file)))
153        (values drv
154                (any (match-lambda
155                       ((name . path)
156                        (and (string=? item path) name)))
157                     (derivation->output-paths drv)))))))
159 (define (non-self-references references drv outputs)
160   "Return the list of references of the OUTPUTS of DRV, excluding self
161 references.  Call REFERENCES to get the list of references."
162   (let ((refs (append-map (compose references
163                                    (cut derivation->output-path drv <>))
164                           outputs))
165         (self (match (derivation->output-paths drv)
166                 (((names . items) ...)
167                  items))))
168     (remove (cut member <> self) refs)))
170 (define (references-oracle store drv)
171   "Return a one-argument procedure that, when passed the file name of DRV's
172 outputs or their dependencies, returns the list of references of that item.
173 Use either local info or substitute info; build DRV if no information is
174 available."
175   (define (output-paths drv)
176     (match (derivation->output-paths drv)
177       (((names . items) ...)
178        items)))
180   (define (references* items)
181     (guard (c ((nix-protocol-error? c)
182                ;; As a last resort, build DRV and query the references of the
183                ;; build result.
185                ;; Warm up the narinfo cache, otherwise each derivation build
186                ;; will result in one HTTP request to get one narinfo, which is
187                ;; much less efficient than fetching them all upfront.
188                (substitution-oracle store (list drv))
190                (and (build-derivations store (list drv))
191                     (map (cut references store <>) items))))
192       (references/substitutes store items)))
194   (let loop ((items (output-paths drv))
195              (result vlist-null))
196     (match items
197       (()
198        (lambda (item)
199          (match (vhash-assoc item result)
200            ((_ . refs) refs)
201            (#f         #f))))
202       (_
203        (let* ((refs   (references* items))
204               (result (fold vhash-cons result items refs)))
205          (loop (remove (cut vhash-assoc <> result)
206                        (delete-duplicates (concatenate refs) string=?))
207                result))))))
209 (define-syntax-rule (with-cache key exp ...)
210   "Cache the value of monadic expression EXP under KEY."
211   (mlet %state-monad ((cache (current-state)))
212     (match (vhash-assoc key cache)
213       ((_ . result)                               ;cache hit
214        (return result))
215       (#f                                         ;cache miss
216        (mlet %state-monad ((result (begin exp ...))
217                            (cache  (current-state)))
218          (mbegin %state-monad
219            (set-current-state (vhash-cons key result cache))
220            (return result)))))))
222 (define* (cumulative-grafts store drv grafts
223                             references
224                             #:key
225                             (outputs (derivation-output-names drv))
226                             (guile (%guile-for-build))
227                             (system (%current-system)))
228   "Augment GRAFTS with additional grafts resulting from the application of
229 GRAFTS to the dependencies of DRV; REFERENCES must be a one-argument procedure
230 that returns the list of references of the store item it is given.  Return the
231 resulting list of grafts.
233 This is a monadic procedure in %STATE-MONAD where the state is a vhash mapping
234 derivations to the corresponding set of grafts."
235   (define (graft-origin? drv graft)
236     ;; Return true if DRV corresponds to the origin of GRAFT.
237     (match graft
238       (($ <graft> (? derivation? origin) output)
239        (match (assoc-ref (derivation->output-paths drv) output)
240          ((? string? result)
241           (string=? result
242                     (derivation->output-path origin output)))
243          (_
244           #f)))
245       (_
246        #f)))
248   (define (dependency-grafts item)
249     (let-values (((drv output) (item->deriver store item)))
250       (if drv
251           ;; If GRAFTS already contains a graft from DRV, do not override it.
252           (if (find (cut graft-origin? drv <>) grafts)
253               (state-return grafts)
254               (cumulative-grafts store drv grafts references
255                                  #:outputs (list output)
256                                  #:guile guile
257                                  #:system system))
258           (state-return grafts))))
260   (with-cache (cons (derivation-file-name drv) outputs)
261     (match (non-self-references references drv outputs)
262       (()                                         ;no dependencies
263        (return grafts))
264       (deps                                       ;one or more dependencies
265        (mlet %state-monad ((grafts (mapm %state-monad dependency-grafts deps)))
266          (let ((grafts (delete-duplicates (concatenate grafts) equal?)))
267            (match (filter (lambda (graft)
268                             (member (graft-origin-file-name graft) deps))
269                           grafts)
270              (()
271               (return grafts))
272              ((applicable ..1)
273               ;; Use APPLICABLE, the subset of GRAFTS that is really
274               ;; applicable to DRV, to avoid creating several identical
275               ;; grafted variants of DRV.
276               (let* ((new    (graft-derivation/shallow store drv applicable
277                                                        #:outputs outputs
278                                                        #:guile guile
279                                                        #:system system))
280                      (grafts (append (map (lambda (output)
281                                             (graft
282                                               (origin drv)
283                                               (origin-output output)
284                                               (replacement new)
285                                               (replacement-output output)))
286                                           outputs)
287                                      grafts)))
288                 (return grafts))))))))))
290 (define* (graft-derivation store drv grafts
291                            #:key
292                            (guile (%guile-for-build))
293                            (outputs (derivation-output-names drv))
294                            (system (%current-system)))
295   "Apply GRAFTS to the OUTPUTS of DRV and all their dependencies, recursively.
296 That is, if GRAFTS apply only indirectly to DRV, graft the dependencies of
297 DRV, and graft DRV itself to refer to those grafted dependencies."
299   ;; First, pre-compute the dependency tree of the outputs of DRV.  Do this
300   ;; upfront to have as much parallelism as possible when querying substitute
301   ;; info or when building DRV.
302   (define references
303     (references-oracle store drv))
305   (match (run-with-state
306              (cumulative-grafts store drv grafts references
307                                 #:outputs outputs
308                                 #:guile guile #:system system)
309            vlist-null)                            ;the initial cache
310     ((first . rest)
311      ;; If FIRST is not a graft for DRV, it means that GRAFTS are not
312      ;; applicable to DRV and nothing needs to be done.
313      (if (equal? drv (graft-origin first))
314          (graft-replacement first)
315          drv))))
318 ;; The following might feel more at home in (guix packages) but since (guix
319 ;; gexp), which is a lower level, needs them, we put them here.
321 (define %graft?
322   ;; Whether to honor package grafts by default.
323   (make-parameter #t))
325 (define (set-grafting enable?)
326   "This monadic procedure enables grafting when ENABLE? is true, and disables
327 it otherwise.  It returns the previous setting."
328   (lambda (store)
329     (values (%graft? enable?) store)))
331 ;; Local Variables:
332 ;; eval: (put 'with-cache 'scheme-indent-function 1)
333 ;; End:
335 ;;; grafts.scm ends here