packages.scm 82 KB

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