packages.scm 83 KB

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