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