packages.scm 85 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2012-2023 Ludovic Courtès <ludo@gnu.org>
  3. ;;; Copyright © 2014, 2015, 2017, 2018, 2019 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, 2022 Efraim Flashner <efraim@flashner.co.il>
  7. ;;; Copyright © 2019 Marius Bakke <mbakke@fastmail.com>
  8. ;;; Copyright © 2020, 2021 Maxim Cournoyer <maxim.cournoyer@gmail.com>
  9. ;;; Copyright © 2021 Chris Marusich <cmmarusich@gmail.com>
  10. ;;; Copyright © 2022 Maxime Devos <maximedevos@telenet.be>
  11. ;;; Copyright © 2022 jgart <jgart@dismail.de>
  12. ;;; Copyright © 2023 Simon Tournier <zimon.toutoune@gmail.com>
  13. ;;;
  14. ;;; This file is part of GNU Guix.
  15. ;;;
  16. ;;; GNU Guix is free software; you can redistribute it and/or modify it
  17. ;;; under the terms of the GNU General Public License as published by
  18. ;;; the Free Software Foundation; either version 3 of the License, or (at
  19. ;;; your option) any later version.
  20. ;;;
  21. ;;; GNU Guix is distributed in the hope that it will be useful, but
  22. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  23. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  24. ;;; GNU General Public License for more details.
  25. ;;;
  26. ;;; You should have received a copy of the GNU General Public License
  27. ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
  28. (define-module (guix packages)
  29. #:use-module ((guix build utils) #:select (compressor tarball?
  30. strip-store-file-name))
  31. #:use-module (guix utils)
  32. #:use-module (guix records)
  33. #:use-module (guix store)
  34. #:use-module (guix monads)
  35. #:use-module (guix gexp)
  36. #:use-module (guix base32)
  37. #:autoload (guix base64) (base64-decode)
  38. #:use-module (guix grafts)
  39. #:use-module (guix derivations)
  40. #:use-module (guix memoization)
  41. #:use-module (guix build-system)
  42. #:use-module (guix search-paths)
  43. #:use-module (guix sets)
  44. #:use-module (guix deprecation)
  45. #:use-module ((guix diagnostics)
  46. #:select (formatted-message define-with-syntax-properties))
  47. #:autoload (guix licenses) (license?)
  48. #:use-module (guix i18n)
  49. #:use-module (ice-9 match)
  50. #:use-module (ice-9 vlist)
  51. #:use-module (ice-9 regex)
  52. #:use-module (srfi srfi-1)
  53. #:use-module (srfi srfi-9 gnu)
  54. #:use-module (srfi srfi-26)
  55. #:use-module (srfi srfi-34)
  56. #:use-module (srfi srfi-35)
  57. #:use-module (srfi srfi-71)
  58. #:use-module (rnrs bytevectors)
  59. #:use-module (web uri)
  60. #:autoload (texinfo) (texi-fragment->stexi)
  61. #:re-export (%current-system
  62. %current-target-system
  63. search-path-specification) ;for convenience
  64. #:replace ((define-public* . define-public))
  65. #:export (content-hash
  66. content-hash?
  67. content-hash-algorithm
  68. content-hash-value
  69. origin
  70. origin?
  71. this-origin
  72. origin-uri
  73. origin-method
  74. origin-hash
  75. origin-file-name
  76. origin-actual-file-name
  77. origin-patches
  78. origin-patch-flags
  79. origin-patch-inputs
  80. origin-patch-guile
  81. origin-snippet
  82. origin-modules
  83. base32
  84. base64
  85. package
  86. package?
  87. this-package
  88. package-name
  89. package-upstream-name
  90. package-upstream-name*
  91. package-version
  92. package-full-name
  93. package-source
  94. package-build-system
  95. package-arguments
  96. package-inputs
  97. package-native-inputs
  98. package-propagated-inputs
  99. package-outputs
  100. package-native-search-paths
  101. package-search-paths
  102. package-replacement
  103. package-synopsis
  104. package-description
  105. package-license
  106. package-home-page
  107. package-supported-systems
  108. package-properties
  109. package-location
  110. package-definition-location
  111. hidden-package
  112. hidden-package?
  113. package-superseded
  114. deprecated-package
  115. package-field-location
  116. this-package-input
  117. this-package-native-input
  118. lookup-package-input
  119. lookup-package-native-input
  120. lookup-package-propagated-input
  121. lookup-package-direct-input
  122. prepend ;syntactic keyword
  123. replace ;syntactic keyword
  124. modify-inputs
  125. add-input-label ; g23
  126. replace-input ; g23
  127. package-direct-sources
  128. package-transitive-sources
  129. package-direct-inputs
  130. package-transitive-inputs
  131. package-transitive-target-inputs
  132. package-transitive-native-inputs
  133. package-transitive-propagated-inputs
  134. package-transitive-native-search-paths
  135. package-transitive-supported-systems
  136. package-mapping
  137. package-input-rewriting
  138. package-input-rewriting/spec
  139. package-source-derivation
  140. package-derivation
  141. package-cross-derivation
  142. package-output
  143. package-grafts
  144. package-patched-vulnerabilities
  145. package-with-patches
  146. package-with-extra-patches
  147. package-with-c-toolchain
  148. package/inherit
  149. transitive-input-references
  150. %32bit-supported-systems
  151. %64bit-supported-systems
  152. %supported-systems
  153. %hurd-systems
  154. %cuirass-supported-systems
  155. supported-package?
  156. &package-error
  157. package-error?
  158. package-error-package
  159. package-license-error?
  160. package-error-invalid-license
  161. &package-input-error
  162. package-input-error?
  163. package-error-invalid-input
  164. &package-cross-build-system-error
  165. package-cross-build-system-error?
  166. package->bag
  167. bag->derivation
  168. bag-direct-inputs
  169. bag-transitive-inputs
  170. bag-transitive-host-inputs
  171. bag-transitive-build-inputs
  172. bag-transitive-target-inputs
  173. package-development-inputs
  174. package-closure
  175. default-guile
  176. guile-for-grafts
  177. default-guile-derivation
  178. set-guile-for-build
  179. package-file
  180. package->derivation
  181. package->cross-derivation
  182. origin->derivation))
  183. ;; The 'source-module-closure' procedure ca. 1.2.0 did not recognize
  184. ;; #:re-export-and-replace: <https://issues.guix.gnu.org/52694>.
  185. ;; Work around it. The #:replace? argument is only supported by
  186. ;; Guile 2.2.7 and later, work-around it if necessary to allow
  187. ;; time-travel from 1.1.0, see <https://issues.guix.gnu.org/53765>.
  188. (let ((major (string->number (major-version))))
  189. (if (or (>= major 3)
  190. (and (= major 2)
  191. (= (string->number (minor-version)) 2) ; there is no Guile 2.3.X
  192. (>= (string->number (micro-version)) 7)))
  193. (module-re-export! (current-module) '(delete) #:replace? #t)
  194. (module-re-export! (current-module) '(delete))))
  195. ;;; Commentary:
  196. ;;;
  197. ;;; This module provides a high-level mechanism to define packages in a
  198. ;;; Guix-based distribution.
  199. ;;;
  200. ;;; Code:
  201. (define-syntax-rule (define-compile-time-decoder name string->bytevector)
  202. "Define NAME as a macro that runs STRING->BYTEVECTOR at macro expansion time
  203. if possible."
  204. (define-syntax name
  205. (lambda (s)
  206. "Return the bytevector corresponding to the given textual
  207. representation."
  208. (syntax-case s ()
  209. ((_ str)
  210. (string? (syntax->datum #'str))
  211. ;; A literal string: do the conversion at expansion time.
  212. (with-syntax ((bv (string->bytevector (syntax->datum #'str))))
  213. #''bv))
  214. ((_ str)
  215. #'(string->bytevector str))))))
  216. (define-compile-time-decoder base32 nix-base32-string->bytevector)
  217. (define-compile-time-decoder base64 base64-decode)
  218. ;; Crytographic content hash.
  219. (define-immutable-record-type <content-hash>
  220. (%content-hash algorithm value)
  221. content-hash?
  222. (algorithm content-hash-algorithm) ;symbol
  223. (value content-hash-value)) ;bytevector
  224. (define-syntax-rule (define-content-hash-constructor name
  225. (algorithm size) ...)
  226. "Define NAME as a <content-hash> constructor that ensures that (1) its
  227. second argument is among the listed ALGORITHM, and (2), when possible, that
  228. its first argument has the right size for the chosen algorithm."
  229. (define-syntax name
  230. (lambda (s)
  231. (syntax-case s (algorithm ...)
  232. ((_ bv algorithm)
  233. (let ((bv* (syntax->datum #'bv)))
  234. (when (and (bytevector? bv*)
  235. (not (= size (bytevector-length bv*))))
  236. (syntax-violation 'content-hash "invalid content hash length" s))
  237. #'(%content-hash 'algorithm bv)))
  238. ...))))
  239. (define-content-hash-constructor build-content-hash
  240. (sha256 32)
  241. (sha512 64)
  242. (sha3-256 32)
  243. (sha3-512 64)
  244. (blake2s-256 64))
  245. (define-syntax content-hash
  246. (lambda (s)
  247. "Return a content hash with the given parameters. The default hash
  248. algorithm is sha256. If the first argument is a literal string, it is decoded
  249. as base32. Otherwise, it must be a bytevector."
  250. ;; What we'd really want here is something like C++ 'constexpr'.
  251. (syntax-case s ()
  252. ((_ str)
  253. (string? (syntax->datum #'str))
  254. #'(content-hash str sha256))
  255. ((_ str algorithm)
  256. (string? (syntax->datum #'str))
  257. (with-syntax ((bv (base32 (syntax->datum #'str))))
  258. #'(content-hash bv algorithm)))
  259. ((_ (id str) algorithm)
  260. (and (string? (syntax->datum #'str))
  261. (free-identifier=? #'id #'base32))
  262. (with-syntax ((bv (nix-base32-string->bytevector (syntax->datum #'str))))
  263. #'(content-hash bv algorithm)))
  264. ((_ (id str) algorithm)
  265. (and (string? (syntax->datum #'str))
  266. (free-identifier=? #'id #'base64))
  267. (with-syntax ((bv (base64-decode (syntax->datum #'str))))
  268. #'(content-hash bv algorithm)))
  269. ((_ bv)
  270. #'(content-hash bv sha256))
  271. ((_ bv hash)
  272. #'(build-content-hash bv hash)))))
  273. (define (print-content-hash hash port)
  274. (format port "#<content-hash ~a:~a>"
  275. (content-hash-algorithm hash)
  276. (and=> (content-hash-value hash)
  277. bytevector->nix-base32-string)))
  278. (set-record-type-printer! <content-hash> print-content-hash)
  279. ;; The source of a package, such as a tarball URL and fetcher---called
  280. ;; "origin" to avoid name clash with `package-source', `source', etc.
  281. (define-record-type* <origin>
  282. %origin make-origin
  283. origin?
  284. this-origin
  285. (uri origin-uri) ; string
  286. (method origin-method) ; procedure
  287. (hash origin-hash) ; <content-hash>
  288. (file-name origin-file-name (default #f)) ; optional file name
  289. ;; Patches are delayed so that the 'search-patch' calls are made lazily,
  290. ;; which reduces I/O on startup and allows patch-not-found errors to be
  291. ;; gracefully handled at run time.
  292. (patches origin-patches ; list of file names
  293. (default '()) (delayed))
  294. (snippet origin-snippet (default #f)) ; sexp or #f
  295. (patch-flags origin-patch-flags ; string-list gexp
  296. (default %default-patch-flags))
  297. ;; Patching requires Guile, GNU Patch, and a few more. These two fields are
  298. ;; used to specify these dependencies when needed.
  299. (patch-inputs origin-patch-inputs ; input list or #f
  300. (default #f))
  301. (modules origin-modules ; list of module names
  302. (default '()))
  303. (patch-guile origin-patch-guile ; package or #f
  304. (default #f)))
  305. (define-syntax origin-compatibility-helper
  306. (syntax-rules (sha256)
  307. ((_ () (fields ...))
  308. (%origin fields ...))
  309. ((_ ((sha256 exp) rest ...) (others ...))
  310. (%origin others ...
  311. (hash (content-hash exp sha256))
  312. rest ...))
  313. ((_ (field rest ...) (others ...))
  314. (origin-compatibility-helper (rest ...)
  315. (others ... field)))))
  316. (define-syntax-rule (origin fields ...)
  317. "Build an <origin> record, automatically converting 'sha256' field
  318. specifications to 'hash'."
  319. (origin-compatibility-helper (fields ...) ()))
  320. (define (print-origin origin port)
  321. "Write a concise representation of ORIGIN to PORT."
  322. (match origin
  323. (($ <origin> uri method hash file-name patches)
  324. (simple-format port "#<origin ~s ~a ~s ~a>"
  325. uri hash
  326. (force patches)
  327. (number->string (object-address origin) 16)))))
  328. (set-record-type-printer! <origin> print-origin)
  329. (define %default-patch-flags
  330. #~("-p1"))
  331. (define (origin-actual-file-name origin)
  332. "Return the file name of ORIGIN, either its 'file-name' field or the file
  333. name of its URI."
  334. (define (uri->file-name uri)
  335. ;; Return the 'base name' of URI or URI itself, where URI is a string.
  336. (let ((path (and=> (string->uri uri) uri-path)))
  337. (if path
  338. (basename path)
  339. uri)))
  340. (or (origin-file-name origin)
  341. (match (origin-uri origin)
  342. ((head . tail)
  343. (uri->file-name head))
  344. ((? string? uri)
  345. (uri->file-name uri))
  346. (else
  347. ;; git, svn, cvs, etc. reference
  348. #f))))
  349. ;; Work around limitations in the 'snippet' mechanism. It is not possible for
  350. ;; a 'snippet' to produce a tarball with a different base name than the
  351. ;; original downloaded source. Moreover, cherry picking dozens of upsteam
  352. ;; patches and applying them suddenly is often impractical; especially when a
  353. ;; comprehensive code reformatting is done upstream. Mainly designed for
  354. ;; Linux and IceCat packages.
  355. ;; XXXX: do not make part of public API (export) such radical capability
  356. ;; before a detailed review process.
  357. (define* (computed-origin-method gexp-promise hash-algo hash
  358. #:optional (name "source")
  359. #:key (system (%current-system))
  360. (guile (default-guile)))
  361. "Return a derivation that executes the G-expression that results
  362. from forcing GEXP-PROMISE."
  363. (mlet %store-monad ((guile (package->derivation guile system)))
  364. (gexp->derivation (or name "computed-origin")
  365. (force gexp-promise)
  366. #:graft? #f ;nothing to graft
  367. #:system system
  368. #:guile-for-build guile)))
  369. (define %32bit-supported-systems
  370. ;; This is the list of 32-bit system types that are supported.
  371. '("i686-linux" "armhf-linux" "i586-gnu" "powerpc-linux"))
  372. (define %64bit-supported-systems
  373. ;; This is the list of 64-bit system types that are supported.
  374. '("x86_64-linux" "mips64el-linux" "aarch64-linux" "powerpc64le-linux"
  375. "riscv64-linux"))
  376. (define %supported-systems
  377. ;; This is the list of system types that are supported. By default, we
  378. ;; expect all packages to build successfully here.
  379. (append %64bit-supported-systems %32bit-supported-systems))
  380. (define %hurd-systems
  381. ;; The GNU/Hurd systems for which support is being developed.
  382. '("i586-gnu"))
  383. (define %cuirass-supported-systems
  384. ;; This is the list of system types for which build machines are available.
  385. ;;
  386. ;; XXX: MIPS is unavailable in CI:
  387. ;; <https://lists.gnu.org/archive/html/guix-devel/2017-03/msg00790.html>.
  388. (fold delete %supported-systems '("mips64el-linux" "powerpc-linux" "riscv64-linux")))
  389. (define-inlinable (sanitize-inputs inputs)
  390. "Sanitize INPUTS by turning it into a list of name/package tuples if it's
  391. not already the case."
  392. (cond ((null? inputs) inputs)
  393. ((and (pair? (car inputs))
  394. (string? (caar inputs)))
  395. inputs)
  396. (else (map add-input-label inputs))))
  397. (define-syntax current-location-vector
  398. (lambda (s)
  399. "Like 'current-source-location' but expand to a literal vector with
  400. one-indexed line numbers."
  401. ;; Storing a literal vector in .go files is more efficient than storing an
  402. ;; alist: less initialization code, fewer relocations, etc.
  403. (syntax-case s ()
  404. ((_)
  405. (match (syntax-source s)
  406. (#f #f)
  407. (properties
  408. (let ((file (assq-ref properties 'filename))
  409. (line (assq-ref properties 'line))
  410. (column (assq-ref properties 'column)))
  411. (and file line column
  412. #`#(#,file #,(+ 1 line) #,column)))))))))
  413. (define-inlinable (sanitize-location loc)
  414. ;; Convert LOC to a vector or to #f.
  415. (cond ((vector? loc) loc)
  416. ((not loc) loc)
  417. (else (vector (location-file loc)
  418. (location-line loc)
  419. (location-column loc)))))
  420. (define-syntax-parameter current-definition-location
  421. ;; Location of the encompassing 'define-public'.
  422. (const #f))
  423. (define-syntax define-public*
  424. (lambda (s)
  425. "Like 'define-public' but set 'current-definition-location' for the
  426. lexical scope of its body."
  427. (define location
  428. (match (syntax-source s)
  429. (#f #f)
  430. (properties
  431. (let ((line (assq-ref properties 'line))
  432. (column (assq-ref properties 'column)))
  433. ;; Don't repeat the file name since it's redundant with 'location'.
  434. ;; Encode the whole thing so that it fits in a fixnum on 32-bit
  435. ;; platforms, which leaves us 29 bits: 7 bits for COLUMN (which is
  436. ;; almost always zero), and 22 bits for LINE.
  437. (and line column
  438. (logior (ash (logand #x7f column) 22)
  439. (logand (- (expt 2 22) 1) (+ 1 line))))))))
  440. (syntax-case s ()
  441. ((_ prototype body ...)
  442. #`(define-public prototype
  443. (syntax-parameterize ((current-definition-location
  444. (lambda (s) #,location)))
  445. body ...))))))
  446. (define-syntax validate-texinfo
  447. (let ((validate? (getenv "GUIX_UNINSTALLED")))
  448. (define ensure-thread-safe-texinfo-parser!
  449. ;; Work around <https://issues.guix.gnu.org/51264> for Guile <= 3.0.7.
  450. (let ((patched? (or (> (string->number (major-version)) 3)
  451. (> (string->number (minor-version)) 0)
  452. (> (string->number (micro-version)) 7)))
  453. (next-token-of/thread-safe
  454. (lambda (pred port)
  455. (let loop ((chars '()))
  456. (match (read-char port)
  457. ((? eof-object?)
  458. (list->string (reverse! chars)))
  459. (chr
  460. (let ((chr* (pred chr)))
  461. (if chr*
  462. (loop (cons chr* chars))
  463. (begin
  464. (unread-char chr port)
  465. (list->string (reverse! chars)))))))))))
  466. (lambda ()
  467. (unless patched?
  468. (set! (@@ (texinfo) next-token-of) next-token-of/thread-safe)
  469. (set! patched? #t)))))
  470. (lambda (s)
  471. "Raise a syntax error when passed a literal string that is not valid
  472. Texinfo. Otherwise, return the string."
  473. (syntax-case s ()
  474. ((_ str)
  475. (string? (syntax->datum #'str))
  476. (if validate?
  477. (catch 'parser-error
  478. (lambda ()
  479. (ensure-thread-safe-texinfo-parser!)
  480. (texi-fragment->stexi (syntax->datum #'str))
  481. #'str)
  482. (lambda _
  483. (syntax-violation 'package "invalid Texinfo markup" #'str)))
  484. #'str))
  485. ((_ obj)
  486. #'obj)))))
  487. (define-syntax valid-license-value?
  488. (syntax-rules (list package-license)
  489. "Return #t if the given value is a valid license field, #f otherwise."
  490. ;; Arrange so that the answer can be given at macro-expansion time in the
  491. ;; most common cases.
  492. ((_ (list x ...))
  493. (and (license? x) ...))
  494. ((_ (package-license _))
  495. #t)
  496. ((_ obj)
  497. (or (license? obj)
  498. ;; Note: Avoid 'not' below due to <https://bugs.gnu.org/58217>.
  499. (eq? #f obj) ;#f is considered valid
  500. (let ((x obj))
  501. (and (pair? x) (every license? x)))))))
  502. (define-with-syntax-properties (validate-license (value properties))
  503. (unless (valid-license-value? value)
  504. (raise
  505. (make-compound-condition
  506. (condition
  507. (&error-location
  508. (location (source-properties->location properties))))
  509. (condition
  510. (&package-license-error (package #f) (license value)))
  511. (formatted-message (G_ "~s: invalid package license~%") value))))
  512. value)
  513. ;; A package.
  514. (define-record-type* <package>
  515. package make-package
  516. package?
  517. this-package
  518. (name package-name) ; string
  519. (version package-version) ; string
  520. (source package-source) ; <origin> instance
  521. (build-system package-build-system) ; <build-system> instance
  522. (arguments package-arguments ; arguments for the build method
  523. (default '()) (thunked))
  524. (inputs package-inputs ; input packages or derivations
  525. (default '()) (thunked)
  526. (sanitize sanitize-inputs))
  527. (propagated-inputs package-propagated-inputs ; same, but propagated
  528. (default '()) (thunked)
  529. (sanitize sanitize-inputs))
  530. (native-inputs package-native-inputs ; native input packages/derivations
  531. (default '()) (thunked)
  532. (sanitize sanitize-inputs))
  533. (outputs package-outputs ; list of strings
  534. (default '("out")))
  535. ; lists of
  536. ; <search-path-specification>,
  537. ; for native and cross
  538. ; inputs
  539. (native-search-paths package-native-search-paths (default '()))
  540. (search-paths package-search-paths (default '()))
  541. ;; The 'replacement' field is marked as "innate" because it never makes
  542. ;; sense to inherit a replacement as is. See the 'package/inherit' macro.
  543. (replacement package-replacement ; package | #f
  544. (default #f) (thunked) (innate))
  545. (synopsis package-synopsis
  546. (sanitize validate-texinfo)) ; one-line description
  547. (description package-description
  548. (sanitize validate-texinfo)) ; one or two paragraphs
  549. (license package-license ; (list of) <license>
  550. (sanitize validate-license))
  551. (home-page package-home-page) ; string
  552. (supported-systems package-supported-systems ; list of strings
  553. (default %supported-systems))
  554. (properties package-properties (default '())) ; alist for anything else
  555. (location package-location-vector
  556. (default (current-location-vector))
  557. (innate) (sanitize sanitize-location))
  558. (definition-location package-definition-location-code
  559. (default (current-definition-location))
  560. (innate)))
  561. (define (add-input-label input)
  562. "Add an input label to INPUT."
  563. (match input
  564. ((? package? package)
  565. (list (package-name package) package))
  566. (((? package? package) output) ;XXX: ugly?
  567. (list (package-name package) package output))
  568. ((? gexp-input?) ;XXX: misplaced because 'native?' field is ignored?
  569. (let ((obj (gexp-input-thing input))
  570. (output (gexp-input-output input)))
  571. `(,(if (package? obj)
  572. (package-name obj)
  573. "_")
  574. ,obj
  575. ,@(if (string=? output "out") '() (list output)))))
  576. (x
  577. `("_" ,x))))
  578. (set-record-type-printer! <package>
  579. (lambda (package port)
  580. (let ((loc (package-location package))
  581. (format simple-format))
  582. (format port "#<package ~a@~a ~a~a>"
  583. (package-name package)
  584. (package-version package)
  585. (if loc
  586. (format #f "~a:~a "
  587. (location-file loc)
  588. (location-line loc))
  589. "")
  590. (number->string (object-address
  591. package)
  592. 16)))))
  593. (define (package-location package)
  594. "Return the source code location of PACKAGE as a <location> record, or #f if
  595. it is not known."
  596. (match (package-location-vector package)
  597. (#f #f)
  598. (#(file line column) (location file line column))))
  599. (define (package-definition-location package)
  600. "Like 'package-location', but return the location of the definition
  601. itself--i.e., that of the enclosing 'define-public' form, if any, or #f."
  602. (match (package-definition-location-code package)
  603. (#f #f)
  604. (code
  605. (let ((column (bit-extract code 22 29))
  606. (line (bit-extract code 0 21)))
  607. (match (package-location-vector package)
  608. (#f #f)
  609. (#(file _ _) (location file line column)))))))
  610. (define-syntax-rule (package/inherit p overrides ...)
  611. "Like (package (inherit P) OVERRIDES ...), except that the same
  612. transformation is done to the package P's replacement, if any. P must be a bare
  613. identifier, and will be bound to either P or its replacement when evaluating
  614. OVERRIDES."
  615. (let loop ((p p))
  616. (package (inherit p)
  617. overrides ...
  618. (replacement (and=> (package-replacement p) loop)))))
  619. (define (package-upstream-name package)
  620. "Return the upstream name of PACKAGE, which could be different from the name
  621. it has in Guix."
  622. (or (assq-ref (package-properties package) 'upstream-name)
  623. (package-name package)))
  624. (define (package-upstream-name* package)
  625. "Return the upstream name of PACKAGE, accounting for commonly-used
  626. package name prefixes in addition to the @code{upstream-name} property."
  627. (let ((namespaces (list "cl-"
  628. "ecl-"
  629. "emacs-"
  630. "ghc-"
  631. "go-"
  632. "guile-"
  633. "java-"
  634. "julia-"
  635. "lua-"
  636. "minetest-"
  637. "node-"
  638. "ocaml-"
  639. "perl-"
  640. "python-"
  641. "r-"
  642. "ruby-"
  643. "rust-"
  644. "sbcl-"
  645. "texlive-"))
  646. (name (package-name package)))
  647. (or (assq-ref (package-properties package) 'upstream-name)
  648. (let loop ((prefixes namespaces))
  649. (match prefixes
  650. (() name)
  651. ((prefix rest ...)
  652. (if (string-prefix? prefix name)
  653. (substring name (string-length prefix))
  654. (loop rest))))))))
  655. (define (hidden-package p)
  656. "Return a \"hidden\" version of P--i.e., one that 'fold-packages' and thus,
  657. user interfaces, ignores."
  658. (package
  659. (inherit p)
  660. (location (package-location p))
  661. (properties `((hidden? . #t)
  662. ,@(package-properties p)))))
  663. (define (hidden-package? p)
  664. "Return true if P is \"hidden\"--i.e., must not be visible to user
  665. interfaces."
  666. (assoc-ref (package-properties p) 'hidden?))
  667. (define (package-superseded p)
  668. "Return the package that supersedes P, or #f if P is still current."
  669. (assoc-ref (package-properties p) 'superseded))
  670. (define (deprecated-package old-name p)
  671. "Return a package called OLD-NAME and marked as superseded by P, a package
  672. object."
  673. (package
  674. (inherit p)
  675. (name old-name)
  676. (properties `((superseded . ,p)))))
  677. (define (package-field-location package field)
  678. "Return the source code location of the definition of FIELD for PACKAGE, or
  679. #f if it could not be determined."
  680. (match (package-location package)
  681. (($ <location> file line column)
  682. (match (search-path %load-path file)
  683. ((? string? file-found)
  684. (catch 'system-error
  685. (lambda ()
  686. ;; In general we want to keep relative file names for modules.
  687. (call-with-input-file file-found
  688. (lambda (port)
  689. (go-to-location port line column)
  690. (match (read port)
  691. (('package inits ...)
  692. (let ((field (assoc field inits)))
  693. (match field
  694. ((_ value)
  695. (let ((loc (and=> (source-properties value)
  696. source-properties->location)))
  697. (and loc
  698. ;; Preserve the original file name, which may be a
  699. ;; relative file name.
  700. (set-field loc (location-file) file))))
  701. (_
  702. #f))))
  703. (_
  704. #f)))))
  705. (lambda _
  706. #f)))
  707. (#f
  708. ;; FILE could not be found in %LOAD-PATH.
  709. #f)))
  710. (_ #f)))
  711. (define-syntax-rule (this-package-input name)
  712. "Return the input NAME of the package being defined--i.e., an input
  713. from the ‘inputs’ or ‘propagated-inputs’ field. Native inputs are not
  714. considered. If this input does not exist, return #f instead."
  715. (or (lookup-package-input this-package name)
  716. (lookup-package-propagated-input this-package name)))
  717. (define-syntax-rule (this-package-native-input name)
  718. "Return the native package input NAME of the package being defined--i.e.,
  719. an input from the ‘native-inputs’ field. If this native input does not
  720. exist, return #f instead."
  721. (lookup-package-native-input this-package name))
  722. ;; Error conditions.
  723. (define-condition-type &package-error &error
  724. package-error?
  725. (package package-error-package))
  726. (define-condition-type &package-license-error &package-error
  727. package-license-error?
  728. (license package-error-invalid-license))
  729. (define-condition-type &package-input-error &package-error
  730. package-input-error?
  731. (input package-error-invalid-input))
  732. (define-condition-type &package-cross-build-system-error &package-error
  733. package-cross-build-system-error?)
  734. (define* (package-full-name package #:optional (delimiter "@"))
  735. "Return the full name of PACKAGE--i.e., `NAME@VERSION'. By specifying
  736. DELIMITER (a string), you can customize what will appear between the name and
  737. the version. By default, DELIMITER is \"@\"."
  738. (string-append (package-name package) delimiter (package-version package)))
  739. (define (patch-file-name patch)
  740. "Return the basename of PATCH's file name, or #f if the file name could not
  741. be determined."
  742. (match patch
  743. ((? string?)
  744. (basename patch))
  745. ((? origin?)
  746. (and=> (origin-actual-file-name patch) basename))))
  747. (define %vulnerability-regexp
  748. ;; Regexp matching a CVE identifier in patch file names.
  749. (make-regexp "CVE-[0-9]{4}-[0-9]+"))
  750. (define (package-patched-vulnerabilities package)
  751. "Return the list of patched vulnerabilities of PACKAGE as a list of CVE
  752. identifiers. The result is inferred from the file names of patches."
  753. (define (patch-vulnerabilities patch)
  754. (map (cut match:substring <> 0)
  755. (list-matches %vulnerability-regexp patch)))
  756. (let ((patches (filter-map patch-file-name
  757. (or (and=> (package-source package)
  758. origin-patches)
  759. '()))))
  760. (append-map patch-vulnerabilities patches)))
  761. (define (%standard-patch-inputs)
  762. (let* ((canonical (module-ref (resolve-interface '(gnu packages base))
  763. 'canonical-package))
  764. (ref (lambda (module var)
  765. ;; Make sure 'canonical-package' is not influenced by
  766. ;; '%current-target-system' since we're going to use the
  767. ;; native package anyway.
  768. (parameterize ((%current-target-system #f))
  769. (canonical
  770. (module-ref (resolve-interface module) var))))))
  771. `(("tar" ,(ref '(gnu packages base) 'tar))
  772. ("xz" ,(ref '(gnu packages compression) 'xz))
  773. ("bzip2" ,(ref '(gnu packages compression) 'bzip2))
  774. ("gzip" ,(ref '(gnu packages compression) 'gzip))
  775. ("lzip" ,(ref '(gnu packages compression) 'lzip))
  776. ("unzip" ,(ref '(gnu packages compression) 'unzip))
  777. ("patch" ,(ref '(gnu packages base) 'patch))
  778. ("locales" ,(ref '(gnu packages base) 'glibc-utf8-locales)))))
  779. (define (default-guile)
  780. "Return the default Guile package used to run the build code of
  781. derivations."
  782. (let ((distro (resolve-interface '(gnu packages commencement))))
  783. (module-ref distro 'guile-final)))
  784. (define (guile-for-grafts)
  785. "Return the Guile package used to build grafting derivations."
  786. ;; Guile 2.2 would not work due to <https://bugs.gnu.org/28211> when
  787. ;; grafting packages.
  788. (let ((distro (resolve-interface '(gnu packages guile))))
  789. (module-ref distro 'guile-2.0)))
  790. (define* (default-guile-derivation #:optional (system (%current-system)))
  791. "Return the derivation for SYSTEM of the default Guile package used to run
  792. the build code of derivation."
  793. (package->derivation (default-guile) system
  794. #:graft? #f))
  795. (define* (patch-and-repack source patches
  796. #:key
  797. inputs
  798. (snippet #f)
  799. (flags %default-patch-flags)
  800. (modules '())
  801. (guile-for-build (%guile-for-build))
  802. (system (%current-system)))
  803. "Unpack SOURCE (a derivation or store path), apply all of PATCHES, and
  804. repack the tarball using the tools listed in INPUTS. When SNIPPET is true,
  805. it must be an s-expression that will run from within the directory where
  806. SOURCE was unpacked, after all of PATCHES have been applied. MODULES
  807. specifies modules in scope when evaluating SNIPPET."
  808. (define source-file-name
  809. ;; SOURCE is usually a derivation, but it could be a store file.
  810. (if (derivation? source)
  811. (derivation->output-path source)
  812. source))
  813. (define lookup-input
  814. ;; The default value of the 'patch-inputs' field, and thus INPUTS is #f,
  815. ;; so deal with that.
  816. (let ((inputs (or inputs (%standard-patch-inputs))))
  817. (lambda (name)
  818. (match (assoc-ref inputs name)
  819. ((package) package)
  820. (#f #f)))))
  821. (define original-file-name (strip-store-file-name source-file-name))
  822. (define (numeric-extension? file-name)
  823. ;; Return true if FILE-NAME ends with digits.
  824. (and=> (file-extension file-name)
  825. (cut string-every char-set:hex-digit <>)))
  826. (define (checkout? directory)
  827. ;; Return true if DIRECTORY is a checkout (git, svn, etc).
  828. (string-suffix? "-checkout" directory))
  829. (define (tarxz-name file-name)
  830. ;; Return a '.tar.xz' file name based on FILE-NAME.
  831. (let ((base (if (numeric-extension? file-name)
  832. original-file-name
  833. (file-sans-extension file-name))))
  834. (string-append base
  835. (if (equal? (file-extension base) "tar")
  836. ".xz"
  837. ".tar.xz"))))
  838. (define instantiate-patch
  839. (match-lambda
  840. ((? string? patch) ;deprecated
  841. (local-file patch #:recursive? #t))
  842. ((? struct? patch) ;origin, local-file, etc.
  843. patch)))
  844. (let ((tar (lookup-input "tar"))
  845. (gzip (lookup-input "gzip"))
  846. (bzip2 (lookup-input "bzip2"))
  847. (lzip (lookup-input "lzip"))
  848. (xz (lookup-input "xz"))
  849. (patch (lookup-input "patch"))
  850. (locales (lookup-input "locales"))
  851. (comp (and=> (compressor source-file-name) lookup-input))
  852. (patches (map instantiate-patch patches)))
  853. (define build
  854. (with-imported-modules '((guix build utils))
  855. #~(begin
  856. (use-modules (ice-9 ftw)
  857. (ice-9 match)
  858. (ice-9 regex)
  859. (srfi srfi-1)
  860. (srfi srfi-26)
  861. (guix build utils))
  862. ;; The --sort option was added to GNU tar in version 1.28, released
  863. ;; 2014-07-28. During bootstrap we must cope with older versions.
  864. (define tar-supports-sort?
  865. (zero? (system* (string-append #+tar "/bin/tar")
  866. "cf" "/dev/null" "--files-from=/dev/null"
  867. "--sort=name")))
  868. (define (apply-patch patch)
  869. (format (current-error-port) "applying '~a'...~%" patch)
  870. ;; Use '--force' so that patches that do not apply perfectly are
  871. ;; rejected. Use '--no-backup-if-mismatch' to prevent making
  872. ;; "*.orig" file if a patch is applied with offset.
  873. (invoke (string-append #+patch "/bin/patch")
  874. "--force" "--no-backup-if-mismatch"
  875. #+@flags "--input" patch))
  876. (define (first-file directory)
  877. ;; Return the name of the first file in DIRECTORY.
  878. (car (scandir directory
  879. (lambda (name)
  880. (not (member name '("." "..")))))))
  881. (define (repack directory output)
  882. ;; Write to OUTPUT a compressed tarball containing DIRECTORY.
  883. (unless tar-supports-sort?
  884. (call-with-output-file ".file_list"
  885. (lambda (port)
  886. (for-each (lambda (name)
  887. (format port "~a~%" name))
  888. (find-files directory
  889. #:directories? #t
  890. #:fail-on-error? #t)))))
  891. (apply invoke #+(file-append tar "/bin/tar")
  892. "cvfa" output
  893. ;; Avoid non-determinism in the archive. Set the mtime
  894. ;; to 1 as is the case in the store (software like gzip
  895. ;; behaves differently when it stumbles upon mtime = 0).
  896. "--mtime=@1"
  897. "--owner=root:0" "--group=root:0"
  898. (if tar-supports-sort?
  899. `("--sort=name" ,directory)
  900. '("--no-recursion"
  901. "--files-from=.file_list"))))
  902. ;; Encoding/decoding errors shouldn't be silent.
  903. (fluid-set! %default-port-conversion-strategy 'error)
  904. (when #+locales
  905. ;; First of all, install a UTF-8 locale so that UTF-8 file names
  906. ;; are correctly interpreted. During bootstrap, LOCALES is #f.
  907. (setenv "LOCPATH"
  908. (string-append #+locales "/lib/locale/"
  909. #+(and locales
  910. (version-major+minor
  911. (package-version locales)))))
  912. (setlocale LC_ALL "en_US.utf8"))
  913. (setenv "PATH"
  914. (string-append #+xz "/bin"
  915. (if #+comp
  916. (string-append ":" #+comp "/bin")
  917. "")))
  918. (setenv "XZ_DEFAULTS" (string-join (%xz-parallel-args)))
  919. ;; SOURCE may be either a directory, a tarball or a simple file.
  920. (let ((name (strip-store-file-name #+source))
  921. (command (and=> #+comp (cut string-append <> "/bin/"
  922. (compressor #+source)))))
  923. (if (file-is-directory? #+source)
  924. (copy-recursively #+source name)
  925. (cond
  926. ((tarball? #+source)
  927. (invoke (string-append #+tar "/bin/tar") "xvf" #+source))
  928. ((and=> (compressor #+source) (cut string= "unzip" <>))
  929. ;; Note: Referring to the store unzip here (#+unzip)
  930. ;; would introduce a cycle.
  931. (invoke "unzip" #+source))
  932. (else
  933. (copy-file #+source name)
  934. (when command
  935. (invoke command "--decompress" name))))))
  936. (let* ((file (first-file "."))
  937. (directory (if (file-is-directory? file)
  938. file
  939. ".")))
  940. (format (current-error-port) "source is at '~a'~%" file)
  941. (with-directory-excursion directory
  942. (for-each apply-patch '#+patches)
  943. #+(if snippet
  944. #~(let ((module (make-fresh-user-module)))
  945. (module-use-interfaces!
  946. module
  947. (map resolve-interface '#+modules))
  948. ((@ (system base compile) compile)
  949. '#+(if (pair? snippet)
  950. (sexp->gexp snippet)
  951. snippet)
  952. #:to 'value
  953. #:opts %auto-compilation-options
  954. #:env module))
  955. #~#t))
  956. ;; If SOURCE is a directory (such as a checkout), return a
  957. ;; directory. Otherwise create a tarball.
  958. (cond
  959. ((file-is-directory? #+source)
  960. (copy-recursively directory #$output
  961. #:log (%make-void-port "w")))
  962. ((or #+comp (tarball? #+source))
  963. (repack directory #$output))
  964. (else ;single uncompressed file
  965. (copy-file file #$output)))))))
  966. (let ((name (if (or (checkout? original-file-name)
  967. (not (compressor original-file-name)))
  968. original-file-name
  969. (tarxz-name original-file-name))))
  970. (gexp->derivation name build
  971. #:graft? #f
  972. #:system system
  973. #:guile-for-build guile-for-build
  974. #:properties `((type . origin)
  975. (patches . ,(length patches)))))))
  976. (define (package-with-patches original patches)
  977. "Return package ORIGINAL with PATCHES applied."
  978. (package (inherit original)
  979. (source (origin (inherit (package-source original))
  980. (patches patches)))
  981. (location (package-location original))))
  982. (define (package-with-extra-patches original patches)
  983. "Return package ORIGINAL with all PATCHES appended to its list of patches."
  984. (package-with-patches original
  985. (append (origin-patches (package-source original))
  986. patches)))
  987. (define (package-with-c-toolchain package toolchain)
  988. "Return a variant of PACKAGE that uses TOOLCHAIN instead of the default GNU
  989. C/C++ toolchain. TOOLCHAIN must be a list of inputs (label/package tuples)
  990. providing equivalent functionality, such as the 'gcc-toolchain' package."
  991. (let ((bs (package-build-system package)))
  992. (package/inherit package
  993. (build-system (build-system-with-c-toolchain bs toolchain)))))
  994. (define (transitive-inputs inputs)
  995. "Return the closure of INPUTS when considering the 'propagated-inputs'
  996. edges. Omit duplicate inputs, except for those already present in INPUTS
  997. itself.
  998. This is implemented as a breadth-first traversal such that INPUTS is
  999. preserved, and only duplicate propagated inputs are removed."
  1000. (define (seen? seen item outputs)
  1001. ;; FIXME: We're using pointer identity here, which is extremely sensitive
  1002. ;; to memoization in package-producing procedures; see
  1003. ;; <https://bugs.gnu.org/30155>.
  1004. (match (vhash-assq item seen)
  1005. ((_ . o) (equal? o outputs))
  1006. (_ #f)))
  1007. (let loop ((inputs inputs)
  1008. (result '())
  1009. (propagated '())
  1010. (first? #t)
  1011. (seen vlist-null))
  1012. (match inputs
  1013. (()
  1014. (if (null? propagated)
  1015. (reverse result)
  1016. (loop (reverse (concatenate propagated)) result '() #f seen)))
  1017. (((and input (label (? package? package) outputs ...)) rest ...)
  1018. (if (and (not first?) (seen? seen package outputs))
  1019. (loop rest result propagated first? seen)
  1020. (loop rest
  1021. (cons input result)
  1022. (cons (package-propagated-inputs package) propagated)
  1023. first?
  1024. (vhash-consq package outputs seen))))
  1025. ((input rest ...)
  1026. (loop rest (cons input result) propagated first? seen)))))
  1027. (define (lookup-input inputs name)
  1028. "Lookup NAME among INPUTS, an input list."
  1029. ;; Note: Currently INPUTS is assumed to be an input list that contains input
  1030. ;; labels. In the future, input labels will be gone and this procedure will
  1031. ;; check package names.
  1032. (match (assoc-ref inputs name)
  1033. ((obj) obj)
  1034. ((obj _) obj)
  1035. (#f #f)))
  1036. (define (lookup-package-input package name)
  1037. "Look up NAME among PACKAGE's inputs. Return it if found, #f otherwise."
  1038. (lookup-input (package-inputs package) name))
  1039. (define (lookup-package-native-input package name)
  1040. "Look up NAME among PACKAGE's native inputs. Return it if found, #f
  1041. otherwise."
  1042. (lookup-input (package-native-inputs package) name))
  1043. (define (lookup-package-propagated-input package name)
  1044. "Look up NAME among PACKAGE's propagated inputs. Return it if found, #f
  1045. otherwise."
  1046. (lookup-input (package-propagated-inputs package) name))
  1047. (define (lookup-package-direct-input package name)
  1048. "Look up NAME among PACKAGE's direct inputs. Return it if found, #f
  1049. otherwise."
  1050. (lookup-input (package-direct-inputs package) name))
  1051. (define (replace-input name replacement inputs)
  1052. "Replace input NAME by REPLACEMENT within INPUTS."
  1053. (map (lambda (input)
  1054. (match input
  1055. (((? string? label) _ . outputs)
  1056. (if (string=? label name)
  1057. (match replacement ;does REPLACEMENT specify an output?
  1058. ((_ _) (cons label replacement))
  1059. (_ (cons* label replacement outputs)))
  1060. input))))
  1061. inputs))
  1062. (define-syntax prepend
  1063. (lambda (s)
  1064. (syntax-violation 'prepend
  1065. "'prepend' may only be used within 'modify-inputs'"
  1066. s)))
  1067. (define-syntax replace
  1068. (lambda (s)
  1069. (syntax-violation 'replace
  1070. "'replace' may only be used within 'modify-inputs'"
  1071. s)))
  1072. (define-syntax modify-inputs
  1073. (syntax-rules (delete prepend append replace)
  1074. "Modify the given package inputs, as returned by 'package-inputs' & co.,
  1075. according to the given clauses. The example below removes the GMP and ACL
  1076. inputs of Coreutils and adds libcap:
  1077. (modify-inputs (package-inputs coreutils)
  1078. (delete \"gmp\" \"acl\")
  1079. (prepend libcap))
  1080. Other types of clauses include 'append' and 'replace'.
  1081. The first argument must be a labeled input list; the result is also a labeled
  1082. input list."
  1083. ;; Note: This macro hides the fact that INPUTS, as returned by
  1084. ;; 'package-inputs' & co., is actually an alist with labels. Eventually,
  1085. ;; it will operate on list of inputs without labels.
  1086. ((_ inputs (delete name) clauses ...)
  1087. (modify-inputs (alist-delete name inputs)
  1088. clauses ...))
  1089. ((_ inputs (delete names ...) clauses ...)
  1090. (modify-inputs (fold alist-delete inputs (list names ...))
  1091. clauses ...))
  1092. ((_ inputs (prepend lst ...) clauses ...)
  1093. (modify-inputs (append (map add-input-label (list lst ...)) inputs)
  1094. clauses ...))
  1095. ((_ inputs (append lst ...) clauses ...)
  1096. (modify-inputs (append inputs (map add-input-label (list lst ...)))
  1097. clauses ...))
  1098. ((_ inputs (replace name replacement) clauses ...)
  1099. (modify-inputs (replace-input name replacement inputs)
  1100. clauses ...))
  1101. ((_ inputs)
  1102. inputs)))
  1103. (define (package-direct-sources package)
  1104. "Return all source origins associated with PACKAGE; including origins in
  1105. PACKAGE's inputs and patches."
  1106. (define (expand source)
  1107. (cons source
  1108. (filter origin? (origin-patches source))))
  1109. `(,@(match (package-source package)
  1110. ((? origin? origin)
  1111. (expand origin))
  1112. (_
  1113. '()))
  1114. ,@(filter-map (match-lambda
  1115. ((_ (? origin? orig) _ ...)
  1116. orig)
  1117. (_ #f))
  1118. (package-direct-inputs package))))
  1119. (define (package-transitive-sources package)
  1120. "Return PACKAGE's direct sources, and their direct sources, recursively."
  1121. (delete-duplicates
  1122. (concatenate (filter-map (match-lambda
  1123. ((_ (? origin? orig) _ ...)
  1124. (list orig))
  1125. ((_ (? package? p) _ ...)
  1126. (package-direct-sources p))
  1127. (_ #f))
  1128. (bag-transitive-inputs
  1129. (package->bag package))))))
  1130. (define (package-direct-inputs package)
  1131. "Return all the direct inputs of PACKAGE---i.e, its direct inputs along
  1132. with their propagated inputs."
  1133. (append (package-native-inputs package)
  1134. (package-inputs package)
  1135. (package-propagated-inputs package)))
  1136. (define (package-transitive-inputs package)
  1137. "Return the transitive inputs of PACKAGE---i.e., its direct inputs along
  1138. with their propagated inputs, recursively."
  1139. (transitive-inputs (package-direct-inputs package)))
  1140. (define (package-transitive-target-inputs package)
  1141. "Return the transitive target inputs of PACKAGE---i.e., its direct inputs
  1142. along with their propagated inputs, recursively. This only includes inputs
  1143. for the target system, and not native inputs."
  1144. (transitive-inputs (append (package-inputs package)
  1145. (package-propagated-inputs package))))
  1146. (define (package-transitive-native-inputs package)
  1147. "Return the transitive native inputs of PACKAGE---i.e., its direct inputs
  1148. along with their propagated inputs, recursively. This only includes inputs
  1149. for the host system (\"native inputs\"), and not target inputs."
  1150. (transitive-inputs (package-native-inputs package)))
  1151. (define (package-transitive-propagated-inputs package)
  1152. "Return the propagated inputs of PACKAGE, and their propagated inputs,
  1153. recursively."
  1154. (transitive-inputs (package-propagated-inputs package)))
  1155. (define (package-transitive-native-search-paths package)
  1156. "Return the list of search paths for PACKAGE and its propagated inputs,
  1157. recursively."
  1158. (append (package-native-search-paths package)
  1159. (append-map (match-lambda
  1160. ((label (? package? p) _ ...)
  1161. (package-native-search-paths p))
  1162. (_
  1163. '()))
  1164. (package-transitive-propagated-inputs package))))
  1165. (define (transitive-input-references alist inputs)
  1166. "Return a list of (assoc-ref ALIST <label>) for each (<label> <package> . _)
  1167. in INPUTS and their transitive propagated inputs."
  1168. (define label
  1169. (match-lambda
  1170. ((label . _)
  1171. label)))
  1172. (map (lambda (input)
  1173. `(assoc-ref ,alist ,(label input)))
  1174. (transitive-inputs inputs)))
  1175. (define package-transitive-supported-systems
  1176. (let ()
  1177. (define (supported-systems-procedure system)
  1178. (define supported-systems
  1179. (mlambdaq (package)
  1180. (parameterize ((%current-system system))
  1181. (fold (lambda (input systems)
  1182. (match input
  1183. ((label (? package? package) . _)
  1184. (lset-intersection string=? systems
  1185. (supported-systems package)))
  1186. (_
  1187. systems)))
  1188. (package-supported-systems package)
  1189. (bag-direct-inputs (package->bag package system #f))))))
  1190. supported-systems)
  1191. (define procs
  1192. ;; Map system strings to one-argument procedures. This allows these
  1193. ;; procedures to have fast 'eq?' memoization on their argument.
  1194. (make-hash-table))
  1195. (lambda* (package #:optional (system (%current-system)))
  1196. "Return the intersection of the systems supported by PACKAGE and those
  1197. supported by its dependencies."
  1198. (match (hash-ref procs system)
  1199. (#f
  1200. (hash-set! procs system (supported-systems-procedure system))
  1201. (package-transitive-supported-systems package system))
  1202. (proc
  1203. (proc package))))))
  1204. (define* (supported-package? package #:optional (system (%current-system)))
  1205. "Return true if PACKAGE is supported on SYSTEM--i.e., if PACKAGE and all its
  1206. dependencies are known to build on SYSTEM."
  1207. (member system (package-transitive-supported-systems package system)))
  1208. (define (bag-direct-inputs bag)
  1209. "Same as 'package-direct-inputs', but applied to a bag."
  1210. (append (bag-build-inputs bag)
  1211. (bag-host-inputs bag)
  1212. (bag-target-inputs bag)))
  1213. (define (bag-transitive-inputs bag)
  1214. "Same as 'package-transitive-inputs', but applied to a bag."
  1215. (parameterize ((%current-target-system #f)
  1216. (%current-system (bag-system bag)))
  1217. (transitive-inputs (bag-direct-inputs bag))))
  1218. (define (bag-transitive-build-inputs bag)
  1219. "Same as 'package-transitive-native-inputs', but applied to a bag."
  1220. (parameterize ((%current-target-system #f)
  1221. (%current-system (bag-system bag)))
  1222. (transitive-inputs (bag-build-inputs bag))))
  1223. (define (bag-transitive-host-inputs bag)
  1224. "Same as 'package-transitive-target-inputs', but applied to a bag."
  1225. (parameterize ((%current-target-system (bag-target bag))
  1226. (%current-system (bag-system bag)))
  1227. (transitive-inputs (bag-host-inputs bag))))
  1228. (define (bag-transitive-target-inputs bag)
  1229. "Return the \"target inputs\" of BAG, recursively."
  1230. (parameterize ((%current-target-system (bag-target bag))
  1231. (%current-system (bag-system bag)))
  1232. (transitive-inputs (bag-target-inputs bag))))
  1233. (define* (package-development-inputs package
  1234. #:optional (system (%current-system))
  1235. #:key target)
  1236. "Return the list of inputs required by PACKAGE for development purposes on
  1237. SYSTEM. When TARGET is true, return the inputs needed to cross-compile
  1238. PACKAGE from SYSTEM to TRIPLET, where TRIPLET is a triplet such as
  1239. \"aarch64-linux-gnu\"."
  1240. (bag-transitive-inputs (package->bag package system target)))
  1241. (define* (package-closure packages #:key (system (%current-system)))
  1242. "Return the closure of PACKAGES on SYSTEM--i.e., PACKAGES and the list of
  1243. packages they depend on, recursively."
  1244. (let loop ((packages packages)
  1245. (visited vlist-null)
  1246. (closure (list->setq packages)))
  1247. (match packages
  1248. (()
  1249. (set->list closure))
  1250. ((package . rest)
  1251. (if (vhash-assq package visited)
  1252. (loop rest visited closure)
  1253. (let* ((bag (package->bag package system))
  1254. (dependencies (filter-map (match-lambda
  1255. ((label (? package? package) . _)
  1256. package)
  1257. (_ #f))
  1258. (bag-direct-inputs bag))))
  1259. (loop (append dependencies rest)
  1260. (vhash-consq package #t visited)
  1261. (fold set-insert closure dependencies))))))))
  1262. (define (build-system-with-package-mapping bs rewrite)
  1263. "Return a variant of BS, a build system, that rewrites a bag's inputs by
  1264. passing them through REWRITE, a procedure that takes an input tuplet and
  1265. returns a \"rewritten\" input tuplet."
  1266. (define lower
  1267. (build-system-lower bs))
  1268. (define (lower* . args)
  1269. (let ((lowered (apply lower args)))
  1270. (bag
  1271. (inherit lowered)
  1272. (build-inputs (map rewrite (bag-build-inputs lowered)))
  1273. (host-inputs (map rewrite (bag-host-inputs lowered)))
  1274. (target-inputs (map rewrite (bag-target-inputs lowered))))))
  1275. (build-system
  1276. (inherit bs)
  1277. (lower lower*)))
  1278. (define* (package-mapping proc #:optional (cut? (const #f))
  1279. #:key deep?)
  1280. "Return a procedure that, given a package, applies PROC to all the packages
  1281. depended on and returns the resulting package. The procedure stops recursion
  1282. when CUT? returns true for a given package. When DEEP? is true, PROC is
  1283. applied to implicit inputs as well."
  1284. (define (rewrite input)
  1285. (match input
  1286. ((label (? package? package) outputs ...)
  1287. (cons* label (replace package) outputs))
  1288. (_
  1289. input)))
  1290. (define mapping-property
  1291. ;; Property indicating whether the package has already been processed.
  1292. (gensym " package-mapping-done"))
  1293. (define replace
  1294. (mlambdaq (p)
  1295. ;; If P is the result of a previous call, return it.
  1296. (cond ((assq-ref (package-properties p) mapping-property)
  1297. p)
  1298. ((cut? p)
  1299. ;; Since P's propagated inputs are really inputs of its dependents,
  1300. ;; rewrite them as well, unless we're doing a "shallow" rewrite.
  1301. (let ((p (proc p)))
  1302. (if (or (not deep?)
  1303. (null? (package-propagated-inputs p)))
  1304. p
  1305. (package
  1306. (inherit p)
  1307. (location (package-location p))
  1308. (replacement (package-replacement p))
  1309. (propagated-inputs (map rewrite (package-propagated-inputs p)))
  1310. (properties `((,mapping-property . #t)
  1311. ,@(package-properties p)))))))
  1312. (else
  1313. ;; Return a variant of P with PROC applied to P and its explicit
  1314. ;; dependencies, recursively. Memoize the transformations. Failing
  1315. ;; to do that, we would build a huge object graph with lots of
  1316. ;; duplicates, which in turns prevents us from benefiting from
  1317. ;; memoization in 'package-derivation'.
  1318. (let ((p (proc p)))
  1319. (package
  1320. (inherit p)
  1321. (location (package-location p))
  1322. (build-system (if deep?
  1323. (build-system-with-package-mapping
  1324. (package-build-system p) rewrite)
  1325. (package-build-system p)))
  1326. (inputs (map rewrite (package-inputs p)))
  1327. (native-inputs (map rewrite (package-native-inputs p)))
  1328. (propagated-inputs (map rewrite (package-propagated-inputs p)))
  1329. (replacement (and=> (package-replacement p) replace))
  1330. (properties `((,mapping-property . #t)
  1331. ,@(package-properties p)))))))))
  1332. replace)
  1333. (define* (package-input-rewriting replacements
  1334. #:optional (rewrite-name identity)
  1335. #:key (deep? #t))
  1336. "Return a procedure that, when passed a package, replaces its direct and
  1337. indirect dependencies, including implicit inputs when DEEP? is true, according
  1338. to REPLACEMENTS. REPLACEMENTS is a list of package pairs; the first element
  1339. of each pair is the package to replace, and the second one is the replacement.
  1340. Optionally, REWRITE-NAME is a one-argument procedure that takes the name of a
  1341. package and returns its new name after rewrite."
  1342. (define replacement-property
  1343. ;; Property to tag right-hand sides in REPLACEMENTS.
  1344. (gensym " package-replacement"))
  1345. (define (rewrite p)
  1346. (if (assq-ref (package-properties p) replacement-property)
  1347. p
  1348. (match (assq-ref replacements p)
  1349. (#f (package/inherit p
  1350. (name (rewrite-name (package-name p)))))
  1351. (new (if deep?
  1352. (package/inherit new
  1353. (properties `((,replacement-property . #t)
  1354. ,@(package-properties new))))
  1355. new)))))
  1356. (define (cut? p)
  1357. (or (assq-ref (package-properties p) replacement-property)
  1358. (assq-ref replacements p)))
  1359. (package-mapping rewrite cut?
  1360. #:deep? deep?))
  1361. (define* (package-input-rewriting/spec replacements #:key (deep? #t))
  1362. "Return a procedure that, given a package, applies the given REPLACEMENTS to
  1363. all the package graph, including implicit inputs unless DEEP? is false.
  1364. REPLACEMENTS is a list of spec/procedures pair; each spec is a package
  1365. specification such as \"gcc\" or \"guile@2\", and each procedure takes a
  1366. matching package and returns a replacement for that package. Matching
  1367. packages that have the 'hidden?' property set are not replaced."
  1368. (define table
  1369. (fold (lambda (replacement table)
  1370. (match replacement
  1371. ((spec . proc)
  1372. (let ((name version (package-name->name+version spec)))
  1373. (vhash-cons name (list version proc) table)))))
  1374. vlist-null
  1375. replacements))
  1376. (define (find-replacement package)
  1377. (vhash-fold* (lambda (item proc)
  1378. (or proc
  1379. (match item
  1380. ((#f proc)
  1381. proc)
  1382. ((version proc)
  1383. (and (version-prefix? version
  1384. (package-version package))
  1385. proc)))))
  1386. #f
  1387. (package-name package)
  1388. table))
  1389. (define replacement-property
  1390. (gensym " package-replacement"))
  1391. (define (rewrite p)
  1392. (if (or (assq-ref (package-properties p) replacement-property)
  1393. (hidden-package? p))
  1394. p
  1395. (match (find-replacement p)
  1396. (#f p)
  1397. (proc
  1398. (let ((new (proc p)))
  1399. ;; Mark NEW as already processed.
  1400. (package/inherit new
  1401. (properties `((,replacement-property . #t)
  1402. ,@(package-properties new)))))))))
  1403. (define (cut? p)
  1404. (or (assq-ref (package-properties p) replacement-property)
  1405. (find-replacement p)))
  1406. (package-mapping rewrite cut?
  1407. #:deep? deep?))
  1408. ;;;
  1409. ;;; Package derivations.
  1410. ;;;
  1411. (define (cache! cache package system thunk)
  1412. "Memoize in CACHE the return values of THUNK as the derivation of PACKAGE on
  1413. SYSTEM."
  1414. ;; FIXME: This memoization should be associated with the open store, because
  1415. ;; otherwise it breaks when switching to a different store.
  1416. (let ((result (thunk)))
  1417. ;; Use `hashq-set!' instead of `hash-set!' because `hash' returns the
  1418. ;; same value for all structs (as of Guile 2.0.6), and because pointer
  1419. ;; equality is sufficient in practice.
  1420. (hashq-set! cache package
  1421. `((,system . ,result)
  1422. ,@(or (hashq-ref cache package) '())))
  1423. result))
  1424. (define-syntax cached
  1425. (syntax-rules (=>)
  1426. "Memoize the result of BODY for the arguments PACKAGE and SYSTEM.
  1427. Return the cached result when available."
  1428. ((_ (=> cache) package system body ...)
  1429. (let ((thunk (lambda () body ...))
  1430. (key system))
  1431. (match (hashq-ref cache package)
  1432. ((alist (... ...))
  1433. (match (assoc-ref alist key)
  1434. (#f (cache! cache package key thunk))
  1435. (value value)))
  1436. (#f
  1437. (cache! cache package key thunk)))))))
  1438. (define* (expand-input package input system #:key target)
  1439. "Expand INPUT, an input tuple, to a name/<gexp-input> tuple. PACKAGE is
  1440. only used to provide contextual information in exceptions."
  1441. (with-monad %store-monad
  1442. (match input
  1443. ;; INPUT doesn't need to be lowered here because it'll be lowered down
  1444. ;; the road in the gexp that refers to it. However, packages need to be
  1445. ;; special-cased to pass #:graft? #f (only the "tip" of the package
  1446. ;; graph needs to have #:graft? #t). Lowering them here also allows
  1447. ;; 'bag->derivation' to delete non-eq? packages that lead to the same
  1448. ;; derivation.
  1449. (((? string? name) (? package? package))
  1450. (mlet %store-monad ((drv (if target
  1451. (package->cross-derivation package
  1452. target system
  1453. #:graft? #f)
  1454. (package->derivation package system
  1455. #:graft? #f))))
  1456. (return (list name (gexp-input drv #:native? (not target))))))
  1457. (((? string? name) (? package? package) (? string? output))
  1458. (mlet %store-monad ((drv (if target
  1459. (package->cross-derivation package
  1460. target system
  1461. #:graft? #f)
  1462. (package->derivation package system
  1463. #:graft? #f))))
  1464. (return (list name (gexp-input drv output #:native? (not target))))))
  1465. (((? string? name) (? file-like? thing))
  1466. (return (list name (gexp-input thing #:native? (not target)))))
  1467. (((? string? name) (? file-like? thing) (? string? output))
  1468. (return (list name (gexp-input thing output #:native? (not target)))))
  1469. (((? string? name)
  1470. (and (? string?) (? file-exists? file)))
  1471. ;; Add FILE to the store. When FILE is in the sub-directory of a
  1472. ;; store path, it needs to be added anyway, so it can be used as a
  1473. ;; source.
  1474. (return (list name (gexp-input (local-file file #:recursive? #t)
  1475. #:native? (not target)))))
  1476. (x
  1477. (raise (condition (&package-input-error
  1478. (package package)
  1479. (input x))))))))
  1480. (define %bag-cache
  1481. ;; 'eq?' cache mapping packages to system+target+graft?-dependent bags.
  1482. ;; It significantly speeds things up when doing repeated calls to
  1483. ;; 'package->bag' as is the case when building a profile.
  1484. (make-weak-key-hash-table 200))
  1485. (define* (package->bag package #:optional
  1486. (system (%current-system))
  1487. (target (%current-target-system))
  1488. #:key (graft? (%graft?)))
  1489. "Compile PACKAGE into a bag for SYSTEM, possibly cross-compiled to TARGET,
  1490. and return it."
  1491. (let ((package (or (and graft? (package-replacement package))
  1492. package)))
  1493. (cached (=> %bag-cache)
  1494. package (list system target)
  1495. ;; Bind %CURRENT-SYSTEM and %CURRENT-TARGET-SYSTEM so that thunked
  1496. ;; field values can refer to it.
  1497. (parameterize ((%current-system system)
  1498. (%current-target-system target))
  1499. (match package
  1500. ((and self
  1501. ($ <package> name version source build-system
  1502. args inputs propagated-inputs native-inputs
  1503. outputs))
  1504. ;; Even though we prefer to use "@" to separate the package
  1505. ;; name from the package version in various user-facing parts
  1506. ;; of Guix, checkStoreName (in nix/libstore/store-api.cc)
  1507. ;; prohibits the use of "@", so use "-" instead.
  1508. (or (make-bag build-system (string-append name "-" version)
  1509. #:system system
  1510. #:target target
  1511. #:source source
  1512. #:inputs (append (inputs self)
  1513. (propagated-inputs self))
  1514. #:outputs outputs
  1515. #:native-inputs (native-inputs self)
  1516. #:arguments (args self))
  1517. (raise (if target
  1518. (condition
  1519. (&package-cross-build-system-error
  1520. (package package)))
  1521. (condition
  1522. (&package-error
  1523. (package package))))))))))))
  1524. (define %package-graft-cache
  1525. ;; Cache mapping <package> records to <graft> records, for packages that
  1526. ;; have a replacement.
  1527. (allocate-store-connection-cache 'package-graft-cache))
  1528. (define (input-graft system)
  1529. "Return a monadic procedure that, given a package with a graft, returns a
  1530. graft, and #f otherwise."
  1531. (with-monad %store-monad
  1532. (match-lambda*
  1533. (((? package? package) output)
  1534. (let ((replacement (package-replacement package)))
  1535. (if replacement
  1536. (mcached eq? (=> %package-graft-cache)
  1537. (mlet %store-monad ((orig (package->derivation package system
  1538. #:graft? #f))
  1539. (new (package->derivation replacement system
  1540. #:graft? #t)))
  1541. (return (graft
  1542. (origin orig)
  1543. (origin-output output)
  1544. (replacement new)
  1545. (replacement-output output))))
  1546. package output system)
  1547. (return #f))))
  1548. (_
  1549. (return #f)))))
  1550. (define (input-cross-graft target system)
  1551. "Same as 'input-graft', but for cross-compilation inputs."
  1552. (with-monad %store-monad
  1553. (match-lambda*
  1554. (((? package? package) output)
  1555. (let ((replacement (package-replacement package)))
  1556. (if replacement
  1557. (mlet %store-monad ((orig (package->cross-derivation package
  1558. target system
  1559. #:graft? #f))
  1560. (new (package->cross-derivation replacement
  1561. target system
  1562. #:graft? #t)))
  1563. (return (graft
  1564. (origin orig)
  1565. (origin-output output)
  1566. (replacement new)
  1567. (replacement-output output))))
  1568. (return #f))))
  1569. (_
  1570. (return #f)))))
  1571. (define* (fold-bag-dependencies proc seed bag
  1572. #:key (native? #t))
  1573. "Fold PROC over the packages BAG depends on. Each package is visited only
  1574. once, in depth-first order. If NATIVE? is true, restrict to native
  1575. dependencies; otherwise, restrict to target dependencies."
  1576. (define bag-direct-inputs*
  1577. (if native?
  1578. (lambda (bag)
  1579. (append (bag-build-inputs bag)
  1580. (bag-target-inputs bag)
  1581. (if (bag-target bag)
  1582. '()
  1583. (bag-host-inputs bag))))
  1584. bag-host-inputs))
  1585. (let loop ((inputs (bag-direct-inputs* bag))
  1586. (result seed)
  1587. (visited vlist-null))
  1588. (match inputs
  1589. (()
  1590. result)
  1591. (((label (? package? head) . rest) . tail)
  1592. (let ((output (match rest (() "out") ((output) output)))
  1593. (outputs (vhash-foldq* cons '() head visited)))
  1594. (if (member output outputs)
  1595. (loop tail result visited)
  1596. (let ((inputs (bag-direct-inputs* (package->bag head))))
  1597. (loop (append inputs tail)
  1598. (proc head output result)
  1599. (vhash-consq head output visited))))))
  1600. ((head . tail)
  1601. (loop tail result visited)))))
  1602. (define* (bag-grafts bag)
  1603. "Return the list of grafts potentially applicable to BAG. Potentially
  1604. applicable grafts are collected by looking at direct or indirect dependencies
  1605. of BAG that have a 'replacement'. Whether a graft is actually applicable
  1606. depends on whether the outputs of BAG depend on the items the grafts refer
  1607. to (see 'graft-derivation'.)"
  1608. (define system (bag-system bag))
  1609. (define target (bag-target bag))
  1610. (mlet %store-monad
  1611. ((native-grafts
  1612. (let ((->graft (input-graft system)))
  1613. (parameterize ((%current-system system)
  1614. (%current-target-system #f))
  1615. (fold-bag-dependencies (lambda (package output grafts)
  1616. (mlet %store-monad ((grafts grafts))
  1617. (>>= (->graft package output)
  1618. (match-lambda
  1619. (#f (return grafts))
  1620. (graft (return (cons graft grafts)))))))
  1621. (return '())
  1622. bag))))
  1623. (target-grafts
  1624. (if target
  1625. (let ((->graft (input-cross-graft target system)))
  1626. (parameterize ((%current-system system)
  1627. (%current-target-system target))
  1628. (fold-bag-dependencies
  1629. (lambda (package output grafts)
  1630. (mlet %store-monad ((grafts grafts))
  1631. (>>= (->graft package output)
  1632. (match-lambda
  1633. (#f (return grafts))
  1634. (graft (return (cons graft grafts)))))))
  1635. (return '())
  1636. bag
  1637. #:native? #f)))
  1638. (return '()))))
  1639. ;; We can end up with several identical grafts if we stumble upon packages
  1640. ;; that are not 'eq?' but map to the same derivation (this can happen when
  1641. ;; using things like 'package-with-explicit-inputs'.) Hence the
  1642. ;; 'delete-duplicates' call.
  1643. (return (delete-duplicates
  1644. (append native-grafts target-grafts)))))
  1645. (define* (package-grafts* package
  1646. #:optional (system (%current-system))
  1647. #:key target)
  1648. "Return the list of grafts applicable to PACKAGE as built for SYSTEM and
  1649. TARGET."
  1650. (let* ((package (or (package-replacement package) package))
  1651. (bag (package->bag package system target)))
  1652. (bag-grafts bag)))
  1653. (define package-grafts
  1654. (store-lower package-grafts*))
  1655. (define-inlinable (derivation=? drv1 drv2)
  1656. "Return true if DRV1 and DRV2 are equal."
  1657. (or (eq? drv1 drv2)
  1658. (string=? (derivation-file-name drv1)
  1659. (derivation-file-name drv2))))
  1660. (define (input=? input1 input2)
  1661. "Return true if INPUT1 and INPUT2 are equivalent."
  1662. (match input1
  1663. ((label1 obj1 . outputs1)
  1664. (match input2
  1665. ((label2 obj2 . outputs2)
  1666. (and (string=? label1 label2)
  1667. (equal? outputs1 outputs2)
  1668. (or (and (derivation? obj1) (derivation? obj2)
  1669. (derivation=? obj1 obj2))
  1670. (equal? obj1 obj2))))))))
  1671. (define* (bag->derivation bag #:optional context)
  1672. "Return the derivation to build BAG for SYSTEM. Optionally, CONTEXT can be
  1673. a package object describing the context in which the call occurs, for improved
  1674. error reporting."
  1675. (if (bag-target bag)
  1676. (bag->cross-derivation bag)
  1677. (mlet* %store-monad ((system -> (bag-system bag))
  1678. (inputs -> (bag-transitive-inputs bag))
  1679. (input-drvs (mapm %store-monad
  1680. (cut expand-input context <> system)
  1681. inputs))
  1682. (paths -> (delete-duplicates
  1683. (append-map (match-lambda
  1684. ((_ (? package? p) _ ...)
  1685. (package-native-search-paths
  1686. p))
  1687. (_ '()))
  1688. inputs))))
  1689. ;; It's possible that INPUTS contains packages that are not 'eq?' but
  1690. ;; that lead to the same derivation. Delete those duplicates to avoid
  1691. ;; issues down the road, such as duplicate entries in '%build-inputs'.
  1692. (apply (bag-build bag) (bag-name bag)
  1693. (delete-duplicates input-drvs input=?)
  1694. #:search-paths paths
  1695. #:outputs (bag-outputs bag) #:system system
  1696. (bag-arguments bag)))))
  1697. (define* (bag->cross-derivation bag #:optional context)
  1698. "Return the derivation to build BAG, which is actually a cross build.
  1699. Optionally, CONTEXT can be a package object denoting the context of the call.
  1700. This is an internal procedure."
  1701. (mlet* %store-monad ((system -> (bag-system bag))
  1702. (target -> (bag-target bag))
  1703. (host -> (bag-transitive-host-inputs bag))
  1704. (host-drvs (mapm %store-monad
  1705. (cut expand-input context <>
  1706. system #:target target)
  1707. host))
  1708. (target* -> (bag-transitive-target-inputs bag))
  1709. (target-drvs (mapm %store-monad
  1710. (cut expand-input context <> system)
  1711. target*))
  1712. (build -> (bag-transitive-build-inputs bag))
  1713. (build-drvs (mapm %store-monad
  1714. (cut expand-input context <> system)
  1715. build))
  1716. (all -> (append build target* host))
  1717. (paths -> (delete-duplicates
  1718. (append-map (match-lambda
  1719. ((_ (? package? p) _ ...)
  1720. (package-search-paths p))
  1721. (_ '()))
  1722. all)))
  1723. (npaths -> (delete-duplicates
  1724. (append-map (match-lambda
  1725. ((_ (? package? p) _ ...)
  1726. (package-native-search-paths
  1727. p))
  1728. (_ '()))
  1729. all))))
  1730. (apply (bag-build bag) (bag-name bag)
  1731. #:build-inputs (delete-duplicates build-drvs input=?)
  1732. #:host-inputs (delete-duplicates host-drvs input=?)
  1733. #:target-inputs (delete-duplicates target-drvs input=?)
  1734. #:search-paths paths
  1735. #:native-search-paths npaths
  1736. #:outputs (bag-outputs bag)
  1737. #:system system #:target target
  1738. (bag-arguments bag))))
  1739. (define bag->derivation*
  1740. (store-lower bag->derivation))
  1741. (define graft-derivation*
  1742. (store-lift graft-derivation))
  1743. (define* (package->derivation package
  1744. #:optional (system (%current-system))
  1745. #:key (graft? (%graft?)))
  1746. "Return the <derivation> object of PACKAGE for SYSTEM."
  1747. ;; Compute the derivation and cache the result. Caching is important
  1748. ;; because some derivations, such as the implicit inputs of the GNU build
  1749. ;; system, will be queried many, many times in a row.
  1750. (mcached (mlet* %store-monad ((bag -> (package->bag package system #f
  1751. #:graft? graft?))
  1752. (drv (bag->derivation bag package)))
  1753. (if graft?
  1754. (>>= (bag-grafts bag)
  1755. (match-lambda
  1756. (()
  1757. (return drv))
  1758. (grafts
  1759. (mlet %store-monad ((guile (package->derivation
  1760. (guile-for-grafts)
  1761. system #:graft? #f)))
  1762. (graft-derivation* drv grafts
  1763. #:system system
  1764. #:guile guile)))))
  1765. (return drv)))
  1766. package system #f graft?))
  1767. (define* (package->cross-derivation package target
  1768. #:optional (system (%current-system))
  1769. #:key (graft? (%graft?)))
  1770. "Cross-build PACKAGE for TARGET (a GNU triplet) from host SYSTEM (a Guix
  1771. system identifying string)."
  1772. (mcached (mlet* %store-monad ((bag -> (package->bag package system target
  1773. #:graft? graft?))
  1774. (drv (bag->derivation bag package)))
  1775. (if graft?
  1776. (>>= (bag-grafts bag)
  1777. (match-lambda
  1778. (()
  1779. (return drv))
  1780. (grafts
  1781. (mlet %store-monad ((guile (package->derivation
  1782. (guile-for-grafts)
  1783. system #:graft? #f)))
  1784. (graft-derivation* drv grafts
  1785. #:system system
  1786. #:guile guile)))))
  1787. (return drv)))
  1788. package system target graft?))
  1789. (define* (package-output store package
  1790. #:optional (output "out") (system (%current-system)))
  1791. "Return the output path of PACKAGE's OUTPUT for SYSTEM---where OUTPUT is the
  1792. symbolic output name, such as \"out\". Note that this procedure calls
  1793. `package-derivation', which is costly."
  1794. (let ((drv (package-derivation store package system)))
  1795. (derivation->output-path drv output)))
  1796. ;;;
  1797. ;;; Monadic interface.
  1798. ;;;
  1799. (define (set-guile-for-build guile)
  1800. "This monadic procedure changes the Guile currently used to run the build
  1801. code of derivations to GUILE, a package object."
  1802. (lambda (store)
  1803. (let ((guile (package-derivation store guile)))
  1804. (values (%guile-for-build guile) store))))
  1805. (define* (package-file package
  1806. #:optional file
  1807. #:key
  1808. system (output "out") target)
  1809. "Return as a monadic value the absolute file name of FILE within the
  1810. OUTPUT directory of PACKAGE. When FILE is omitted, return the name of the
  1811. OUTPUT directory of PACKAGE. When TARGET is true, use it as a
  1812. cross-compilation target triplet.
  1813. Note that this procedure does _not_ build PACKAGE. Thus, the result might or
  1814. might not designate an existing file. We recommend not using this procedure
  1815. unless you know what you are doing."
  1816. (lambda (store)
  1817. (define compute-derivation
  1818. (if target
  1819. (cut package-cross-derivation <> <> target <>)
  1820. package-derivation))
  1821. (let* ((system (or system (%current-system)))
  1822. (drv (compute-derivation store package system))
  1823. (out (derivation->output-path drv output)))
  1824. (values (if file
  1825. (string-append out "/" file)
  1826. out)
  1827. store))))
  1828. (define package-derivation
  1829. (store-lower package->derivation))
  1830. (define package-cross-derivation
  1831. (store-lower package->cross-derivation))
  1832. (define-gexp-compiler (package-compiler (package <package>) system target)
  1833. ;; Compile PACKAGE to a derivation for SYSTEM, optionally cross-compiled for
  1834. ;; TARGET. This is used when referring to a package from within a gexp.
  1835. (if target
  1836. (package->cross-derivation package target system)
  1837. (package->derivation package system)))
  1838. (define* (origin->derivation origin
  1839. #:optional (system (%current-system)))
  1840. "Return the derivation corresponding to ORIGIN."
  1841. (match origin
  1842. (($ <origin> uri method hash name (= force ()) #f)
  1843. ;; No patches, no snippet: this is a fixed-output derivation.
  1844. (method uri
  1845. (content-hash-algorithm hash)
  1846. (content-hash-value hash)
  1847. name #:system system))
  1848. (($ <origin> uri method hash name (= force (patches ...)) snippet
  1849. flags inputs (modules ...) guile-for-build)
  1850. ;; Patches and/or a snippet.
  1851. (mlet %store-monad ((source (method uri
  1852. (content-hash-algorithm hash)
  1853. (content-hash-value hash)
  1854. name #:system system))
  1855. (guile (package->derivation (or guile-for-build
  1856. (default-guile))
  1857. system
  1858. #:graft? #f)))
  1859. (patch-and-repack source patches
  1860. #:inputs inputs
  1861. #:snippet snippet
  1862. #:flags flags
  1863. #:system system
  1864. #:modules modules
  1865. #:guile-for-build guile)))))
  1866. (define-gexp-compiler (origin-compiler (origin <origin>) system target)
  1867. ;; Compile ORIGIN to a derivation for SYSTEM. This is used when referring
  1868. ;; to an origin from within a gexp.
  1869. (origin->derivation origin system))
  1870. (define package-source-derivation ;somewhat deprecated
  1871. (let ((lower (store-lower lower-object)))
  1872. (lambda* (store source #:optional (system (%current-system)))
  1873. "Return the derivation or file corresponding to SOURCE, which can be an
  1874. a file name or any object handled by 'lower-object', such as an <origin>.
  1875. When SOURCE is a file name, return either the interned file name (if SOURCE is
  1876. outside of the store) or SOURCE itself (if SOURCE is already a store item.)"
  1877. (match source
  1878. ((and (? string?) (? direct-store-path?) file)
  1879. file)
  1880. ((? string? file)
  1881. (add-to-store store (basename file) #t "sha256" file))
  1882. (_
  1883. (lower store source system))))))