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