grafts.scm 15 KB

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