packages.scm 78 KB

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