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