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