grafts.scm 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  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-9 gnu)
  28. #:use-module (srfi srfi-26)
  29. #:use-module (srfi srfi-34)
  30. #:use-module (srfi srfi-71)
  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 %graft-cache
  156. ;; Cache that maps derivation/outputs/grafts tuples to lists of grafts.
  157. (allocate-store-connection-cache 'grafts))
  158. (define record-cache-lookup!
  159. (cache-lookup-recorder "derivation-graft-cache"
  160. "Derivation graft cache"))
  161. (define-syntax-rule (with-cache key exp ...)
  162. "Cache the value of monadic expression EXP under KEY."
  163. (mlet* %state-monad ((cache (current-state))
  164. (result -> (vhash-assoc key cache)))
  165. (record-cache-lookup! result cache)
  166. (match result
  167. ((_ . result) ;cache hit
  168. (return result))
  169. (#f ;cache miss
  170. (mlet %state-monad ((result (begin exp ...))
  171. (cache (current-state)))
  172. (mbegin %state-monad
  173. (set-current-state (vhash-cons key result cache))
  174. (return result)))))))
  175. (define (reference-origins drv items)
  176. "Return the derivation/output pairs among the inputs of DRV, recursively,
  177. that produce ITEMS. Elements of ITEMS not produced by a derivation (i.e.,
  178. it's a content-addressed \"source\"), or not produced by a dependency of DRV,
  179. have no corresponding element in the resulting list."
  180. (define (lookup-derivers drv result items)
  181. ;; Return RESULT augmented by all the drv/output pairs producing one of
  182. ;; ITEMS, and ITEMS stripped of matching items.
  183. (fold2 (match-lambda*
  184. (((output . file) result items)
  185. (if (member file items)
  186. (values (alist-cons drv output result)
  187. (delete file items))
  188. (values result items))))
  189. result items
  190. (derivation->output-paths drv)))
  191. ;; Perform a breadth-first traversal of the dependency graph of DRV in
  192. ;; search of the derivations that produce ITEMS.
  193. (let loop ((drv (list drv))
  194. (items items)
  195. (result '())
  196. (visited (setq)))
  197. (match drv
  198. (()
  199. result)
  200. ((drv . rest)
  201. (cond ((null? items)
  202. result)
  203. ((set-contains? visited drv)
  204. (loop rest items result visited))
  205. (else
  206. (let* ((inputs
  207. (map derivation-input-derivation
  208. (derivation-inputs drv)))
  209. (result items
  210. (fold2 lookup-derivers
  211. result items inputs)))
  212. (loop (append rest inputs)
  213. items result
  214. (set-insert drv visited)))))))))
  215. (define* (cumulative-grafts store drv grafts
  216. #:key
  217. (outputs (derivation-output-names drv))
  218. (guile (%guile-for-build))
  219. (system (%current-system)))
  220. "Augment GRAFTS with additional grafts resulting from the application of
  221. GRAFTS to the dependencies of DRV. Return the resulting list of grafts.
  222. This is a monadic procedure in %STATE-MONAD where the state is a vhash mapping
  223. derivations to the corresponding set of grafts."
  224. (define (graft-origin? drv graft)
  225. ;; Return true if DRV corresponds to the origin of GRAFT.
  226. (match graft
  227. (($ <graft> (? derivation? origin) output)
  228. (match (assoc-ref (derivation->output-paths drv) output)
  229. ((? string? result)
  230. (string=? result
  231. (derivation->output-path origin output)))
  232. (_
  233. #f)))
  234. (_
  235. #f)))
  236. (define (dependency-grafts items)
  237. (mapm %store-monad
  238. (lambda (drv+output)
  239. (match drv+output
  240. ((drv . output)
  241. ;; If GRAFTS already contains a graft from DRV, do not
  242. ;; override it.
  243. (if (find (cut graft-origin? drv <>) grafts)
  244. (state-return grafts)
  245. (cumulative-grafts store drv grafts
  246. #:outputs (list output)
  247. #:guile guile
  248. #:system system)))))
  249. (reference-origins drv items)))
  250. (with-cache (list (derivation-file-name drv) outputs grafts)
  251. (match (non-self-references store drv outputs)
  252. (() ;no dependencies
  253. (return grafts))
  254. (deps ;one or more dependencies
  255. (mlet %state-monad ((grafts (dependency-grafts deps)))
  256. (let ((grafts (delete-duplicates (concatenate grafts) equal?)))
  257. (match (filter (lambda (graft)
  258. (member (graft-origin-file-name graft) deps))
  259. grafts)
  260. (()
  261. (return grafts))
  262. ((applicable ..1)
  263. ;; Use APPLICABLE, the subset of GRAFTS that is really
  264. ;; applicable to DRV, to avoid creating several identical
  265. ;; grafted variants of DRV.
  266. (let* ((new (graft-derivation/shallow store drv applicable
  267. #:outputs outputs
  268. #:guile guile
  269. #:system system))
  270. (grafts (append (map (lambda (output)
  271. (graft
  272. (origin drv)
  273. (origin-output output)
  274. (replacement new)
  275. (replacement-output output)))
  276. outputs)
  277. grafts)))
  278. (return grafts))))))))))
  279. (define* (graft-derivation store drv grafts
  280. #:key
  281. (guile (%guile-for-build))
  282. (outputs (derivation-output-names drv))
  283. (system (%current-system)))
  284. "Apply GRAFTS to the OUTPUTS of DRV and all their dependencies, recursively.
  285. That is, if GRAFTS apply only indirectly to DRV, graft the dependencies of
  286. DRV, and graft DRV itself to refer to those grafted dependencies."
  287. (let ((grafts cache
  288. (run-with-state
  289. (cumulative-grafts store drv grafts
  290. #:outputs outputs
  291. #:guile guile #:system system)
  292. (store-connection-cache store %graft-cache))))
  293. ;; Save CACHE in STORE to benefit from it on the next call.
  294. ;; XXX: Ideally we'd use %STORE-MONAD and 'mcached' and avoid mutating
  295. ;; STORE.
  296. (set-store-connection-cache! store %graft-cache cache)
  297. (match grafts
  298. ((first . rest)
  299. ;; If FIRST is not a graft for DRV, it means that GRAFTS are not
  300. ;; applicable to DRV and nothing needs to be done.
  301. (if (equal? drv (graft-origin first))
  302. (graft-replacement first)
  303. drv)))))
  304. ;; The following might feel more at home in (guix packages) but since (guix
  305. ;; gexp), which is a lower level, needs them, we put them here.
  306. (define %graft?
  307. ;; Whether to honor package grafts by default.
  308. (make-parameter #t))
  309. (define-inlinable (set-grafting enable?)
  310. ;; This monadic procedure enables grafting when ENABLE? is true, and
  311. ;; disables it otherwise. It returns the previous setting.
  312. (lambda (store)
  313. (values (%graft? enable?) store)))
  314. (define-inlinable (grafting?)
  315. ;; Return a Boolean indicating whether grafting is enabled.
  316. (lambda (store)
  317. (values (%graft?) store)))
  318. ;; Local Variables:
  319. ;; eval: (put 'with-cache 'scheme-indent-function 1)
  320. ;; End:
  321. ;;; grafts.scm ends here