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