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