gexp.scm 89 KB

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