Merge branch 'master' into core-updates
[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 target)
127 "Return the list of packages to cross-build for TARGET."
128 ;; Don't cross-build the bootstrap tarballs for MinGW.
129 (if (string-contains target "mingw")
130 (drop-right %core-packages 6)
131 %core-packages))
132
133 (define %cross-targets
134 '("mips64el-linux-gnu"
135 "mips64el-linux-gnuabi64"
136 "arm-linux-gnueabihf"
137 "aarch64-linux-gnu"
138 "powerpc-linux-gnu"
139 "riscv64-linux-gnu"
140 "i586-pc-gnu" ;aka. GNU/Hurd
141 "i686-w64-mingw32"
142 "x86_64-w64-mingw32"))
143
144 (define (cross-jobs store system)
145 "Return a list of cross-compilation jobs for SYSTEM."
146 (define (from-32-to-64? target)
147 ;; Return true if SYSTEM is 32-bit and TARGET is 64-bit. This hack
148 ;; prevents known-to-fail cross-builds from i686-linux or armhf-linux to
149 ;; mips64el-linux-gnuabi64.
150 (and (or (string-prefix? "i686-" system)
151 (string-prefix? "i586-" system)
152 (string-prefix? "armhf-" system))
153 (string-contains target "64"))) ;x86_64, mips64el, aarch64, etc.
154
155 (define (same? target)
156 ;; Return true if SYSTEM and TARGET are the same thing. This is so we
157 ;; don't try to cross-compile to 'mips64el-linux-gnu' from
158 ;; 'mips64el-linux'.
159 (or (string-contains target system)
160 (and (string-prefix? "armhf" system) ;armhf-linux
161 (string-prefix? "arm" target)))) ;arm-linux-gnueabihf
162
163 (define (pointless? target)
164 ;; Return #t if it makes no sense to cross-build to TARGET from SYSTEM.
165 (match system
166 ((or "x86_64-linux" "i686-linux")
167 (if (string-contains target "mingw")
168 (not (string=? "x86_64-linux" system))
169 #f))
170 (_
171 ;; Don't try to cross-compile from non-Intel platforms: this isn't
172 ;; very useful and these are often brittle configurations.
173 #t)))
174
175 (define (either proc1 proc2 proc3)
176 (lambda (x)
177 (or (proc1 x) (proc2 x) (proc3 x))))
178
179 (append-map (lambda (target)
180 (map (lambda (package)
181 (package-cross-job store (job-name package)
182 package target system))
183 (packages-to-cross-build target)))
184 (remove (either from-32-to-64? same? pointless?)
185 %cross-targets)))
186
187 (define %guixsd-supported-systems
188 '("x86_64-linux" "i686-linux" "armhf-linux"))
189
190 (define %u-boot-systems
191 '("armhf-linux"))
192
193 (define (qemu-jobs store system)
194 "Return a list of jobs that build QEMU images for SYSTEM."
195 (define (->alist drv)
196 `((derivation . ,(derivation-file-name drv))
197 (description . "Stand-alone QEMU image of the GNU system")
198 (long-description . "This is a demo stand-alone QEMU image of the GNU
199 system.")
200 (license . ,(license-name gpl3+))
201 (max-silent-time . 600)
202 (timeout . 3600)
203 (home-page . ,%guix-home-page-url)
204 (maintainers . ("bug-guix@gnu.org"))))
205
206 (define (->job name drv)
207 (let ((name (symbol-append name (string->symbol ".")
208 (string->symbol system))))
209 `(,name . ,(lambda ()
210 (parameterize ((%graft? #f))
211 (->alist drv))))))
212
213 (define MiB
214 (expt 2 20))
215
216 (if (member system %guixsd-supported-systems)
217 (if (member system %u-boot-systems)
218 (list (->job 'flash-image
219 (run-with-store store
220 (mbegin %store-monad
221 (set-guile-for-build (default-guile))
222 (system-disk-image
223 (operating-system (inherit installation-os)
224 (bootloader (bootloader-configuration
225 (bootloader u-boot-bootloader)
226 (target #f))))
227 #:disk-image-size
228 (* 1500 MiB))))))
229 (list (->job 'usb-image
230 (run-with-store store
231 (mbegin %store-monad
232 (set-guile-for-build (default-guile))
233 (system-disk-image installation-os
234 #:disk-image-size
235 (* 1500 MiB)))))
236 (->job 'iso9660-image
237 (run-with-store store
238 (mbegin %store-monad
239 (set-guile-for-build (default-guile))
240 (system-disk-image installation-os
241 #:file-system-type
242 "iso9660"))))))
243 '()))
244
245 (define channel-build-system
246 ;; Build system used to "convert" a channel instance to a package.
247 (let* ((build (lambda* (store name inputs
248 #:key source commit system
249 #:allow-other-keys)
250 (run-with-store store
251 ;; SOURCE can be a lowerable object such as <local-file>
252 ;; or a file name. Adjust accordingly.
253 (mlet* %store-monad ((source (if (string? source)
254 (return source)
255 (lower-object source)))
256 (instance
257 -> (checkout->channel-instance
258 source #:commit commit)))
259 (channel-instances->derivation (list instance)))
260 #:system system)))
261 (lower (lambda* (name #:key system source commit
262 #:allow-other-keys)
263 (bag
264 (name name)
265 (system system)
266 (build build)
267 (arguments `(#:source ,source
268 #:commit ,commit))))))
269 (build-system (name 'channel)
270 (description "Turn a channel instance into a package.")
271 (lower lower))))
272
273 (define* (channel-source->package source #:key commit)
274 "Return a package for the given channel SOURCE, a lowerable object."
275 (package
276 (inherit guix)
277 (version (string-append (package-version guix) "+"))
278 (build-system channel-build-system)
279 (arguments `(#:source ,source
280 #:commit ,commit))
281 (inputs '())
282 (native-inputs '())
283 (propagated-inputs '())))
284
285 (define* (system-test-jobs store system
286 #:key source commit)
287 "Return a list of jobs for the system tests."
288 (define (test->thunk test)
289 (lambda ()
290 (define drv
291 (run-with-store store
292 (mbegin %store-monad
293 (set-current-system system)
294 (set-grafting #f)
295 (set-guile-for-build (default-guile))
296 (system-test-value test))))
297
298 `((derivation . ,(derivation-file-name drv))
299 (description . ,(format #f "Guix '~a' system test"
300 (system-test-name test)))
301 (long-description . ,(system-test-description test))
302 (license . ,(license-name gpl3+))
303 (max-silent-time . 600)
304 (timeout . 3600)
305 (home-page . ,%guix-home-page-url)
306 (maintainers . ("bug-guix@gnu.org")))))
307
308 (define (->job test)
309 (let ((name (string->symbol
310 (string-append "test." (system-test-name test)
311 "." system))))
312 (cons name (test->thunk test))))
313
314 (if (and (member system %guixsd-supported-systems)
315
316 ;; XXX: Our build farm has too few ARMv7 machines and they are very
317 ;; slow, so skip system tests there.
318 (not (string=? system "armhf-linux")))
319 ;; Override the value of 'current-guix' used by system tests. Using a
320 ;; channel instance makes tests that rely on 'current-guix' less
321 ;; expensive. It also makes sure we get a valid Guix package when this
322 ;; code is not running from a checkout.
323 (parameterize ((current-guix-package
324 (channel-source->package source #:commit commit)))
325 (map ->job (all-system-tests)))
326 '()))
327
328 (define (tarball-jobs store system)
329 "Return Hydra jobs to build the self-contained Guix binary tarball."
330 (define (->alist drv)
331 `((derivation . ,(derivation-file-name drv))
332 (description . "Stand-alone binary Guix tarball")
333 (long-description . "This is a tarball containing binaries of Guix and
334 all its dependencies, and ready to be installed on \"foreign\" distributions.")
335 (license . ,(license-name gpl3+))
336 (home-page . ,%guix-home-page-url)
337 (maintainers . ("bug-guix@gnu.org"))))
338
339 (define (->job name drv)
340 (let ((name (symbol-append name (string->symbol ".")
341 (string->symbol system))))
342 `(,name . ,(lambda ()
343 (parameterize ((%graft? #f))
344 (->alist drv))))))
345
346 ;; XXX: Add a job for the stable Guix?
347 (list (->job 'binary-tarball
348 (run-with-store store
349 (mbegin %store-monad
350 (set-guile-for-build (default-guile))
351 (>>= (profile-derivation (packages->manifest (list guix)))
352 (lambda (profile)
353 (self-contained-tarball "guix-binary" profile
354 #:localstatedir? #t
355 #:compressor
356 (lookup-compressor "xz")))))
357 #:system system))))
358
359 (define job-name
360 ;; Return the name of a package's job.
361 (compose string->symbol
362 (cut package-full-name <> "-")))
363
364 (define package->job
365 (let ((base-packages
366 (delete-duplicates
367 (append-map (match-lambda
368 ((_ package _ ...)
369 (match (package-transitive-inputs package)
370 (((_ inputs _ ...) ...)
371 inputs))))
372 (%final-inputs)))))
373 (lambda (store package system)
374 "Return a job for PACKAGE on SYSTEM, or #f if this combination is not
375 valid."
376 (cond ((member package base-packages)
377 (package-job store (symbol-append 'base. (job-name package))
378 package system))
379 ((supported-package? package system)
380 (let ((drv (package-derivation store package system
381 #:graft? #f)))
382 (and (substitutable-derivation? drv)
383 (package-job store (job-name package)
384 package system))))
385 (else
386 #f)))))
387
388 (define (all-packages)
389 "Return the list of packages to build."
390 (define (adjust package result)
391 (cond ((package-replacement package)
392 ;; XXX: If PACKAGE and its replacement have the same name/version,
393 ;; then both Cuirass jobs will have the same name, which
394 ;; effectively means that the second one will be ignored. Thus,
395 ;; return the replacement first.
396 (cons* (package-replacement package) ;build both
397 package
398 result))
399 ((package-superseded package)
400 result) ;don't build it
401 (else
402 (cons package result))))
403
404 (fold-packages adjust
405 (fold adjust '() ;include base packages
406 (match (%final-inputs)
407 (((labels packages _ ...) ...)
408 packages)))
409 #:select? (const #t))) ;include hidden packages
410
411 (define (arguments->manifests arguments)
412 "Return the list of manifests extracted from ARGUMENTS."
413 (map (match-lambda
414 ((input-name . relative-path)
415 (let* ((checkout (assq-ref arguments (string->symbol input-name)))
416 (base (assq-ref checkout 'file-name)))
417 (in-vicinity base relative-path))))
418 (assq-ref arguments 'manifests)))
419
420 (define (manifests->packages store manifests)
421 "Return the list of packages found in MANIFESTS."
422 (define (load-manifest manifest)
423 (save-module-excursion
424 (lambda ()
425 (set-current-module (make-user-module '((guix profiles) (gnu))))
426 (primitive-load manifest))))
427
428 (delete-duplicates!
429 (map manifest-entry-item
430 (append-map (compose manifest-entries
431 load-manifest)
432 manifests))))
433
434 (define (find-current-checkout arguments)
435 "Find the first checkout of ARGUMENTS that provided the current file.
436 Return #f if no such checkout is found."
437 (let ((current-root
438 (canonicalize-path
439 (string-append (dirname (current-filename)) "/.."))))
440 (find (lambda (argument)
441 (and=> (assq-ref argument 'file-name)
442 (lambda (name)
443 (string=? name current-root)))) arguments)))
444
445 \f
446 ;;;
447 ;;; Hydra entry point.
448 ;;;
449
450 (define (hydra-jobs store arguments)
451 "Return Hydra jobs."
452 (define subset
453 (match (assoc-ref arguments 'subset)
454 ("core" 'core) ; only build core packages
455 ("hello" 'hello) ; only build hello
456 (((? string?) (? string?) ...) 'list) ; only build selected list of packages
457 ("manifests" 'manifests) ; only build packages in the list of manifests
458 (_ 'all))) ; build everything
459
460 (define systems
461 (match (assoc-ref arguments 'systems)
462 (#f %hydra-supported-systems)
463 ((lst ...) lst)
464 ((? string? str) (call-with-input-string str read))))
465
466 (define checkout
467 (or (find-current-checkout arguments)
468 (assq-ref arguments 'superior-guix-checkout)))
469
470 (define commit
471 (assq-ref checkout 'revision))
472
473 (define source
474 (assq-ref checkout 'file-name))
475
476 ;; Turn off grafts. Grafting is meant to happen on the user's machines.
477 (parameterize ((%graft? #f))
478 ;; Return one job for each package, except bootstrap packages.
479 (append-map (lambda (system)
480 (format (current-error-port)
481 "evaluating for '~a' (heap size: ~a MiB)...~%"
482 system
483 (round
484 (/ (assoc-ref (gc-stats) 'heap-size)
485 (expt 2. 20))))
486 (invalidate-derivation-caches!)
487 (case subset
488 ((all)
489 ;; Build everything, including replacements.
490 (let ((all (all-packages))
491 (job (lambda (package)
492 (package->job store package
493 system))))
494 (append (filter-map job all)
495 (qemu-jobs store system)
496 (system-test-jobs store system
497 #:source source
498 #:commit commit)
499 (tarball-jobs store system)
500 (cross-jobs store system))))
501 ((core)
502 ;; Build core packages only.
503 (append (map (lambda (package)
504 (package-job store (job-name package)
505 package system))
506 %core-packages)
507 (cross-jobs store system)))
508 ((hello)
509 ;; Build hello package only.
510 (if (string=? system (%current-system))
511 (let ((hello (specification->package "hello")))
512 (list (package-job store (job-name hello) hello system)))
513 '()))
514 ((list)
515 ;; Build selected list of packages only.
516 (if (string=? system (%current-system))
517 (let* ((names (assoc-ref arguments 'subset))
518 (packages (map specification->package names)))
519 (map (lambda (package)
520 (package-job store (job-name package)
521 package system))
522 packages))
523 '()))
524 ((manifests)
525 ;; Build packages in the list of manifests.
526 (let* ((manifests (arguments->manifests arguments))
527 (packages (manifests->packages store manifests)))
528 (map (lambda (package)
529 (package-job store (job-name package)
530 package system))
531 packages)))
532 (else
533 (error "unknown subset" subset))))
534 systems)))