gexp.scm 92 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2014-2022 Ludovic Courtès <ludo@gnu.org>
  3. ;;; Copyright © 2018 Clément Lassieur <clement@lassieur.org>
  4. ;;; Copyright © 2018 Jan Nieuwenhuizen <janneke@gnu.org>
  5. ;;; Copyright © 2019, 2020 Mathieu Othacehe <m.othacehe@gmail.com>
  6. ;;; Copyright © 2020 Maxim Cournoyer <maxim.cournoyer@gmail.com>
  7. ;;; Copyright © 2021, 2022 Maxime Devos <maximedevos@telenet.be>
  8. ;;;
  9. ;;; This file is part of GNU Guix.
  10. ;;;
  11. ;;; GNU Guix is free software; you can redistribute it and/or modify it
  12. ;;; under the terms of the GNU General Public License as published by
  13. ;;; the Free Software Foundation; either version 3 of the License, or (at
  14. ;;; your option) any later version.
  15. ;;;
  16. ;;; GNU Guix is distributed in the hope that it will be useful, but
  17. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  18. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. ;;; GNU General Public License for more details.
  20. ;;;
  21. ;;; You should have received a copy of the GNU General Public License
  22. ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
  23. (define-module (guix gexp)
  24. #:use-module (guix store)
  25. #:use-module (guix monads)
  26. #:use-module (guix derivations)
  27. #:use-module (guix grafts)
  28. #:use-module (guix utils)
  29. #:use-module (guix diagnostics)
  30. #:use-module (guix i18n)
  31. #:use-module (rnrs bytevectors)
  32. #:use-module (srfi srfi-1)
  33. #:use-module (srfi srfi-9)
  34. #:use-module (srfi srfi-9 gnu)
  35. #:use-module (srfi srfi-26)
  36. #:use-module (srfi srfi-34)
  37. #:use-module (srfi srfi-35)
  38. #:use-module (ice-9 format)
  39. #:use-module (ice-9 match)
  40. #:export (gexp
  41. gexp?
  42. sexp->gexp
  43. with-imported-modules
  44. with-extensions
  45. let-system
  46. gexp->approximate-sexp
  47. gexp-input
  48. gexp-input?
  49. gexp-input-thing
  50. gexp-input-output
  51. gexp-input-native?
  52. assume-valid-file-name
  53. local-file
  54. local-file?
  55. local-file-file
  56. local-file-absolute-file-name
  57. local-file-name
  58. local-file-recursive?
  59. local-file-select?
  60. plain-file
  61. plain-file?
  62. plain-file-name
  63. plain-file-content
  64. computed-file
  65. computed-file?
  66. computed-file-name
  67. computed-file-gexp
  68. computed-file-options
  69. program-file
  70. program-file?
  71. program-file-name
  72. program-file-gexp
  73. program-file-guile
  74. program-file-module-path
  75. scheme-file
  76. scheme-file?
  77. scheme-file-name
  78. scheme-file-gexp
  79. file-append
  80. file-append?
  81. file-append-base
  82. file-append-suffix
  83. raw-derivation-file
  84. raw-derivation-file?
  85. with-parameters
  86. parameterized?
  87. load-path-expression
  88. gexp-modules
  89. lower-gexp
  90. lowered-gexp?
  91. lowered-gexp-sexp
  92. lowered-gexp-inputs
  93. lowered-gexp-sources
  94. lowered-gexp-guile
  95. lowered-gexp-load-path
  96. lowered-gexp-load-compiled-path
  97. with-build-variables
  98. input-tuples->gexp
  99. outputs->gexp
  100. gexp->derivation
  101. gexp->file
  102. gexp->script
  103. text-file*
  104. mixed-text-file
  105. file-union
  106. directory-union
  107. imported-files
  108. imported-modules
  109. compiled-modules
  110. define-gexp-compiler
  111. gexp-compiler?
  112. file-like?
  113. lower-object
  114. &gexp-error
  115. gexp-error?
  116. &gexp-input-error
  117. gexp-input-error?
  118. gexp-error-invalid-input))
  119. ;;; Commentary:
  120. ;;;
  121. ;;; This module implements "G-expressions", or "gexps". Gexps are like
  122. ;;; S-expressions (sexps), with two differences:
  123. ;;;
  124. ;;; 1. References (un-quotations) to derivations or packages in a gexp are
  125. ;;; replaced by the corresponding output file name; in addition, the
  126. ;;; 'ungexp-native' unquote-like form allows code to explicitly refer to
  127. ;;; the native code of a given package, in case of cross-compilation;
  128. ;;;
  129. ;;; 2. Gexps embed information about the derivations they refer to.
  130. ;;;
  131. ;;; Gexps make it easy to write to files Scheme code that refers to store
  132. ;;; items, or to write Scheme code to build derivations.
  133. ;;;
  134. ;;; Code:
  135. ;; "G expressions".
  136. (define-record-type <gexp>
  137. (make-gexp references modules extensions proc location)
  138. gexp?
  139. (references gexp-references) ;list of <gexp-input>
  140. (modules gexp-self-modules) ;list of module names
  141. (extensions gexp-self-extensions) ;list of lowerable things
  142. (proc gexp-proc) ;procedure
  143. (location %gexp-location)) ;location alist
  144. (define (gexp-location gexp)
  145. "Return the source code location of GEXP."
  146. (and=> (%gexp-location gexp) source-properties->location))
  147. (define* (gexp->approximate-sexp gexp)
  148. "Return the S-expression corresponding to GEXP, but do not lower anything.
  149. As a result, the S-expression will be approximate if GEXP has references."
  150. (define (gexp-like? thing)
  151. (or (gexp? thing) (gexp-input? thing)))
  152. (apply (gexp-proc gexp)
  153. (map (lambda (reference)
  154. (match reference
  155. (($ <gexp-input> thing output native)
  156. (cond ((gexp-like? thing)
  157. (gexp->approximate-sexp thing))
  158. ((not (record? thing)) ; a S-exp
  159. thing)
  160. (#true
  161. ;; Simply returning 'thing' won't work in some
  162. ;; situations; see 'write-gexp' below.
  163. '(*approximate*))))
  164. (($ <gexp-output>) '(*approximate*))))
  165. (gexp-references gexp))))
  166. (define (write-gexp gexp port)
  167. "Write GEXP on PORT."
  168. (display "#<gexp " port)
  169. ;; Try to write the underlying sexp. Now, this trick doesn't work when
  170. ;; doing things like (ungexp-splicing (gexp ())) because GEXP's procedure
  171. ;; tries to use 'append' on that, which fails with wrong-type-arg.
  172. (false-if-exception
  173. (write (apply (gexp-proc gexp)
  174. (gexp-references gexp))
  175. port))
  176. (let ((loc (gexp-location gexp)))
  177. (when loc
  178. (format port " ~a" (location->string loc))))
  179. (format port " ~a>"
  180. (number->string (object-address gexp) 16)))
  181. (set-record-type-printer! <gexp> write-gexp)
  182. (define (gexp-with-hidden-inputs gexp inputs)
  183. "Add INPUTS, a list of <gexp-input>, to the references of GEXP. These are
  184. \"hidden inputs\" because they do not actually appear in the expansion of GEXP
  185. returned by 'gexp->sexp'."
  186. (make-gexp (append inputs (gexp-references gexp))
  187. (gexp-self-modules gexp)
  188. (gexp-self-extensions gexp)
  189. (let ((extra (length inputs)))
  190. (lambda args
  191. (apply (gexp-proc gexp) (drop args extra))))
  192. (gexp-location gexp)))
  193. ;;;
  194. ;;; Methods.
  195. ;;;
  196. ;; Compiler for a type of objects that may be introduced in a gexp.
  197. (define-record-type <gexp-compiler>
  198. (gexp-compiler type lower expand)
  199. gexp-compiler?
  200. (type gexp-compiler-type) ;record type descriptor
  201. (lower gexp-compiler-lower)
  202. (expand gexp-compiler-expand)) ;#f | DRV -> sexp
  203. (define-condition-type &gexp-error &error
  204. gexp-error?)
  205. (define-condition-type &gexp-input-error &gexp-error
  206. gexp-input-error?
  207. (input gexp-error-invalid-input))
  208. (define %gexp-compilers
  209. ;; 'eq?' mapping of record type descriptor to <gexp-compiler>.
  210. (make-hash-table 20))
  211. (define (default-expander thing obj output)
  212. "This is the default expander for \"things\" that appear in gexps. It
  213. returns its output file name of OBJ's OUTPUT."
  214. (match obj
  215. ((? derivation? drv)
  216. (derivation->output-path drv output))
  217. ((? string? file)
  218. file)
  219. ((? self-quoting? obj)
  220. obj)))
  221. (define (register-compiler! compiler)
  222. "Register COMPILER as a gexp compiler."
  223. (hashq-set! %gexp-compilers
  224. (gexp-compiler-type compiler) compiler))
  225. (define (lookup-compiler object)
  226. "Search for a compiler for OBJECT. Upon success, return the three argument
  227. procedure to lower it; otherwise return #f."
  228. (and=> (hashq-ref %gexp-compilers (struct-vtable object))
  229. gexp-compiler-lower))
  230. (define (file-like? object)
  231. "Return #t if OBJECT leads to a file in the store once unquoted in a
  232. G-expression; otherwise return #f."
  233. (and (struct? object) (->bool (lookup-compiler object))))
  234. (define (lookup-expander object)
  235. "Search for an expander for OBJECT. Upon success, return the three argument
  236. procedure to expand it; otherwise return #f."
  237. (and=> (hashq-ref %gexp-compilers (struct-vtable object))
  238. gexp-compiler-expand))
  239. (define* (lower-object obj
  240. #:optional (system (%current-system))
  241. #:key (target 'current))
  242. "Return as a value in %STORE-MONAD the derivation or store item
  243. corresponding to OBJ for SYSTEM, cross-compiling for TARGET if TARGET is true.
  244. OBJ must be an object that has an associated gexp compiler, such as a
  245. <package>."
  246. (mlet %store-monad ((target (if (eq? target 'current)
  247. (current-target-system)
  248. (return target)))
  249. (graft? (grafting?)))
  250. (let loop ((obj obj))
  251. (match (lookup-compiler obj)
  252. (#f
  253. (raise (condition (&gexp-input-error (input obj)))))
  254. (lower
  255. ;; Cache in STORE the result of lowering OBJ. If OBJ is a
  256. ;; derivation, bypass the cache.
  257. (if (derivation? obj)
  258. (return obj)
  259. (mcached (mlet %store-monad ((lowered (lower obj system target)))
  260. (if (and (struct? lowered)
  261. (not (derivation? lowered)))
  262. (loop lowered)
  263. (return lowered)))
  264. obj
  265. system target graft?)))))))
  266. (define* (lower+expand-object obj
  267. #:optional (system (%current-system))
  268. #:key target (output "out"))
  269. "Return as a value in %STORE-MONAD the output of object OBJ expands to for
  270. SYSTEM and TARGET. Object such as <package>, <file-append>, or <plain-file>
  271. expand to file names, but it's possible to expand to a plain data type."
  272. (let loop ((obj obj)
  273. (expand (and (struct? obj) (lookup-expander obj))))
  274. (match (lookup-compiler obj)
  275. (#f
  276. (raise (condition (&gexp-input-error (input obj)))))
  277. (lower
  278. (mlet* %store-monad ((graft? (grafting?))
  279. (lowered (if (derivation? obj)
  280. (return obj)
  281. (mcached (lower obj system target)
  282. obj
  283. system target graft?))))
  284. ;; LOWER might return something that needs to be further
  285. ;; lowered.
  286. (if (struct? lowered)
  287. ;; If we lack an expander, delegate to that of LOWERED.
  288. (if (not expand)
  289. (loop lowered (lookup-expander lowered))
  290. (return (expand obj lowered output)))
  291. (if (not expand) ;self-quoting
  292. (return lowered)
  293. (return (expand obj lowered output)))))))))
  294. (define-syntax define-gexp-compiler
  295. (syntax-rules (=> compiler expander)
  296. "Define NAME as a compiler for objects matching PREDICATE encountered in
  297. gexps.
  298. In the simplest form of the macro, BODY must return (1) a derivation for
  299. a record of the specified type, for SYSTEM and TARGET (the latter of which is
  300. #f except when cross-compiling), (2) another record that can itself be
  301. compiled down to a derivation, or (3) an object of a primitive data type.
  302. The more elaborate form allows you to specify an expander:
  303. (define-gexp-compiler something-compiler <something>
  304. compiler => (lambda (param system target) ...)
  305. expander => (lambda (param drv output) ...))
  306. The expander specifies how an object is converted to its sexp representation."
  307. ((_ (name (param record-type) system target) body ...)
  308. (define-gexp-compiler name record-type
  309. compiler => (lambda (param system target) body ...)
  310. expander => default-expander))
  311. ((_ name record-type
  312. compiler => compile
  313. expander => expand)
  314. (begin
  315. (define name
  316. (gexp-compiler record-type compile expand))
  317. (register-compiler! name)))))
  318. (define-gexp-compiler (derivation-compiler (drv <derivation>) system target)
  319. ;; Derivations are the lowest-level representation, so this is the identity
  320. ;; compiler.
  321. (with-monad %store-monad
  322. (return drv)))
  323. ;; Expand to a raw ".drv" file for the lowerable object it wraps. In other
  324. ;; words, this gives the raw ".drv" file instead of its build result.
  325. (define-record-type <raw-derivation-file>
  326. (raw-derivation-file obj)
  327. raw-derivation-file?
  328. (obj raw-derivation-file-object)) ;lowerable object
  329. (define-gexp-compiler raw-derivation-file-compiler <raw-derivation-file>
  330. compiler => (lambda (obj system target)
  331. (mlet %store-monad ((obj (lower-object
  332. (raw-derivation-file-object obj)
  333. system #:target target)))
  334. ;; Returning the .drv file name instead of the <derivation>
  335. ;; record ensures that 'lower-gexp' will classify it as a
  336. ;; "source" and not as an "input".
  337. (return (if (derivation? obj)
  338. (derivation-file-name obj)
  339. obj))))
  340. expander => (lambda (obj lowered output)
  341. (if (derivation? lowered)
  342. (derivation-file-name lowered)
  343. lowered)))
  344. ;;;
  345. ;;; System dependencies.
  346. ;;;
  347. ;; Binding form for the current system and cross-compilation target.
  348. (define-record-type <system-binding>
  349. (system-binding proc)
  350. system-binding?
  351. (proc system-binding-proc))
  352. (define-syntax let-system
  353. (syntax-rules ()
  354. "Introduce a system binding in a gexp. The simplest form is:
  355. (let-system system
  356. (cond ((string=? system \"x86_64-linux\") ...)
  357. (else ...)))
  358. which binds SYSTEM to the currently targeted system. The second form is
  359. similar, but it also shows the cross-compilation target:
  360. (let-system (system target)
  361. ...)
  362. Here TARGET is bound to the cross-compilation triplet or #f."
  363. ((_ (system target) exp0 exp ...)
  364. (system-binding (lambda (system target)
  365. exp0 exp ...)))
  366. ((_ system exp0 exp ...)
  367. (system-binding (lambda (system target)
  368. exp0 exp ...)))))
  369. (define-gexp-compiler system-binding-compiler <system-binding>
  370. compiler => (lambda (binding system target)
  371. (match binding
  372. (($ <system-binding> proc)
  373. (with-monad %store-monad
  374. ;; PROC is expected to return a lowerable object.
  375. ;; 'lower-object' takes care of residualizing it to a
  376. ;; derivation or similar.
  377. (return (proc system target))))))
  378. ;; Delegate to the expander of the object returned by PROC.
  379. expander => #f)
  380. ;;;
  381. ;;; File declarations.
  382. ;;;
  383. ;; A local file name. FILE is the file name the user entered, which can be a
  384. ;; relative file name, and ABSOLUTE is a promise that computes its canonical
  385. ;; absolute file name. We keep it in a promise to compute it lazily and avoid
  386. ;; repeated 'stat' calls.
  387. (define-record-type <local-file>
  388. (%%local-file file absolute name recursive? select?)
  389. local-file?
  390. (file local-file-file) ;string
  391. (absolute %local-file-absolute-file-name) ;promise string
  392. (name local-file-name) ;string
  393. (recursive? local-file-recursive?) ;Boolean
  394. (select? local-file-select?)) ;string stat -> Boolean
  395. (define (true file stat) #t)
  396. (define* (%local-file file promise #:optional (name (basename file))
  397. #:key
  398. (literal? #t) location
  399. recursive? (select? true))
  400. ;; This intermediate procedure is part of our ABI, but the underlying
  401. ;; %%LOCAL-FILE is not.
  402. (when (and (not literal?) (not (string-prefix? "/" file)))
  403. (warning (and=> location source-properties->location)
  404. (G_ "resolving '~a' relative to current directory~%")
  405. file))
  406. (%%local-file file promise name recursive? select?))
  407. (define (absolute-file-name file directory)
  408. "Return the canonical absolute file name for FILE, which lives in the
  409. vicinity of DIRECTORY."
  410. (canonicalize-path
  411. (cond ((string-prefix? "/" file) file)
  412. ((not directory) file)
  413. ((string-prefix? "/" directory)
  414. (string-append directory "/" file))
  415. (else file))))
  416. (define-syntax-rule (assume-valid-file-name file)
  417. "This is a syntactic keyword to tell 'local-file' that it can assume that
  418. the given file name is valid, even if it's not a string literal, and thus not
  419. warn about it."
  420. file)
  421. (define-syntax local-file
  422. (lambda (s)
  423. "Return an object representing local file FILE to add to the store; this
  424. object can be used in a gexp. If FILE is a relative file name, it is looked
  425. up relative to the source file where this form appears. FILE will be added to
  426. the store under NAME--by default the base name of FILE.
  427. When RECURSIVE? is true, the contents of FILE are added recursively; if FILE
  428. designates a flat file and RECURSIVE? is true, its contents are added, and its
  429. permission bits are kept.
  430. When RECURSIVE? is true, call (SELECT? FILE STAT) for each directory entry,
  431. where FILE is the entry's absolute file name and STAT is the result of
  432. 'lstat'; exclude entries for which SELECT? does not return true.
  433. This is the declarative counterpart of the 'interned-file' monadic procedure.
  434. It is implemented as a macro to capture the current source directory where it
  435. appears."
  436. (syntax-case s (assume-valid-file-name)
  437. ((_ file rest ...)
  438. (string? (syntax->datum #'file))
  439. ;; FILE is a literal, so resolve it relative to the source directory.
  440. #'(%local-file file
  441. (delay (absolute-file-name file (current-source-directory)))
  442. rest ...))
  443. ((_ (assume-valid-file-name file) rest ...)
  444. ;; FILE is not a literal, so resolve it relative to the current
  445. ;; directory. Since the user declared FILE is valid, do not pass
  446. ;; #:literal? #f so that we do not warn about it later on.
  447. #'(%local-file file
  448. (delay (absolute-file-name file (getcwd)))
  449. rest ...))
  450. ((_ file rest ...)
  451. ;; Resolve FILE relative to the current directory.
  452. (with-syntax ((location (datum->syntax s (syntax-source s))))
  453. #`(%local-file file
  454. (delay (absolute-file-name file (getcwd)))
  455. rest ...
  456. #:location 'location
  457. #:literal? #f))) ;warn if FILE is relative
  458. ((_)
  459. #'(syntax-error "missing file name"))
  460. (id
  461. (identifier? #'id)
  462. ;; XXX: We could return #'(lambda (file . rest) ...). However,
  463. ;; (syntax-source #'id) is #f so (current-source-directory) would not
  464. ;; work. Thus, simply forbid this form.
  465. #'(syntax-error
  466. "'local-file' is a macro and cannot be used like this")))))
  467. (define (local-file-absolute-file-name file)
  468. "Return the absolute file name for FILE, a <local-file> instance. A
  469. 'system-error' exception is raised if FILE could not be found."
  470. (force (%local-file-absolute-file-name file)))
  471. (define-gexp-compiler (local-file-compiler (file <local-file>) system target)
  472. ;; "Compile" FILE by adding it to the store.
  473. (match file
  474. (($ <local-file> file (= force absolute) name recursive? select?)
  475. ;; Canonicalize FILE so that if it's a symlink, it is resolved. Failing
  476. ;; to do that, when RECURSIVE? is #t, we could end up creating a dangling
  477. ;; symlink in the store, and when RECURSIVE? is #f 'add-to-store' would
  478. ;; just throw an error, both of which are inconvenient.
  479. (interned-file absolute name
  480. #:recursive? recursive? #:select? select?))))
  481. (define-record-type <plain-file>
  482. (%plain-file name content references)
  483. plain-file?
  484. (name plain-file-name) ;string
  485. (content plain-file-content) ;string or bytevector
  486. (references plain-file-references)) ;list (currently unused)
  487. (define (plain-file name content)
  488. "Return an object representing a text file called NAME with the given
  489. CONTENT (a string) to be added to the store.
  490. This is the declarative counterpart of 'text-file'."
  491. ;; XXX: For now just ignore 'references' because it's not clear how to use
  492. ;; them in a declarative context.
  493. (%plain-file name content '()))
  494. (define-gexp-compiler (plain-file-compiler (file <plain-file>) system target)
  495. ;; "Compile" FILE by adding it to the store.
  496. (match file
  497. (($ <plain-file> name (and (? string?) content) references)
  498. (text-file name content references))
  499. (($ <plain-file> name (and (? bytevector?) content) references)
  500. (binary-file name content references))))
  501. (define-record-type <computed-file>
  502. (%computed-file name gexp guile options)
  503. computed-file?
  504. (name computed-file-name) ;string
  505. (gexp computed-file-gexp) ;gexp
  506. (guile computed-file-guile) ;<package>
  507. (options computed-file-options)) ;list of arguments
  508. (define* (computed-file name gexp
  509. #:key guile (local-build? #t) (options '()))
  510. "Return an object representing the store item NAME, a file or directory
  511. computed by GEXP. When LOCAL-BUILD? is #t (the default), it ensures the
  512. corresponding derivation is built locally. OPTIONS may be used to pass
  513. additional arguments to 'gexp->derivation'.
  514. This is the declarative counterpart of 'gexp->derivation'."
  515. (let ((options* `(#:local-build? ,local-build? ,@options)))
  516. (%computed-file name gexp guile options*)))
  517. (define-gexp-compiler (computed-file-compiler (file <computed-file>)
  518. system target)
  519. ;; Compile FILE by returning a derivation whose build expression is its
  520. ;; gexp.
  521. (match file
  522. (($ <computed-file> name gexp guile options)
  523. (mlet %store-monad ((guile (lower-object (or guile (default-guile))
  524. system #:target #f)))
  525. (apply gexp->derivation name gexp #:guile-for-build guile
  526. #:system system #:target target options)))))
  527. (define-record-type <program-file>
  528. (%program-file name gexp guile path)
  529. program-file?
  530. (name program-file-name) ;string
  531. (gexp program-file-gexp) ;gexp
  532. (guile program-file-guile) ;package
  533. (path program-file-module-path)) ;list of strings
  534. (define* (program-file name gexp #:key (guile #f) (module-path %load-path))
  535. "Return an object representing the executable store item NAME that runs
  536. GEXP. GUILE is the Guile package used to execute that script. Imported
  537. modules of GEXP are looked up in MODULE-PATH.
  538. This is the declarative counterpart of 'gexp->script'."
  539. (%program-file name gexp guile module-path))
  540. (define-gexp-compiler (program-file-compiler (file <program-file>)
  541. system target)
  542. ;; Compile FILE by returning a derivation that builds the script.
  543. (match file
  544. (($ <program-file> name gexp guile module-path)
  545. (gexp->script name gexp
  546. #:module-path module-path
  547. #:guile (or guile (default-guile))
  548. #:system system
  549. #:target target))))
  550. (define-record-type <scheme-file>
  551. (%scheme-file name gexp splice? load-path?)
  552. scheme-file?
  553. (name scheme-file-name) ;string
  554. (gexp scheme-file-gexp) ;gexp
  555. (splice? scheme-file-splice?) ;Boolean
  556. (load-path? scheme-file-set-load-path?)) ;Boolean
  557. (define* (scheme-file name gexp #:key splice? (set-load-path? #t))
  558. "Return an object representing the Scheme file NAME that contains GEXP.
  559. This is the declarative counterpart of 'gexp->file'."
  560. (%scheme-file name gexp splice? set-load-path?))
  561. (define-gexp-compiler (scheme-file-compiler (file <scheme-file>)
  562. system target)
  563. ;; Compile FILE by returning a derivation that builds the file.
  564. (match file
  565. (($ <scheme-file> name gexp splice? set-load-path?)
  566. (gexp->file name gexp
  567. #:set-load-path? set-load-path?
  568. #:splice? splice?
  569. #:system system
  570. #:target target))))
  571. ;; Appending SUFFIX to BASE's output file name.
  572. (define-record-type <file-append>
  573. (%file-append base suffix)
  574. file-append?
  575. (base file-append-base) ;<package> | <derivation> | ...
  576. (suffix file-append-suffix)) ;list of strings
  577. (define (write-file-append file port)
  578. (match file
  579. (($ <file-append> base suffix)
  580. (format port "#<file-append ~s ~s>" base
  581. (string-join suffix)))))
  582. (set-record-type-printer! <file-append> write-file-append)
  583. (define (file-append base . suffix)
  584. "Return a <file-append> object that expands to the concatenation of BASE and
  585. SUFFIX."
  586. (%file-append base suffix))
  587. (define-gexp-compiler file-append-compiler <file-append>
  588. compiler => (lambda (obj system target)
  589. (match obj
  590. (($ <file-append> base _)
  591. (lower-object base system #:target target))))
  592. expander => (lambda (obj lowered output)
  593. (match obj
  594. (($ <file-append> base suffix)
  595. (let* ((expand (or (lookup-expander base)
  596. (lookup-expander lowered)))
  597. (base (expand base lowered output)))
  598. (string-append base (string-concatenate suffix)))))))
  599. ;; Representation of SRFI-39 parameter settings in the dynamic scope of an
  600. ;; object lowering.
  601. (define-record-type <parameterized>
  602. (parameterized bindings thunk)
  603. parameterized?
  604. (bindings parameterized-bindings) ;list of parameter/value pairs
  605. (thunk parameterized-thunk)) ;thunk
  606. (define-syntax-rule (with-parameters ((param value) ...) body ...)
  607. "Bind each PARAM to the corresponding VALUE for the extent during which BODY
  608. is lowered. Consider this example:
  609. (with-parameters ((%current-system \"x86_64-linux\"))
  610. coreutils)
  611. It returns a <parameterized> object that ensures %CURRENT-SYSTEM is set to
  612. x86_64-linux when COREUTILS is lowered."
  613. (parameterized (list (list param (lambda () value)) ...)
  614. (lambda ()
  615. body ...)))
  616. (define-gexp-compiler compile-parameterized <parameterized>
  617. compiler =>
  618. (lambda (parameterized system target)
  619. (match (parameterized-bindings parameterized)
  620. (((parameters values) ...)
  621. (let ((fluids (map parameter-fluid parameters))
  622. (thunk (parameterized-thunk parameterized)))
  623. ;; Install the PARAMETERS for the dynamic extent of THUNK.
  624. (with-fluids* fluids
  625. (map (lambda (thunk) (thunk)) values)
  626. (lambda ()
  627. ;; Special-case '%current-system' and '%current-target-system' to
  628. ;; make sure we get the desired effect.
  629. (let ((system (if (memq %current-system parameters)
  630. (%current-system)
  631. system))
  632. (target (if (memq %current-target-system parameters)
  633. (%current-target-system)
  634. target)))
  635. (lower-object (thunk) system #:target target))))))))
  636. expander => (lambda (parameterized lowered output)
  637. (match (parameterized-bindings parameterized)
  638. (((parameters values) ...)
  639. (let ((fluids (map parameter-fluid parameters))
  640. (thunk (parameterized-thunk parameterized)))
  641. ;; Install the PARAMETERS for the dynamic extent of THUNK.
  642. (with-fluids* fluids
  643. (map (lambda (thunk) (thunk)) values)
  644. (lambda ()
  645. ;; Delegate to the expander of the wrapped object.
  646. (let* ((base (thunk))
  647. (expand (lookup-expander base)))
  648. (expand base lowered output)))))))))
  649. ;;;
  650. ;;; Inputs & outputs.
  651. ;;;
  652. ;; The input of a gexp.
  653. (define-record-type <gexp-input>
  654. (%gexp-input thing output native?)
  655. gexp-input?
  656. (thing gexp-input-thing) ;<package> | <origin> | <derivation> | ...
  657. (output gexp-input-output) ;string
  658. (native? gexp-input-native?)) ;Boolean
  659. (define (write-gexp-input input port)
  660. (match input
  661. (($ <gexp-input> thing output #f)
  662. (format port "#<gexp-input ~s:~a>" thing output))
  663. (($ <gexp-input> thing output #t)
  664. (format port "#<gexp-input native ~s:~a>" thing output))))
  665. (set-record-type-printer! <gexp-input> write-gexp-input)
  666. (define* (gexp-input thing ;convenience procedure
  667. #:optional (output "out")
  668. #:key native?)
  669. "Return a new <gexp-input> for the OUTPUT of THING; NATIVE? determines
  670. whether this should be considered a \"native\" input or not."
  671. (%gexp-input thing output native?))
  672. ;; Reference to one of the derivation's outputs, for gexps used in
  673. ;; derivations.
  674. (define-record-type <gexp-output>
  675. (gexp-output name)
  676. gexp-output?
  677. (name gexp-output-name))
  678. (define (write-gexp-output output port)
  679. (match output
  680. (($ <gexp-output> name)
  681. (format port "#<gexp-output ~a>" name))))
  682. (set-record-type-printer! <gexp-output> write-gexp-output)
  683. (define* (gexp-attribute gexp self-attribute #:optional (equal? equal?)
  684. #:key (validate (const #t)))
  685. "Recurse on GEXP and the expressions it refers to, summing the items
  686. returned by SELF-ATTRIBUTE, a procedure that takes a gexp. Use EQUAL? as the
  687. second argument to 'delete-duplicates'. Pass VALIDATE every gexp and
  688. attribute that is traversed."
  689. (if (gexp? gexp)
  690. (delete-duplicates
  691. (append (let ((attribute (self-attribute gexp)))
  692. (validate gexp attribute)
  693. attribute)
  694. (reverse
  695. (fold (lambda (input result)
  696. (match input
  697. (($ <gexp-input> (? gexp? exp))
  698. (append (gexp-attribute exp self-attribute
  699. #:validate validate)
  700. result))
  701. (($ <gexp-input> (lst ...))
  702. (fold/tree (lambda (obj result)
  703. (match obj
  704. ((? gexp? exp)
  705. (append (gexp-attribute exp self-attribute
  706. #:validate validate)
  707. result))
  708. (_
  709. result)))
  710. result
  711. lst))
  712. (_
  713. result)))
  714. '()
  715. (gexp-references gexp))))
  716. equal?)
  717. '())) ;plain Scheme data type
  718. (define (gexp-modules gexp)
  719. "Return the list of Guile module names GEXP relies on. If (gexp? GEXP) is
  720. false, meaning that GEXP is a plain Scheme object, return the empty list."
  721. (define (module=? m1 m2)
  722. ;; Return #t when M1 equals M2. Special-case '=>' specs because their
  723. ;; right-hand side may not be comparable with 'equal?': it's typically a
  724. ;; file-like object that embeds a gexp, which in turn embeds closure;
  725. ;; those closures may be 'eq?' when running compiled code but are unlikely
  726. ;; to be 'eq?' when running on 'eval'. Ignore the right-hand side to
  727. ;; avoid this discrepancy.
  728. (match m1
  729. (((name1 ...) '=> _)
  730. (match m2
  731. (((name2 ...) '=> _) (equal? name1 name2))
  732. (_ #f)))
  733. (_
  734. (equal? m1 m2))))
  735. (define (validate-modules gexp modules)
  736. ;; Warn if MODULES, imported by GEXP, contains modules that in general
  737. ;; should not be imported from the host because they vary from user to
  738. ;; user and may thus be a source of non-reproducibility. This includes
  739. ;; (guix config) as well as modules that come with Guile.
  740. (match (filter (match-lambda
  741. ((or ('guix 'config) ('ice-9 . _)) #t)
  742. (_ #f))
  743. modules)
  744. (() #t)
  745. (suspects
  746. (warning (gexp-location gexp)
  747. (N_ "importing module~{ ~a~} from the host~%"
  748. "importing modules~{ ~a~} from the host~%"
  749. (length suspects))
  750. suspects))))
  751. (gexp-attribute gexp gexp-self-modules module=?
  752. #:validate validate-modules))
  753. (define (gexp-extensions gexp)
  754. "Return the list of Guile extensions (packages) GEXP relies on. If (gexp?
  755. GEXP) is false, meaning that GEXP is a plain Scheme object, return the empty
  756. list."
  757. (gexp-attribute gexp gexp-self-extensions))
  758. (define (self-quoting? x)
  759. (letrec-syntax ((one-of (syntax-rules ()
  760. ((_) #f)
  761. ((_ pred rest ...)
  762. (or (pred x)
  763. (one-of rest ...))))))
  764. (one-of symbol? string? keyword? pair? null? array?
  765. number? boolean? char?)))
  766. (define (lower-inputs inputs system target)
  767. "Turn any object from INPUTS into a derivation input for SYSTEM or a store
  768. item (a \"source\"); return the corresponding input list as a monadic value.
  769. When TARGET is true, use it as the cross-compilation target triplet."
  770. (define (store-item? obj)
  771. (and (string? obj) (store-path? obj)))
  772. (define filterm
  773. (lift1 (cut filter ->bool <>) %store-monad))
  774. (with-monad %store-monad
  775. (>>= (mapm/accumulate-builds
  776. (match-lambda
  777. (($ <gexp-input> (? store-item? item))
  778. (return item))
  779. (($ <gexp-input> thing output native?)
  780. (mlet %store-monad ((obj (lower-object thing system
  781. #:target
  782. (and (not native?)
  783. target))))
  784. (return (match obj
  785. ((? derivation? drv)
  786. (derivation-input drv (list output)))
  787. ((? store-item? item)
  788. item)
  789. ((? self-quoting?)
  790. ;; Some inputs such as <system-binding> can lower to
  791. ;; a self-quoting object that FILTERM will filter
  792. ;; out.
  793. #f))))))
  794. inputs)
  795. filterm)))
  796. (define* (lower-reference-graphs graphs #:key system target)
  797. "Given GRAPHS, a list of (FILE-NAME INPUT ...) lists for use as a
  798. #:reference-graphs argument, lower it such that each INPUT is replaced by the
  799. corresponding <derivation-input> or store item."
  800. (define tuple->gexp-input
  801. (match-lambda
  802. ((thing)
  803. (%gexp-input thing "out" (not target)))
  804. ((thing output)
  805. (%gexp-input thing output (not target)))))
  806. (match graphs
  807. (((file-names . inputs) ...)
  808. (mlet %store-monad ((inputs (lower-inputs (map tuple->gexp-input inputs)
  809. system target)))
  810. (return (map cons file-names inputs))))))
  811. (define* (lower-references lst #:key system target)
  812. "Based on LST, a list of output names and packages, return a list of output
  813. names and file names suitable for the #:allowed-references argument to
  814. 'derivation'."
  815. (with-monad %store-monad
  816. (define lower
  817. (match-lambda
  818. ((? string? output)
  819. (return output))
  820. (($ <gexp-input> thing output native?)
  821. (mlet %store-monad ((drv (lower-object thing system
  822. #:target (if native?
  823. #f target))))
  824. (return (derivation->output-path drv output))))
  825. (thing
  826. (mlet %store-monad ((drv (lower-object thing system
  827. #:target target)))
  828. (return (derivation->output-path drv))))))
  829. (mapm/accumulate-builds lower lst)))
  830. (define default-guile-derivation
  831. ;; Here we break the abstraction by talking to the higher-level layer.
  832. ;; Thus, do the resolution lazily to hide the circular dependency.
  833. (let ((proc (delay
  834. (let ((iface (resolve-interface '(guix packages))))
  835. (module-ref iface 'default-guile-derivation)))))
  836. (lambda (system)
  837. ((force proc) system))))
  838. ;; Representation of a gexp instantiated for a given target and system.
  839. ;; It's an intermediate representation between <gexp> and <derivation>.
  840. (define-record-type <lowered-gexp>
  841. (lowered-gexp sexp inputs sources guile load-path load-compiled-path)
  842. lowered-gexp?
  843. (sexp lowered-gexp-sexp) ;sexp
  844. (inputs lowered-gexp-inputs) ;list of <derivation-input>
  845. (sources lowered-gexp-sources) ;list of store items
  846. (guile lowered-gexp-guile) ;<derivation-input> | #f
  847. (load-path lowered-gexp-load-path) ;list of store items
  848. (load-compiled-path lowered-gexp-load-compiled-path)) ;list of store items
  849. (define* (imported+compiled-modules modules system
  850. #:key (extensions '())
  851. deprecation-warnings guile
  852. (module-path %load-path))
  853. "Return a pair where the first element is the imported MODULES and the
  854. second element is the derivation to compile them."
  855. (mcached equal?
  856. (mlet %store-monad ((modules (if (pair? modules)
  857. (imported-modules modules
  858. #:guile guile
  859. #:system system
  860. #:module-path module-path)
  861. (return #f)))
  862. (compiled (if (pair? modules)
  863. (compiled-modules modules
  864. #:system system
  865. #:module-path module-path
  866. #:extensions extensions
  867. #:guile guile
  868. #:deprecation-warnings
  869. deprecation-warnings)
  870. (return #f))))
  871. (return (cons modules compiled)))
  872. modules
  873. system extensions guile deprecation-warnings module-path))
  874. (define (sexp->string sexp)
  875. "Like 'object->string', but deterministic and slightly faster."
  876. ;; Explicitly use UTF-8 for determinism, and also because UTF-8 output is
  877. ;; faster.
  878. (with-fluids ((%default-port-encoding "UTF-8"))
  879. (call-with-output-string
  880. (lambda (port)
  881. (write sexp port)))))
  882. (define* (lower-gexp exp
  883. #:key
  884. (module-path %load-path)
  885. (system (%current-system))
  886. (target 'current)
  887. (graft? (%graft?))
  888. (guile-for-build (%guile-for-build))
  889. (effective-version "3.0")
  890. deprecation-warnings)
  891. "*Note: This API is subject to change; use at your own risk!*
  892. Lower EXP, a gexp, instantiating it for SYSTEM and TARGET. Return a
  893. <lowered-gexp> ready to be used.
  894. Lowered gexps are an intermediate representation that's useful for
  895. applications that deal with gexps outside in a way that is disconnected from
  896. derivations--e.g., code evaluated for its side effects."
  897. (define %modules
  898. (delete-duplicates (gexp-modules exp)))
  899. (define (search-path modules extensions suffix)
  900. (append (match modules
  901. ((? derivation? drv)
  902. (list (derivation->output-path drv)))
  903. (#f
  904. '())
  905. ((? store-path? item)
  906. (list item)))
  907. (map (lambda (extension)
  908. (string-append (match extension
  909. ((? derivation? drv)
  910. (derivation->output-path drv))
  911. ((? store-path? item)
  912. item))
  913. suffix))
  914. extensions)))
  915. (mlet* %store-monad ( ;; The following binding forces '%current-system' and
  916. ;; '%current-target-system' to be looked up at >>=
  917. ;; time.
  918. (graft? (set-grafting graft?))
  919. (system -> (or system (%current-system)))
  920. (target -> (if (eq? target 'current)
  921. (%current-target-system)
  922. target))
  923. (guile (if guile-for-build
  924. (return guile-for-build)
  925. (default-guile-derivation system)))
  926. (inputs (lower-inputs (gexp-inputs exp)
  927. system target))
  928. (sexp (gexp->sexp exp system target))
  929. (extensions -> (gexp-extensions exp))
  930. (exts (mapm %store-monad
  931. (lambda (obj)
  932. (lower-object obj system
  933. #:target #f))
  934. extensions))
  935. (modules+compiled (imported+compiled-modules
  936. %modules system
  937. #:extensions extensions
  938. #:deprecation-warnings
  939. deprecation-warnings
  940. #:guile guile
  941. #:module-path module-path))
  942. (modules -> (car modules+compiled))
  943. (compiled -> (cdr modules+compiled)))
  944. (define load-path
  945. (search-path modules exts
  946. (string-append "/share/guile/site/" effective-version)))
  947. (define load-compiled-path
  948. (search-path compiled exts
  949. (string-append "/lib/guile/" effective-version
  950. "/site-ccache")))
  951. (mbegin %store-monad
  952. (set-grafting graft?) ;restore the initial setting
  953. (return (lowered-gexp sexp
  954. `(,@(if (derivation? modules)
  955. (list (derivation-input modules))
  956. '())
  957. ,@(if compiled
  958. (list (derivation-input compiled))
  959. '())
  960. ,@(map derivation-input exts)
  961. ,@(filter derivation-input? inputs))
  962. (filter string? (cons modules inputs))
  963. (derivation-input guile '("out"))
  964. load-path
  965. load-compiled-path)))))
  966. (define* (gexp->derivation name exp
  967. #:key
  968. system (target 'current)
  969. hash hash-algo recursive?
  970. (env-vars '())
  971. (modules '())
  972. (module-path %load-path)
  973. (guile-for-build (%guile-for-build))
  974. (effective-version "3.0")
  975. (graft? (%graft?))
  976. references-graphs
  977. allowed-references disallowed-references
  978. leaked-env-vars
  979. local-build? (substitutable? #t)
  980. (properties '())
  981. deprecation-warnings
  982. (script-name (string-append name "-builder")))
  983. "Return a derivation NAME that runs EXP (a gexp) with GUILE-FOR-BUILD (a
  984. derivation) on SYSTEM; EXP is stored in a file called SCRIPT-NAME. When
  985. TARGET is true, it is used as the cross-compilation target triplet for
  986. packages referred to by EXP.
  987. MODULES is deprecated in favor of 'with-imported-modules'. Its meaning is to
  988. make MODULES available in the evaluation context of EXP; MODULES is a list of
  989. names of Guile modules searched in MODULE-PATH to be copied in the store,
  990. compiled, and made available in the load path during the execution of
  991. EXP---e.g., '((guix build utils) (guix build gnu-build-system)).
  992. EFFECTIVE-VERSION determines the string to use when adding extensions of
  993. EXP (see 'with-extensions') to the search path---e.g., \"2.2\".
  994. GRAFT? determines whether packages referred to by EXP should be grafted when
  995. applicable.
  996. When REFERENCES-GRAPHS is true, it must be a list of tuples of one of the
  997. following forms:
  998. (FILE-NAME PACKAGE)
  999. (FILE-NAME PACKAGE OUTPUT)
  1000. (FILE-NAME DERIVATION)
  1001. (FILE-NAME DERIVATION OUTPUT)
  1002. (FILE-NAME STORE-ITEM)
  1003. The right-hand-side of each element of REFERENCES-GRAPHS is automatically made
  1004. an input of the build process of EXP. In the build environment, each
  1005. FILE-NAME contains the reference graph of the corresponding item, in a simple
  1006. text format.
  1007. ALLOWED-REFERENCES must be either #f or a list of output names and packages.
  1008. In the latter case, the list denotes store items that the result is allowed to
  1009. refer to. Any reference to another store item will lead to a build error.
  1010. Similarly for DISALLOWED-REFERENCES, which can list items that must not be
  1011. referenced by the outputs.
  1012. DEPRECATION-WARNINGS determines whether to show deprecation warnings while
  1013. compiling modules. It can be #f, #t, or 'detailed.
  1014. The other arguments are as for 'derivation'."
  1015. (define outputs (gexp-outputs exp))
  1016. (define requested-graft? graft?)
  1017. (define (graphs-file-names graphs)
  1018. ;; Return a list of (FILE-NAME . STORE-PATH) pairs made from GRAPHS.
  1019. (map (match-lambda
  1020. ((file-name . (? derivation-input? input))
  1021. (cons file-name (first (derivation-input-output-paths input))))
  1022. ((file-name . (? string? item))
  1023. (cons file-name item)))
  1024. graphs))
  1025. (define (add-modules exp modules)
  1026. (if (null? modules)
  1027. exp
  1028. (make-gexp (gexp-references exp)
  1029. (append modules (gexp-self-modules exp))
  1030. (gexp-self-extensions exp)
  1031. (gexp-proc exp)
  1032. (gexp-location exp))))
  1033. (mlet* %store-monad ( ;; The following binding forces '%current-system' and
  1034. ;; '%current-target-system' to be looked up at >>=
  1035. ;; time.
  1036. (graft? (set-grafting graft?))
  1037. (system -> (or system (%current-system)))
  1038. (target -> (if (eq? target 'current)
  1039. (%current-target-system)
  1040. target))
  1041. (exp -> (add-modules exp modules))
  1042. (lowered (lower-gexp exp
  1043. #:module-path module-path
  1044. #:system system
  1045. #:target target
  1046. #:graft? requested-graft?
  1047. #:guile-for-build
  1048. guile-for-build
  1049. #:effective-version
  1050. effective-version
  1051. #:deprecation-warnings
  1052. deprecation-warnings))
  1053. (graphs (if references-graphs
  1054. (lower-reference-graphs references-graphs
  1055. #:system system
  1056. #:target target)
  1057. (return #f)))
  1058. (allowed (if allowed-references
  1059. (lower-references allowed-references
  1060. #:system system
  1061. #:target target)
  1062. (return #f)))
  1063. (disallowed (if disallowed-references
  1064. (lower-references disallowed-references
  1065. #:system system
  1066. #:target target)
  1067. (return #f)))
  1068. (guile -> (lowered-gexp-guile lowered))
  1069. (builder (text-file script-name
  1070. (sexp->string
  1071. (lowered-gexp-sexp lowered)))))
  1072. (mbegin %store-monad
  1073. (set-grafting graft?) ;restore the initial setting
  1074. (raw-derivation name
  1075. (string-append (derivation-input-output-path guile)
  1076. "/bin/guile")
  1077. `("--no-auto-compile"
  1078. ,@(append-map (lambda (directory)
  1079. `("-L" ,directory))
  1080. (lowered-gexp-load-path lowered))
  1081. ,@(append-map (lambda (directory)
  1082. `("-C" ,directory))
  1083. (lowered-gexp-load-compiled-path lowered))
  1084. ,builder)
  1085. #:outputs outputs
  1086. #:env-vars env-vars
  1087. #:system system
  1088. #:inputs `(,guile
  1089. ,@(lowered-gexp-inputs lowered)
  1090. ,@(match graphs
  1091. (((_ . inputs) ...)
  1092. (filter derivation-input? inputs))
  1093. (#f '())))
  1094. #:sources `(,builder
  1095. ,@(if (and (string? modules)
  1096. (store-path? modules))
  1097. (list modules)
  1098. '())
  1099. ,@(lowered-gexp-sources lowered)
  1100. ,@(match graphs
  1101. (((_ . inputs) ...)
  1102. (filter string? inputs))
  1103. (#f '())))
  1104. #:hash hash #:hash-algo hash-algo #:recursive? recursive?
  1105. #:references-graphs (and=> graphs graphs-file-names)
  1106. #:allowed-references allowed
  1107. #:disallowed-references disallowed
  1108. #:leaked-env-vars leaked-env-vars
  1109. #:local-build? local-build?
  1110. #:substitutable? substitutable?
  1111. #:properties properties))))
  1112. (define (fold/tree proc seed lst)
  1113. "Like 'fold', but recurse into sub-lists of LST and accept improper lists."
  1114. (let loop ((obj lst)
  1115. (result seed))
  1116. (match obj
  1117. ((head . tail)
  1118. (loop tail (loop head result)))
  1119. (_
  1120. (proc obj result)))))
  1121. (define (gexp-inputs exp)
  1122. "Return the list of <gexp-input> for EXP."
  1123. (define set-gexp-input-native?
  1124. (match-lambda
  1125. (($ <gexp-input> thing output)
  1126. (%gexp-input thing output #t))))
  1127. (define (interesting? obj)
  1128. (or (file-like? obj)
  1129. (and (string? obj) (direct-store-path? obj))))
  1130. (define (add-reference-inputs ref result)
  1131. (match ref
  1132. (($ <gexp-input> (? gexp? exp) _ #t)
  1133. (append (map set-gexp-input-native? (gexp-inputs exp))
  1134. result))
  1135. (($ <gexp-input> (? gexp? exp) _ #f)
  1136. (append (gexp-inputs exp) result))
  1137. (($ <gexp-input> (? string? str))
  1138. (if (direct-store-path? str)
  1139. (cons ref result)
  1140. result))
  1141. (($ <gexp-input> (? struct? thing) output n?)
  1142. (if (lookup-compiler thing)
  1143. ;; THING is a derivation, or a package, or an origin, etc.
  1144. (cons ref result)
  1145. result))
  1146. (($ <gexp-input> (? pair? lst) output n?)
  1147. ;; XXX: Scan LST for inputs. Inherit N?.
  1148. (fold/tree (lambda (obj result)
  1149. (match obj
  1150. ((? gexp-input? x)
  1151. (cons (%gexp-input (gexp-input-thing x)
  1152. (gexp-input-output x)
  1153. n?)
  1154. result))
  1155. ((? interesting? x)
  1156. (cons (%gexp-input x "out" n?) result))
  1157. ((? gexp? x)
  1158. (append (gexp-inputs x) result))
  1159. (_
  1160. result)))
  1161. result
  1162. lst))
  1163. (_
  1164. ;; Ignore references to other kinds of objects.
  1165. result)))
  1166. (fold-right add-reference-inputs
  1167. '()
  1168. (gexp-references exp)))
  1169. (define (gexp-outputs exp)
  1170. "Return the outputs referred to by EXP as a list of strings."
  1171. (define (add-reference-output ref result)
  1172. (match ref
  1173. (($ <gexp-output> name)
  1174. (cons name result))
  1175. (($ <gexp-input> (? gexp? exp))
  1176. (append (gexp-outputs exp) result))
  1177. (($ <gexp-input> (? pair? lst))
  1178. ;; XXX: Scan LST for outputs.
  1179. (fold/tree (lambda (obj result)
  1180. (match obj
  1181. (($ <gexp-output> name) (cons name result))
  1182. ((? gexp? x) (append (gexp-outputs x) result))
  1183. (_ result)))
  1184. result
  1185. lst))
  1186. (_
  1187. result)))
  1188. (delete-duplicates
  1189. (fold add-reference-output '() (gexp-references exp))))
  1190. (define (gexp->sexp exp system target)
  1191. "Return (monadically) the sexp corresponding to EXP for the given OUTPUT,
  1192. and in the current monad setting (system type, etc.)"
  1193. (define* (reference->sexp ref #:optional native?)
  1194. (with-monad %store-monad
  1195. (match ref
  1196. (($ <gexp-output> output)
  1197. ;; Output file names are not known in advance but the daemon defines
  1198. ;; an environment variable for each of them at build time, so use
  1199. ;; that trick.
  1200. (return `((@ (guile) getenv) ,output)))
  1201. (($ <gexp-input> (? gexp? exp) output n?)
  1202. (gexp->sexp exp
  1203. system (if (or n? native?) #f target)))
  1204. (($ <gexp-input> (refs ...) output n?)
  1205. (mapm %store-monad
  1206. (lambda (ref)
  1207. ;; XXX: Automatically convert REF to an gexp-input.
  1208. (if (or (symbol? ref) (number? ref)
  1209. (boolean? ref) (null? ref) (array? ref))
  1210. (return ref)
  1211. (reference->sexp
  1212. (if (gexp-input? ref)
  1213. ref
  1214. (%gexp-input ref "out" n?))
  1215. (or n? native?))))
  1216. refs))
  1217. (($ <gexp-input> (? struct? thing) output n?)
  1218. (let ((target (if (or n? native?) #f target)))
  1219. (lower+expand-object thing system
  1220. #:target target
  1221. #:output output)))
  1222. (($ <gexp-input> (? self-quoting? x))
  1223. (return x))
  1224. (($ <gexp-input> x)
  1225. (raise (condition (&gexp-input-error (input x)))))
  1226. (x
  1227. (return x)))))
  1228. (mlet %store-monad
  1229. ((args (mapm %store-monad
  1230. reference->sexp (gexp-references exp))))
  1231. (return (apply (gexp-proc exp) args))))
  1232. (define-syntax-parameter current-imported-modules
  1233. ;; Current list of imported modules.
  1234. (identifier-syntax '()))
  1235. (define-syntax-rule (with-imported-modules modules body ...)
  1236. "Mark the gexps defined in BODY... as requiring MODULES in their execution
  1237. environment."
  1238. (syntax-parameterize ((current-imported-modules
  1239. (identifier-syntax modules)))
  1240. body ...))
  1241. (define-syntax-parameter current-imported-extensions
  1242. ;; Current list of extensions.
  1243. (identifier-syntax '()))
  1244. (define-syntax-rule (with-extensions extensions body ...)
  1245. "Mark the gexps defined in BODY... as requiring EXTENSIONS in their
  1246. execution environment."
  1247. (syntax-parameterize ((current-imported-extensions
  1248. (identifier-syntax extensions)))
  1249. body ...))
  1250. (define-syntax gexp
  1251. (lambda (s)
  1252. (define (collect-escapes exp)
  1253. ;; Return all the 'ungexp' present in EXP.
  1254. (let loop ((exp exp)
  1255. (result '()))
  1256. (syntax-case exp (ungexp
  1257. ungexp-splicing
  1258. ungexp-native
  1259. ungexp-native-splicing)
  1260. ((ungexp _)
  1261. (cons exp result))
  1262. ((ungexp _ _)
  1263. (cons exp result))
  1264. ((ungexp-splicing _ ...)
  1265. (cons exp result))
  1266. ((ungexp-native _ ...)
  1267. (cons exp result))
  1268. ((ungexp-native-splicing _ ...)
  1269. (cons exp result))
  1270. ((exp0 . exp)
  1271. (let ((result (loop #'exp0 result)))
  1272. (loop #'exp result)))
  1273. (_
  1274. result))))
  1275. (define (escape->ref exp)
  1276. ;; Turn 'ungexp' form EXP into a "reference".
  1277. (syntax-case exp (ungexp ungexp-splicing
  1278. ungexp-native ungexp-native-splicing
  1279. output)
  1280. ((ungexp output)
  1281. #'(gexp-output "out"))
  1282. ((ungexp output name)
  1283. #'(gexp-output name))
  1284. ((ungexp thing)
  1285. #'(%gexp-input thing "out" #f))
  1286. ((ungexp drv-or-pkg out)
  1287. #'(%gexp-input drv-or-pkg out #f))
  1288. ((ungexp-splicing lst)
  1289. #'(%gexp-input lst "out" #f))
  1290. ((ungexp-native thing)
  1291. #'(%gexp-input thing "out" #t))
  1292. ((ungexp-native drv-or-pkg out)
  1293. #'(%gexp-input drv-or-pkg out #t))
  1294. ((ungexp-native-splicing lst)
  1295. #'(%gexp-input lst "out" #t))))
  1296. (define (substitute-ungexp exp substs)
  1297. ;; Given EXP, an 'ungexp' or 'ungexp-native' form, substitute it with
  1298. ;; the corresponding form in SUBSTS.
  1299. (match (assoc exp substs)
  1300. ((_ id)
  1301. id)
  1302. (_ ;internal error
  1303. (with-syntax ((exp exp))
  1304. #'(syntax-error "error: no 'ungexp' substitution" exp)))))
  1305. (define (substitute-ungexp-splicing exp substs)
  1306. (syntax-case exp ()
  1307. ((exp rest ...)
  1308. (match (assoc #'exp substs)
  1309. ((_ id)
  1310. (with-syntax ((id id))
  1311. #`(append id
  1312. #,(substitute-references #'(rest ...) substs))))
  1313. (_
  1314. #'(syntax-error "error: no 'ungexp-splicing' substitution"
  1315. exp))))))
  1316. (define (substitute-references exp substs)
  1317. ;; Return a variant of EXP where all the cars of SUBSTS have been
  1318. ;; replaced by the corresponding cdr.
  1319. (syntax-case exp (ungexp ungexp-native
  1320. ungexp-splicing ungexp-native-splicing)
  1321. ((ungexp _ ...)
  1322. (substitute-ungexp exp substs))
  1323. ((ungexp-native _ ...)
  1324. (substitute-ungexp exp substs))
  1325. (((ungexp-splicing _ ...) rest ...)
  1326. (substitute-ungexp-splicing exp substs))
  1327. (((ungexp-native-splicing _ ...) rest ...)
  1328. (substitute-ungexp-splicing exp substs))
  1329. ((exp0 . exp)
  1330. #`(cons #,(substitute-references #'exp0 substs)
  1331. #,(substitute-references #'exp substs)))
  1332. (x #''x)))
  1333. (syntax-case s (ungexp output)
  1334. ((_ exp)
  1335. (let* ((escapes (delete-duplicates (collect-escapes #'exp)))
  1336. (formals (generate-temporaries escapes))
  1337. (sexp (substitute-references #'exp (zip escapes formals)))
  1338. (refs (map escape->ref escapes)))
  1339. #`(make-gexp (list #,@refs)
  1340. current-imported-modules
  1341. current-imported-extensions
  1342. (lambda #,formals
  1343. #,sexp)
  1344. (current-source-location)))))))
  1345. ;;;
  1346. ;;; Module handling.
  1347. ;;;
  1348. (define %utils-module
  1349. ;; This file provides 'mkdir-p', needed to implement 'imported-files' and
  1350. ;; other primitives below. Note: We give the file name relative to this
  1351. ;; file you are currently reading; 'search-path' could return a file name
  1352. ;; relative to the current working directory.
  1353. (local-file "build/utils.scm"
  1354. "build-utils.scm"))
  1355. (define* (imported-files/derivation files
  1356. #:key (name "file-import")
  1357. (symlink? #f)
  1358. (system (%current-system))
  1359. (guile (%guile-for-build)))
  1360. "Return a derivation that imports FILES into STORE. FILES must be a list
  1361. of (FINAL-PATH . FILE) pairs. Each FILE is mapped to FINAL-PATH in the
  1362. resulting store path. FILE can be either a file name, or a file-like object,
  1363. as returned by 'local-file' for example. If SYMLINK? is true, create symlinks
  1364. to the source files instead of copying them."
  1365. (define file-pair
  1366. (match-lambda
  1367. ((final-path . (? string? file-name))
  1368. (mlet %store-monad ((file (interned-file file-name
  1369. (basename final-path))))
  1370. (return (list final-path file))))
  1371. ((final-path . file-like)
  1372. (mlet %store-monad ((file (lower-object file-like system)))
  1373. (return (list final-path file))))))
  1374. (mlet %store-monad ((files (mapm %store-monad file-pair files)))
  1375. (define build
  1376. (gexp
  1377. (begin
  1378. (primitive-load (ungexp %utils-module)) ;for 'mkdir-p'
  1379. (use-modules (ice-9 match))
  1380. (mkdir (ungexp output)) (chdir (ungexp output))
  1381. (for-each (match-lambda
  1382. ((final-path store-path)
  1383. (mkdir-p (dirname final-path))
  1384. ((ungexp (if symlink? 'symlink 'copy-file))
  1385. store-path final-path)))
  1386. '(ungexp files)))))
  1387. ;; TODO: Pass FILES as an environment variable so that BUILD remains
  1388. ;; exactly the same regardless of FILES: less disk space, and fewer
  1389. ;; 'add-to-store' RPCs.
  1390. (gexp->derivation name build
  1391. #:system system
  1392. #:guile-for-build guile
  1393. #:local-build? #t
  1394. #:substitutable? #f
  1395. ;; Avoid deprecation warnings about the use of the _IO*
  1396. ;; constants in (guix build utils).
  1397. #:env-vars
  1398. '(("GUILE_WARN_DEPRECATED" . "no")))))
  1399. (define* (imported-files files
  1400. #:key (name "file-import")
  1401. ;; The following parameters make sense when creating
  1402. ;; an actual derivation.
  1403. (system (%current-system))
  1404. (guile (%guile-for-build)))
  1405. "Import FILES into the store and return the resulting derivation or store
  1406. file name (a derivation is created if and only if some elements of FILES are
  1407. file-like objects and not local file names.) FILES must be a list
  1408. of (FINAL-PATH . FILE) pairs. Each FILE is mapped to FINAL-PATH in the
  1409. resulting store path. FILE can be either a file name, or a file-like object,
  1410. as returned by 'local-file' for example."
  1411. (if (any (match-lambda
  1412. ((_ . (? struct? source)) #t)
  1413. (_ #f))
  1414. files)
  1415. (imported-files/derivation files #:name name
  1416. #:symlink? derivation?
  1417. #:system system #:guile guile)
  1418. (interned-file-tree `(,name directory
  1419. ,@(file-mapping->tree files)))))
  1420. (define* (imported-modules modules
  1421. #:key (name "module-import")
  1422. (system (%current-system))
  1423. (guile (%guile-for-build))
  1424. (module-path %load-path))
  1425. "Return a derivation that contains the source files of MODULES, a list of
  1426. module names such as `(ice-9 q)'. All of MODULES must be either names of
  1427. modules to be found in the MODULE-PATH search path, or a module name followed
  1428. by an arrow followed by a file-like object. For example:
  1429. (imported-modules `((guix build utils)
  1430. (guix gcrypt)
  1431. ((guix config) => ,(scheme-file …))))
  1432. In this example, the first two modules are taken from MODULE-PATH, and the
  1433. last one is created from the given <scheme-file> object."
  1434. (let ((files (map (match-lambda
  1435. (((module ...) '=> file)
  1436. (cons (module->source-file-name module)
  1437. file))
  1438. ((module ...)
  1439. (let ((f (module->source-file-name module)))
  1440. (cons f (search-path* module-path f)))))
  1441. modules)))
  1442. (imported-files files #:name name
  1443. #:system system
  1444. #:guile guile)))
  1445. (define* (compiled-modules modules
  1446. #:key (name "module-import-compiled")
  1447. (system (%current-system))
  1448. target
  1449. (guile (%guile-for-build))
  1450. (module-path %load-path)
  1451. (extensions '())
  1452. (deprecation-warnings #f)
  1453. (optimization-level 1))
  1454. "Return a derivation that builds a tree containing the `.go' files
  1455. corresponding to MODULES. All the MODULES are built in a context where
  1456. they can refer to each other. When TARGET is true, cross-compile MODULES for
  1457. TARGET, a GNU triplet."
  1458. (define total (length modules))
  1459. (mlet %store-monad ((modules (imported-modules modules
  1460. #:system system
  1461. #:guile guile
  1462. #:module-path
  1463. module-path))
  1464. (extensions (mapm %store-monad
  1465. (lambda (extension)
  1466. (lower-object extension system
  1467. #:target #f))
  1468. extensions)))
  1469. (define build
  1470. (gexp-with-hidden-inputs
  1471. (gexp
  1472. (begin
  1473. (primitive-load (ungexp %utils-module)) ;for 'mkdir-p'
  1474. (use-modules (ice-9 ftw)
  1475. (ice-9 format)
  1476. (srfi srfi-1)
  1477. (srfi srfi-26)
  1478. (system base target)
  1479. (system base compile))
  1480. (define modules
  1481. (getenv "modules"))
  1482. (define total
  1483. (string->number (getenv "module count")))
  1484. (define extensions
  1485. (string-split (getenv "extensions") #\space))
  1486. (define target
  1487. (getenv "target"))
  1488. (define optimization-level
  1489. (string->number (getenv "optimization level")))
  1490. (define optimizations-for-level
  1491. (or (and=> (false-if-exception
  1492. (resolve-interface '(system base optimize)))
  1493. (lambda (iface)
  1494. (module-ref iface 'optimizations-for-level))) ;Guile 3.0
  1495. (const '())))
  1496. (define (regular? file)
  1497. (not (member file '("." ".."))))
  1498. (define (process-entry entry output processed)
  1499. (if (file-is-directory? entry)
  1500. (let ((output (string-append output "/" (basename entry))))
  1501. (mkdir-p output)
  1502. (process-directory entry output processed))
  1503. (let* ((base (basename entry ".scm"))
  1504. (output (string-append output "/" base ".go")))
  1505. (format #t "[~2@a/~2@a] Compiling '~a'...~%"
  1506. (+ 1 processed total)
  1507. (* total 2)
  1508. entry)
  1509. (with-target (or target %host-type)
  1510. (lambda ()
  1511. (compile-file entry
  1512. #:output-file output
  1513. #:opts
  1514. `(,@%auto-compilation-options
  1515. ,@(optimizations-for-level
  1516. optimization-level)))))
  1517. (+ 1 processed))))
  1518. (define (process-directory directory output processed)
  1519. (let ((entries (map (cut string-append directory "/" <>)
  1520. (scandir directory regular?))))
  1521. (fold (cut process-entry <> output <>)
  1522. processed
  1523. entries)))
  1524. (define* (load-from-directory directory
  1525. #:optional (loaded 0))
  1526. "Load all the source files found in DIRECTORY."
  1527. ;; XXX: This works around <https://bugs.gnu.org/15602>.
  1528. (let ((entries (map (cut string-append directory "/" <>)
  1529. (scandir directory regular?))))
  1530. (fold (lambda (file loaded)
  1531. (if (file-is-directory? file)
  1532. (load-from-directory file loaded)
  1533. (begin
  1534. (format #t "[~2@a/~2@a] Loading '~a'...~%"
  1535. (+ 1 loaded) (* 2 total)
  1536. file)
  1537. (save-module-excursion
  1538. (lambda ()
  1539. (primitive-load file)))
  1540. (+ 1 loaded))))
  1541. loaded
  1542. entries)))
  1543. (setvbuf (current-output-port)
  1544. (cond-expand (guile-2.2 'line) (else _IOLBF)))
  1545. (define mkdir-p
  1546. ;; Capture 'mkdir-p'.
  1547. (@ (guix build utils) mkdir-p))
  1548. ;; Remove environment variables for internal consumption.
  1549. (unsetenv "modules")
  1550. (unsetenv "module count")
  1551. (unsetenv "extensions")
  1552. (unsetenv "target")
  1553. (unsetenv "optimization level")
  1554. ;; Add EXTENSIONS to the search path.
  1555. (set! %load-path
  1556. (append (map (lambda (extension)
  1557. (string-append extension
  1558. "/share/guile/site/"
  1559. (effective-version)))
  1560. extensions)
  1561. %load-path))
  1562. (set! %load-compiled-path
  1563. (append (map (lambda (extension)
  1564. (string-append extension "/lib/guile/"
  1565. (effective-version)
  1566. "/site-ccache"))
  1567. extensions)
  1568. %load-compiled-path))
  1569. (set! %load-path (cons modules %load-path))
  1570. ;; Above we loaded our own (guix build utils) but now we may need to
  1571. ;; load a compile a different one. Thus, force a reload.
  1572. (let ((utils (string-append modules
  1573. "/guix/build/utils.scm")))
  1574. (when (file-exists? utils)
  1575. (load utils)))
  1576. (mkdir (ungexp output))
  1577. (chdir modules)
  1578. (load-from-directory ".")
  1579. (process-directory "." (ungexp output) 0)))
  1580. (append (map gexp-input extensions)
  1581. (list (gexp-input modules)))))
  1582. (gexp->derivation name build
  1583. #:script-name "compile-modules"
  1584. #:system system
  1585. #:target target
  1586. #:guile-for-build guile
  1587. #:local-build? #t
  1588. #:env-vars
  1589. `(("modules"
  1590. . ,(if (derivation? modules)
  1591. (derivation->output-path modules)
  1592. modules))
  1593. ("module count" . ,(number->string total))
  1594. ("extensions"
  1595. . ,(string-join
  1596. (map (match-lambda
  1597. ((? derivation? drv)
  1598. (derivation->output-path drv))
  1599. ((? string? str) str))
  1600. extensions)))
  1601. ("optimization level"
  1602. . ,(number->string optimization-level))
  1603. ,@(if target
  1604. `(("target" . ,target))
  1605. '())
  1606. ,@(case deprecation-warnings
  1607. ((#f)
  1608. '(("GUILE_WARN_DEPRECATED" . "no")))
  1609. ((detailed)
  1610. '(("GUILE_WARN_DEPRECATED" . "detailed")))
  1611. (else
  1612. '()))))))
  1613. ;;;
  1614. ;;; Convenience procedures.
  1615. ;;;
  1616. (define (default-guile)
  1617. ;; Lazily resolve 'guile-3.0' (not 'guile-final' because this is for
  1618. ;; programs returned by 'program-file' and we don't want to keep references
  1619. ;; to several Guile packages). This module must not refer to (gnu …)
  1620. ;; modules directly, to avoid circular dependencies, hence this hack.
  1621. (module-ref (resolve-interface '(gnu packages guile))
  1622. 'guile-3.0))
  1623. (define* (load-path-expression modules #:optional (path %load-path)
  1624. #:key (extensions '()) system target
  1625. (guile (default-guile)))
  1626. "Return as a monadic value a gexp that sets '%load-path' and
  1627. '%load-compiled-path' to point to MODULES, a list of module names. MODULES
  1628. are searched for in PATH. Return #f when MODULES and EXTENSIONS are empty.
  1629. Assume MODULES are compiled with GUILE."
  1630. (if (and (null? modules) (null? extensions))
  1631. (with-monad %store-monad
  1632. (return #f))
  1633. (mlet* %store-monad ((guile (lower-object guile system #:target #f))
  1634. (compiled (compiled-modules modules
  1635. #:guile guile
  1636. #:extensions extensions
  1637. #:module-path path
  1638. #:system system
  1639. #:target target))
  1640. (modules (imported-modules modules
  1641. #:guile guile
  1642. #:module-path path
  1643. #:system system)))
  1644. (return
  1645. (gexp (eval-when (expand load eval)
  1646. ;; Augment the load paths and delete duplicates. Do that
  1647. ;; without loading (srfi srfi-1) or anything.
  1648. (let ((extensions '((ungexp-splicing extensions)))
  1649. (prepend (lambda (items lst)
  1650. ;; This is O(N²) but N is typically small.
  1651. (let loop ((items items)
  1652. (lst lst))
  1653. (if (null? items)
  1654. lst
  1655. (loop (cdr items)
  1656. (cons (car items)
  1657. (delete (car items) lst))))))))
  1658. (set! %load-path
  1659. (prepend (cons (ungexp modules)
  1660. (map (lambda (extension)
  1661. (string-append extension
  1662. "/share/guile/site/"
  1663. (effective-version)))
  1664. extensions))
  1665. %load-path))
  1666. (set! %load-compiled-path
  1667. (prepend (cons (ungexp compiled)
  1668. (map (lambda (extension)
  1669. (string-append extension
  1670. "/lib/guile/"
  1671. (effective-version)
  1672. "/site-ccache"))
  1673. extensions))
  1674. %load-compiled-path)))))))))
  1675. (define* (input-tuples->gexp inputs #:key native?)
  1676. "Given INPUTS, a list of label/gexp-input tuples, return a gexp that expands
  1677. to an input alist."
  1678. (define references
  1679. (map (match-lambda
  1680. ((label input) input))
  1681. inputs))
  1682. (define labels
  1683. (match inputs
  1684. (((labels . _) ...)
  1685. labels)))
  1686. (define (proc . args)
  1687. (cons 'quote (list (map cons labels args))))
  1688. ;; This gexp is more efficient than an equivalent hand-written gexp: fewer
  1689. ;; allocations, no need to scan long list-valued <gexp-input> records in
  1690. ;; search of file-like objects, etc.
  1691. (make-gexp references '() '() proc
  1692. (source-properties inputs)))
  1693. (define (outputs->gexp outputs)
  1694. "Given OUTPUTS, a list of output names, return a gexp that expands to an
  1695. output alist."
  1696. (define references
  1697. (map gexp-output outputs))
  1698. (define (proc . args)
  1699. `(list ,@(map (lambda (name)
  1700. `(cons ,name ((@ (guile) getenv) ,name)))
  1701. outputs)))
  1702. ;; This gexp is more efficient than an equivalent hand-written gexp.
  1703. (make-gexp references '() '() proc
  1704. (source-properties outputs)))
  1705. (define (with-build-variables inputs outputs body)
  1706. "Return a gexp that surrounds BODY with a definition of the legacy
  1707. '%build-inputs', '%outputs', and '%output' variables based on INPUTS, a list
  1708. of name/gexp-input tuples, and OUTPUTS, a list of strings."
  1709. ;; These two variables are defined for backward compatibility. They are
  1710. ;; used by package expressions. These must be top-level defines so that
  1711. ;; 'use-modules' form in BODY that are required for macro expansion work as
  1712. ;; expected.
  1713. (gexp (begin
  1714. (define %build-inputs
  1715. (ungexp (input-tuples->gexp inputs)))
  1716. (define %outputs
  1717. (ungexp (outputs->gexp outputs)))
  1718. (define %output
  1719. (assoc-ref %outputs "out"))
  1720. (ungexp body))))
  1721. (define (sexp->gexp sexp)
  1722. "Turn SEXP into a gexp without any references.
  1723. Using this is a way for the caller to tell that SEXP doesn't need to be
  1724. scanned for file-like objects, thereby reducing processing costs. This is
  1725. particularly useful if SEXP is a long list or a deep tree."
  1726. (make-gexp '() '() '()
  1727. (lambda () sexp)
  1728. (source-properties sexp)))
  1729. (define* (gexp->script name exp
  1730. #:key (guile (default-guile))
  1731. (module-path %load-path)
  1732. (system (%current-system))
  1733. (target 'current))
  1734. "Return an executable script NAME that runs EXP using GUILE, with EXP's
  1735. imported modules in its search path. Look up EXP's modules in MODULE-PATH."
  1736. (mlet* %store-monad ((target (if (eq? target 'current)
  1737. (current-target-system)
  1738. (return target)))
  1739. (set-load-path
  1740. (load-path-expression (gexp-modules exp)
  1741. module-path
  1742. #:guile guile
  1743. #:extensions
  1744. (gexp-extensions exp)
  1745. #:system system
  1746. #:target target))
  1747. (guile-for-build
  1748. (lower-object guile system #:target #f)))
  1749. (gexp->derivation name
  1750. (gexp
  1751. (call-with-output-file (ungexp output)
  1752. (lambda (port)
  1753. ;; Note: that makes a long shebang. When the store
  1754. ;; is /gnu/store, that fits within the 128-byte
  1755. ;; limit imposed by Linux, but that may go beyond
  1756. ;; when running tests.
  1757. (format port
  1758. "#!~a/bin/guile --no-auto-compile~%!#~%"
  1759. (ungexp guile))
  1760. (ungexp-splicing
  1761. (if set-load-path
  1762. (gexp ((write '(ungexp set-load-path) port)))
  1763. (gexp ())))
  1764. (write '(ungexp exp) port)
  1765. (chmod port #o555))))
  1766. #:system system
  1767. #:target target
  1768. #:module-path module-path
  1769. #:guile-for-build guile-for-build
  1770. ;; These derivations are not worth offloading or
  1771. ;; substituting.
  1772. #:local-build? #t
  1773. #:substitutable? #f)))
  1774. (define* (gexp->file name exp #:key
  1775. (set-load-path? #t)
  1776. (module-path %load-path)
  1777. (splice? #f)
  1778. (system (%current-system))
  1779. (target 'current))
  1780. "Return a derivation that builds a file NAME containing EXP. When SPLICE?
  1781. is true, EXP is considered to be a list of expressions that will be spliced in
  1782. the resulting file.
  1783. When SET-LOAD-PATH? is true, emit code in the resulting file to set
  1784. '%load-path' and '%load-compiled-path' to honor EXP's imported modules.
  1785. Lookup EXP's modules in MODULE-PATH."
  1786. (define modules (gexp-modules exp))
  1787. (define extensions (gexp-extensions exp))
  1788. (mlet* %store-monad
  1789. ((target (if (eq? target 'current)
  1790. (current-target-system)
  1791. (return target)))
  1792. (no-load-path? -> (or (not set-load-path?)
  1793. (and (null? modules)
  1794. (null? extensions))))
  1795. (set-load-path
  1796. (load-path-expression modules module-path
  1797. #:extensions extensions
  1798. #:system system
  1799. #:target target)))
  1800. (if no-load-path?
  1801. (gexp->derivation name
  1802. (gexp
  1803. (call-with-output-file (ungexp output)
  1804. (lambda (port)
  1805. (for-each
  1806. (lambda (exp)
  1807. (write exp port))
  1808. '(ungexp (if splice?
  1809. exp
  1810. (gexp ((ungexp exp)))))))))
  1811. #:local-build? #t
  1812. #:substitutable? #f
  1813. #:system system
  1814. #:target target)
  1815. (gexp->derivation name
  1816. (gexp
  1817. (call-with-output-file (ungexp output)
  1818. (lambda (port)
  1819. (write '(ungexp set-load-path) port)
  1820. (for-each
  1821. (lambda (exp)
  1822. (write exp port))
  1823. '(ungexp (if splice?
  1824. exp
  1825. (gexp ((ungexp exp)))))))))
  1826. #:module-path module-path
  1827. #:local-build? #t
  1828. #:substitutable? #f
  1829. #:system system
  1830. #:target target))))
  1831. (define* (text-file* name #:rest text)
  1832. "Return as a monadic value a derivation that builds a text file containing
  1833. all of TEXT. TEXT may list, in addition to strings, objects of any type that
  1834. can be used in a gexp: packages, derivations, local file objects, etc. The
  1835. resulting store file holds references to all these."
  1836. (define builder
  1837. (gexp (call-with-output-file (ungexp output "out")
  1838. (lambda (port)
  1839. (display (string-append (ungexp-splicing text)) port)))))
  1840. (gexp->derivation name builder
  1841. #:local-build? #t
  1842. #:substitutable? #f))
  1843. (define* (mixed-text-file name #:key guile #:rest text)
  1844. "Return an object representing store file NAME containing TEXT. TEXT is a
  1845. sequence of strings and file-like objects, as in:
  1846. (mixed-text-file \"profile\"
  1847. \"export PATH=\" coreutils \"/bin:\" grep \"/bin\")
  1848. This is the declarative counterpart of 'text-file*'."
  1849. (define build
  1850. (let ((text (if guile (drop text 2) text)))
  1851. (gexp (call-with-output-file (ungexp output "out")
  1852. (lambda (port)
  1853. (set-port-encoding! port "UTF-8")
  1854. (display (string-append (ungexp-splicing text)) port))))))
  1855. (computed-file name build #:guile guile))
  1856. (define* (file-union name files #:key guile)
  1857. "Return a <computed-file> that builds a directory containing all of FILES.
  1858. Each item in FILES must be a two-element list where the first element is the
  1859. file name to use in the new directory, and the second element is a gexp
  1860. denoting the target file. Here's an example:
  1861. (file-union \"etc\"
  1862. `((\"hosts\" ,(plain-file \"hosts\"
  1863. \"127.0.0.1 localhost\"))
  1864. (\"bashrc\" ,(plain-file \"bashrc\"
  1865. \"alias ls='ls --color'\"))
  1866. (\"libvirt/qemu.conf\" ,(plain-file \"qemu.conf\" \"\"))))
  1867. This yields an 'etc' directory containing these two files."
  1868. (computed-file name
  1869. (with-imported-modules '((guix build utils))
  1870. (gexp
  1871. (begin
  1872. (use-modules (guix build utils))
  1873. (mkdir (ungexp output))
  1874. (chdir (ungexp output))
  1875. (ungexp-splicing
  1876. (map (match-lambda
  1877. ((target source)
  1878. (gexp
  1879. (begin
  1880. ;; Stat the source to abort early if it does
  1881. ;; not exist.
  1882. (stat (ungexp source))
  1883. (mkdir-p (dirname (ungexp target)))
  1884. (symlink (ungexp source)
  1885. (ungexp target))))))
  1886. files)))))
  1887. #:guile guile))
  1888. (define* (directory-union name things
  1889. #:key (copy? #f) (quiet? #f)
  1890. (resolve-collision 'warn-about-collision))
  1891. "Return a directory that is the union of THINGS, where THINGS is a list of
  1892. file-like objects denoting directories. For example:
  1893. (directory-union \"guile+emacs\" (list guile emacs))
  1894. yields a directory that is the union of the 'guile' and 'emacs' packages.
  1895. Call RESOLVE-COLLISION when several files collide, passing it the list of
  1896. colliding files. RESOLVE-COLLISION must return the chosen file or #f, in
  1897. which case the colliding entry is skipped altogether.
  1898. When HARD-LINKS? is true, create hard links instead of symlinks. When QUIET?
  1899. is true, the derivation will not print anything."
  1900. (define symlink
  1901. (if copy?
  1902. (gexp (lambda (old new)
  1903. (if (file-is-directory? old)
  1904. (symlink old new)
  1905. (copy-file old new))))
  1906. (gexp symlink)))
  1907. (define log-port
  1908. (if quiet?
  1909. (gexp (%make-void-port "w"))
  1910. (gexp (current-error-port))))
  1911. (match things
  1912. ((one)
  1913. ;; Only one thing; return it.
  1914. one)
  1915. (_
  1916. (computed-file name
  1917. (with-imported-modules '((guix build union))
  1918. (gexp (begin
  1919. (use-modules (guix build union)
  1920. (srfi srfi-1)) ;for 'first' and 'last'
  1921. (union-build (ungexp output)
  1922. '(ungexp things)
  1923. #:log-port (ungexp log-port)
  1924. #:symlink (ungexp symlink)
  1925. #:resolve-collision
  1926. (ungexp resolve-collision)))))))))
  1927. ;;;
  1928. ;;; Syntactic sugar.
  1929. ;;;
  1930. (eval-when (expand load eval)
  1931. (define-once read-syntax-redefined?
  1932. ;; Have we already redefined 'read-syntax'? This needs to be done on
  1933. ;; 3.0.8 only to work around <https://issues.guix.gnu.org/54003>.
  1934. (or (not (module-variable the-scm-module 'read-syntax))
  1935. (not (guile-version>? "3.0.7"))))
  1936. (define read-procedure
  1937. ;; The current read procedure being called: either 'read' or
  1938. ;; 'read-syntax'.
  1939. (make-parameter read))
  1940. (define read-syntax*
  1941. ;; Replacement for 'read-syntax'.
  1942. (let ((read-syntax (and=> (module-variable the-scm-module 'read-syntax)
  1943. variable-ref)))
  1944. (lambda (port . rest)
  1945. (parameterize ((read-procedure read-syntax))
  1946. (apply read-syntax port rest)))))
  1947. (unless read-syntax-redefined?
  1948. (set! (@ (guile) read-syntax) read-syntax*)
  1949. (set! read-syntax-redefined? #t))
  1950. (define* (read-ungexp chr port #:optional native?)
  1951. "Read an 'ungexp' or 'ungexp-splicing' form from PORT. When NATIVE? is
  1952. true, use 'ungexp-native' and 'ungexp-native-splicing' instead."
  1953. (define unquote-symbol
  1954. (match (peek-char port)
  1955. (#\@
  1956. (read-char port)
  1957. (if native?
  1958. 'ungexp-native-splicing
  1959. 'ungexp-splicing))
  1960. (_
  1961. (if native?
  1962. 'ungexp-native
  1963. 'ungexp))))
  1964. (define symbolic?
  1965. ;; Depending on whether (read-procedure) is 'read' or 'read-syntax', we
  1966. ;; might get either sexps or syntax objects. Adjust accordingly.
  1967. (if (eq? (read-procedure) read)
  1968. symbol?
  1969. (compose symbol? syntax->datum)))
  1970. (define symbolic->string
  1971. (if (eq? (read-procedure) read)
  1972. symbol->string
  1973. (compose symbol->string syntax->datum)))
  1974. (define wrapped-symbol
  1975. (if (eq? (read-procedure) read)
  1976. (lambda (_ symbol) symbol)
  1977. datum->syntax))
  1978. (match ((read-procedure) port)
  1979. ((? symbolic? symbol)
  1980. (let ((str (symbolic->string symbol)))
  1981. (match (string-index-right str #\:)
  1982. (#f
  1983. `(,unquote-symbol ,symbol))
  1984. (colon
  1985. (let ((name (string->symbol (substring str 0 colon)))
  1986. (output (substring str (+ colon 1))))
  1987. `(,unquote-symbol ,(wrapped-symbol symbol name) ,output))))))
  1988. (x
  1989. `(,unquote-symbol ,x))))
  1990. (define (read-gexp chr port)
  1991. "Read a 'gexp' form from PORT."
  1992. `(gexp ,((read-procedure) port)))
  1993. ;; Extend the reader
  1994. (read-hash-extend #\~ read-gexp)
  1995. (read-hash-extend #\$ read-ungexp)
  1996. (read-hash-extend #\+ (cut read-ungexp <> <> #t)))
  1997. ;;; gexp.scm ends here