packages.scm 82 KB

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