ci: Use a valid 'current-guix'.
[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 #:allow-other-keys)
197 (run-with-store store
198 (channel-instances->derivation (list instance)))))
199 (lower (lambda* (name #:key system instance #:allow-other-keys)
200 (bag
201 (name name)
202 (system system)
203 (build build)
204 (arguments `(#:instance ,instance))))))
205 (build-system (name 'channel)
206 (description "Turn a channel instance into a package.")
207 (lower lower))))
208
209 (define (channel-instance->package instance)
210 "Return a package for the given channel INSTANCE."
211 (package
212 (inherit guix)
213 (version (or (string-take (channel-instance-commit instance) 7)
214 (string-append (package-version guix) "+")))
215 (build-system channel-build-system)
216 (arguments `(#:instance ,instance))
217 (inputs '())
218 (native-inputs '())
219 (propagated-inputs '())))
220
221 (define* (system-test-jobs store system
222 #:key source commit)
223 "Return a list of jobs for the system tests."
224 (define instance
225 (checkout->channel-instance source #:commit commit))
226
227 (define (test->thunk test)
228 (lambda ()
229 (define drv
230 (run-with-store store
231 (mbegin %store-monad
232 (set-current-system system)
233 (set-grafting #f)
234 (set-guile-for-build (default-guile))
235 (system-test-value test))))
236
237 `((derivation . ,(derivation-file-name drv))
238 (description . ,(format #f "GuixSD '~a' system test"
239 (system-test-name test)))
240 (long-description . ,(system-test-description test))
241 (license . ,(license-name gpl3+))
242 (max-silent-time . 600)
243 (timeout . 3600)
244 (home-page . ,%guix-home-page-url)
245 (maintainers . ("bug-guix@gnu.org")))))
246
247 (define (->job test)
248 (let ((name (string->symbol
249 (string-append "test." (system-test-name test)
250 "." system))))
251 (cons name (test->thunk test))))
252
253 (if (member system %guixsd-supported-systems)
254 ;; Override the value of 'current-guix' used by system tests. Using a
255 ;; channel instance makes tests that rely on 'current-guix' less
256 ;; expensive. It also makes sure we get a valid Guix package when this
257 ;; code is not running from a checkout.
258 (parameterize ((current-guix-package
259 (channel-instance->package instance)))
260 (map ->job (all-system-tests)))
261 '()))
262
263 (define (tarball-jobs store system)
264 "Return Hydra jobs to build the self-contained Guix binary tarball."
265 (define (->alist drv)
266 `((derivation . ,(derivation-file-name drv))
267 (description . "Stand-alone binary Guix tarball")
268 (long-description . "This is a tarball containing binaries of Guix and
269 all its dependencies, and ready to be installed on non-GuixSD distributions.")
270 (license . ,(license-name gpl3+))
271 (home-page . ,%guix-home-page-url)
272 (maintainers . ("bug-guix@gnu.org"))))
273
274 (define (->job name drv)
275 (let ((name (symbol-append name (string->symbol ".")
276 (string->symbol system))))
277 `(,name . ,(lambda ()
278 (parameterize ((%graft? #f))
279 (->alist drv))))))
280
281 ;; XXX: Add a job for the stable Guix?
282 (list (->job 'binary-tarball
283 (run-with-store store
284 (mbegin %store-monad
285 (set-guile-for-build (default-guile))
286 (>>= (profile-derivation (packages->manifest (list guix)))
287 (lambda (profile)
288 (self-contained-tarball "guix-binary" profile
289 #:localstatedir? #t
290 #:compressor
291 (lookup-compressor "xz")))))
292 #:system system))))
293
294 (define job-name
295 ;; Return the name of a package's job.
296 (compose string->symbol
297 (cut package-full-name <> "-")))
298
299 (define package->job
300 (let ((base-packages
301 (delete-duplicates
302 (append-map (match-lambda
303 ((_ package _ ...)
304 (match (package-transitive-inputs package)
305 (((_ inputs _ ...) ...)
306 inputs))))
307 (%final-inputs)))))
308 (lambda (store package system)
309 "Return a job for PACKAGE on SYSTEM, or #f if this combination is not
310 valid."
311 (cond ((member package base-packages)
312 (package-job store (symbol-append 'base. (job-name package))
313 package system))
314 ((supported-package? package system)
315 (let ((drv (package-derivation store package system
316 #:graft? #f)))
317 (and (substitutable-derivation? drv)
318 (package-job store (job-name package)
319 package system))))
320 (else
321 #f)))))
322
323 (define (all-packages)
324 "Return the list of packages to build."
325 (define (adjust package result)
326 (cond ((package-replacement package)
327 (cons* package ;build both
328 (package-replacement package)
329 result))
330 ((package-superseded package)
331 result) ;don't build it
332 (else
333 (cons package result))))
334
335 (fold-packages adjust
336 (fold adjust '() ;include base packages
337 (match (%final-inputs)
338 (((labels packages _ ...) ...)
339 packages)))
340 #:select? (const #t))) ;include hidden packages
341
342 (define (arguments->manifests arguments)
343 "Return the list of manifests extracted from ARGUMENTS."
344 (map (match-lambda
345 ((input-name . relative-path)
346 (let* ((checkout (assq-ref arguments (string->symbol input-name)))
347 (base (assq-ref checkout 'file-name)))
348 (in-vicinity base relative-path))))
349 (assq-ref arguments 'manifests)))
350
351 (define (manifests->packages store manifests)
352 "Return the list of packages found in MANIFESTS."
353 (define (load-manifest manifest)
354 (save-module-excursion
355 (lambda ()
356 (set-current-module (make-user-module '((guix profiles) (gnu))))
357 (primitive-load manifest))))
358
359 (delete-duplicates!
360 (map manifest-entry-item
361 (append-map (compose manifest-entries
362 load-manifest)
363 manifests))))
364
365 \f
366 ;;;
367 ;;; Hydra entry point.
368 ;;;
369
370 (define (hydra-jobs store arguments)
371 "Return Hydra jobs."
372 (define subset
373 (match (assoc-ref arguments 'subset)
374 ("core" 'core) ; only build core packages
375 ("hello" 'hello) ; only build hello
376 (((? string?) (? string?) ...) 'list) ; only build selected list of packages
377 ("manifests" 'manifests) ; only build packages in the list of manifests
378 (_ 'all))) ; build everything
379
380 (define systems
381 (match (assoc-ref arguments 'systems)
382 (#f %hydra-supported-systems)
383 ((lst ...) lst)
384 ((? string? str) (call-with-input-string str read))))
385
386 (define checkout
387 ;; Extract metadata about the 'guix' checkout. Its key in ARGUMENTS may
388 ;; vary, so pick up the first one that's neither 'subset' nor 'systems'.
389 (any (match-lambda
390 ((key . value)
391 (and (not (memq key '(systems subset)))
392 value)))
393 arguments))
394
395 (define commit
396 (assq-ref checkout 'revision))
397
398 (define source
399 (assq-ref checkout 'file-name))
400
401 (define (cross-jobs system)
402 (define (from-32-to-64? target)
403 ;; Return true if SYSTEM is 32-bit and TARGET is 64-bit. This hack
404 ;; prevents known-to-fail cross-builds from i686-linux or armhf-linux to
405 ;; mips64el-linux-gnuabi64.
406 (and (or (string-prefix? "i686-" system)
407 (string-prefix? "i586-" system)
408 (string-prefix? "armhf-" system))
409 (string-contains target "64"))) ;x86_64, mips64el, aarch64, etc.
410
411 (define (same? target)
412 ;; Return true if SYSTEM and TARGET are the same thing. This is so we
413 ;; don't try to cross-compile to 'mips64el-linux-gnu' from
414 ;; 'mips64el-linux'.
415 (or (string-contains target system)
416 (and (string-prefix? "armhf" system) ;armhf-linux
417 (string-prefix? "arm" target)))) ;arm-linux-gnueabihf
418
419 (define (pointless? target)
420 ;; Return #t if it makes no sense to cross-build to TARGET from SYSTEM.
421 (match system
422 ((or "x86_64-linux" "i686-linux")
423 (if (string-contains target "mingw")
424 (not (string=? "x86_64-linux" system))
425 #f))
426 (_
427 ;; Don't try to cross-compile from non-Intel platforms: this isn't
428 ;; very useful and these are often brittle configurations.
429 #t)))
430
431 (define (either proc1 proc2 proc3)
432 (lambda (x)
433 (or (proc1 x) (proc2 x) (proc3 x))))
434
435 (append-map (lambda (target)
436 (map (lambda (package)
437 (package-cross-job store (job-name package)
438 package target system))
439 %packages-to-cross-build))
440 (remove (either from-32-to-64? same? pointless?)
441 %cross-targets)))
442
443 ;; Turn off grafts. Grafting is meant to happen on the user's machines.
444 (parameterize ((%graft? #f))
445 ;; Return one job for each package, except bootstrap packages.
446 (append-map (lambda (system)
447 (format (current-error-port)
448 "evaluating for '~a' (heap size: ~a MiB)...~%"
449 system
450 (round
451 (/ (assoc-ref (gc-stats) 'heap-size)
452 (expt 2. 20))))
453 (invalidate-derivation-caches!)
454 (case subset
455 ((all)
456 ;; Build everything, including replacements.
457 (let ((all (all-packages))
458 (job (lambda (package)
459 (package->job store package
460 system))))
461 (append (filter-map job all)
462 (qemu-jobs store system)
463 (system-test-jobs store system
464 #:source source
465 #:commit commit)
466 (tarball-jobs store system)
467 (cross-jobs system))))
468 ((core)
469 ;; Build core packages only.
470 (append (map (lambda (package)
471 (package-job store (job-name package)
472 package system))
473 %core-packages)
474 (cross-jobs system)))
475 ((hello)
476 ;; Build hello package only.
477 (if (string=? system (%current-system))
478 (let ((hello (specification->package "hello")))
479 (list (package-job store (job-name hello) hello system)))
480 '()))
481 ((list)
482 ;; Build selected list of packages only.
483 (if (string=? system (%current-system))
484 (let* ((names (assoc-ref arguments 'subset))
485 (packages (map specification->package names)))
486 (map (lambda (package)
487 (package-job store (job-name package)
488 package system))
489 packages))
490 '()))
491 ((manifests)
492 ;; Build packages in the list of manifests.
493 (let* ((manifests (arguments->manifests arguments))
494 (packages (manifests->packages store manifests)))
495 (map (lambda (package)
496 (package-job store (job-name package)
497 package system))
498 packages)))
499 (else
500 (error "unknown subset" subset))))
501 systems)))