grafts.scm 14 KB

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