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