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