ci: Skip system tests on armhf-linux.
[jackhill/guix/guix.git] / build-aux / build-self.scm
CommitLineData
f81ac34d 1;;; GNU Guix --- Functional package management for GNU
f0527ce3 2;;; Copyright © 2014, 2016, 2017, 2018 Ludovic Courtès <ludo@gnu.org>
f81ac34d
LC
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)
cd295fbe
LC
20 #:use-module (gnu)
21 #:use-module (guix)
f0527ce3 22 #:use-module (guix ui)
cd295fbe 23 #:use-module (guix config)
f0527ce3 24 #:use-module (guix modules)
ca719424 25 #:use-module (guix build-system gnu)
f81ac34d 26 #:use-module (srfi srfi-1)
b006ba50 27 #:use-module (srfi srfi-19)
ac4d2ec8
LC
28 #:use-module (srfi srfi-34)
29 #:use-module (srfi srfi-35)
f0527ce3 30 #:use-module (rnrs io ports)
838ba73d 31 #:use-module (ice-9 match)
f0527ce3 32 #:use-module (ice-9 popen)
f81ac34d
LC
33 #:export (build))
34
35;;; Commentary:
36;;;
37;;; When loaded, this module returns a monadic procedure of at least one
38;;; argument: the source tree to build. It returns a derivation that
39;;; builds it.
40;;;
cd295fbe
LC
41;;; This file uses modules provided by the already-installed Guix. Those
42;;; modules may be arbitrarily old compared to the version we want to
43;;; build. Because of that, it must rely on the smallest set of features
44;;; that are likely to be provided by the (guix) and (gnu) modules, and by
45;;; Guile itself, forever and ever.
46;;;
f81ac34d
LC
47;;; Code:
48
cd295fbe 49\f
f0527ce3
LC
50;;;
51;;; Generating (guix config).
52;;;
53;;; This is copied from (guix self) because we cannot assume (guix self) is
54;;; available at this point.
55;;;
56
57(define %dependency-variables
58 ;; (guix config) variables corresponding to dependencies.
e8cb9c01 59 '(%libgcrypt %libz %xz %gzip %bzip2))
f0527ce3
LC
60
61(define %persona-variables
62 ;; (guix config) variables that define Guix's persona.
63 '(%guix-package-name
64 %guix-version
65 %guix-bug-report-address
66 %guix-home-page-url))
67
68(define %config-variables
45779fa6
LC
69 ;; (guix config) variables corresponding to Guix configuration.
70 (letrec-syntax ((variables (syntax-rules ()
71 ((_)
72 '())
73 ((_ variable rest ...)
74 (cons `(variable . ,variable)
75 (variables rest ...))))))
7af5c2a2 76 (variables %localstatedir %storedir %sysconfdir %system)))
f0527ce3 77
ca719424 78(define* (make-config.scm #:key zlib gzip xz bzip2
f0527ce3
LC
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
7af5c2a2
LC
94 %store-directory
95 %state-directory
96 %store-database-directory
97 %config-directory
f0527ce3
LC
98 %libz
99 %gzip
100 %bzip2
e8cb9c01 101 %xz))
f0527ce3
LC
102
103 ;; XXX: Work around <http://bugs.gnu.org/15602>.
104 (eval-when (expand load eval)
105 #$@(map (match-lambda
106 ((name . value)
107 #~(define-public #$name #$value)))
108 %config-variables)
109
7af5c2a2
LC
110 (define %store-directory
111 (or (and=> (getenv "NIX_STORE_DIR") canonicalize-path)
112 %storedir))
113
114 (define %state-directory
115 ;; This must match `NIX_STATE_DIR' as defined in
116 ;; `nix/local.mk'.
117 (or (getenv "NIX_STATE_DIR")
118 (string-append %localstatedir "/guix")))
119
120 (define %store-database-directory
121 (or (getenv "NIX_DB_DIR")
122 (string-append %state-directory "/db")))
123
124 (define %config-directory
125 ;; This must match `GUIX_CONFIGURATION_DIRECTORY' as
126 ;; defined in `nix/local.mk'.
127 (or (getenv "GUIX_CONFIGURATION_DIRECTORY")
128 (string-append %sysconfdir "/guix")))
129
f0527ce3
LC
130 (define %guix-package-name #$package-name)
131 (define %guix-version #$package-version)
132 (define %guix-bug-report-address #$bug-report-address)
133 (define %guix-home-page-url #$home-page-url)
134
135 (define %gzip
136 #+(and gzip (file-append gzip "/bin/gzip")))
137 (define %bzip2
138 #+(and bzip2 (file-append bzip2 "/bin/bzip2")))
139 (define %xz
140 #+(and xz (file-append xz "/bin/xz")))
141
f0527ce3
LC
142 (define %libz
143 #+(and zlib
e8cb9c01 144 (file-append zlib "/lib/libz")))))))
cd295fbe 145
f0527ce3
LC
146\f
147;;;
148;;; 'gexp->script'.
149;;;
150;;; This is our own variant of 'gexp->script' with an extra #:module-path
151;;; parameter, which was unavailable in (guix gexp) until commit
152;;; 1ae16033f34cebe802023922436883867010850f (March 2018.)
153;;;
cd295fbe 154
f0527ce3
LC
155(define (load-path-expression modules path)
156 "Return as a monadic value a gexp that sets '%load-path' and
157'%load-compiled-path' to point to MODULES, a list of module names. MODULES
158are searched for in PATH."
159 (mlet %store-monad ((modules (imported-modules modules
160 #:module-path path))
161 (compiled (compiled-modules modules
162 #:module-path path)))
163 (return (gexp (eval-when (expand load eval)
164 (set! %load-path
165 (cons (ungexp modules) %load-path))
166 (set! %load-compiled-path
167 (cons (ungexp compiled)
168 %load-compiled-path)))))))
169
170(define* (gexp->script name exp
171 #:key (guile (default-guile))
172 (module-path %load-path))
173 "Return an executable script NAME that runs EXP using GUILE, with EXP's
174imported modules in its search path."
175 (mlet %store-monad ((set-load-path
176 (load-path-expression (gexp-modules exp)
177 module-path)))
178 (gexp->derivation name
179 (gexp
180 (call-with-output-file (ungexp output)
181 (lambda (port)
182 ;; Note: that makes a long shebang. When the store
183 ;; is /gnu/store, that fits within the 128-byte
184 ;; limit imposed by Linux, but that may go beyond
185 ;; when running tests.
186 (format port
187 "#!~a/bin/guile --no-auto-compile~%!#~%"
188 (ungexp guile))
189
190 (write '(ungexp set-load-path) port)
191 (write '(ungexp exp) port)
192 (chmod port #o555))))
193 #:module-path module-path)))
b006ba50 194
f0527ce3 195\f
b006ba50
LC
196(define (date-version-string)
197 "Return the current date and hour in UTC timezone, for use as a poor
198person's version identifier."
cd295fbe 199 ;; XXX: Replace with a Git commit id.
b006ba50
LC
200 (date->string (current-date 0) "~Y~m~d.~H"))
201
ca719424
LC
202(define guile-gcrypt
203 ;; The host Guix may or may not have 'guile-gcrypt', which was introduced in
204 ;; August 2018. If it has it, it's at least version 0.1.0, which is good
205 ;; enough. If it doesn't, specify our own package because the target Guix
206 ;; requires it.
207 (match (find-best-packages-by-name "guile-gcrypt" #f)
208 (()
209 (package
210 (name "guile-gcrypt")
211 (version "0.1.0")
212 (home-page "https://notabug.org/cwebber/guile-gcrypt")
213 (source (origin
214 (method url-fetch)
215 (uri (string-append home-page "/archive/v" version ".tar.gz"))
216 (sha256
217 (base32
218 "1gir7ifknbmbvjlql5j6wzk7bkb5lnmq80q59ngz43hhpclrk5k3"))
219 (file-name (string-append name "-" version ".tar.gz"))))
220 (build-system gnu-build-system)
3ffcad7d
LC
221 (arguments
222 ;; The 'bootstrap' phase appeared in 'core-updates', which was merged
223 ;; into 'master' ca. June 2018.
224 '(#:phases (modify-phases %standard-phases
225 (delete 'bootstrap)
226 (add-before 'configure 'bootstrap
227 (lambda _
228 (unless (zero? (system* "autoreconf" "-vfi"))
229 (error "autoreconf failed"))
230 #t)))))
ca719424
LC
231 (native-inputs
232 `(("pkg-config" ,(specification->package "pkg-config"))
233 ("autoconf" ,(specification->package "autoconf"))
234 ("automake" ,(specification->package "automake"))
235 ("texinfo" ,(specification->package "texinfo"))))
236 (inputs
237 `(("guile" ,(specification->package "guile"))
238 ("libgcrypt" ,(specification->package "libgcrypt"))))
239 (synopsis "Cryptography library for Guile using Libgcrypt")
240 (description
241 "Guile-Gcrypt provides a Guile 2.x interface to a subset of the
242GNU Libgcrypt crytographic library. It provides modules for cryptographic
243hash functions, message authentication codes (MAC), public-key cryptography,
244strong randomness, and more. It is implemented using the foreign function
245interface (FFI) of Guile.")
246 (license #f))) ;license:gpl3+
247 ((package . _)
248 package)))
249
f0527ce3 250(define* (build-program source version
8a0d9bc8
LC
251 #:optional (guile-version (effective-version))
252 #:key (pull-version 0))
f0527ce3
LC
253 "Return a program that computes the derivation to build Guix from SOURCE."
254 (define select?
255 ;; Select every module but (guix config) and non-Guix modules.
256 (match-lambda
257 (('guix 'config) #f)
258 (('guix _ ...) #t)
259 (('gnu _ ...) #t)
260 (_ #f)))
261
ca719424
LC
262 (define fake-gcrypt-hash
263 ;; Fake (gcrypt hash) module; see below.
264 (scheme-file "hash.scm"
265 #~(define-module (gcrypt hash)
266 #:export (sha1 sha256))))
267
78c9058d
LC
268 (define fake-git
269 (scheme-file "git.scm" #~(define-module (git))))
270
f0527ce3 271 (with-imported-modules `(((guix config)
ca719424
LC
272 => ,(make-config.scm))
273
274 ;; To avoid relying on 'with-extensions', which was
275 ;; introduced in 0.15.0, provide a fake (gcrypt
276 ;; hash) just so that we can build modules, and
277 ;; adjust %LOAD-PATH later on.
278 ((gcrypt hash) => ,fake-gcrypt-hash)
279
78c9058d
LC
280 ;; (guix git-download) depends on (git) but only
281 ;; for peripheral functionality. Provide a dummy
282 ;; (git) to placate it.
283 ((git) => ,fake-git)
284
f0527ce3
LC
285 ,@(source-module-closure `((guix store)
286 (guix self)
287 (guix derivations)
288 (gnu packages bootstrap))
289 (list source)
290 #:select? select?))
291 (gexp->script "compute-guix-derivation"
292 #~(begin
293 (use-modules (ice-9 match))
294
295 (eval-when (expand load eval)
296 ;; Don't augment '%load-path'.
297 (unsetenv "GUIX_PACKAGE_PATH")
298
299 ;; (gnu packages …) modules are going to be looked up
300 ;; under SOURCE. (guix config) is looked up in FRONT.
1f1d76a1
LC
301 (match (command-line)
302 ((_ source _ ...)
303 (match %load-path
304 ((front _ ...)
305 (unless (string=? front source) ;already done?
ca719424
LC
306 (set! %load-path
307 (list source
308 (string-append #$guile-gcrypt
309 "/share/guile/site/"
310 (effective-version))
311 front)))))))
f0527ce3 312
ca719424
LC
313 ;; Only load Guile-Gcrypt, our own modules, or those
314 ;; of Guile.
f0527ce3
LC
315 (match %load-compiled-path
316 ((front _ ... sys1 sys2)
ca719424
LC
317 (unless (string-prefix? #$guile-gcrypt front)
318 (set! %load-compiled-path
319 (list (string-append #$guile-gcrypt
320 "/lib/guile/"
321 (effective-version)
322 "/site-ccache")
323 front sys1 sys2))))))
f0527ce3
LC
324
325 (use-modules (guix store)
326 (guix self)
327 (guix derivations)
328 (srfi srfi-1))
329
330 (define (spin system)
331 (define spin
332 (circular-list "-" "\\" "|" "/" "-" "\\" "|" "/"))
333
334 (format (current-error-port)
335 "Computing Guix derivation for '~a'... "
336 system)
c108c46f
LC
337 (when (isatty? (current-error-port))
338 (let loop ((spin spin))
339 (display (string-append "\b" (car spin))
340 (current-error-port))
341 (force-output (current-error-port))
342 (sleep 1)
343 (loop (cdr spin)))))
f0527ce3
LC
344
345 (match (command-line)
790c3e01
LC
346 ((_ source system version protocol-version)
347 ;; The current input port normally wraps a file
348 ;; descriptor connected to the daemon, or it is
349 ;; connected to /dev/null. In the former case, reuse
350 ;; the connection such that we inherit build options
351 ;; such as substitute URLs and so on; in the latter
352 ;; case, attempt to open a new connection.
353 (let* ((proto (string->number protocol-version))
354 (store (if (integer? proto)
355 (port->connection (duplicate-port
356 (current-input-port)
357 "w+0")
358 #:version proto)
359 (open-connection))))
f0527ce3
LC
360 (call-with-new-thread
361 (lambda ()
362 (spin system)))
363
364 (display
8a0d9bc8 365 (and=>
f0527ce3 366 (run-with-store store
1f1d76a1 367 (guix-derivation source version
8a0d9bc8
LC
368 #$guile-version
369 #:pull-version
370 #$pull-version)
371 #:system system)
372 derivation-file-name))))))
f0527ce3 373 #:module-path (list source))))
838ba73d 374
f81ac34d 375;; The procedure below is our return value.
b006ba50 376(define* (build source
f0527ce3 377 #:key verbose? (version (date-version-string)) system
8a0d9bc8 378 (pull-version 0)
1428bce3
LC
379
380 ;; For the standalone Guix, default to Guile 2.2. For old
381 ;; versions of 'guix pull' (pre-0.15.0), we have to use the
382 ;; same Guile as the current one.
383 (guile-version (if (> pull-version 0)
384 "2.2"
385 (effective-version)))
386
f81ac34d
LC
387 #:allow-other-keys
388 #:rest rest)
389 "Return a derivation that unpacks SOURCE into STORE and compiles Scheme
390files."
f0527ce3
LC
391 ;; Build the build program and then use it as a trampoline to build from
392 ;; SOURCE.
8a0d9bc8
LC
393 (mlet %store-monad ((build (build-program source version guile-version
394 #:pull-version pull-version))
790c3e01
LC
395 (system (if system (return system) (current-system)))
396 (port ((store-lift nix-server-socket)))
397 (major ((store-lift nix-server-major-version)))
398 (minor ((store-lift nix-server-minor-version))))
f0527ce3
LC
399 (mbegin %store-monad
400 (show-what-to-build* (list build))
401 (built-derivations (list build))
790c3e01
LC
402
403 ;; Use the port beneath the current store as the stdin of BUILD. This
404 ;; way, we know 'open-pipe*' will not close it on 'exec'. If PORT is
405 ;; not a file port (e.g., it's an SSH channel), then the subprocess's
406 ;; stdin will actually be /dev/null.
407 (let* ((pipe (with-input-from-port port
408 (lambda ()
409 (setenv "GUILE_WARN_DEPRECATED" "no") ;be quiet and drive
410 (open-pipe* OPEN_READ
411 (derivation->output-path build)
412 source system version
413 (if (file-port? port)
414 (number->string
415 (logior major minor))
416 "none")))))
f27a7128
LC
417 (str (get-string-all pipe))
418 (status (close-pipe pipe)))
419 (match str
f0527ce3 420 ((? eof-object?)
f27a7128 421 (error "build program failed" (list build status)))
f0527ce3
LC
422 ((? derivation-path? drv)
423 (mbegin %store-monad
afb82831 424 (return (newline (current-error-port)))
f0527ce3
LC
425 ((store-lift add-temp-root) drv)
426 (return (read-derivation-from-file drv))))
8a0d9bc8
LC
427 ("#f"
428 ;; Unsupported PULL-VERSION.
429 (return #f))
f0527ce3 430 ((? string? str)
ac4d2ec8
LC
431 (raise (condition
432 (&message
433 (message (format #f "You found a bug: the program '~a'
434failed to compute the derivation for Guix (version: ~s; system: ~s;
435host version: ~s; pull-version: ~s).
436Please report it by email to <~a>.~%"
437 (derivation->output-path build)
438 version system %guix-version pull-version
439 %guix-bug-report-address)))))))))))
f81ac34d
LC
440
441;; This file is loaded by 'guix pull'; return it the build procedure.
442build
443
cd295fbe
LC
444;; Local Variables:
445;; eval: (put 'with-load-path 'scheme-indent-function 1)
446;; End:
447
f81ac34d 448;;; build-self.scm ends here