grafts.scm 14 KB

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