gnu: Add ktsuss.
[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 Jan Nieuwenhuizen <janneke@gnu.org>
4 ;;; Copyright © 2018, 2019 Clément Lassieur <clement@lassieur.org>
5 ;;;
6 ;;; This file is part of GNU Guix.
7 ;;;
8 ;;; GNU Guix is free software; you can redistribute it and/or modify it
9 ;;; under the terms of the GNU General Public License as published by
10 ;;; the Free Software Foundation; either version 3 of the License, or (at
11 ;;; your option) any later version.
12 ;;;
13 ;;; GNU Guix is distributed in the hope that it will be useful, but
14 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
15 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 ;;; GNU General Public License for more details.
17 ;;;
18 ;;; You should have received a copy of the GNU General Public License
19 ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
20
21 (define-module (gnu ci)
22 #:use-module (guix config)
23 #:use-module (guix store)
24 #:use-module (guix grafts)
25 #:use-module (guix profiles)
26 #:use-module (guix packages)
27 #:use-module (guix channels)
28 #:use-module (guix derivations)
29 #:use-module (guix build-system)
30 #:use-module (guix monads)
31 #:use-module (guix gexp)
32 #:use-module (guix ui)
33 #:use-module ((guix licenses)
34 #:select (gpl3+ license? license-name))
35 #:use-module ((guix utils) #:select (%current-system))
36 #:use-module ((guix scripts system) #:select (read-operating-system))
37 #:use-module ((guix scripts pack)
38 #:select (lookup-compressor self-contained-tarball))
39 #:use-module (gnu bootloader)
40 #:use-module (gnu bootloader u-boot)
41 #:use-module (gnu packages)
42 #:use-module (gnu packages gcc)
43 #:use-module (gnu packages base)
44 #:use-module (gnu packages gawk)
45 #:use-module (gnu packages guile)
46 #:use-module (gnu packages gettext)
47 #:use-module (gnu packages compression)
48 #:use-module (gnu packages multiprecision)
49 #:use-module (gnu packages make-bootstrap)
50 #:use-module (gnu packages package-management)
51 #:use-module (gnu system)
52 #:use-module (gnu system vm)
53 #:use-module (gnu system install)
54 #:use-module (gnu tests)
55 #:use-module (srfi srfi-1)
56 #:use-module (srfi srfi-26)
57 #:use-module (ice-9 match)
58 #:export (%cross-targets
59 channel-source->package
60 hydra-jobs))
61
62 ;;; Commentary:
63 ;;;
64 ;;; This file defines build jobs for the Hydra and Cuirass continuation
65 ;;; integration tools.
66 ;;;
67 ;;; Code:
68
69 (define* (package->alist store package system
70 #:optional (package-derivation package-derivation))
71 "Convert PACKAGE to an alist suitable for Hydra."
72 (parameterize ((%graft? #f))
73 `((derivation . ,(derivation-file-name
74 (package-derivation store package system
75 #:graft? #f)))
76 (description . ,(package-synopsis package))
77 (long-description . ,(package-description package))
78
79 ;; XXX: Hydra ignores licenses that are not a <license> structure or a
80 ;; list thereof.
81 (license . ,(let loop ((license (package-license package)))
82 (match license
83 ((? license?)
84 (license-name license))
85 ((lst ...)
86 (map loop license)))))
87
88 (home-page . ,(package-home-page package))
89 (maintainers . ("bug-guix@gnu.org"))
90 (max-silent-time . ,(or (assoc-ref (package-properties package)
91 'max-silent-time)
92 3600)) ;1 hour by default
93 (timeout . ,(or (assoc-ref (package-properties package) 'timeout)
94 72000))))) ;20 hours by default
95
96 (define (package-job store job-name package system)
97 "Return a job called JOB-NAME that builds PACKAGE on SYSTEM."
98 (let ((job-name (symbol-append job-name (string->symbol ".")
99 (string->symbol system))))
100 `(,job-name . ,(cut package->alist store package system))))
101
102 (define (package-cross-job store job-name package target system)
103 "Return a job called TARGET.JOB-NAME that cross-builds PACKAGE for TARGET on
104 SYSTEM."
105 `(,(symbol-append (string->symbol target) (string->symbol ".") job-name
106 (string->symbol ".") (string->symbol system)) .
107 ,(cute package->alist store package system
108 (lambda* (store package system #:key graft?)
109 (package-cross-derivation store package target system
110 #:graft? graft?)))))
111
112 (define %core-packages
113 ;; Note: Don't put the '-final' package variants because (1) that's
114 ;; implicit, and (2) they cannot be cross-built (due to the explicit input
115 ;; chain.)
116 (list gcc-4.8 gcc-4.9 gcc-5 glibc binutils
117 gmp mpfr mpc coreutils findutils diffutils patch sed grep
118 gawk gnu-gettext hello guile-2.0 guile-2.2 zlib gzip xz
119 %bootstrap-binaries-tarball
120 %binutils-bootstrap-tarball
121 (%glibc-bootstrap-tarball)
122 %gcc-bootstrap-tarball
123 %guile-bootstrap-tarball
124 %bootstrap-tarballs))
125
126 (define %packages-to-cross-build
127 %core-packages)
128
129 (define %cross-targets
130 '("mips64el-linux-gnu"
131 "arm-linux-gnueabihf"
132 "aarch64-linux-gnu"
133 "powerpc-linux-gnu"
134 "riscv64-linux-gnu"
135 "i586-pc-gnu" ;aka. GNU/Hurd
136 "i686-w64-mingw32"
137 "x86_64-w64-mingw32"))
138
139 (define (cross-jobs store system)
140 "Return a list of cross-compilation jobs for SYSTEM."
141 (define (from-32-to-64? target)
142 ;; Return true if SYSTEM is 32-bit and TARGET is 64-bit. This hack
143 ;; prevents known-to-fail cross-builds from i686-linux or armhf-linux to
144 ;; mips64el-linux-gnuabi64.
145 (and (or (string-prefix? "i686-" system)
146 (string-prefix? "i586-" system)
147 (string-prefix? "armhf-" system))
148 (string-contains target "64"))) ;x86_64, mips64el, aarch64, etc.
149
150 (define (same? target)
151 ;; Return true if SYSTEM and TARGET are the same thing. This is so we
152 ;; don't try to cross-compile to 'mips64el-linux-gnu' from
153 ;; 'mips64el-linux'.
154 (or (string-contains target system)
155 (and (string-prefix? "armhf" system) ;armhf-linux
156 (string-prefix? "arm" target)))) ;arm-linux-gnueabihf
157
158 (define (pointless? target)
159 ;; Return #t if it makes no sense to cross-build to TARGET from SYSTEM.
160 (match system
161 ((or "x86_64-linux" "i686-linux")
162 (if (string-contains target "mingw")
163 (not (string=? "x86_64-linux" system))
164 #f))
165 (_
166 ;; Don't try to cross-compile from non-Intel platforms: this isn't
167 ;; very useful and these are often brittle configurations.
168 #t)))
169
170 (define (either proc1 proc2 proc3)
171 (lambda (x)
172 (or (proc1 x) (proc2 x) (proc3 x))))
173
174 (append-map (lambda (target)
175 (map (lambda (package)
176 (package-cross-job store (job-name package)
177 package target system))
178 %packages-to-cross-build))
179 (remove (either from-32-to-64? same? pointless?)
180 %cross-targets)))
181
182 (define %guixsd-supported-systems
183 '("x86_64-linux" "i686-linux" "armhf-linux"))
184
185 (define %u-boot-systems
186 '("armhf-linux"))
187
188 (define (qemu-jobs store system)
189 "Return a list of jobs that build QEMU images for SYSTEM."
190 (define (->alist drv)
191 `((derivation . ,(derivation-file-name drv))
192 (description . "Stand-alone QEMU image of the GNU system")
193 (long-description . "This is a demo stand-alone QEMU image of the GNU
194 system.")
195 (license . ,(license-name gpl3+))
196 (max-silent-time . 600)
197 (timeout . 3600)
198 (home-page . ,%guix-home-page-url)
199 (maintainers . ("bug-guix@gnu.org"))))
200
201 (define (->job name drv)
202 (let ((name (symbol-append name (string->symbol ".")
203 (string->symbol system))))
204 `(,name . ,(lambda ()
205 (parameterize ((%graft? #f))
206 (->alist drv))))))
207
208 (define MiB
209 (expt 2 20))
210
211 (if (member system %guixsd-supported-systems)
212 (if (member system %u-boot-systems)
213 (list (->job 'flash-image
214 (run-with-store store
215 (mbegin %store-monad
216 (set-guile-for-build (default-guile))
217 (system-disk-image
218 (operating-system (inherit installation-os)
219 (bootloader (bootloader-configuration
220 (bootloader u-boot-bootloader)
221 (target #f))))
222 #:disk-image-size
223 (* 1500 MiB))))))
224 (list (->job 'usb-image
225 (run-with-store store
226 (mbegin %store-monad
227 (set-guile-for-build (default-guile))
228 (system-disk-image installation-os
229 #:disk-image-size
230 (* 1500 MiB)))))
231 (->job 'iso9660-image
232 (run-with-store store
233 (mbegin %store-monad
234 (set-guile-for-build (default-guile))
235 (system-disk-image installation-os
236 #:file-system-type
237 "iso9660"))))))
238 '()))
239
240 (define channel-build-system
241 ;; Build system used to "convert" a channel instance to a package.
242 (let* ((build (lambda* (store name inputs
243 #:key source commit system
244 #:allow-other-keys)
245 (run-with-store store
246 ;; SOURCE can be a lowerable object such as <local-file>
247 ;; or a file name. Adjust accordingly.
248 (mlet* %store-monad ((source (if (string? source)
249 (return source)
250 (lower-object source)))
251 (instance
252 -> (checkout->channel-instance
253 source #:commit commit)))
254 (channel-instances->derivation (list instance)))
255 #:system system)))
256 (lower (lambda* (name #:key system source commit
257 #:allow-other-keys)
258 (bag
259 (name name)
260 (system system)
261 (build build)
262 (arguments `(#:source ,source
263 #:commit ,commit))))))
264 (build-system (name 'channel)
265 (description "Turn a channel instance into a package.")
266 (lower lower))))
267
268 (define* (channel-source->package source #:key commit)
269 "Return a package for the given channel SOURCE, a lowerable object."
270 (package
271 (inherit guix)
272 (version (string-append (package-version guix) "+"))
273 (build-system channel-build-system)
274 (arguments `(#:source ,source
275 #:commit ,commit))
276 (inputs '())
277 (native-inputs '())
278 (propagated-inputs '())))
279
280 (define* (system-test-jobs store system
281 #:key source commit)
282 "Return a list of jobs for the system tests."
283 (define (test->thunk test)
284 (lambda ()
285 (define drv
286 (run-with-store store
287 (mbegin %store-monad
288 (set-current-system system)
289 (set-grafting #f)
290 (set-guile-for-build (default-guile))
291 (system-test-value test))))
292
293 `((derivation . ,(derivation-file-name drv))
294 (description . ,(format #f "Guix '~a' system test"
295 (system-test-name test)))
296 (long-description . ,(system-test-description test))
297 (license . ,(license-name gpl3+))
298 (max-silent-time . 600)
299 (timeout . 3600)
300 (home-page . ,%guix-home-page-url)
301 (maintainers . ("bug-guix@gnu.org")))))
302
303 (define (->job test)
304 (let ((name (string->symbol
305 (string-append "test." (system-test-name test)
306 "." system))))
307 (cons name (test->thunk test))))
308
309 (if (and (member system %guixsd-supported-systems)
310
311 ;; XXX: Our build farm has too few ARMv7 machines and they are very
312 ;; slow, so skip system tests there.
313 (not (string=? system "armhf-linux")))
314 ;; Override the value of 'current-guix' used by system tests. Using a
315 ;; channel instance makes tests that rely on 'current-guix' less
316 ;; expensive. It also makes sure we get a valid Guix package when this
317 ;; code is not running from a checkout.
318 (parameterize ((current-guix-package
319 (channel-source->package source #:commit commit)))
320 (map ->job (all-system-tests)))
321 '()))
322
323 (define (tarball-jobs store system)
324 "Return Hydra jobs to build the self-contained Guix binary tarball."
325 (define (->alist drv)
326 `((derivation . ,(derivation-file-name drv))
327 (description . "Stand-alone binary Guix tarball")
328 (long-description . "This is a tarball containing binaries of Guix and
329 all its dependencies, and ready to be installed on \"foreign\" distributions.")
330 (license . ,(license-name gpl3+))
331 (home-page . ,%guix-home-page-url)
332 (maintainers . ("bug-guix@gnu.org"))))
333
334 (define (->job name drv)
335 (let ((name (symbol-append name (string->symbol ".")
336 (string->symbol system))))
337 `(,name . ,(lambda ()
338 (parameterize ((%graft? #f))
339 (->alist drv))))))
340
341 ;; XXX: Add a job for the stable Guix?
342 (list (->job 'binary-tarball
343 (run-with-store store
344 (mbegin %store-monad
345 (set-guile-for-build (default-guile))
346 (>>= (profile-derivation (packages->manifest (list guix)))
347 (lambda (profile)
348 (self-contained-tarball "guix-binary" profile
349 #:localstatedir? #t
350 #:compressor
351 (lookup-compressor "xz")))))
352 #:system system))))
353
354 (define job-name
355 ;; Return the name of a package's job.
356 (compose string->symbol
357 (cut package-full-name <> "-")))
358
359 (define package->job
360 (let ((base-packages
361 (delete-duplicates
362 (append-map (match-lambda
363 ((_ package _ ...)
364 (match (package-transitive-inputs package)
365 (((_ inputs _ ...) ...)
366 inputs))))
367 (%final-inputs)))))
368 (lambda (store package system)
369 "Return a job for PACKAGE on SYSTEM, or #f if this combination is not
370 valid."
371 (cond ((member package base-packages)
372 (package-job store (symbol-append 'base. (job-name package))
373 package system))
374 ((supported-package? package system)
375 (let ((drv (package-derivation store package system
376 #:graft? #f)))
377 (and (substitutable-derivation? drv)
378 (package-job store (job-name package)
379 package system))))
380 (else
381 #f)))))
382
383 (define (all-packages)
384 "Return the list of packages to build."
385 (define (adjust package result)
386 (cond ((package-replacement package)
387 ;; XXX: If PACKAGE and its replacement have the same name/version,
388 ;; then both Cuirass jobs will have the same name, which
389 ;; effectively means that the second one will be ignored. Thus,
390 ;; return the replacement first.
391 (cons* (package-replacement package) ;build both
392 package
393 result))
394 ((package-superseded package)
395 result) ;don't build it
396 (else
397 (cons package result))))
398
399 (fold-packages adjust
400 (fold adjust '() ;include base packages
401 (match (%final-inputs)
402 (((labels packages _ ...) ...)
403 packages)))
404 #:select? (const #t))) ;include hidden packages
405
406 (define (arguments->manifests arguments)
407 "Return the list of manifests extracted from ARGUMENTS."
408 (map (match-lambda
409 ((input-name . relative-path)
410 (let* ((checkout (assq-ref arguments (string->symbol input-name)))
411 (base (assq-ref checkout 'file-name)))
412 (in-vicinity base relative-path))))
413 (assq-ref arguments 'manifests)))
414
415 (define (manifests->packages store manifests)
416 "Return the list of packages found in MANIFESTS."
417 (define (load-manifest manifest)
418 (save-module-excursion
419 (lambda ()
420 (set-current-module (make-user-module '((guix profiles) (gnu))))
421 (primitive-load manifest))))
422
423 (delete-duplicates!
424 (map manifest-entry-item
425 (append-map (compose manifest-entries
426 load-manifest)
427 manifests))))
428
429 (define (find-current-checkout arguments)
430 "Find the first checkout of ARGUMENTS that provided the current file.
431 Return #f if no such checkout is found."
432 (let ((current-root
433 (canonicalize-path
434 (string-append (dirname (current-filename)) "/.."))))
435 (find (lambda (argument)
436 (and=> (assq-ref argument 'file-name)
437 (lambda (name)
438 (string=? name current-root)))) arguments)))
439
440 \f
441 ;;;
442 ;;; Hydra entry point.
443 ;;;
444
445 (define (hydra-jobs store arguments)
446 "Return Hydra jobs."
447 (define subset
448 (match (assoc-ref arguments 'subset)
449 ("core" 'core) ; only build core packages
450 ("hello" 'hello) ; only build hello
451 (((? string?) (? string?) ...) 'list) ; only build selected list of packages
452 ("manifests" 'manifests) ; only build packages in the list of manifests
453 (_ 'all))) ; build everything
454
455 (define systems
456 (match (assoc-ref arguments 'systems)
457 (#f %hydra-supported-systems)
458 ((lst ...) lst)
459 ((? string? str) (call-with-input-string str read))))
460
461 (define checkout
462 (or (find-current-checkout arguments)
463 (assq-ref arguments 'superior-guix-checkout)))
464
465 (define commit
466 (assq-ref checkout 'revision))
467
468 (define source
469 (assq-ref checkout 'file-name))
470
471 ;; Turn off grafts. Grafting is meant to happen on the user's machines.
472 (parameterize ((%graft? #f))
473 ;; Return one job for each package, except bootstrap packages.
474 (append-map (lambda (system)
475 (format (current-error-port)
476 "evaluating for '~a' (heap size: ~a MiB)...~%"
477 system
478 (round
479 (/ (assoc-ref (gc-stats) 'heap-size)
480 (expt 2. 20))))
481 (invalidate-derivation-caches!)
482 (case subset
483 ((all)
484 ;; Build everything, including replacements.
485 (let ((all (all-packages))
486 (job (lambda (package)
487 (package->job store package
488 system))))
489 (append (filter-map job all)
490 (qemu-jobs store system)
491 (system-test-jobs store system
492 #:source source
493 #:commit commit)
494 (tarball-jobs store system)
495 (cross-jobs store system))))
496 ((core)
497 ;; Build core packages only.
498 (append (map (lambda (package)
499 (package-job store (job-name package)
500 package system))
501 %core-packages)
502 (cross-jobs store system)))
503 ((hello)
504 ;; Build hello package only.
505 (if (string=? system (%current-system))
506 (let ((hello (specification->package "hello")))
507 (list (package-job store (job-name hello) hello system)))
508 '()))
509 ((list)
510 ;; Build selected list of packages only.
511 (if (string=? system (%current-system))
512 (let* ((names (assoc-ref arguments 'subset))
513 (packages (map specification->package names)))
514 (map (lambda (package)
515 (package-job store (job-name package)
516 package system))
517 packages))
518 '()))
519 ((manifests)
520 ;; Build packages in the list of manifests.
521 (let* ((manifests (arguments->manifests arguments))
522 (packages (manifests->packages store manifests)))
523 (map (lambda (package)
524 (package-job store (job-name package)
525 package system))
526 packages)))
527 (else
528 (error "unknown subset" subset))))
529 systems)))