grafts.scm 14 KB

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