Merge branch 'master' into staging
[jackhill/guix/guix.git] / build-aux / build-self.scm
1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2014, 2016, 2017, 2018 Ludovic Courtès <ludo@gnu.org>
3 ;;;
4 ;;; This file is part of GNU Guix.
5 ;;;
6 ;;; GNU Guix is free software; you can redistribute it and/or modify it
7 ;;; under the terms of the GNU General Public License as published by
8 ;;; the Free Software Foundation; either version 3 of the License, or (at
9 ;;; your option) any later version.
10 ;;;
11 ;;; GNU Guix is distributed in the hope that it will be useful, but
12 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
13 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 ;;; GNU General Public License for more details.
15 ;;;
16 ;;; You should have received a copy of the GNU General Public License
17 ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
18
19 (define-module (build-self)
20 #:use-module (gnu)
21 #:use-module (guix)
22 #:use-module (guix ui)
23 #:use-module (guix config)
24 #:use-module (guix modules)
25 #:use-module (srfi srfi-1)
26 #:use-module (srfi srfi-19)
27 #:use-module (rnrs io ports)
28 #:use-module (ice-9 match)
29 #:use-module (ice-9 popen)
30 #:export (build))
31
32 ;;; Commentary:
33 ;;;
34 ;;; When loaded, this module returns a monadic procedure of at least one
35 ;;; argument: the source tree to build. It returns a derivation that
36 ;;; builds it.
37 ;;;
38 ;;; This file uses modules provided by the already-installed Guix. Those
39 ;;; modules may be arbitrarily old compared to the version we want to
40 ;;; build. Because of that, it must rely on the smallest set of features
41 ;;; that are likely to be provided by the (guix) and (gnu) modules, and by
42 ;;; Guile itself, forever and ever.
43 ;;;
44 ;;; Code:
45
46 \f
47 ;;;
48 ;;; Generating (guix config).
49 ;;;
50 ;;; This is copied from (guix self) because we cannot assume (guix self) is
51 ;;; available at this point.
52 ;;;
53
54 (define %dependency-variables
55 ;; (guix config) variables corresponding to dependencies.
56 '(%libgcrypt %libz %xz %gzip %bzip2 %nix-instantiate))
57
58 (define %persona-variables
59 ;; (guix config) variables that define Guix's persona.
60 '(%guix-package-name
61 %guix-version
62 %guix-bug-report-address
63 %guix-home-page-url))
64
65 (define %config-variables
66 ;; (guix config) variables corresponding to Guix configuration (storedir,
67 ;; localstatedir, etc.)
68 (sort (filter pair?
69 (module-map (lambda (name var)
70 (and (not (memq name %dependency-variables))
71 (not (memq name %persona-variables))
72 (cons name (variable-ref var))))
73 (resolve-interface '(guix config))))
74 (lambda (name+value1 name+value2)
75 (string<? (symbol->string (car name+value1))
76 (symbol->string (car name+value2))))))
77
78 (define* (make-config.scm #:key libgcrypt zlib gzip xz bzip2
79 (package-name "GNU Guix")
80 (package-version "0")
81 (bug-report-address "bug-guix@gnu.org")
82 (home-page-url "https://gnu.org/s/guix"))
83
84 ;; Hack so that Geiser is not confused.
85 (define defmod 'define-module)
86
87 (scheme-file "config.scm"
88 #~(begin
89 (#$defmod (guix config)
90 #:export (%guix-package-name
91 %guix-version
92 %guix-bug-report-address
93 %guix-home-page-url
94 %libgcrypt
95 %libz
96 %gzip
97 %bzip2
98 %xz
99 %nix-instantiate))
100
101 ;; XXX: Work around <http://bugs.gnu.org/15602>.
102 (eval-when (expand load eval)
103 #$@(map (match-lambda
104 ((name . value)
105 #~(define-public #$name #$value)))
106 %config-variables)
107
108 (define %guix-package-name #$package-name)
109 (define %guix-version #$package-version)
110 (define %guix-bug-report-address #$bug-report-address)
111 (define %guix-home-page-url #$home-page-url)
112
113 (define %gzip
114 #+(and gzip (file-append gzip "/bin/gzip")))
115 (define %bzip2
116 #+(and bzip2 (file-append bzip2 "/bin/bzip2")))
117 (define %xz
118 #+(and xz (file-append xz "/bin/xz")))
119
120 (define %libgcrypt
121 #+(and libgcrypt
122 (file-append libgcrypt "/lib/libgcrypt")))
123 (define %libz
124 #+(and zlib
125 (file-append zlib "/lib/libz")))
126
127 (define %nix-instantiate ;for (guix import snix)
128 "nix-instantiate")))))
129
130 \f
131 ;;;
132 ;;; 'gexp->script'.
133 ;;;
134 ;;; This is our own variant of 'gexp->script' with an extra #:module-path
135 ;;; parameter, which was unavailable in (guix gexp) until commit
136 ;;; 1ae16033f34cebe802023922436883867010850f (March 2018.)
137 ;;;
138
139 (define (load-path-expression modules path)
140 "Return as a monadic value a gexp that sets '%load-path' and
141 '%load-compiled-path' to point to MODULES, a list of module names. MODULES
142 are searched for in PATH."
143 (mlet %store-monad ((modules (imported-modules modules
144 #:module-path path))
145 (compiled (compiled-modules modules
146 #:module-path path)))
147 (return (gexp (eval-when (expand load eval)
148 (set! %load-path
149 (cons (ungexp modules) %load-path))
150 (set! %load-compiled-path
151 (cons (ungexp compiled)
152 %load-compiled-path)))))))
153
154 (define* (gexp->script name exp
155 #:key (guile (default-guile))
156 (module-path %load-path))
157 "Return an executable script NAME that runs EXP using GUILE, with EXP's
158 imported modules in its search path."
159 (mlet %store-monad ((set-load-path
160 (load-path-expression (gexp-modules exp)
161 module-path)))
162 (gexp->derivation name
163 (gexp
164 (call-with-output-file (ungexp output)
165 (lambda (port)
166 ;; Note: that makes a long shebang. When the store
167 ;; is /gnu/store, that fits within the 128-byte
168 ;; limit imposed by Linux, but that may go beyond
169 ;; when running tests.
170 (format port
171 "#!~a/bin/guile --no-auto-compile~%!#~%"
172 (ungexp guile))
173
174 (write '(ungexp set-load-path) port)
175 (write '(ungexp exp) port)
176 (chmod port #o555))))
177 #:module-path module-path)))
178
179 \f
180 (define (date-version-string)
181 "Return the current date and hour in UTC timezone, for use as a poor
182 person's version identifier."
183 ;; XXX: Replace with a Git commit id.
184 (date->string (current-date 0) "~Y~m~d.~H"))
185
186 (define* (build-program source version
187 #:optional (guile-version (effective-version))
188 #:key (pull-version 0))
189 "Return a program that computes the derivation to build Guix from SOURCE."
190 (define select?
191 ;; Select every module but (guix config) and non-Guix modules.
192 (match-lambda
193 (('guix 'config) #f)
194 (('guix _ ...) #t)
195 (('gnu _ ...) #t)
196 (_ #f)))
197
198 (with-imported-modules `(((guix config)
199 => ,(make-config.scm
200 #:libgcrypt
201 (specification->package "libgcrypt")))
202 ,@(source-module-closure `((guix store)
203 (guix self)
204 (guix derivations)
205 (gnu packages bootstrap))
206 (list source)
207 #:select? select?))
208 (gexp->script "compute-guix-derivation"
209 #~(begin
210 (use-modules (ice-9 match))
211
212 (eval-when (expand load eval)
213 ;; Don't augment '%load-path'.
214 (unsetenv "GUIX_PACKAGE_PATH")
215
216 ;; (gnu packages …) modules are going to be looked up
217 ;; under SOURCE. (guix config) is looked up in FRONT.
218 (match %load-path
219 ((#$source _ ...)
220 #t) ;already done
221 ((front _ ...)
222 (set! %load-path (list #$source front))))
223
224 ;; Only load our own modules or those of Guile.
225 (match %load-compiled-path
226 ((front _ ... sys1 sys2)
227 (set! %load-compiled-path
228 (list front sys1 sys2)))))
229
230 (use-modules (guix store)
231 (guix self)
232 (guix derivations)
233 (srfi srfi-1))
234
235 (define (spin system)
236 (define spin
237 (circular-list "-" "\\" "|" "/" "-" "\\" "|" "/"))
238
239 (format (current-error-port)
240 "Computing Guix derivation for '~a'... "
241 system)
242 (let loop ((spin spin))
243 (display (string-append "\b" (car spin))
244 (current-error-port))
245 (force-output (current-error-port))
246 (sleep 1)
247 (loop (cdr spin))))
248
249 (match (command-line)
250 ((_ _ system)
251 (with-store store
252 (call-with-new-thread
253 (lambda ()
254 (spin system)))
255
256 (display
257 (and=>
258 (run-with-store store
259 (guix-derivation #$source #$version
260 #$guile-version
261 #:pull-version
262 #$pull-version)
263 #:system system)
264 derivation-file-name))))))
265 #:module-path (list source))))
266
267 ;; The procedure below is our return value.
268 (define* (build source
269 #:key verbose? (version (date-version-string)) system
270 (guile-version (match ((@ (guile) version))
271 ("2.2.2" "2.2.2")
272 (_ (effective-version))))
273 (pull-version 0)
274 #:allow-other-keys
275 #:rest rest)
276 "Return a derivation that unpacks SOURCE into STORE and compiles Scheme
277 files."
278 ;; Build the build program and then use it as a trampoline to build from
279 ;; SOURCE.
280 (mlet %store-monad ((build (build-program source version guile-version
281 #:pull-version pull-version))
282 (system (if system (return system) (current-system))))
283 (mbegin %store-monad
284 (show-what-to-build* (list build))
285 (built-derivations (list build))
286 (let* ((pipe (begin
287 (setenv "GUILE_WARN_DEPRECATED" "no") ;be quiet and drive
288 (open-pipe* OPEN_READ
289 (derivation->output-path build)
290 source system)))
291 (str (get-string-all pipe))
292 (status (close-pipe pipe)))
293 (match str
294 ((? eof-object?)
295 (error "build program failed" (list build status)))
296 ((? derivation-path? drv)
297 (mbegin %store-monad
298 (return (newline (current-output-port)))
299 ((store-lift add-temp-root) drv)
300 (return (read-derivation-from-file drv))))
301 ("#f"
302 ;; Unsupported PULL-VERSION.
303 (return #f))
304 ((? string? str)
305 (error "invalid build result" (list build str))))))))
306
307 ;; This file is loaded by 'guix pull'; return it the build procedure.
308 build
309
310 ;; Local Variables:
311 ;; eval: (put 'with-load-path 'scheme-indent-function 1)
312 ;; End:
313
314 ;;; build-self.scm ends here