gexp.scm 83 KB

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