doc: Mention value /var to localstatedir option.
[jackhill/guix/guix.git] / gnu / ci.scm
1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019 Ludovic Courtès <ludo@gnu.org>
3 ;;; Copyright © 2017 Jan Nieuwenhuizen <janneke@gnu.org>
4 ;;; Copyright © 2018 Clément Lassieur <clement@lassieur.org>
5 ;;;
6 ;;; This file is part of GNU Guix.
7 ;;;
8 ;;; GNU Guix is free software; you can redistribute it and/or modify it
9 ;;; under the terms of the GNU General Public License as published by
10 ;;; the Free Software Foundation; either version 3 of the License, or (at
11 ;;; your option) any later version.
12 ;;;
13 ;;; GNU Guix is distributed in the hope that it will be useful, but
14 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
15 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 ;;; GNU General Public License for more details.
17 ;;;
18 ;;; You should have received a copy of the GNU General Public License
19 ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
20
21 (define-module (gnu ci)
22 #:use-module (guix config)
23 #:use-module (guix store)
24 #:use-module (guix grafts)
25 #:use-module (guix profiles)
26 #:use-module (guix packages)
27 #:use-module (guix channels)
28 #:use-module (guix derivations)
29 #:use-module (guix build-system)
30 #:use-module (guix monads)
31 #:use-module (guix ui)
32 #:use-module ((guix licenses)
33 #:select (gpl3+ license? license-name))
34 #:use-module ((guix utils) #:select (%current-system))
35 #:use-module ((guix scripts system) #:select (read-operating-system))
36 #:use-module ((guix scripts pack)
37 #:select (lookup-compressor self-contained-tarball))
38 #:use-module (gnu bootloader)
39 #:use-module (gnu bootloader u-boot)
40 #:use-module (gnu packages)
41 #:use-module (gnu packages gcc)
42 #:use-module (gnu packages base)
43 #:use-module (gnu packages gawk)
44 #:use-module (gnu packages guile)
45 #:use-module (gnu packages gettext)
46 #:use-module (gnu packages compression)
47 #:use-module (gnu packages multiprecision)
48 #:use-module (gnu packages make-bootstrap)
49 #:use-module (gnu packages package-management)
50 #:use-module (gnu system)
51 #:use-module (gnu system vm)
52 #:use-module (gnu system install)
53 #:use-module (gnu tests)
54 #:use-module (srfi srfi-1)
55 #:use-module (srfi srfi-26)
56 #:use-module (ice-9 match)
57 #:export (hydra-jobs))
58
59 ;;; Commentary:
60 ;;;
61 ;;; This file defines build jobs for the Hydra and Cuirass continuation
62 ;;; integration tools.
63 ;;;
64 ;;; Code:
65
66 (define* (package->alist store package system
67 #:optional (package-derivation package-derivation))
68 "Convert PACKAGE to an alist suitable for Hydra."
69 (parameterize ((%graft? #f))
70 `((derivation . ,(derivation-file-name
71 (package-derivation store package system
72 #:graft? #f)))
73 (description . ,(package-synopsis package))
74 (long-description . ,(package-description package))
75
76 ;; XXX: Hydra ignores licenses that are not a <license> structure or a
77 ;; list thereof.
78 (license . ,(let loop ((license (package-license package)))
79 (match license
80 ((? license?)
81 (license-name license))
82 ((lst ...)
83 (map loop license)))))
84
85 (home-page . ,(package-home-page package))
86 (maintainers . ("bug-guix@gnu.org"))
87 (max-silent-time . ,(or (assoc-ref (package-properties package)
88 'max-silent-time)
89 3600)) ;1 hour by default
90 (timeout . ,(or (assoc-ref (package-properties package) 'timeout)
91 72000))))) ;20 hours by default
92
93 (define (package-job store job-name package system)
94 "Return a job called JOB-NAME that builds PACKAGE on SYSTEM."
95 (let ((job-name (symbol-append job-name (string->symbol ".")
96 (string->symbol system))))
97 `(,job-name . ,(cut package->alist store package system))))
98
99 (define (package-cross-job store job-name package target system)
100 "Return a job called TARGET.JOB-NAME that cross-builds PACKAGE for TARGET on
101 SYSTEM."
102 `(,(symbol-append (string->symbol target) (string->symbol ".") job-name
103 (string->symbol ".") (string->symbol system)) .
104 ,(cute package->alist store package system
105 (lambda* (store package system #:key graft?)
106 (package-cross-derivation store package target system
107 #:graft? graft?)))))
108
109 (define %core-packages
110 ;; Note: Don't put the '-final' package variants because (1) that's
111 ;; implicit, and (2) they cannot be cross-built (due to the explicit input
112 ;; chain.)
113 (list gcc-4.8 gcc-4.9 gcc-5 glibc binutils
114 gmp mpfr mpc coreutils findutils diffutils patch sed grep
115 gawk gnu-gettext hello guile-2.0 guile-2.2 zlib gzip xz
116 %bootstrap-binaries-tarball
117 %binutils-bootstrap-tarball
118 (%glibc-bootstrap-tarball)
119 %gcc-bootstrap-tarball
120 %guile-bootstrap-tarball
121 %bootstrap-tarballs))
122
123 (define %packages-to-cross-build
124 %core-packages)
125
126 (define %cross-targets
127 '("mips64el-linux-gnu"
128 "mips64el-linux-gnuabi64"
129 "arm-linux-gnueabihf"
130 "aarch64-linux-gnu"
131 "powerpc-linux-gnu"
132 "i586-pc-gnu" ;aka. GNU/Hurd
133 "i686-w64-mingw32"
134 "x86_64-w64-mingw32"))
135
136 (define %guixsd-supported-systems
137 '("x86_64-linux" "i686-linux" "armhf-linux"))
138
139 (define %u-boot-systems
140 '("armhf-linux"))
141
142 (define (qemu-jobs store system)
143 "Return a list of jobs that build QEMU images for SYSTEM."
144 (define (->alist drv)
145 `((derivation . ,(derivation-file-name drv))
146 (description . "Stand-alone QEMU image of the GNU system")
147 (long-description . "This is a demo stand-alone QEMU image of the GNU
148 system.")
149 (license . ,(license-name gpl3+))
150 (max-silent-time . 600)
151 (timeout . 3600)
152 (home-page . ,%guix-home-page-url)
153 (maintainers . ("bug-guix@gnu.org"))))
154
155 (define (->job name drv)
156 (let ((name (symbol-append name (string->symbol ".")
157 (string->symbol system))))
158 `(,name . ,(lambda ()
159 (parameterize ((%graft? #f))
160 (->alist drv))))))
161
162 (define MiB
163 (expt 2 20))
164
165 (if (member system %guixsd-supported-systems)
166 (if (member system %u-boot-systems)
167 (list (->job 'flash-image
168 (run-with-store store
169 (mbegin %store-monad
170 (set-guile-for-build (default-guile))
171 (system-disk-image
172 (operating-system (inherit installation-os)
173 (bootloader (bootloader-configuration
174 (bootloader u-boot-bootloader)
175 (target #f))))
176 #:disk-image-size
177 (* 1500 MiB))))))
178 (list (->job 'usb-image
179 (run-with-store store
180 (mbegin %store-monad
181 (set-guile-for-build (default-guile))
182 (system-disk-image installation-os
183 #:disk-image-size
184 (* 1500 MiB)))))
185 (->job 'iso9660-image
186 (run-with-store store
187 (mbegin %store-monad
188 (set-guile-for-build (default-guile))
189 (system-disk-image installation-os
190 #:file-system-type
191 "iso9660"))))))
192 '()))
193
194 (define channel-build-system
195 ;; Build system used to "convert" a channel instance to a package.
196 (let* ((build (lambda* (store name inputs
197 #:key instance system
198 #:allow-other-keys)
199 (run-with-store store
200 (channel-instances->derivation (list instance))
201 #:system system)))
202 (lower (lambda* (name #:key system instance #:allow-other-keys)
203 (bag
204 (name name)
205 (system system)
206 (build build)
207 (arguments `(#:instance ,instance))))))
208 (build-system (name 'channel)
209 (description "Turn a channel instance into a package.")
210 (lower lower))))
211
212 (define (channel-instance->package instance)
213 "Return a package for the given channel INSTANCE."
214 (package
215 (inherit guix)
216 (version (or (string-take (channel-instance-commit instance) 7)
217 (string-append (package-version guix) "+")))
218 (build-system channel-build-system)
219 (arguments `(#:instance ,instance))
220 (inputs '())
221 (native-inputs '())
222 (propagated-inputs '())))
223
224 (define* (system-test-jobs store system
225 #:key source commit)
226 "Return a list of jobs for the system tests."
227 (define instance
228 (checkout->channel-instance source #:commit commit))
229
230 (define (test->thunk test)
231 (lambda ()
232 (define drv
233 (run-with-store store
234 (mbegin %store-monad
235 (set-current-system system)
236 (set-grafting #f)
237 (set-guile-for-build (default-guile))
238 (system-test-value test))))
239
240 `((derivation . ,(derivation-file-name drv))
241 (description . ,(format #f "Guix '~a' system test"
242 (system-test-name test)))
243 (long-description . ,(system-test-description test))
244 (license . ,(license-name gpl3+))
245 (max-silent-time . 600)
246 (timeout . 3600)
247 (home-page . ,%guix-home-page-url)
248 (maintainers . ("bug-guix@gnu.org")))))
249
250 (define (->job test)
251 (let ((name (string->symbol
252 (string-append "test." (system-test-name test)
253 "." system))))
254 (cons name (test->thunk test))))
255
256 (if (and (member system %guixsd-supported-systems)
257
258 ;; XXX: Our build farm has too few ARMv7 machines and they are very
259 ;; slow, so skip system tests there.
260 (not (string=? system "armhf-linux")))
261 ;; Override the value of 'current-guix' used by system tests. Using a
262 ;; channel instance makes tests that rely on 'current-guix' less
263 ;; expensive. It also makes sure we get a valid Guix package when this
264 ;; code is not running from a checkout.
265 (parameterize ((current-guix-package
266 (channel-instance->package instance)))
267 (map ->job (all-system-tests)))
268 '()))
269
270 (define (tarball-jobs store system)
271 "Return Hydra jobs to build the self-contained Guix binary tarball."
272 (define (->alist drv)
273 `((derivation . ,(derivation-file-name drv))
274 (description . "Stand-alone binary Guix tarball")
275 (long-description . "This is a tarball containing binaries of Guix and
276 all its dependencies, and ready to be installed on \"foreign\" distributions.")
277 (license . ,(license-name gpl3+))
278 (home-page . ,%guix-home-page-url)
279 (maintainers . ("bug-guix@gnu.org"))))
280
281 (define (->job name drv)
282 (let ((name (symbol-append name (string->symbol ".")
283 (string->symbol system))))
284 `(,name . ,(lambda ()
285 (parameterize ((%graft? #f))
286 (->alist drv))))))
287
288 ;; XXX: Add a job for the stable Guix?
289 (list (->job 'binary-tarball
290 (run-with-store store
291 (mbegin %store-monad
292 (set-guile-for-build (default-guile))
293 (>>= (profile-derivation (packages->manifest (list guix)))
294 (lambda (profile)
295 (self-contained-tarball "guix-binary" profile
296 #:localstatedir? #t
297 #:compressor
298 (lookup-compressor "xz")))))
299 #:system system))))
300
301 (define job-name
302 ;; Return the name of a package's job.
303 (compose string->symbol
304 (cut package-full-name <> "-")))
305
306 (define package->job
307 (let ((base-packages
308 (delete-duplicates
309 (append-map (match-lambda
310 ((_ package _ ...)
311 (match (package-transitive-inputs package)
312 (((_ inputs _ ...) ...)
313 inputs))))
314 (%final-inputs)))))
315 (lambda (store package system)
316 "Return a job for PACKAGE on SYSTEM, or #f if this combination is not
317 valid."
318 (cond ((member package base-packages)
319 (package-job store (symbol-append 'base. (job-name package))
320 package system))
321 ((supported-package? package system)
322 (let ((drv (package-derivation store package system
323 #:graft? #f)))
324 (and (substitutable-derivation? drv)
325 (package-job store (job-name package)
326 package system))))
327 (else
328 #f)))))
329
330 (define (all-packages)
331 "Return the list of packages to build."
332 (define (adjust package result)
333 (cond ((package-replacement package)
334 ;; XXX: If PACKAGE and its replacement have the same name/version,
335 ;; then both Cuirass jobs will have the same name, which
336 ;; effectively means that the second one will be ignored. Thus,
337 ;; return the replacement first.
338 (cons* (package-replacement package) ;build both
339 package
340 result))
341 ((package-superseded package)
342 result) ;don't build it
343 (else
344 (cons package result))))
345
346 (fold-packages adjust
347 (fold adjust '() ;include base packages
348 (match (%final-inputs)
349 (((labels packages _ ...) ...)
350 packages)))
351 #:select? (const #t))) ;include hidden packages
352
353 (define (arguments->manifests arguments)
354 "Return the list of manifests extracted from ARGUMENTS."
355 (map (match-lambda
356 ((input-name . relative-path)
357 (let* ((checkout (assq-ref arguments (string->symbol input-name)))
358 (base (assq-ref checkout 'file-name)))
359 (in-vicinity base relative-path))))
360 (assq-ref arguments 'manifests)))
361
362 (define (manifests->packages store manifests)
363 "Return the list of packages found in MANIFESTS."
364 (define (load-manifest manifest)
365 (save-module-excursion
366 (lambda ()
367 (set-current-module (make-user-module '((guix profiles) (gnu))))
368 (primitive-load manifest))))
369
370 (delete-duplicates!
371 (map manifest-entry-item
372 (append-map (compose manifest-entries
373 load-manifest)
374 manifests))))
375
376 \f
377 ;;;
378 ;;; Hydra entry point.
379 ;;;
380
381 (define (hydra-jobs store arguments)
382 "Return Hydra jobs."
383 (define subset
384 (match (assoc-ref arguments 'subset)
385 ("core" 'core) ; only build core packages
386 ("hello" 'hello) ; only build hello
387 (((? string?) (? string?) ...) 'list) ; only build selected list of packages
388 ("manifests" 'manifests) ; only build packages in the list of manifests
389 (_ 'all))) ; build everything
390
391 (define systems
392 (match (assoc-ref arguments 'systems)
393 (#f %hydra-supported-systems)
394 ((lst ...) lst)
395 ((? string? str) (call-with-input-string str read))))
396
397 (define checkout
398 ;; Extract metadata about the 'guix' checkout. Its key in ARGUMENTS may
399 ;; vary, so pick up the first one that's neither 'subset' nor 'systems'.
400 (any (match-lambda
401 ((key . value)
402 (and (not (memq key '(systems subset)))
403 value)))
404 arguments))
405
406 (define commit
407 (assq-ref checkout 'revision))
408
409 (define source
410 (assq-ref checkout 'file-name))
411
412 (define (cross-jobs system)
413 (define (from-32-to-64? target)
414 ;; Return true if SYSTEM is 32-bit and TARGET is 64-bit. This hack
415 ;; prevents known-to-fail cross-builds from i686-linux or armhf-linux to
416 ;; mips64el-linux-gnuabi64.
417 (and (or (string-prefix? "i686-" system)
418 (string-prefix? "i586-" system)
419 (string-prefix? "armhf-" system))
420 (string-contains target "64"))) ;x86_64, mips64el, aarch64, etc.
421
422 (define (same? target)
423 ;; Return true if SYSTEM and TARGET are the same thing. This is so we
424 ;; don't try to cross-compile to 'mips64el-linux-gnu' from
425 ;; 'mips64el-linux'.
426 (or (string-contains target system)
427 (and (string-prefix? "armhf" system) ;armhf-linux
428 (string-prefix? "arm" target)))) ;arm-linux-gnueabihf
429
430 (define (pointless? target)
431 ;; Return #t if it makes no sense to cross-build to TARGET from SYSTEM.
432 (match system
433 ((or "x86_64-linux" "i686-linux")
434 (if (string-contains target "mingw")
435 (not (string=? "x86_64-linux" system))
436 #f))
437 (_
438 ;; Don't try to cross-compile from non-Intel platforms: this isn't
439 ;; very useful and these are often brittle configurations.
440 #t)))
441
442 (define (either proc1 proc2 proc3)
443 (lambda (x)
444 (or (proc1 x) (proc2 x) (proc3 x))))
445
446 (append-map (lambda (target)
447 (map (lambda (package)
448 (package-cross-job store (job-name package)
449 package target system))
450 %packages-to-cross-build))
451 (remove (either from-32-to-64? same? pointless?)
452 %cross-targets)))
453
454 ;; Turn off grafts. Grafting is meant to happen on the user's machines.
455 (parameterize ((%graft? #f))
456 ;; Return one job for each package, except bootstrap packages.
457 (append-map (lambda (system)
458 (format (current-error-port)
459 "evaluating for '~a' (heap size: ~a MiB)...~%"
460 system
461 (round
462 (/ (assoc-ref (gc-stats) 'heap-size)
463 (expt 2. 20))))
464 (invalidate-derivation-caches!)
465 (case subset
466 ((all)
467 ;; Build everything, including replacements.
468 (let ((all (all-packages))
469 (job (lambda (package)
470 (package->job store package
471 system))))
472 (append (filter-map job all)
473 (qemu-jobs store system)
474 (system-test-jobs store system
475 #:source source
476 #:commit commit)
477 (tarball-jobs store system)
478 (cross-jobs system))))
479 ((core)
480 ;; Build core packages only.
481 (append (map (lambda (package)
482 (package-job store (job-name package)
483 package system))
484 %core-packages)
485 (cross-jobs system)))
486 ((hello)
487 ;; Build hello package only.
488 (if (string=? system (%current-system))
489 (let ((hello (specification->package "hello")))
490 (list (package-job store (job-name hello) hello system)))
491 '()))
492 ((list)
493 ;; Build selected list of packages only.
494 (if (string=? system (%current-system))
495 (let* ((names (assoc-ref arguments 'subset))
496 (packages (map specification->package names)))
497 (map (lambda (package)
498 (package-job store (job-name package)
499 package system))
500 packages))
501 '()))
502 ((manifests)
503 ;; Build packages in the list of manifests.
504 (let* ((manifests (arguments->manifests arguments))
505 (packages (manifests->packages store manifests)))
506 (map (lambda (package)
507 (package-job store (job-name package)
508 package system))
509 packages)))
510 (else
511 (error "unknown subset" subset))))
512 systems)))