ci: Introduce new subsets.
[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 (parameterize ((current-guix-package
321 (channel-source->package source #:commit commit)))
322 (let ((name (string-append "test." (system-test-name test)
323 "." system))
324 (drv (run-with-store store
325 (mbegin %store-monad
326 (set-current-system system)
327 (set-grafting #f)
328 (set-guile-for-build (default-guile))
329 (system-test-value test)))))
330
331 (derivation->job name drv))))
332
333 (if (member system %guix-system-supported-systems)
334 ;; Override the value of 'current-guix' used by system tests. Using a
335 ;; channel instance makes tests that rely on 'current-guix' less
336 ;; expensive. It also makes sure we get a valid Guix package when this
337 ;; code is not running from a checkout.
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 #:localstatedir? #t
358 #:compressor
359 (lookup-compressor "xz")))))
360 #:system system))))
361
362 (define job-name
363 ;; Return the name of a package's job.
364 package-name)
365
366 (define package->job
367 (let ((base-packages
368 (delete-duplicates
369 (append-map (match-lambda
370 ((_ package _ ...)
371 (match (package-transitive-inputs package)
372 (((_ inputs _ ...) ...)
373 inputs))))
374 (%final-inputs)))))
375 (lambda (store package system)
376 "Return a job for PACKAGE on SYSTEM, or #f if this combination is not
377 valid."
378 (cond ((member package base-packages)
379 (package-job store (string-append "base." (job-name package))
380 package system))
381 ((supported-package? package system)
382 (let ((drv (package-derivation store package system
383 #:graft? #f)))
384 (and (substitutable-derivation? drv)
385 (package-job store (job-name package)
386 package system))))
387 (else
388 #f)))))
389
390 (define (all-packages)
391 "Return the list of packages to build."
392 (define (adjust package result)
393 (cond ((package-replacement package)
394 ;; XXX: If PACKAGE and its replacement have the same name/version,
395 ;; then both Cuirass jobs will have the same name, which
396 ;; effectively means that the second one will be ignored. Thus,
397 ;; return the replacement first.
398 (cons* (package-replacement package) ;build both
399 package
400 result))
401 ((package-superseded package)
402 result) ;don't build it
403 (else
404 (cons package result))))
405
406 (fold-packages adjust
407 (fold adjust '() ;include base packages
408 (match (%final-inputs)
409 (((labels packages _ ...) ...)
410 packages)))
411 #:select? (const #t))) ;include hidden packages
412
413 (define (arguments->manifests arguments channels)
414 "Return the list of manifests extracted from ARGUMENTS."
415 (map (lambda (manifest)
416 (any (lambda (checkout)
417 (let ((path (in-vicinity checkout manifest)))
418 (and (file-exists? path)
419 path)))
420 (map channel-url channels)))
421 arguments))
422
423 (define (manifests->packages store manifests)
424 "Return the list of packages found in MANIFESTS."
425 (define (load-manifest manifest)
426 (save-module-excursion
427 (lambda ()
428 (set-current-module (make-user-module '((guix profiles) (gnu))))
429 (primitive-load manifest))))
430
431 (delete-duplicates!
432 (map manifest-entry-item
433 (append-map (compose manifest-entries
434 load-manifest)
435 manifests))))
436
437 \f
438 ;;;
439 ;;; Cuirass entry point.
440 ;;;
441
442 (define (cuirass-jobs store arguments)
443 "Register Cuirass jobs."
444 (define subset
445 (assoc-ref arguments 'subset))
446
447 (define systems
448 (match (assoc-ref arguments 'systems)
449 (#f %cuirass-supported-systems)
450 ((lst ...) lst)
451 ((? string? str) (call-with-input-string str read))))
452
453 (define channels
454 (let ((channels (assq-ref arguments 'channels)))
455 (map sexp->channel channels)))
456
457 (define guix
458 (find guix-channel? channels))
459
460 (define commit
461 (channel-commit guix))
462
463 (define source
464 (channel-url guix))
465
466 ;; Turn off grafts. Grafting is meant to happen on the user's machines.
467 (parameterize ((%graft? #f))
468 ;; Return one job for each package, except bootstrap packages.
469 (append-map
470 (lambda (system)
471 (format (current-error-port)
472 "evaluating for '~a' (heap size: ~a MiB)...~%"
473 system
474 (round
475 (/ (assoc-ref (gc-stats) 'heap-size)
476 (expt 2. 20))))
477 (invalidate-derivation-caches!)
478 (match subset
479 ('all
480 ;; Build everything, including replacements.
481 (let ((all (all-packages))
482 (job (lambda (package)
483 (package->job store package system))))
484 (append
485 (filter-map job all)
486 (cross-jobs store system))))
487 ('core
488 ;; Build core packages only.
489 (append
490 (map (lambda (package)
491 (package-job store (job-name package)
492 package system))
493 %core-packages)
494 (cross-jobs store system)))
495 ('guix
496 ;; Build Guix modules only.
497 (guix-jobs store systems
498 #:source source
499 #:commit commit))
500 ('hello
501 ;; Build hello package only.
502 (let ((hello (specification->package "hello")))
503 (list (package-job store (job-name hello)
504 hello system))))
505 ('images
506 ;; Build Guix System images only.
507 (image-jobs store system))
508 ('system-tests
509 ;; Build Guix System tests only.
510 (system-test-jobs store system
511 #:source source
512 #:commit commit))
513 ('tarball
514 ;; Build Guix tarball only.
515 (tarball-jobs store system))
516 (('channels . channels)
517 ;; Build only the packages from CHANNELS.
518 (let ((all (all-packages)))
519 (filter-map
520 (lambda (package)
521 (any (lambda (channel)
522 (and (member (channel-name channel) channels)
523 (package->job store package system)))
524 (package-channels package)))
525 all)))
526 (('packages . rest)
527 ;; Build selected list of packages only.
528 (let ((packages (map specification->package rest)))
529 (map (lambda (package)
530 (package-job store (job-name package)
531 package system))
532 packages)))
533 (('manifests . rest)
534 ;; Build packages in the list of manifests.
535 (let* ((manifests (arguments->manifests rest channels))
536 (packages (manifests->packages store manifests)))
537 (map (lambda (package)
538 (package-job store (job-name package)
539 package system))
540 packages)))
541 (else
542 (error "unknown subset" subset))))
543 systems)))