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