evaluate.scm 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. ;;; This program replicates the behavior of Hydra's 'hydra-eval-guile-job'.
  20. ;;; It evaluates the Hydra job defined by the program passed as its first
  21. ;;; arguments and outputs an sexp of the jobs on standard output.
  22. (use-modules (guix store)
  23. (srfi srfi-19)
  24. (ice-9 match)
  25. (ice-9 pretty-print)
  26. (ice-9 format))
  27. (define %top-srcdir
  28. (and=> (assq-ref (current-source-location) 'filename)
  29. (lambda (file)
  30. (canonicalize-path
  31. (string-append (dirname file) "/../..")))))
  32. (define %user-module
  33. ;; Hydra user module.
  34. (let ((m (make-module)))
  35. (beautify-user-module! m)
  36. m))
  37. (cond-expand
  38. (guile-2.2
  39. ;; Guile 2.2.2 has a bug whereby 'time-monotonic' objects have seconds and
  40. ;; nanoseconds swapped (fixed in Guile commit 886ac3e). Work around it.
  41. (define time-monotonic time-tai))
  42. (else #t))
  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. (define (call-with-time-display thunk)
  51. "Call THUNK and write to the current output port its duration."
  52. (call-with-time thunk
  53. (lambda (time . results)
  54. (format #t "~,3f seconds~%"
  55. (+ (time-second time)
  56. (/ (time-nanosecond time) 1e9)))
  57. (apply values results))))
  58. (define (assert-valid-job job thing)
  59. "Raise an error if THING is not an alist with a valid 'derivation' entry.
  60. Otherwise return THING."
  61. (unless (and (list? thing)
  62. (and=> (assoc-ref thing 'derivation)
  63. (lambda (value)
  64. (and (string? value)
  65. (string-suffix? ".drv" value)))))
  66. (error "job did not produce a valid alist" job thing))
  67. thing)
  68. ;; Without further ado...
  69. (match (command-line)
  70. ((command file cuirass? ...)
  71. ;; Load FILE, a Scheme file that defines Hydra jobs.
  72. (let ((port (current-output-port)))
  73. (save-module-excursion
  74. (lambda ()
  75. (set-current-module %user-module)
  76. (primitive-load file)))
  77. (with-store store
  78. ;; Make sure we don't resort to substitutes.
  79. (set-build-options store
  80. #:use-substitutes? #f
  81. #:substitute-urls '())
  82. ;; Grafts can trigger early builds. We do not want that to happen
  83. ;; during evaluation, so use a sledgehammer to catch such problems.
  84. (set! build-things
  85. (lambda (store . args)
  86. (format (current-error-port)
  87. "error: trying to build things during evaluation!~%")
  88. (format (current-error-port)
  89. "'build-things' arguments: ~s~%" args)
  90. (exit 1)))
  91. ;; Call the entry point of FILE and print the resulting job sexp.
  92. (pretty-print
  93. (match ((module-ref %user-module
  94. (if (equal? cuirass? "cuirass")
  95. 'cuirass-jobs
  96. 'hydra-jobs))
  97. store `((guix
  98. . ((file-name . ,%top-srcdir)))))
  99. (((names . thunks) ...)
  100. (map (lambda (job thunk)
  101. (format (current-error-port) "evaluating '~a'... " job)
  102. (force-output (current-error-port))
  103. (cons job
  104. (assert-valid-job job
  105. (call-with-time-display thunk))))
  106. names thunks)))
  107. port))))
  108. ((command _ ...)
  109. (format (current-error-port) "Usage: ~a FILE [cuirass]
  110. Evaluate the Hydra or Cuirass jobs defined in FILE.~%"
  111. command)
  112. (exit 1)))
  113. ;;; Local Variables:
  114. ;;; eval: (put 'call-with-time 'scheme-indent-function 1)
  115. ;;; End: