gexp.scm 94 KB

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