gexp.scm 85 KB

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