gexp.scm 94 KB

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