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