packages.scm 86 KB

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