packages.scm 64 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020 Ludovic Courtès <ludo@gnu.org>
  3. ;;; Copyright © 2014, 2015, 2017, 2018 Mark H Weaver <mhw@netris.org>
  4. ;;; Copyright © 2015 Eric Bavier <bavier@member.fsf.org>
  5. ;;; Copyright © 2016 Alex Kost <alezost@gmail.com>
  6. ;;; Copyright © 2017, 2019, 2020 Efraim Flashner <efraim@flashner.co.il>
  7. ;;; Copyright © 2019 Marius Bakke <mbakke@fastmail.com>
  8. ;;;
  9. ;;; This file is part of GNU Guix.
  10. ;;;
  11. ;;; GNU Guix is free software; you can redistribute it and/or modify it
  12. ;;; under the terms of the GNU General Public License as published by
  13. ;;; the Free Software Foundation; either version 3 of the License, or (at
  14. ;;; your option) any later version.
  15. ;;;
  16. ;;; GNU Guix is distributed in the hope that it will be useful, but
  17. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  18. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. ;;; GNU General Public License for more details.
  20. ;;;
  21. ;;; You should have received a copy of the GNU General Public License
  22. ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
  23. (define-module (guix packages)
  24. #:use-module (guix utils)
  25. #:use-module (guix records)
  26. #:use-module (guix store)
  27. #:use-module (guix monads)
  28. #:use-module (guix gexp)
  29. #:use-module (guix base32)
  30. #:autoload (guix base64) (base64-decode)
  31. #:use-module (guix grafts)
  32. #:use-module (guix derivations)
  33. #:use-module (guix memoization)
  34. #:use-module (guix build-system)
  35. #:use-module (guix search-paths)
  36. #:use-module (guix sets)
  37. #:use-module (guix deprecation)
  38. #:use-module (guix i18n)
  39. #:use-module (ice-9 match)
  40. #:use-module (ice-9 vlist)
  41. #:use-module (ice-9 regex)
  42. #:use-module (srfi srfi-1)
  43. #:use-module (srfi srfi-9 gnu)
  44. #:use-module (srfi srfi-11)
  45. #:use-module (srfi srfi-26)
  46. #:use-module (srfi srfi-34)
  47. #:use-module (srfi srfi-35)
  48. #:use-module (rnrs bytevectors)
  49. #:use-module (web uri)
  50. #:re-export (%current-system
  51. %current-target-system
  52. search-path-specification) ;for convenience
  53. #:export (content-hash
  54. content-hash?
  55. content-hash-algorithm
  56. content-hash-value
  57. origin
  58. origin?
  59. this-origin
  60. origin-uri
  61. origin-method
  62. origin-hash
  63. origin-sha256 ;deprecated
  64. origin-file-name
  65. origin-actual-file-name
  66. origin-patches
  67. origin-patch-flags
  68. origin-patch-inputs
  69. origin-patch-guile
  70. origin-snippet
  71. origin-modules
  72. base32
  73. base64
  74. package
  75. package?
  76. this-package
  77. package-name
  78. package-upstream-name
  79. package-version
  80. package-full-name
  81. package-source
  82. package-build-system
  83. package-arguments
  84. package-inputs
  85. package-native-inputs
  86. package-propagated-inputs
  87. package-outputs
  88. package-native-search-paths
  89. package-search-paths
  90. package-replacement
  91. package-synopsis
  92. package-description
  93. package-license
  94. package-home-page
  95. package-supported-systems
  96. package-properties
  97. package-location
  98. hidden-package
  99. hidden-package?
  100. package-superseded
  101. deprecated-package
  102. package-field-location
  103. package-direct-sources
  104. package-transitive-sources
  105. package-direct-inputs
  106. package-transitive-inputs
  107. package-transitive-target-inputs
  108. package-transitive-native-inputs
  109. package-transitive-propagated-inputs
  110. package-transitive-native-search-paths
  111. package-transitive-supported-systems
  112. package-mapping
  113. package-input-rewriting
  114. package-input-rewriting/spec
  115. package-source-derivation
  116. package-derivation
  117. package-cross-derivation
  118. package-output
  119. package-grafts
  120. package-patched-vulnerabilities
  121. package-with-patches
  122. package-with-extra-patches
  123. package/inherit
  124. transitive-input-references
  125. %supported-systems
  126. %hurd-systems
  127. %hydra-supported-systems
  128. supported-package?
  129. &package-error
  130. package-error?
  131. package-error-package
  132. &package-input-error
  133. package-input-error?
  134. package-error-invalid-input
  135. &package-cross-build-system-error
  136. package-cross-build-system-error?
  137. package->bag
  138. bag->derivation
  139. bag-direct-inputs
  140. bag-transitive-inputs
  141. bag-transitive-host-inputs
  142. bag-transitive-build-inputs
  143. bag-transitive-target-inputs
  144. package-closure
  145. default-guile
  146. default-guile-derivation
  147. set-guile-for-build
  148. package-file
  149. package->derivation
  150. package->cross-derivation
  151. origin->derivation))
  152. ;;; Commentary:
  153. ;;;
  154. ;;; This module provides a high-level mechanism to define packages in a
  155. ;;; Guix-based distribution.
  156. ;;;
  157. ;;; Code:
  158. ;; Crytographic content hash.
  159. (define-immutable-record-type <content-hash>
  160. (%content-hash algorithm value)
  161. content-hash?
  162. (algorithm content-hash-algorithm) ;symbol
  163. (value content-hash-value)) ;bytevector
  164. (define-syntax-rule (define-content-hash-constructor name
  165. (algorithm size) ...)
  166. "Define NAME as a <content-hash> constructor that ensures that (1) its
  167. second argument is among the listed ALGORITHM, and (2), when possible, that
  168. its first argument has the right size for the chosen algorithm."
  169. (define-syntax name
  170. (lambda (s)
  171. (syntax-case s (algorithm ...)
  172. ((_ bv algorithm)
  173. (let ((bv* (syntax->datum #'bv)))
  174. (when (and (bytevector? bv*)
  175. (not (= size (bytevector-length bv*))))
  176. (syntax-violation 'content-hash "invalid content hash length" s))
  177. #'(%content-hash 'algorithm bv)))
  178. ...))))
  179. (define-content-hash-constructor build-content-hash
  180. (sha256 32)
  181. (sha512 64)
  182. (sha3-256 32)
  183. (sha3-512 64)
  184. (blake2s-256 64))
  185. (define-syntax content-hash
  186. (lambda (s)
  187. "Return a content hash with the given parameters. The default hash
  188. algorithm is sha256. If the first argument is a literal string, it is decoded
  189. as base32. Otherwise, it must be a bytevector."
  190. ;; What we'd really want here is something like C++ 'constexpr'.
  191. (syntax-case s ()
  192. ((_ str)
  193. (string? (syntax->datum #'str))
  194. #'(content-hash str sha256))
  195. ((_ str algorithm)
  196. (string? (syntax->datum #'str))
  197. (with-syntax ((bv (base32 (syntax->datum #'str))))
  198. #'(content-hash bv algorithm)))
  199. ((_ (id str) algorithm)
  200. (and (string? (syntax->datum #'str))
  201. (free-identifier=? #'id #'base32))
  202. (with-syntax ((bv (nix-base32-string->bytevector (syntax->datum #'str))))
  203. #'(content-hash bv algorithm)))
  204. ((_ (id str) algorithm)
  205. (and (string? (syntax->datum #'str))
  206. (free-identifier=? #'id #'base64))
  207. (with-syntax ((bv (base64-decode (syntax->datum #'str))))
  208. #'(content-hash bv algorithm)))
  209. ((_ bv)
  210. #'(content-hash bv sha256))
  211. ((_ bv hash)
  212. #'(build-content-hash bv hash)))))
  213. (define (print-content-hash hash port)
  214. (format port "#<content-hash ~a:~a>"
  215. (content-hash-algorithm hash)
  216. (and=> (content-hash-value hash)
  217. bytevector->nix-base32-string)))
  218. (set-record-type-printer! <content-hash> print-content-hash)
  219. ;; The source of a package, such as a tarball URL and fetcher---called
  220. ;; "origin" to avoid name clash with `package-source', `source', etc.
  221. (define-record-type* <origin>
  222. %origin make-origin
  223. origin?
  224. this-origin
  225. (uri origin-uri) ; string
  226. (method origin-method) ; procedure
  227. (hash origin-hash) ; <content-hash>
  228. (file-name origin-file-name (default #f)) ; optional file name
  229. ;; Patches are delayed so that the 'search-patch' calls are made lazily,
  230. ;; which reduces I/O on startup and allows patch-not-found errors to be
  231. ;; gracefully handled at run time.
  232. (patches origin-patches ; list of file names
  233. (default '()) (delayed))
  234. (snippet origin-snippet (default #f)) ; sexp or #f
  235. (patch-flags origin-patch-flags ; list of strings
  236. (default '("-p1")))
  237. ;; Patching requires Guile, GNU Patch, and a few more. These two fields are
  238. ;; used to specify these dependencies when needed.
  239. (patch-inputs origin-patch-inputs ; input list or #f
  240. (default #f))
  241. (modules origin-modules ; list of module names
  242. (default '()))
  243. (patch-guile origin-patch-guile ; package or #f
  244. (default #f)))
  245. (define-syntax origin-compatibility-helper
  246. (syntax-rules (sha256)
  247. ((_ () (fields ...))
  248. (%origin fields ...))
  249. ((_ ((sha256 exp) rest ...) (others ...))
  250. (%origin others ...
  251. (hash (content-hash exp sha256))
  252. rest ...))
  253. ((_ (field rest ...) (others ...))
  254. (origin-compatibility-helper (rest ...)
  255. (others ... field)))))
  256. (define-syntax-rule (origin fields ...)
  257. "Build an <origin> record, automatically converting 'sha256' field
  258. specifications to 'hash'."
  259. (origin-compatibility-helper (fields ...) ()))
  260. (define-deprecated (origin-sha256 origin)
  261. origin-hash
  262. (let ((hash (origin-hash origin)))
  263. (unless (eq? (content-hash-algorithm hash) 'sha256)
  264. (raise (condition (&message
  265. (message (G_ "no SHA256 hash for origin"))))))
  266. (content-hash-value hash)))
  267. (define (print-origin origin port)
  268. "Write a concise representation of ORIGIN to PORT."
  269. (match origin
  270. (($ <origin> uri method hash file-name patches)
  271. (simple-format port "#<origin ~s ~a ~s ~a>"
  272. uri hash
  273. (force patches)
  274. (number->string (object-address origin) 16)))))
  275. (set-record-type-printer! <origin> print-origin)
  276. (define-syntax-rule (define-compile-time-decoder name string->bytevector)
  277. "Define NAME as a macro that runs STRING->BYTEVECTOR at macro expansion time
  278. if possible."
  279. (define-syntax name
  280. (lambda (s)
  281. "Return the bytevector corresponding to the given textual
  282. representation."
  283. (syntax-case s ()
  284. ((_ str)
  285. (string? (syntax->datum #'str))
  286. ;; A literal string: do the conversion at expansion time.
  287. (with-syntax ((bv (string->bytevector (syntax->datum #'str))))
  288. #''bv))
  289. ((_ str)
  290. #'(string->bytevector str))))))
  291. (define-compile-time-decoder base32 nix-base32-string->bytevector)
  292. (define-compile-time-decoder base64 base64-decode)
  293. (define (origin-actual-file-name origin)
  294. "Return the file name of ORIGIN, either its 'file-name' field or the file
  295. name of its URI."
  296. (define (uri->file-name uri)
  297. ;; Return the 'base name' of URI or URI itself, where URI is a string.
  298. (let ((path (and=> (string->uri uri) uri-path)))
  299. (if path
  300. (basename path)
  301. uri)))
  302. (or (origin-file-name origin)
  303. (match (origin-uri origin)
  304. ((head . tail)
  305. (uri->file-name head))
  306. ((? string? uri)
  307. (uri->file-name uri))
  308. (else
  309. ;; git, svn, cvs, etc. reference
  310. #f))))
  311. (define %supported-systems
  312. ;; This is the list of system types that are supported. By default, we
  313. ;; expect all packages to build successfully here.
  314. '("x86_64-linux" "i686-linux" "armhf-linux" "aarch64-linux" "mips64el-linux" "i586-gnu"))
  315. (define %hurd-systems
  316. ;; The GNU/Hurd systems for which support is being developed.
  317. '("i586-gnu" "i686-gnu"))
  318. (define %hydra-supported-systems
  319. ;; This is the list of system types for which build machines are available.
  320. ;;
  321. ;; XXX: MIPS is unavailable in CI:
  322. ;; <https://lists.gnu.org/archive/html/guix-devel/2017-03/msg00790.html>.
  323. (fold delete %supported-systems '("mips64el-linux")))
  324. ;; A package.
  325. (define-record-type* <package>
  326. package make-package
  327. package?
  328. this-package
  329. (name package-name) ; string
  330. (version package-version) ; string
  331. (source package-source) ; <origin> instance
  332. (build-system package-build-system) ; build system
  333. (arguments package-arguments ; arguments for the build method
  334. (default '()) (thunked))
  335. (inputs package-inputs ; input packages or derivations
  336. (default '()) (thunked))
  337. (propagated-inputs package-propagated-inputs ; same, but propagated
  338. (default '()) (thunked))
  339. (native-inputs package-native-inputs ; native input packages/derivations
  340. (default '()) (thunked))
  341. (outputs package-outputs ; list of strings
  342. (default '("out")))
  343. ; lists of
  344. ; <search-path-specification>,
  345. ; for native and cross
  346. ; inputs
  347. (native-search-paths package-native-search-paths (default '()))
  348. (search-paths package-search-paths (default '()))
  349. ;; The 'replacement' field is marked as "innate" because it never makes
  350. ;; sense to inherit a replacement as is. See the 'package/inherit' macro.
  351. (replacement package-replacement ; package | #f
  352. (default #f) (thunked) (innate))
  353. (synopsis package-synopsis) ; one-line description
  354. (description package-description) ; one or two paragraphs
  355. (license package-license)
  356. (home-page package-home-page)
  357. (supported-systems package-supported-systems ; list of strings
  358. (default %supported-systems))
  359. (properties package-properties (default '())) ; alist for anything else
  360. (location package-location
  361. (default (and=> (current-source-location)
  362. source-properties->location))
  363. (innate)))
  364. (set-record-type-printer! <package>
  365. (lambda (package port)
  366. (let ((loc (package-location package))
  367. (format simple-format))
  368. (format port "#<package ~a@~a ~a~a>"
  369. (package-name package)
  370. (package-version package)
  371. (if loc
  372. (format #f "~a:~a "
  373. (location-file loc)
  374. (location-line loc))
  375. "")
  376. (number->string (object-address
  377. package)
  378. 16)))))
  379. (define-syntax-rule (package/inherit p overrides ...)
  380. "Like (package (inherit P) OVERRIDES ...), except that the same
  381. transformation is done to the package replacement, if any. P must be a bare
  382. identifier, and will be bound to either P or its replacement when evaluating
  383. OVERRIDES."
  384. (let loop ((p p))
  385. (package (inherit p)
  386. overrides ...
  387. (replacement (and=> (package-replacement p) loop)))))
  388. (define (package-upstream-name package)
  389. "Return the upstream name of PACKAGE, which could be different from the name
  390. it has in Guix."
  391. (or (assq-ref (package-properties package) 'upstream-name)
  392. (package-name package)))
  393. (define (hidden-package p)
  394. "Return a \"hidden\" version of P--i.e., one that 'fold-packages' and thus,
  395. user interfaces, ignores."
  396. (package
  397. (inherit p)
  398. (properties `((hidden? . #t)
  399. ,@(package-properties p)))))
  400. (define (hidden-package? p)
  401. "Return true if P is \"hidden\"--i.e., must not be visible to user
  402. interfaces."
  403. (assoc-ref (package-properties p) 'hidden?))
  404. (define (package-superseded p)
  405. "Return the package the supersedes P, or #f if P is still current."
  406. (assoc-ref (package-properties p) 'superseded))
  407. (define (deprecated-package old-name p)
  408. "Return a package called OLD-NAME and marked as superseded by P, a package
  409. object."
  410. (package
  411. (inherit p)
  412. (name old-name)
  413. (properties `((superseded . ,p)))))
  414. (define (package-field-location package field)
  415. "Return the source code location of the definition of FIELD for PACKAGE, or
  416. #f if it could not be determined."
  417. (define (goto port line column)
  418. (unless (and (= (port-column port) (- column 1))
  419. (= (port-line port) (- line 1)))
  420. (unless (eof-object? (read-char port))
  421. (goto port line column))))
  422. (match (package-location package)
  423. (($ <location> file line column)
  424. (catch 'system-error
  425. (lambda ()
  426. ;; In general we want to keep relative file names for modules.
  427. (call-with-input-file (search-path %load-path file)
  428. (lambda (port)
  429. (goto port line column)
  430. (match (read port)
  431. (('package inits ...)
  432. (let ((field (assoc field inits)))
  433. (match field
  434. ((_ value)
  435. (let ((loc (and=> (source-properties value)
  436. source-properties->location)))
  437. (and loc
  438. ;; Preserve the original file name, which may be a
  439. ;; relative file name.
  440. (set-field loc (location-file) file))))
  441. (_
  442. #f))))
  443. (_
  444. #f)))))
  445. (lambda _
  446. #f)))
  447. (_ #f)))
  448. ;; Error conditions.
  449. (define-condition-type &package-error &error
  450. package-error?
  451. (package package-error-package))
  452. (define-condition-type &package-input-error &package-error
  453. package-input-error?
  454. (input package-error-invalid-input))
  455. (define-condition-type &package-cross-build-system-error &package-error
  456. package-cross-build-system-error?)
  457. (define* (package-full-name package #:optional (delimiter "@"))
  458. "Return the full name of PACKAGE--i.e., `NAME@VERSION'. By specifying
  459. DELIMITER (a string), you can customize what will appear between the name and
  460. the version. By default, DELIMITER is \"@\"."
  461. (string-append (package-name package) delimiter (package-version package)))
  462. (define (patch-file-name patch)
  463. "Return the basename of PATCH's file name, or #f if the file name could not
  464. be determined."
  465. (match patch
  466. ((? string?)
  467. (basename patch))
  468. ((? origin?)
  469. (and=> (origin-actual-file-name patch) basename))))
  470. (define %vulnerability-regexp
  471. ;; Regexp matching a CVE identifier in patch file names.
  472. (make-regexp "CVE-[0-9]{4}-[0-9]+"))
  473. (define (package-patched-vulnerabilities package)
  474. "Return the list of patched vulnerabilities of PACKAGE as a list of CVE
  475. identifiers. The result is inferred from the file names of patches."
  476. (define (patch-vulnerabilities patch)
  477. (map (cut match:substring <> 0)
  478. (list-matches %vulnerability-regexp patch)))
  479. (let ((patches (filter-map patch-file-name
  480. (or (and=> (package-source package)
  481. origin-patches)
  482. '()))))
  483. (append-map patch-vulnerabilities patches)))
  484. (define (%standard-patch-inputs)
  485. (let* ((canonical (module-ref (resolve-interface '(gnu packages base))
  486. 'canonical-package))
  487. (ref (lambda (module var)
  488. (canonical
  489. (module-ref (resolve-interface module) var)))))
  490. `(("tar" ,(ref '(gnu packages base) 'tar))
  491. ("xz" ,(ref '(gnu packages compression) 'xz))
  492. ("bzip2" ,(ref '(gnu packages compression) 'bzip2))
  493. ("gzip" ,(ref '(gnu packages compression) 'gzip))
  494. ("lzip" ,(ref '(gnu packages compression) 'lzip))
  495. ("unzip" ,(ref '(gnu packages compression) 'unzip))
  496. ("patch" ,(ref '(gnu packages base) 'patch))
  497. ("locales" ,(ref '(gnu packages base) 'glibc-utf8-locales)))))
  498. (define (default-guile)
  499. "Return the default Guile package used to run the build code of
  500. derivations."
  501. (let ((distro (resolve-interface '(gnu packages commencement))))
  502. (module-ref distro 'guile-final)))
  503. (define (guile-for-grafts)
  504. "Return the Guile package used to build grafting derivations."
  505. ;; Guile 2.2 would not work due to <https://bugs.gnu.org/28211> when
  506. ;; grafting packages.
  507. (let ((distro (resolve-interface '(gnu packages guile))))
  508. (module-ref distro 'guile-2.0)))
  509. (define* (default-guile-derivation #:optional (system (%current-system)))
  510. "Return the derivation for SYSTEM of the default Guile package used to run
  511. the build code of derivation."
  512. (package->derivation (default-guile) system
  513. #:graft? #f))
  514. (define* (patch-and-repack source patches
  515. #:key
  516. inputs
  517. (snippet #f)
  518. (flags '("-p1"))
  519. (modules '())
  520. (guile-for-build (%guile-for-build))
  521. (system (%current-system)))
  522. "Unpack SOURCE (a derivation or store path), apply all of PATCHES, and
  523. repack the tarball using the tools listed in INPUTS. When SNIPPET is true,
  524. it must be an s-expression that will run from within the directory where
  525. SOURCE was unpacked, after all of PATCHES have been applied. MODULES
  526. specifies modules in scope when evaluating SNIPPET."
  527. (define source-file-name
  528. ;; SOURCE is usually a derivation, but it could be a store file.
  529. (if (derivation? source)
  530. (derivation->output-path source)
  531. source))
  532. (define lookup-input
  533. ;; The default value of the 'patch-inputs' field, and thus INPUTS is #f,
  534. ;; so deal with that.
  535. (let ((inputs (or inputs (%standard-patch-inputs))))
  536. (lambda (name)
  537. (match (assoc-ref inputs name)
  538. ((package) package)
  539. (#f #f)))))
  540. (define decompression-type
  541. (cond ((string-suffix? "gz" source-file-name) "gzip")
  542. ((string-suffix? "Z" source-file-name) "gzip")
  543. ((string-suffix? "bz2" source-file-name) "bzip2")
  544. ((string-suffix? "lz" source-file-name) "lzip")
  545. ((string-suffix? "zip" source-file-name) "unzip")
  546. (else "xz")))
  547. (define original-file-name
  548. ;; Remove the store prefix plus the slash, hash, and hyphen.
  549. (let* ((sans (string-drop source-file-name
  550. (+ (string-length (%store-prefix)) 1)))
  551. (dash (string-index sans #\-)))
  552. (string-drop sans (+ 1 dash))))
  553. (define (numeric-extension? file-name)
  554. ;; Return true if FILE-NAME ends with digits.
  555. (and=> (file-extension file-name)
  556. (cut string-every char-set:hex-digit <>)))
  557. (define (checkout? directory)
  558. ;; Return true if DIRECTORY is a checkout (git, svn, etc).
  559. (string-suffix? "-checkout" directory))
  560. (define (tarxz-name file-name)
  561. ;; Return a '.tar.xz' file name based on FILE-NAME.
  562. (let ((base (cond ((numeric-extension? file-name)
  563. original-file-name)
  564. ((checkout? file-name)
  565. (string-drop-right file-name 9))
  566. (else (file-sans-extension file-name)))))
  567. (string-append base
  568. (if (equal? (file-extension base) "tar")
  569. ".xz"
  570. ".tar.xz"))))
  571. (define instantiate-patch
  572. (match-lambda
  573. ((? string? patch) ;deprecated
  574. (interned-file patch #:recursive? #t))
  575. ((? struct? patch) ;origin, local-file, etc.
  576. (lower-object patch system))))
  577. (mlet %store-monad ((tar -> (lookup-input "tar"))
  578. (xz -> (lookup-input "xz"))
  579. (patch -> (lookup-input "patch"))
  580. (locales -> (lookup-input "locales"))
  581. (decomp -> (lookup-input decompression-type))
  582. (patches (sequence %store-monad
  583. (map instantiate-patch patches))))
  584. (define build
  585. (with-imported-modules '((guix build utils))
  586. #~(begin
  587. (use-modules (ice-9 ftw)
  588. (srfi srfi-1)
  589. (guix build utils))
  590. ;; The --sort option was added to GNU tar in version 1.28, released
  591. ;; 2014-07-28. During bootstrap we must cope with older versions.
  592. (define tar-supports-sort?
  593. (zero? (system* (string-append #+tar "/bin/tar")
  594. "cf" "/dev/null" "--files-from=/dev/null"
  595. "--sort=name")))
  596. (define (apply-patch patch)
  597. (format (current-error-port) "applying '~a'...~%" patch)
  598. ;; Use '--force' so that patches that do not apply perfectly are
  599. ;; rejected. Use '--no-backup-if-mismatch' to prevent making
  600. ;; "*.orig" file if a patch is applied with offset.
  601. (invoke (string-append #+patch "/bin/patch")
  602. "--force" "--no-backup-if-mismatch"
  603. #+@flags "--input" patch))
  604. (define (first-file directory)
  605. ;; Return the name of the first file in DIRECTORY.
  606. (car (scandir directory
  607. (lambda (name)
  608. (not (member name '("." "..")))))))
  609. ;; Encoding/decoding errors shouldn't be silent.
  610. (fluid-set! %default-port-conversion-strategy 'error)
  611. (when #+locales
  612. ;; First of all, install a UTF-8 locale so that UTF-8 file names
  613. ;; are correctly interpreted. During bootstrap, LOCALES is #f.
  614. (setenv "LOCPATH"
  615. (string-append #+locales "/lib/locale/"
  616. #+(and locales
  617. (version-major+minor
  618. (package-version locales)))))
  619. (setlocale LC_ALL "en_US.utf8"))
  620. (setenv "PATH" (string-append #+xz "/bin" ":"
  621. #+decomp "/bin"))
  622. ;; SOURCE may be either a directory or a tarball.
  623. (if (file-is-directory? #+source)
  624. (let* ((store (%store-directory))
  625. (len (+ 1 (string-length store)))
  626. (base (string-drop #+source len))
  627. (dash (string-index base #\-))
  628. (directory (string-drop base (+ 1 dash))))
  629. (mkdir directory)
  630. (copy-recursively #+source directory))
  631. #+(if (string=? decompression-type "unzip")
  632. #~(invoke "unzip" #+source)
  633. #~(invoke (string-append #+tar "/bin/tar")
  634. "xvf" #+source)))
  635. (let ((directory (first-file ".")))
  636. (format (current-error-port)
  637. "source is under '~a'~%" directory)
  638. (chdir directory)
  639. (for-each apply-patch '#+patches)
  640. (let ((result #+(if snippet
  641. #~(let ((module (make-fresh-user-module)))
  642. (module-use-interfaces!
  643. module
  644. (map resolve-interface '#+modules))
  645. ((@ (system base compile) compile)
  646. '#+snippet
  647. #:to 'value
  648. #:opts %auto-compilation-options
  649. #:env module))
  650. #~#t)))
  651. ;; Issue a warning unless the result is #t.
  652. (unless (eqv? result #t)
  653. (format (current-error-port) "\
  654. ## WARNING: the snippet returned `~s'. Return values other than #t
  655. ## are deprecated. Please migrate this package so that its snippet
  656. ## reports errors by raising an exception, and otherwise returns #t.~%"
  657. result))
  658. (unless result
  659. (error "snippet returned false")))
  660. (chdir "..")
  661. (unless tar-supports-sort?
  662. (call-with-output-file ".file_list"
  663. (lambda (port)
  664. (for-each (lambda (name)
  665. (format port "~a~%" name))
  666. (find-files directory
  667. #:directories? #t
  668. #:fail-on-error? #t)))))
  669. (apply invoke
  670. (string-append #+tar "/bin/tar")
  671. "cvfa" #$output
  672. ;; Avoid non-determinism in the archive. Set the mtime
  673. ;; to 1 as is the case in the store (software like gzip
  674. ;; behaves differently when it stumbles upon mtime = 0).
  675. "--mtime=@1"
  676. "--owner=root:0"
  677. "--group=root:0"
  678. (if tar-supports-sort?
  679. `("--sort=name"
  680. ,directory)
  681. '("--no-recursion"
  682. "--files-from=.file_list")))))))
  683. (let ((name (tarxz-name original-file-name)))
  684. (gexp->derivation name build
  685. #:graft? #f
  686. #:system system
  687. #:guile-for-build guile-for-build
  688. #:properties `((type . origin)
  689. (patches . ,(length patches)))))))
  690. (define (package-with-patches original patches)
  691. "Return package ORIGINAL with PATCHES applied."
  692. (package (inherit original)
  693. (source (origin (inherit (package-source original))
  694. (patches patches)))))
  695. (define (package-with-extra-patches original patches)
  696. "Return package ORIGINAL with all PATCHES appended to its list of patches."
  697. (package-with-patches original
  698. (append (origin-patches (package-source original))
  699. patches)))
  700. (define (transitive-inputs inputs)
  701. "Return the closure of INPUTS when considering the 'propagated-inputs'
  702. edges. Omit duplicate inputs, except for those already present in INPUTS
  703. itself.
  704. This is implemented as a breadth-first traversal such that INPUTS is
  705. preserved, and only duplicate propagated inputs are removed."
  706. (define (seen? seen item outputs)
  707. ;; FIXME: We're using pointer identity here, which is extremely sensitive
  708. ;; to memoization in package-producing procedures; see
  709. ;; <https://bugs.gnu.org/30155>.
  710. (match (vhash-assq item seen)
  711. ((_ . o) (equal? o outputs))
  712. (_ #f)))
  713. (let loop ((inputs inputs)
  714. (result '())
  715. (propagated '())
  716. (first? #t)
  717. (seen vlist-null))
  718. (match inputs
  719. (()
  720. (if (null? propagated)
  721. (reverse result)
  722. (loop (reverse (concatenate propagated)) result '() #f seen)))
  723. (((and input (label (? package? package) outputs ...)) rest ...)
  724. (if (and (not first?) (seen? seen package outputs))
  725. (loop rest result propagated first? seen)
  726. (loop rest
  727. (cons input result)
  728. (cons (package-propagated-inputs package) propagated)
  729. first?
  730. (vhash-consq package outputs seen))))
  731. ((input rest ...)
  732. (loop rest (cons input result) propagated first? seen)))))
  733. (define (package-direct-sources package)
  734. "Return all source origins associated with PACKAGE; including origins in
  735. PACKAGE's inputs."
  736. `(,@(or (and=> (package-source package) list) '())
  737. ,@(filter-map (match-lambda
  738. ((_ (? origin? orig) _ ...)
  739. orig)
  740. (_ #f))
  741. (package-direct-inputs package))))
  742. (define (package-transitive-sources package)
  743. "Return PACKAGE's direct sources, and their direct sources, recursively."
  744. (delete-duplicates
  745. (concatenate (filter-map (match-lambda
  746. ((_ (? origin? orig) _ ...)
  747. (list orig))
  748. ((_ (? package? p) _ ...)
  749. (package-direct-sources p))
  750. (_ #f))
  751. (bag-transitive-inputs
  752. (package->bag package))))))
  753. (define (package-direct-inputs package)
  754. "Return all the direct inputs of PACKAGE---i.e, its direct inputs along
  755. with their propagated inputs."
  756. (append (package-native-inputs package)
  757. (package-inputs package)
  758. (package-propagated-inputs package)))
  759. (define (package-transitive-inputs package)
  760. "Return the transitive inputs of PACKAGE---i.e., its direct inputs along
  761. with their propagated inputs, recursively."
  762. (transitive-inputs (package-direct-inputs package)))
  763. (define (package-transitive-target-inputs package)
  764. "Return the transitive target inputs of PACKAGE---i.e., its direct inputs
  765. along with their propagated inputs, recursively. This only includes inputs
  766. for the target system, and not native inputs."
  767. (transitive-inputs (append (package-inputs package)
  768. (package-propagated-inputs package))))
  769. (define (package-transitive-native-inputs package)
  770. "Return the transitive native inputs of PACKAGE---i.e., its direct inputs
  771. along with their propagated inputs, recursively. This only includes inputs
  772. for the host system (\"native inputs\"), and not target inputs."
  773. (transitive-inputs (package-native-inputs package)))
  774. (define (package-transitive-propagated-inputs package)
  775. "Return the propagated inputs of PACKAGE, and their propagated inputs,
  776. recursively."
  777. (transitive-inputs (package-propagated-inputs package)))
  778. (define (package-transitive-native-search-paths package)
  779. "Return the list of search paths for PACKAGE and its propagated inputs,
  780. recursively."
  781. (append (package-native-search-paths package)
  782. (append-map (match-lambda
  783. ((label (? package? p) _ ...)
  784. (package-native-search-paths p))
  785. (_
  786. '()))
  787. (package-transitive-propagated-inputs package))))
  788. (define (transitive-input-references alist inputs)
  789. "Return a list of (assoc-ref ALIST <label>) for each (<label> <package> . _)
  790. in INPUTS and their transitive propagated inputs."
  791. (define label
  792. (match-lambda
  793. ((label . _)
  794. label)))
  795. (map (lambda (input)
  796. `(assoc-ref ,alist ,(label input)))
  797. (transitive-inputs inputs)))
  798. (define package-transitive-supported-systems
  799. (let ()
  800. (define supported-systems
  801. (mlambda (package system)
  802. (parameterize ((%current-system system))
  803. (fold (lambda (input systems)
  804. (match input
  805. ((label (? package? package) . _)
  806. (lset-intersection string=? systems
  807. (supported-systems package system)))
  808. (_
  809. systems)))
  810. (package-supported-systems package)
  811. (bag-direct-inputs (package->bag package))))))
  812. (lambda* (package #:optional (system (%current-system)))
  813. "Return the intersection of the systems supported by PACKAGE and those
  814. supported by its dependencies."
  815. (supported-systems package system))))
  816. (define* (supported-package? package #:optional (system (%current-system)))
  817. "Return true if PACKAGE is supported on SYSTEM--i.e., if PACKAGE and all its
  818. dependencies are known to build on SYSTEM."
  819. (member system (package-transitive-supported-systems package system)))
  820. (define (bag-direct-inputs bag)
  821. "Same as 'package-direct-inputs', but applied to a bag."
  822. (append (bag-build-inputs bag)
  823. (bag-host-inputs bag)
  824. (bag-target-inputs bag)))
  825. (define (bag-transitive-inputs bag)
  826. "Same as 'package-transitive-inputs', but applied to a bag."
  827. (parameterize ((%current-target-system #f)
  828. (%current-system (bag-system bag)))
  829. (transitive-inputs (bag-direct-inputs bag))))
  830. (define (bag-transitive-build-inputs bag)
  831. "Same as 'package-transitive-native-inputs', but applied to a bag."
  832. (parameterize ((%current-target-system #f)
  833. (%current-system (bag-system bag)))
  834. (transitive-inputs (bag-build-inputs bag))))
  835. (define (bag-transitive-host-inputs bag)
  836. "Same as 'package-transitive-target-inputs', but applied to a bag."
  837. (parameterize ((%current-target-system (bag-target bag))
  838. (%current-system (bag-system bag)))
  839. (transitive-inputs (bag-host-inputs bag))))
  840. (define (bag-transitive-target-inputs bag)
  841. "Return the \"target inputs\" of BAG, recursively."
  842. (parameterize ((%current-target-system (bag-target bag))
  843. (%current-system (bag-system bag)))
  844. (transitive-inputs (bag-target-inputs bag))))
  845. (define* (package-closure packages #:key (system (%current-system)))
  846. "Return the closure of PACKAGES on SYSTEM--i.e., PACKAGES and the list of
  847. packages they depend on, recursively."
  848. (let loop ((packages packages)
  849. (visited vlist-null)
  850. (closure (list->setq packages)))
  851. (match packages
  852. (()
  853. (set->list closure))
  854. ((package . rest)
  855. (if (vhash-assq package visited)
  856. (loop rest visited closure)
  857. (let* ((bag (package->bag package system))
  858. (dependencies (filter-map (match-lambda
  859. ((label (? package? package) . _)
  860. package)
  861. (_ #f))
  862. (bag-direct-inputs bag))))
  863. (loop (append dependencies rest)
  864. (vhash-consq package #t visited)
  865. (fold set-insert closure dependencies))))))))
  866. (define (build-system-with-package-mapping bs rewrite)
  867. "Return a variant of BS, a build system, that rewrites a bag's inputs by
  868. passing them through REWRITE, a procedure that takes an input tuplet and
  869. returns a \"rewritten\" input tuplet."
  870. (define lower
  871. (build-system-lower bs))
  872. (define (lower* . args)
  873. (let ((lowered (apply lower args)))
  874. (bag
  875. (inherit lowered)
  876. (build-inputs (map rewrite (bag-build-inputs lowered)))
  877. (host-inputs (map rewrite (bag-host-inputs lowered)))
  878. (target-inputs (map rewrite (bag-target-inputs lowered))))))
  879. (build-system
  880. (inherit bs)
  881. (lower lower*)))
  882. (define* (package-mapping proc #:optional (cut? (const #f))
  883. #:key deep?)
  884. "Return a procedure that, given a package, applies PROC to all the packages
  885. depended on and returns the resulting package. The procedure stops recursion
  886. when CUT? returns true for a given package. When DEEP? is true, PROC is
  887. applied to implicit inputs as well."
  888. (define (rewrite input)
  889. (match input
  890. ((label (? package? package) outputs ...)
  891. (let ((proc (if (cut? package) proc replace)))
  892. (cons* label (proc package) outputs)))
  893. (_
  894. input)))
  895. (define mapping-property
  896. ;; Property indicating whether the package has already been processed.
  897. (gensym " package-mapping-done"))
  898. (define replace
  899. (mlambdaq (p)
  900. ;; If P is the result of a previous call, return it.
  901. (if (assq-ref (package-properties p) mapping-property)
  902. p
  903. ;; Return a variant of P with PROC applied to P and its explicit
  904. ;; dependencies, recursively. Memoize the transformations. Failing
  905. ;; to do that, we would build a huge object graph with lots of
  906. ;; duplicates, which in turns prevents us from benefiting from
  907. ;; memoization in 'package-derivation'.
  908. (let ((p (proc p)))
  909. (package
  910. (inherit p)
  911. (location (package-location p))
  912. (build-system (if deep?
  913. (build-system-with-package-mapping
  914. (package-build-system p) rewrite)
  915. (package-build-system p)))
  916. (inputs (map rewrite (package-inputs p)))
  917. (native-inputs (map rewrite (package-native-inputs p)))
  918. (propagated-inputs (map rewrite (package-propagated-inputs p)))
  919. (replacement (and=> (package-replacement p) replace))
  920. (properties `((,mapping-property . #t)
  921. ,@(package-properties p))))))))
  922. replace)
  923. (define* (package-input-rewriting replacements
  924. #:optional (rewrite-name identity)
  925. #:key (deep? #t))
  926. "Return a procedure that, when passed a package, replaces its direct and
  927. indirect dependencies, including implicit inputs when DEEP? is true, according
  928. to REPLACEMENTS. REPLACEMENTS is a list of package pairs; the first element
  929. of each pair is the package to replace, and the second one is the replacement.
  930. Optionally, REWRITE-NAME is a one-argument procedure that takes the name of a
  931. package and returns its new name after rewrite."
  932. (define replacement-property
  933. ;; Property to tag right-hand sides in REPLACEMENTS.
  934. (gensym " package-replacement"))
  935. (define (rewrite p)
  936. (if (assq-ref (package-properties p) replacement-property)
  937. p
  938. (match (assq-ref replacements p)
  939. (#f (package/inherit p
  940. (name (rewrite-name (package-name p)))))
  941. (new (if deep?
  942. (package/inherit new
  943. (properties `((,replacement-property . #t)
  944. ,@(package-properties new))))
  945. new)))))
  946. (define (cut? p)
  947. (or (assq-ref (package-properties p) replacement-property)
  948. (assq-ref replacements p)))
  949. (package-mapping rewrite cut?
  950. #:deep? deep?))
  951. (define* (package-input-rewriting/spec replacements #:key (deep? #t))
  952. "Return a procedure that, given a package, applies the given REPLACEMENTS to
  953. all the package graph, including implicit inputs unless DEEP? is false.
  954. REPLACEMENTS is a list of spec/procedures pair; each spec is a package
  955. specification such as \"gcc\" or \"guile@2\", and each procedure takes a
  956. matching package and returns a replacement for that package."
  957. (define table
  958. (fold (lambda (replacement table)
  959. (match replacement
  960. ((spec . proc)
  961. (let-values (((name version)
  962. (package-name->name+version spec)))
  963. (vhash-cons name (list version proc) table)))))
  964. vlist-null
  965. replacements))
  966. (define (find-replacement package)
  967. (vhash-fold* (lambda (item proc)
  968. (or proc
  969. (match item
  970. ((#f proc)
  971. proc)
  972. ((version proc)
  973. (and (version-prefix? version
  974. (package-version package))
  975. proc)))))
  976. #f
  977. (package-name package)
  978. table))
  979. (define replacement-property
  980. (gensym " package-replacement"))
  981. (define (rewrite p)
  982. (if (assq-ref (package-properties p) replacement-property)
  983. p
  984. (match (find-replacement p)
  985. (#f p)
  986. (proc
  987. (let ((new (proc p)))
  988. ;; Mark NEW as already processed.
  989. (package/inherit new
  990. (properties `((,replacement-property . #t)
  991. ,@(package-properties new)))))))))
  992. (define (cut? p)
  993. (or (assq-ref (package-properties p) replacement-property)
  994. (find-replacement p)))
  995. (package-mapping rewrite cut?
  996. #:deep? deep?))
  997. ;;;
  998. ;;; Package derivations.
  999. ;;;
  1000. (define %derivation-cache
  1001. ;; Package to derivation-path mapping.
  1002. (make-weak-key-hash-table 100))
  1003. (define (cache! cache package system thunk)
  1004. "Memoize in CACHE the return values of THUNK as the derivation of PACKAGE on
  1005. SYSTEM."
  1006. ;; FIXME: This memoization should be associated with the open store, because
  1007. ;; otherwise it breaks when switching to a different store.
  1008. (let ((result (thunk)))
  1009. ;; Use `hashq-set!' instead of `hash-set!' because `hash' returns the
  1010. ;; same value for all structs (as of Guile 2.0.6), and because pointer
  1011. ;; equality is sufficient in practice.
  1012. (hashq-set! cache package
  1013. `((,system . ,result)
  1014. ,@(or (hashq-ref cache package) '())))
  1015. result))
  1016. (define-syntax cached
  1017. (syntax-rules (=>)
  1018. "Memoize the result of BODY for the arguments PACKAGE and SYSTEM.
  1019. Return the cached result when available."
  1020. ((_ (=> cache) package system body ...)
  1021. (let ((thunk (lambda () body ...))
  1022. (key system))
  1023. (match (hashq-ref cache package)
  1024. ((alist (... ...))
  1025. (match (assoc-ref alist key)
  1026. (#f (cache! cache package key thunk))
  1027. (value value)))
  1028. (#f
  1029. (cache! cache package key thunk)))))
  1030. ((_ package system body ...)
  1031. (cached (=> %derivation-cache) package system body ...))))
  1032. (define* (expand-input store package input system #:optional cross-system)
  1033. "Expand INPUT, an input tuple, such that it contains only references to
  1034. derivation paths or store paths. PACKAGE is only used to provide contextual
  1035. information in exceptions."
  1036. (define (intern file)
  1037. ;; Add FILE to the store. Set the `recursive?' bit to #t, so that
  1038. ;; file permissions are preserved.
  1039. (add-to-store store (basename file) #t "sha256" file))
  1040. (define derivation
  1041. (if cross-system
  1042. (cut package-cross-derivation store <> cross-system system
  1043. #:graft? #f)
  1044. (cut package-derivation store <> system #:graft? #f)))
  1045. (match input
  1046. (((? string? name) (? package? package))
  1047. (list name (derivation package)))
  1048. (((? string? name) (? package? package)
  1049. (? string? sub-drv))
  1050. (list name (derivation package)
  1051. sub-drv))
  1052. (((? string? name)
  1053. (and (? string?) (? derivation-path?) drv))
  1054. (list name drv))
  1055. (((? string? name)
  1056. (and (? string?) (? file-exists? file)))
  1057. ;; Add FILE to the store. When FILE is in the sub-directory of a
  1058. ;; store path, it needs to be added anyway, so it can be used as a
  1059. ;; source.
  1060. (list name (intern file)))
  1061. (((? string? name) (? struct? source))
  1062. ;; 'package-source-derivation' calls 'lower-object', which can throw
  1063. ;; '&gexp-input-error'. However '&gexp-input-error' lacks source
  1064. ;; location info, so we catch and rethrow here (XXX: not optimal
  1065. ;; performance-wise).
  1066. (guard (c ((gexp-input-error? c)
  1067. (raise (condition
  1068. (&package-input-error
  1069. (package package)
  1070. (input (gexp-error-invalid-input c)))))))
  1071. (list name (package-source-derivation store source system))))
  1072. (x
  1073. (raise (condition (&package-input-error
  1074. (package package)
  1075. (input x)))))))
  1076. (define %bag-cache
  1077. ;; 'eq?' cache mapping packages to system+target+graft?-dependent bags.
  1078. ;; It significantly speeds things up when doing repeated calls to
  1079. ;; 'package->bag' as is the case when building a profile.
  1080. (make-weak-key-hash-table 200))
  1081. (define* (package->bag package #:optional
  1082. (system (%current-system))
  1083. (target (%current-target-system))
  1084. #:key (graft? (%graft?)))
  1085. "Compile PACKAGE into a bag for SYSTEM, possibly cross-compiled to TARGET,
  1086. and return it."
  1087. (let ((package (or (and graft? (package-replacement package))
  1088. package)))
  1089. (cached (=> %bag-cache)
  1090. package (list system target)
  1091. ;; Bind %CURRENT-SYSTEM and %CURRENT-TARGET-SYSTEM so that thunked
  1092. ;; field values can refer to it.
  1093. (parameterize ((%current-system system)
  1094. (%current-target-system target))
  1095. (match package
  1096. ((and self
  1097. ($ <package> name version source build-system
  1098. args inputs propagated-inputs native-inputs
  1099. outputs))
  1100. ;; Even though we prefer to use "@" to separate the package
  1101. ;; name from the package version in various user-facing parts
  1102. ;; of Guix, checkStoreName (in nix/libstore/store-api.cc)
  1103. ;; prohibits the use of "@", so use "-" instead.
  1104. (or (make-bag build-system (string-append name "-" version)
  1105. #:system system
  1106. #:target target
  1107. #:source source
  1108. #:inputs (append (inputs self)
  1109. (propagated-inputs self))
  1110. #:outputs outputs
  1111. #:native-inputs (native-inputs self)
  1112. #:arguments (args self))
  1113. (raise (if target
  1114. (condition
  1115. (&package-cross-build-system-error
  1116. (package package)))
  1117. (condition
  1118. (&package-error
  1119. (package package))))))))))))
  1120. (define %graft-cache
  1121. ;; 'eq?' cache mapping package objects to a graft corresponding to their
  1122. ;; replacement package.
  1123. (make-weak-key-hash-table 200))
  1124. (define (input-graft store system)
  1125. "Return a procedure that, given a package with a replacement and an output name,
  1126. returns a graft, and #f otherwise."
  1127. (match-lambda*
  1128. (((? package? package) output)
  1129. (let ((replacement (package-replacement package)))
  1130. (and replacement
  1131. (cached (=> %graft-cache) package (cons output system)
  1132. (let ((orig (package-derivation store package system
  1133. #:graft? #f))
  1134. (new (package-derivation store replacement system
  1135. #:graft? #t)))
  1136. (graft
  1137. (origin orig)
  1138. (origin-output output)
  1139. (replacement new)
  1140. (replacement-output output)))))))))
  1141. (define (input-cross-graft store target system)
  1142. "Same as 'input-graft', but for cross-compilation inputs."
  1143. (match-lambda*
  1144. (((? package? package) output)
  1145. (let ((replacement (package-replacement package)))
  1146. (and replacement
  1147. (let ((orig (package-cross-derivation store package target system
  1148. #:graft? #f))
  1149. (new (package-cross-derivation store replacement
  1150. target system
  1151. #:graft? #t)))
  1152. (graft
  1153. (origin orig)
  1154. (origin-output output)
  1155. (replacement new)
  1156. (replacement-output output))))))))
  1157. (define* (fold-bag-dependencies proc seed bag
  1158. #:key (native? #t))
  1159. "Fold PROC over the packages BAG depends on. Each package is visited only
  1160. once, in depth-first order. If NATIVE? is true, restrict to native
  1161. dependencies; otherwise, restrict to target dependencies."
  1162. (define bag-direct-inputs*
  1163. (if native?
  1164. (lambda (bag)
  1165. (append (bag-build-inputs bag)
  1166. (bag-target-inputs bag)
  1167. (if (bag-target bag)
  1168. '()
  1169. (bag-host-inputs bag))))
  1170. bag-host-inputs))
  1171. (let loop ((inputs (bag-direct-inputs* bag))
  1172. (result seed)
  1173. (visited vlist-null))
  1174. (match inputs
  1175. (()
  1176. result)
  1177. (((label (? package? head) . rest) . tail)
  1178. (let ((output (match rest (() "out") ((output) output)))
  1179. (outputs (vhash-foldq* cons '() head visited)))
  1180. (if (member output outputs)
  1181. (loop tail result visited)
  1182. (let ((inputs (bag-direct-inputs* (package->bag head))))
  1183. (loop (append inputs tail)
  1184. (proc head output result)
  1185. (vhash-consq head output visited))))))
  1186. ((head . tail)
  1187. (loop tail result visited)))))
  1188. (define* (bag-grafts store bag)
  1189. "Return the list of grafts potentially applicable to BAG. Potentially
  1190. applicable grafts are collected by looking at direct or indirect dependencies
  1191. of BAG that have a 'replacement'. Whether a graft is actually applicable
  1192. depends on whether the outputs of BAG depend on the items the grafts refer
  1193. to (see 'graft-derivation'.)"
  1194. (define system (bag-system bag))
  1195. (define target (bag-target bag))
  1196. (define native-grafts
  1197. (let ((->graft (input-graft store system)))
  1198. (parameterize ((%current-system system)
  1199. (%current-target-system #f))
  1200. (fold-bag-dependencies (lambda (package output grafts)
  1201. (match (->graft package output)
  1202. (#f grafts)
  1203. (graft (cons graft grafts))))
  1204. '()
  1205. bag))))
  1206. (define target-grafts
  1207. (if target
  1208. (let ((->graft (input-cross-graft store target system)))
  1209. (parameterize ((%current-system system)
  1210. (%current-target-system target))
  1211. (fold-bag-dependencies (lambda (package output grafts)
  1212. (match (->graft package output)
  1213. (#f grafts)
  1214. (graft (cons graft grafts))))
  1215. '()
  1216. bag
  1217. #:native? #f)))
  1218. '()))
  1219. ;; We can end up with several identical grafts if we stumble upon packages
  1220. ;; that are not 'eq?' but map to the same derivation (this can happen when
  1221. ;; using things like 'package-with-explicit-inputs'.) Hence the
  1222. ;; 'delete-duplicates' call.
  1223. (delete-duplicates
  1224. (append native-grafts target-grafts)))
  1225. (define* (package-grafts store package
  1226. #:optional (system (%current-system))
  1227. #:key target)
  1228. "Return the list of grafts applicable to PACKAGE as built for SYSTEM and
  1229. TARGET."
  1230. (let* ((package (or (package-replacement package) package))
  1231. (bag (package->bag package system target)))
  1232. (bag-grafts store bag)))
  1233. (define* (bag->derivation store bag
  1234. #:optional context)
  1235. "Return the derivation to build BAG for SYSTEM. Optionally, CONTEXT can be
  1236. a package object describing the context in which the call occurs, for improved
  1237. error reporting."
  1238. (if (bag-target bag)
  1239. (bag->cross-derivation store bag)
  1240. (let* ((system (bag-system bag))
  1241. (inputs (bag-transitive-inputs bag))
  1242. (input-drvs (map (cut expand-input store context <> system)
  1243. inputs))
  1244. (paths (delete-duplicates
  1245. (append-map (match-lambda
  1246. ((_ (? package? p) _ ...)
  1247. (package-native-search-paths
  1248. p))
  1249. (_ '()))
  1250. inputs))))
  1251. (apply (bag-build bag)
  1252. store (bag-name bag) input-drvs
  1253. #:search-paths paths
  1254. #:outputs (bag-outputs bag) #:system system
  1255. (bag-arguments bag)))))
  1256. (define* (bag->cross-derivation store bag
  1257. #:optional context)
  1258. "Return the derivation to build BAG, which is actually a cross build.
  1259. Optionally, CONTEXT can be a package object denoting the context of the call.
  1260. This is an internal procedure."
  1261. (let* ((system (bag-system bag))
  1262. (target (bag-target bag))
  1263. (host (bag-transitive-host-inputs bag))
  1264. (host-drvs (map (cut expand-input store context <> system target)
  1265. host))
  1266. (target* (bag-transitive-target-inputs bag))
  1267. (target-drvs (map (cut expand-input store context <> system)
  1268. target*))
  1269. (build (bag-transitive-build-inputs bag))
  1270. (build-drvs (map (cut expand-input store context <> system)
  1271. build))
  1272. (all (append build target* host))
  1273. (paths (delete-duplicates
  1274. (append-map (match-lambda
  1275. ((_ (? package? p) _ ...)
  1276. (package-search-paths p))
  1277. (_ '()))
  1278. all)))
  1279. (npaths (delete-duplicates
  1280. (append-map (match-lambda
  1281. ((_ (? package? p) _ ...)
  1282. (package-native-search-paths
  1283. p))
  1284. (_ '()))
  1285. all))))
  1286. (apply (bag-build bag)
  1287. store (bag-name bag)
  1288. #:native-drvs build-drvs
  1289. #:target-drvs (append host-drvs target-drvs)
  1290. #:search-paths paths
  1291. #:native-search-paths npaths
  1292. #:outputs (bag-outputs bag)
  1293. #:system system #:target target
  1294. (bag-arguments bag))))
  1295. (define* (package-derivation store package
  1296. #:optional (system (%current-system))
  1297. #:key (graft? (%graft?)))
  1298. "Return the <derivation> object of PACKAGE for SYSTEM."
  1299. ;; Compute the derivation and cache the result. Caching is important
  1300. ;; because some derivations, such as the implicit inputs of the GNU build
  1301. ;; system, will be queried many, many times in a row.
  1302. (cached package (cons system graft?)
  1303. (let* ((bag (package->bag package system #f #:graft? graft?))
  1304. (drv (bag->derivation store bag package)))
  1305. (if graft?
  1306. (match (bag-grafts store bag)
  1307. (()
  1308. drv)
  1309. (grafts
  1310. (let ((guile (package-derivation store (guile-for-grafts)
  1311. system #:graft? #f)))
  1312. ;; TODO: As an optimization, we can simply graft the tip
  1313. ;; of the derivation graph since 'graft-derivation'
  1314. ;; recurses anyway.
  1315. (graft-derivation store drv grafts
  1316. #:system system
  1317. #:guile guile))))
  1318. drv))))
  1319. (define* (package-cross-derivation store package target
  1320. #:optional (system (%current-system))
  1321. #:key (graft? (%graft?)))
  1322. "Cross-build PACKAGE for TARGET (a GNU triplet) from host SYSTEM (a Guix
  1323. system identifying string)."
  1324. (cached package (list system target graft?)
  1325. (let* ((bag (package->bag package system target #:graft? graft?))
  1326. (drv (bag->derivation store bag package)))
  1327. (if graft?
  1328. (match (bag-grafts store bag)
  1329. (()
  1330. drv)
  1331. (grafts
  1332. (graft-derivation store drv grafts
  1333. #:system system
  1334. #:guile
  1335. (package-derivation store (guile-for-grafts)
  1336. system #:graft? #f))))
  1337. drv))))
  1338. (define* (package-output store package
  1339. #:optional (output "out") (system (%current-system)))
  1340. "Return the output path of PACKAGE's OUTPUT for SYSTEM---where OUTPUT is the
  1341. symbolic output name, such as \"out\". Note that this procedure calls
  1342. `package-derivation', which is costly."
  1343. (let ((drv (package-derivation store package system)))
  1344. (derivation->output-path drv output)))
  1345. ;;;
  1346. ;;; Monadic interface.
  1347. ;;;
  1348. (define (set-guile-for-build guile)
  1349. "This monadic procedure changes the Guile currently used to run the build
  1350. code of derivations to GUILE, a package object."
  1351. (lambda (store)
  1352. (let ((guile (package-derivation store guile)))
  1353. (values (%guile-for-build guile) store))))
  1354. (define* (package-file package
  1355. #:optional file
  1356. #:key
  1357. system (output "out") target)
  1358. "Return as a monadic value the absolute file name of FILE within the
  1359. OUTPUT directory of PACKAGE. When FILE is omitted, return the name of the
  1360. OUTPUT directory of PACKAGE. When TARGET is true, use it as a
  1361. cross-compilation target triplet.
  1362. Note that this procedure does _not_ build PACKAGE. Thus, the result might or
  1363. might not designate an existing file. We recommend not using this procedure
  1364. unless you know what you are doing."
  1365. (lambda (store)
  1366. (define compute-derivation
  1367. (if target
  1368. (cut package-cross-derivation <> <> target <>)
  1369. package-derivation))
  1370. (let* ((system (or system (%current-system)))
  1371. (drv (compute-derivation store package system))
  1372. (out (derivation->output-path drv output)))
  1373. (values (if file
  1374. (string-append out "/" file)
  1375. out)
  1376. store))))
  1377. (define package->derivation
  1378. (store-lift package-derivation))
  1379. (define package->cross-derivation
  1380. (store-lift package-cross-derivation))
  1381. (define-gexp-compiler (package-compiler (package <package>) system target)
  1382. ;; Compile PACKAGE to a derivation for SYSTEM, optionally cross-compiled for
  1383. ;; TARGET. This is used when referring to a package from within a gexp.
  1384. (if target
  1385. (package->cross-derivation package target system)
  1386. (package->derivation package system)))
  1387. (define* (origin->derivation origin
  1388. #:optional (system (%current-system)))
  1389. "Return the derivation corresponding to ORIGIN."
  1390. (match origin
  1391. (($ <origin> uri method hash name (= force ()) #f)
  1392. ;; No patches, no snippet: this is a fixed-output derivation.
  1393. (method uri
  1394. (content-hash-algorithm hash)
  1395. (content-hash-value hash)
  1396. name #:system system))
  1397. (($ <origin> uri method hash name (= force (patches ...)) snippet
  1398. (flags ...) inputs (modules ...) guile-for-build)
  1399. ;; Patches and/or a snippet.
  1400. (mlet %store-monad ((source (method uri
  1401. (content-hash-algorithm hash)
  1402. (content-hash-value hash)
  1403. name #:system system))
  1404. (guile (package->derivation (or guile-for-build
  1405. (default-guile))
  1406. system
  1407. #:graft? #f)))
  1408. (patch-and-repack source patches
  1409. #:inputs inputs
  1410. #:snippet snippet
  1411. #:flags flags
  1412. #:system system
  1413. #:modules modules
  1414. #:guile-for-build guile)))))
  1415. (define-gexp-compiler (origin-compiler (origin <origin>) system target)
  1416. ;; Compile ORIGIN to a derivation for SYSTEM. This is used when referring
  1417. ;; to an origin from within a gexp.
  1418. (origin->derivation origin system))
  1419. (define package-source-derivation ;somewhat deprecated
  1420. (let ((lower (store-lower lower-object)))
  1421. (lambda* (store source #:optional (system (%current-system)))
  1422. "Return the derivation or file corresponding to SOURCE, which can be an
  1423. a file name or any object handled by 'lower-object', such as an <origin>.
  1424. When SOURCE is a file name, return either the interned file name (if SOURCE is
  1425. outside of the store) or SOURCE itself (if SOURCE is already a store item.)"
  1426. (match source
  1427. ((and (? string?) (? direct-store-path?) file)
  1428. file)
  1429. ((? string? file)
  1430. (add-to-store store (basename file) #t "sha256" file))
  1431. (_
  1432. (lower store source system))))))