WIP: bees service
[jackhill/guix/guix.git] / gnu / ci.scm
CommitLineData
59fb5c1c 1;;; GNU Guix --- Functional package management for GNU
2032d847 2;;; Copyright © 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020 Ludovic Courtès <ludo@gnu.org>
db2785cd 3;;; Copyright © 2017, 2020 Jan (janneke) Nieuwenhuizen <janneke@gnu.org>
f71b0a00 4;;; Copyright © 2018, 2019 Clément Lassieur <clement@lassieur.org>
f43ec1c5 5;;; Copyright © 2020 Julien Lepiller <julien@lepiller.eu>
76bea3f8 6;;; Copyright © 2020, 2021 Mathieu Othacehe <othacehe@gnu.org>
59fb5c1c
LC
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)
76bea3f8 24 #:use-module (guix channels)
59fb5c1c 25 #:use-module (guix config)
61a11653 26 #:use-module (guix describe)
59fb5c1c
LC
27 #:use-module (guix store)
28 #:use-module (guix grafts)
29 #:use-module (guix profiles)
30 #:use-module (guix packages)
7e6d8d36 31 #:use-module (guix channels)
f43ec1c5 32 #:use-module (guix config)
59fb5c1c 33 #:use-module (guix derivations)
7e6d8d36 34 #:use-module (guix build-system)
59fb5c1c 35 #:use-module (guix monads)
dd1ee160 36 #:use-module (guix gexp)
59fb5c1c 37 #:use-module (guix ui)
b5f8c2c8
LC
38 #:use-module ((guix licenses)
39 #:select (gpl3+ license? license-name))
59fb5c1c
LC
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)
f19cf27c 46 #:use-module (gnu image)
59fb5c1c
LC
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)
f19cf27c 58 #:use-module (gnu system image)
59fb5c1c
LC
59 #:use-module (gnu system vm)
60 #:use-module (gnu system install)
b06ba9e0 61 #:use-module (gnu system images hurd)
846e5240 62 #:use-module (gnu system images novena)
8a4f1eef 63 #:use-module (gnu system images pine64)
17d9a91e 64 #:use-module (gnu system images pinebook-pro)
59fb5c1c
LC
65 #:use-module (gnu tests)
66 #:use-module (srfi srfi-1)
67 #:use-module (srfi srfi-26)
68 #:use-module (ice-9 match)
76bea3f8
MO
69 #:export (%core-packages
70 %cross-targets
f292c501 71 channel-source->package
76bea3f8 72 cuirass-jobs))
59fb5c1c
LC
73
74;;; Commentary:
75;;;
76bea3f8 76;;; This file defines build jobs for Cuirass.
59fb5c1c
LC
77;;;
78;;; Code:
79
76bea3f8
MO
80(define* (derivation->job name drv
81 #:key
76bea3f8
MO
82 (max-silent-time 3600)
83 (timeout 3600))
14ada964 84 "Return a Cuirass job called NAME and describing DRV.
76bea3f8
MO
85
86MAX-SILENT-TIME and TIMEOUT are build options passed to the daemon when
87building 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))
76bea3f8
MO
98 (#:max-silent-time . ,max-silent-time)
99 (#:timeout . ,timeout)))
100
101(define* (package-job store job-name package system
102 #:key cross? target)
59fb5c1c 103 "Return a job called JOB-NAME that builds PACKAGE on SYSTEM."
76bea3f8
MO
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)))))
59fb5c1c
LC
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
123SYSTEM."
76bea3f8
MO
124 (let ((name (string-append target "." job-name "." system)))
125 (package-job store name package system
126 #:cross? #t
127 #:target target)))
59fb5c1c
LC
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.)
84f38f03 133 (list gcc-7 gcc-8 gcc-9 gcc-10 glibc binutils
59fb5c1c
LC
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
668a5198
LC
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))
59fb5c1c
LC
149
150(define %cross-targets
151 '("mips64el-linux-gnu"
59fb5c1c
LC
152 "arm-linux-gnueabihf"
153 "aarch64-linux-gnu"
154 "powerpc-linux-gnu"
8d9aece8 155 "powerpc64le-linux-gnu"
2032d847 156 "riscv64-linux-gnu"
59fb5c1c 157 "i586-pc-gnu" ;aka. GNU/Hurd
67dac6b8
CD
158 "i686-w64-mingw32"
159 "x86_64-w64-mingw32"))
59fb5c1c 160
3046e73b
LC
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))
50b99c90 200 (packages-to-cross-build target)))
3046e73b
LC
201 (remove (either from-32-to-64? same? pointless?)
202 %cross-targets)))
203
76bea3f8
MO
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
b06ba9e0
MO
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"))
59fb5c1c 225
b06ba9e0 226(define %guix-system-images
8a4f1eef 227 (list hurd-barebones-qcow2-image
17d9a91e 228 pine64-barebones-raw-image
846e5240
DM
229 pinebook-pro-barebones-raw-image
230 novena-barebones-raw-image))
59fb5c1c 231
fc2fa7ad
MO
232(define (hours hours)
233 (* 3600 hours))
234
b06ba9e0 235(define (image-jobs store system)
14ada964 236 "Return a list of jobs that build images for SYSTEM."
59fb5c1c 237 (define (->job name drv)
76bea3f8
MO
238 (let ((name (string-append name "." system)))
239 (parameterize ((%graft? #f))
14ada964 240 (derivation->job name drv))))
59fb5c1c 241
b06ba9e0
MO
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
59fb5c1c
LC
248 (define MiB
249 (expt 2 20))
250
b06ba9e0 251 (if (member system %guix-system-supported-systems)
76bea3f8 252 `(,(->job "usb-image"
b06ba9e0
MO
253 (build-image
254 (image
255 (inherit efi-disk-image)
b06ba9e0 256 (operating-system installation-os))))
76bea3f8 257 ,(->job "iso9660-image"
b06ba9e0
MO
258 (build-image
259 (image
f43ec1c5 260 (inherit (image-with-label
76bea3f8
MO
261 iso9660-image
262 (string-append "GUIX_" system "_"
263 (if (> (string-length %guix-version) 7)
264 (substring %guix-version 0 7)
265 %guix-version))))
b06ba9e0
MO
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)
76bea3f8
MO
270 (->job (symbol->string (image-name image))
271 (build-image image)))
b06ba9e0
MO
272 %guix-system-images)
273 '()))
1189f405 274 '()))
59fb5c1c 275
7e6d8d36
LC
276(define channel-build-system
277 ;; Build system used to "convert" a channel instance to a package.
278 (let* ((build (lambda* (store name inputs
dd1ee160 279 #:key source commit system
c3ab921e 280 #:allow-other-keys)
7e6d8d36 281 (run-with-store store
bc8b2ffd
LC
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)))
dd1ee160
LC
287 (instance
288 -> (checkout->channel-instance
289 source #:commit commit)))
290 (channel-instances->derivation (list instance)))
c3ab921e 291 #:system system)))
dd1ee160
LC
292 (lower (lambda* (name #:key system source commit
293 #:allow-other-keys)
7e6d8d36
LC
294 (bag
295 (name name)
296 (system system)
297 (build build)
dd1ee160
LC
298 (arguments `(#:source ,source
299 #:commit ,commit))))))
7e6d8d36
LC
300 (build-system (name 'channel)
301 (description "Turn a channel instance into a package.")
302 (lower lower))))
303
dd1ee160
LC
304(define* (channel-source->package source #:key commit)
305 "Return a package for the given channel SOURCE, a lowerable object."
7e6d8d36
LC
306 (package
307 (inherit guix)
dd1ee160 308 (version (string-append (package-version guix) "+"))
7e6d8d36 309 (build-system channel-build-system)
dd1ee160
LC
310 (arguments `(#:source ,source
311 #:commit ,commit))
7e6d8d36
LC
312 (inputs '())
313 (native-inputs '())
314 (propagated-inputs '())))
315
316(define* (system-test-jobs store system
317 #:key source commit)
59fb5c1c 318 "Return a list of jobs for the system tests."
59fb5c1c 319 (define (->job test)
fc37346f
MO
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)))))
76bea3f8 328
fc37346f 329 (derivation->job name drv)))
59fb5c1c 330
b06ba9e0 331 (if (member system %guix-system-supported-systems)
7e6d8d36
LC
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.
fc37346f
MO
336 (parameterize ((current-guix-package
337 (channel-source->package source #:commit commit)))
338 (map ->job (all-system-tests)))
59fb5c1c
LC
339 '()))
340
341(define (tarball-jobs store system)
76bea3f8 342 "Return jobs to build the self-contained Guix binary tarball."
59fb5c1c 343 (define (->job name drv)
76bea3f8
MO
344 (let ((name (string-append name "." system)))
345 (parameterize ((%graft? #f))
14ada964 346 (derivation->job name drv))))
59fb5c1c
LC
347
348 ;; XXX: Add a job for the stable Guix?
76bea3f8
MO
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
2ccb715a 357 #:profile-name "current-guix"
76bea3f8
MO
358 #:localstatedir? #t
359 #:compressor
360 (lookup-compressor "xz")))))
361 #:system system))))
59fb5c1c
LC
362
363(define job-name
364 ;; Return the name of a package's job.
76bea3f8 365 package-name)
59fb5c1c
LC
366
367(define package->job
368 (let ((base-packages
369 (delete-duplicates
370 (append-map (match-lambda
76bea3f8
MO
371 ((_ package _ ...)
372 (match (package-transitive-inputs package)
373 (((_ inputs _ ...) ...)
374 inputs))))
59fb5c1c
LC
375 (%final-inputs)))))
376 (lambda (store package system)
377 "Return a job for PACKAGE on SYSTEM, or #f if this combination is not
378valid."
379 (cond ((member package base-packages)
76bea3f8 380 (package-job store (string-append "base." (job-name package))
59fb5c1c
LC
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)
f00ff8db
LC
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
59fb5c1c
LC
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
76bea3f8 414(define (arguments->manifests arguments channels)
59fb5c1c 415 "Return the list of manifests extracted from ARGUMENTS."
862af8c2
MO
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)))
76bea3f8 422 arguments))
59fb5c1c
LC
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;;;
76bea3f8 440;;; Cuirass entry point.
59fb5c1c
LC
441;;;
442
76bea3f8
MO
443(define (cuirass-jobs store arguments)
444 "Register Cuirass jobs."
59fb5c1c 445 (define subset
76bea3f8 446 (assoc-ref arguments 'subset))
59fb5c1c
LC
447
448 (define systems
449 (match (assoc-ref arguments 'systems)
76bea3f8 450 (#f %cuirass-supported-systems)
59fb5c1c
LC
451 ((lst ...) lst)
452 ((? string? str) (call-with-input-string str read))))
453
76bea3f8
MO
454 (define channels
455 (let ((channels (assq-ref arguments 'channels)))
456 (map sexp->channel channels)))
457
458 (define guix
459 (find guix-channel? channels))
7e6d8d36
LC
460
461 (define commit
76bea3f8 462 (channel-commit guix))
7e6d8d36
LC
463
464 (define source
76bea3f8 465 (channel-url guix))
7e6d8d36 466
59fb5c1c
LC
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.
76bea3f8
MO
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)
76bea3f8
MO
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))))
2afc79b5
MO
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))
61a11653
MO
517 (('channels . channels)
518 ;; Build only the packages from CHANNELS.
519 (let ((all (all-packages)))
520 (filter-map
521 (lambda (package)
cbfcbb79
MO
522 (any (lambda (channel)
523 (and (member (channel-name channel) channels)
524 (package->job store package system)))
525 (package-channels package)))
61a11653 526 all)))
76bea3f8
MO
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)))