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