WIP: bees service
[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 ;;; 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 channels)
25 #:use-module (guix config)
26 #:use-module (guix describe)
27 #:use-module (guix store)
28 #:use-module (guix grafts)
29 #:use-module (guix profiles)
30 #:use-module (guix packages)
31 #:use-module (guix channels)
32 #:use-module (guix config)
33 #:use-module (guix derivations)
34 #:use-module (guix build-system)
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 (lookup-compressor self-contained-tarball))
44 #:use-module (gnu bootloader)
45 #:use-module (gnu bootloader u-boot)
46 #:use-module (gnu image)
47 #:use-module (gnu packages)
48 #:use-module (gnu packages gcc)
49 #:use-module (gnu packages base)
50 #:use-module (gnu packages gawk)
51 #:use-module (gnu packages guile)
52 #:use-module (gnu packages gettext)
53 #:use-module (gnu packages compression)
54 #:use-module (gnu packages multiprecision)
55 #:use-module (gnu packages make-bootstrap)
56 #:use-module (gnu packages package-management)
57 #:use-module (gnu system)
58 #:use-module (gnu system image)
59 #:use-module (gnu system vm)
60 #:use-module (gnu system install)
61 #:use-module (gnu system images hurd)
62 #:use-module (gnu system images novena)
63 #:use-module (gnu system images pine64)
64 #:use-module (gnu system images pinebook-pro)
65 #:use-module (gnu tests)
66 #:use-module (srfi srfi-1)
67 #:use-module (srfi srfi-26)
68 #:use-module (ice-9 match)
69 #:export (%core-packages
70 %cross-targets
71 channel-source->package
72 cuirass-jobs))
73
74 ;;; Commentary:
75 ;;;
76 ;;; This file defines build jobs for Cuirass.
77 ;;;
78 ;;; Code:
79
80 (define* (derivation->job name drv
81 #:key
82 (max-silent-time 3600)
83 (timeout 3600))
84 "Return a Cuirass job called NAME and describing DRV.
85
86 MAX-SILENT-TIME and TIMEOUT are build options passed to the daemon when
87 building the derivation."
88 `((#:job-name . ,name)
89 (#:derivation . ,(derivation-file-name drv))
90 (#:outputs . ,(filter-map
91 (lambda (res)
92 (match res
93 ((name . path)
94 `(,name . ,path))))
95 (derivation->output-paths drv)))
96 (#:nix-name . ,(derivation-name drv))
97 (#:system . ,(derivation-system drv))
98 (#:max-silent-time . ,max-silent-time)
99 (#:timeout . ,timeout)))
100
101 (define* (package-job store job-name package system
102 #:key cross? target)
103 "Return a job called JOB-NAME that builds PACKAGE on SYSTEM."
104 (let ((job-name (string-append job-name "." system)))
105 (parameterize ((%graft? #f))
106 (let* ((drv (if cross?
107 (package-cross-derivation store package target system
108 #:graft? #f)
109 (package-derivation store package system
110 #:graft? #f)))
111 (max-silent-time (or (assoc-ref (package-properties package)
112 'max-silent-time)
113 3600))
114 (timeout (or (assoc-ref (package-properties package)
115 'timeout)
116 72000)))
117 (derivation->job job-name drv
118 #:max-silent-time max-silent-time
119 #:timeout timeout)))))
120
121 (define (package-cross-job store job-name package target system)
122 "Return a job called TARGET.JOB-NAME that cross-builds PACKAGE for TARGET on
123 SYSTEM."
124 (let ((name (string-append target "." job-name "." system)))
125 (package-job store name package system
126 #:cross? #t
127 #:target target)))
128
129 (define %core-packages
130 ;; Note: Don't put the '-final' package variants because (1) that's
131 ;; implicit, and (2) they cannot be cross-built (due to the explicit input
132 ;; chain.)
133 (list gcc-7 gcc-8 gcc-9 gcc-10 glibc binutils
134 gmp mpfr mpc coreutils findutils diffutils patch sed grep
135 gawk gnu-gettext hello guile-2.0 guile-2.2 zlib gzip xz
136 %bootstrap-binaries-tarball
137 %binutils-bootstrap-tarball
138 (%glibc-bootstrap-tarball)
139 %gcc-bootstrap-tarball
140 %guile-bootstrap-tarball
141 %bootstrap-tarballs))
142
143 (define (packages-to-cross-build target)
144 "Return the list of packages to cross-build for TARGET."
145 ;; Don't cross-build the bootstrap tarballs for MinGW.
146 (if (string-contains target "mingw")
147 (drop-right %core-packages 6)
148 %core-packages))
149
150 (define %cross-targets
151 '("mips64el-linux-gnu"
152 "arm-linux-gnueabihf"
153 "aarch64-linux-gnu"
154 "powerpc-linux-gnu"
155 "powerpc64le-linux-gnu"
156 "riscv64-linux-gnu"
157 "i586-pc-gnu" ;aka. GNU/Hurd
158 "i686-w64-mingw32"
159 "x86_64-w64-mingw32"))
160
161 (define (cross-jobs store system)
162 "Return a list of cross-compilation jobs for SYSTEM."
163 (define (from-32-to-64? target)
164 ;; Return true if SYSTEM is 32-bit and TARGET is 64-bit. This hack
165 ;; prevents known-to-fail cross-builds from i686-linux or armhf-linux to
166 ;; mips64el-linux-gnuabi64.
167 (and (or (string-prefix? "i686-" system)
168 (string-prefix? "i586-" system)
169 (string-prefix? "armhf-" system))
170 (string-contains target "64"))) ;x86_64, mips64el, aarch64, etc.
171
172 (define (same? target)
173 ;; Return true if SYSTEM and TARGET are the same thing. This is so we
174 ;; don't try to cross-compile to 'mips64el-linux-gnu' from
175 ;; 'mips64el-linux'.
176 (or (string-contains target system)
177 (and (string-prefix? "armhf" system) ;armhf-linux
178 (string-prefix? "arm" target)))) ;arm-linux-gnueabihf
179
180 (define (pointless? target)
181 ;; Return #t if it makes no sense to cross-build to TARGET from SYSTEM.
182 (match system
183 ((or "x86_64-linux" "i686-linux")
184 (if (string-contains target "mingw")
185 (not (string=? "x86_64-linux" system))
186 #f))
187 (_
188 ;; Don't try to cross-compile from non-Intel platforms: this isn't
189 ;; very useful and these are often brittle configurations.
190 #t)))
191
192 (define (either proc1 proc2 proc3)
193 (lambda (x)
194 (or (proc1 x) (proc2 x) (proc3 x))))
195
196 (append-map (lambda (target)
197 (map (lambda (package)
198 (package-cross-job store (job-name package)
199 package target system))
200 (packages-to-cross-build target)))
201 (remove (either from-32-to-64? same? pointless?)
202 %cross-targets)))
203
204 (define* (guix-jobs store systems #:key source commit)
205 "Return a list of jobs for Guix itself."
206 (define build
207 (primitive-load (string-append source "/build-aux/build-self.scm")))
208
209 (map
210 (lambda (system)
211 (let ((name (string->symbol
212 (string-append "guix." system)))
213 (drv (run-with-store store
214 (build source #:version commit #:system system
215 #:pull-version 1
216 #:guile-version "2.2"))))
217 (derivation->job name drv)))
218 systems))
219
220 ;; Architectures that are able to build or cross-build Guix System images.
221 ;; This does not mean that other architectures are not supported, only that
222 ;; they are often not fast enough to support Guix System images building.
223 (define %guix-system-supported-systems
224 '("x86_64-linux" "i686-linux"))
225
226 (define %guix-system-images
227 (list hurd-barebones-qcow2-image
228 pine64-barebones-raw-image
229 pinebook-pro-barebones-raw-image
230 novena-barebones-raw-image))
231
232 (define (hours hours)
233 (* 3600 hours))
234
235 (define (image-jobs store system)
236 "Return a list of jobs that build images for SYSTEM."
237 (define (->job name drv)
238 (let ((name (string-append name "." system)))
239 (parameterize ((%graft? #f))
240 (derivation->job name drv))))
241
242 (define (build-image image)
243 (run-with-store store
244 (mbegin %store-monad
245 (set-guile-for-build (default-guile))
246 (lower-object (system-image image)))))
247
248 (define MiB
249 (expt 2 20))
250
251 (if (member system %guix-system-supported-systems)
252 `(,(->job "usb-image"
253 (build-image
254 (image
255 (inherit efi-disk-image)
256 (operating-system installation-os))))
257 ,(->job "iso9660-image"
258 (build-image
259 (image
260 (inherit (image-with-label
261 iso9660-image
262 (string-append "GUIX_" system "_"
263 (if (> (string-length %guix-version) 7)
264 (substring %guix-version 0 7)
265 %guix-version))))
266 (operating-system installation-os))))
267 ;; Only cross-compile Guix System images from x86_64-linux for now.
268 ,@(if (string=? system "x86_64-linux")
269 (map (lambda (image)
270 (->job (symbol->string (image-name image))
271 (build-image image)))
272 %guix-system-images)
273 '()))
274 '()))
275
276 (define channel-build-system
277 ;; Build system used to "convert" a channel instance to a package.
278 (let* ((build (lambda* (store name inputs
279 #:key source commit system
280 #:allow-other-keys)
281 (run-with-store store
282 ;; SOURCE can be a lowerable object such as <local-file>
283 ;; or a file name. Adjust accordingly.
284 (mlet* %store-monad ((source (if (string? source)
285 (return source)
286 (lower-object source)))
287 (instance
288 -> (checkout->channel-instance
289 source #:commit commit)))
290 (channel-instances->derivation (list instance)))
291 #:system system)))
292 (lower (lambda* (name #:key system source commit
293 #:allow-other-keys)
294 (bag
295 (name name)
296 (system system)
297 (build build)
298 (arguments `(#:source ,source
299 #:commit ,commit))))))
300 (build-system (name 'channel)
301 (description "Turn a channel instance into a package.")
302 (lower lower))))
303
304 (define* (channel-source->package source #:key commit)
305 "Return a package for the given channel SOURCE, a lowerable object."
306 (package
307 (inherit guix)
308 (version (string-append (package-version guix) "+"))
309 (build-system channel-build-system)
310 (arguments `(#:source ,source
311 #:commit ,commit))
312 (inputs '())
313 (native-inputs '())
314 (propagated-inputs '())))
315
316 (define* (system-test-jobs store system
317 #:key source commit)
318 "Return a list of jobs for the system tests."
319 (define (->job test)
320 (let ((name (string-append "test." (system-test-name test)
321 "." system))
322 (drv (run-with-store store
323 (mbegin %store-monad
324 (set-current-system system)
325 (set-grafting #f)
326 (set-guile-for-build (default-guile))
327 (system-test-value test)))))
328
329 (derivation->job name drv)))
330
331 (if (member system %guix-system-supported-systems)
332 ;; Override the value of 'current-guix' used by system tests. Using a
333 ;; channel instance makes tests that rely on 'current-guix' less
334 ;; expensive. It also makes sure we get a valid Guix package when this
335 ;; code is not running from a checkout.
336 (parameterize ((current-guix-package
337 (channel-source->package source #:commit commit)))
338 (map ->job (all-system-tests)))
339 '()))
340
341 (define (tarball-jobs store system)
342 "Return jobs to build the self-contained Guix binary tarball."
343 (define (->job name drv)
344 (let ((name (string-append name "." system)))
345 (parameterize ((%graft? #f))
346 (derivation->job name drv))))
347
348 ;; XXX: Add a job for the stable Guix?
349 (list
350 (->job "binary-tarball"
351 (run-with-store store
352 (mbegin %store-monad
353 (set-guile-for-build (default-guile))
354 (>>= (profile-derivation (packages->manifest (list guix)))
355 (lambda (profile)
356 (self-contained-tarball "guix-binary" profile
357 #:profile-name "current-guix"
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 package-name)
366
367 (define package->job
368 (let ((base-packages
369 (delete-duplicates
370 (append-map (match-lambda
371 ((_ package _ ...)
372 (match (package-transitive-inputs package)
373 (((_ inputs _ ...) ...)
374 inputs))))
375 (%final-inputs)))))
376 (lambda (store package system)
377 "Return a job for PACKAGE on SYSTEM, or #f if this combination is not
378 valid."
379 (cond ((member package base-packages)
380 (package-job store (string-append "base." (job-name package))
381 package system))
382 ((supported-package? package system)
383 (let ((drv (package-derivation store package system
384 #:graft? #f)))
385 (and (substitutable-derivation? drv)
386 (package-job store (job-name package)
387 package system))))
388 (else
389 #f)))))
390
391 (define (all-packages)
392 "Return the list of packages to build."
393 (define (adjust package result)
394 (cond ((package-replacement package)
395 ;; XXX: If PACKAGE and its replacement have the same name/version,
396 ;; then both Cuirass jobs will have the same name, which
397 ;; effectively means that the second one will be ignored. Thus,
398 ;; return the replacement first.
399 (cons* (package-replacement package) ;build both
400 package
401 result))
402 ((package-superseded package)
403 result) ;don't build it
404 (else
405 (cons package result))))
406
407 (fold-packages adjust
408 (fold adjust '() ;include base packages
409 (match (%final-inputs)
410 (((labels packages _ ...) ...)
411 packages)))
412 #:select? (const #t))) ;include hidden packages
413
414 (define (arguments->manifests arguments channels)
415 "Return the list of manifests extracted from ARGUMENTS."
416 (map (lambda (manifest)
417 (any (lambda (checkout)
418 (let ((path (in-vicinity checkout manifest)))
419 (and (file-exists? path)
420 path)))
421 (map channel-url channels)))
422 arguments))
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 \f
439 ;;;
440 ;;; Cuirass entry point.
441 ;;;
442
443 (define (cuirass-jobs store arguments)
444 "Register Cuirass jobs."
445 (define subset
446 (assoc-ref arguments 'subset))
447
448 (define systems
449 (match (assoc-ref arguments 'systems)
450 (#f %cuirass-supported-systems)
451 ((lst ...) lst)
452 ((? string? str) (call-with-input-string str read))))
453
454 (define channels
455 (let ((channels (assq-ref arguments 'channels)))
456 (map sexp->channel channels)))
457
458 (define guix
459 (find guix-channel? channels))
460
461 (define commit
462 (channel-commit guix))
463
464 (define source
465 (channel-url guix))
466
467 ;; Turn off grafts. Grafting is meant to happen on the user's machines.
468 (parameterize ((%graft? #f))
469 ;; Return one job for each package, except bootstrap packages.
470 (append-map
471 (lambda (system)
472 (format (current-error-port)
473 "evaluating for '~a' (heap size: ~a MiB)...~%"
474 system
475 (round
476 (/ (assoc-ref (gc-stats) 'heap-size)
477 (expt 2. 20))))
478 (invalidate-derivation-caches!)
479 (match subset
480 ('all
481 ;; Build everything, including replacements.
482 (let ((all (all-packages))
483 (job (lambda (package)
484 (package->job store package system))))
485 (append
486 (filter-map job all)
487 (cross-jobs store system))))
488 ('core
489 ;; Build core packages only.
490 (append
491 (map (lambda (package)
492 (package-job store (job-name package)
493 package system))
494 %core-packages)
495 (cross-jobs store system)))
496 ('guix
497 ;; Build Guix modules only.
498 (guix-jobs store systems
499 #:source source
500 #:commit commit))
501 ('hello
502 ;; Build hello package only.
503 (let ((hello (specification->package "hello")))
504 (list (package-job store (job-name hello)
505 hello system))))
506 ('images
507 ;; Build Guix System images only.
508 (image-jobs store system))
509 ('system-tests
510 ;; Build Guix System tests only.
511 (system-test-jobs store system
512 #:source source
513 #:commit commit))
514 ('tarball
515 ;; Build Guix tarball only.
516 (tarball-jobs store system))
517 (('channels . channels)
518 ;; Build only the packages from CHANNELS.
519 (let ((all (all-packages)))
520 (filter-map
521 (lambda (package)
522 (any (lambda (channel)
523 (and (member (channel-name channel) channels)
524 (package->job store package system)))
525 (package-channels package)))
526 all)))
527 (('packages . rest)
528 ;; Build selected list of packages only.
529 (let ((packages (map specification->package rest)))
530 (map (lambda (package)
531 (package-job store (job-name package)
532 package system))
533 packages)))
534 (('manifests . rest)
535 ;; Build packages in the list of manifests.
536 (let* ((manifests (arguments->manifests rest channels))
537 (packages (manifests->packages store manifests)))
538 (map (lambda (package)
539 (package-job store (job-name package)
540 package system))
541 packages)))
542 (else
543 (error "unknown subset" subset))))
544 systems)))