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