Merge remote-tracking branch 'origin/master' into core-updates
[jackhill/guix/guix.git] / build-aux / hydra / evaluate.scm
1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2016, 2017 Ludovic Courtès <ludo@gnu.org>
3 ;;; Copyright © 2017 Jan Nieuwenhuizen <janneke@gnu.org>
4 ;;;
5 ;;; This file is part of GNU Guix.
6 ;;;
7 ;;; GNU Guix is free software; you can redistribute it and/or modify it
8 ;;; under the terms of the GNU General Public License as published by
9 ;;; the Free Software Foundation; either version 3 of the License, or (at
10 ;;; your option) any later version.
11 ;;;
12 ;;; GNU Guix is distributed in the hope that it will be useful, but
13 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
14 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 ;;; GNU General Public License for more details.
16 ;;;
17 ;;; You should have received a copy of the GNU General Public License
18 ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
19
20 ;;; This program replicates the behavior of Hydra's 'hydra-eval-guile-job'.
21 ;;; It evaluates the Hydra job defined by the program passed as its first
22 ;;; arguments and outputs an sexp of the jobs on standard output.
23
24 (use-modules (guix store)
25 (srfi srfi-19)
26 (ice-9 match)
27 (ice-9 pretty-print)
28 (ice-9 format))
29
30 (define %user-module
31 ;; Hydra user module.
32 (let ((m (make-module)))
33 (beautify-user-module! m)
34 m))
35
36 (cond-expand
37 (guile-2.2
38 ;; Guile 2.2.2 has a bug whereby 'time-monotonic' objects have seconds and
39 ;; nanoseconds swapped (fixed in Guile commit 886ac3e). Work around it.
40 (define time-monotonic time-tai))
41 (else #t))
42
43 (define (call-with-time thunk kont)
44 "Call THUNK and pass KONT the elapsed time followed by THUNK's return
45 values."
46 (let* ((start (current-time time-monotonic))
47 (result (call-with-values thunk list))
48 (end (current-time time-monotonic)))
49 (apply kont (time-difference end start) result)))
50
51 (define (call-with-time-display thunk)
52 "Call THUNK and write to the current output port its duration."
53 (call-with-time thunk
54 (lambda (time . results)
55 (format #t "~,3f seconds~%"
56 (+ (time-second time)
57 (/ (time-nanosecond time) 1e9)))
58 (apply values results))))
59
60 (define (assert-valid-job job thing)
61 "Raise an error if THING is not an alist with a valid 'derivation' entry.
62 Otherwise return THING."
63 (unless (and (list? thing)
64 (and=> (assoc-ref thing 'derivation)
65 (lambda (value)
66 (and (string? value)
67 (string-suffix? ".drv" value)))))
68 (error "job did not produce a valid alist" job thing))
69 thing)
70
71 \f
72 ;; Without further ado...
73 (match (command-line)
74 ((command file cuirass? ...)
75 ;; Load FILE, a Scheme file that defines Hydra jobs.
76 (let ((port (current-output-port)))
77 (save-module-excursion
78 (lambda ()
79 (set-current-module %user-module)
80 (primitive-load file)))
81
82 (with-store store
83 ;; Make sure we don't resort to substitutes.
84 (set-build-options store
85 #:use-substitutes? #f
86 #:substitute-urls '())
87
88 ;; Grafts can trigger early builds. We do not want that to happen
89 ;; during evaluation, so use a sledgehammer to catch such problems.
90 (set! build-things
91 (lambda (store . args)
92 (format (current-error-port)
93 "error: trying to build things during evaluation!~%")
94 (format (current-error-port)
95 "'build-things' arguments: ~s~%" args)
96 (exit 1)))
97
98 ;; Call the entry point of FILE and print the resulting job sexp.
99 (pretty-print
100 (match ((module-ref %user-module
101 (if (equal? cuirass? "cuirass")
102 'cuirass-jobs
103 'hydra-jobs))
104 store '())
105 (((names . thunks) ...)
106 (map (lambda (job thunk)
107 (format (current-error-port) "evaluating '~a'... " job)
108 (force-output (current-error-port))
109 (cons job
110 (assert-valid-job job
111 (call-with-time-display thunk))))
112 names thunks)))
113 port))))
114 ((command _ ...)
115 (format (current-error-port) "Usage: ~a FILE [cuirass]
116 Evaluate the Hydra or Cuirass jobs defined in FILE.~%"
117 command)
118 (exit 1)))
119
120 ;;; Local Variables:
121 ;;; eval: (put 'call-with-time 'scheme-indent-function 1)
122 ;;; End:
123