evaluate.scm 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2016, 2017, 2018 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. (guix git-download)
  24. ((guix build utils) #:select (with-directory-excursion))
  25. (srfi srfi-19)
  26. (ice-9 match)
  27. (ice-9 pretty-print)
  28. (ice-9 format))
  29. (define %top-srcdir
  30. (and=> (assq-ref (current-source-location) 'filename)
  31. (lambda (file)
  32. (canonicalize-path
  33. (string-append (dirname file) "/../..")))))
  34. (define %user-module
  35. ;; Hydra user module.
  36. (let ((m (make-module)))
  37. (beautify-user-module! m)
  38. m))
  39. (cond-expand
  40. (guile-2.2
  41. ;; Guile 2.2.2 has a bug whereby 'time-monotonic' objects have seconds and
  42. ;; nanoseconds swapped (fixed in Guile commit 886ac3e). Work around it.
  43. (define time-monotonic time-tai))
  44. (else #t))
  45. (define (call-with-time thunk kont)
  46. "Call THUNK and pass KONT the elapsed time followed by THUNK's return
  47. values."
  48. (let* ((start (current-time time-monotonic))
  49. (result (call-with-values thunk list))
  50. (end (current-time time-monotonic)))
  51. (apply kont (time-difference end start) result)))
  52. (define (call-with-time-display thunk)
  53. "Call THUNK and write to the current output port its duration."
  54. (call-with-time thunk
  55. (lambda (time . results)
  56. (format #t "~,3f seconds~%"
  57. (+ (time-second time)
  58. (/ (time-nanosecond time) 1e9)))
  59. (apply values results))))
  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. ;; Without further ado...
  71. (match (command-line)
  72. ((command file cuirass? ...)
  73. ;; Load FILE, a Scheme file that defines Hydra jobs.
  74. (let ((port (current-output-port))
  75. (real-build-things build-things))
  76. (with-store store
  77. ;; Make sure we don't resort to substitutes.
  78. (set-build-options store
  79. #:use-substitutes? #f
  80. #:substitute-urls '())
  81. ;; Grafts can trigger early builds. We do not want that to happen
  82. ;; during evaluation, so use a sledgehammer to catch such problems.
  83. ;; An exception, though, is the evaluation of Guix itself, which
  84. ;; requires building a "trampoline" program.
  85. (set! build-things
  86. (lambda (store . args)
  87. (format (current-error-port)
  88. "warning: building things during evaluation~%")
  89. (format (current-error-port)
  90. "'build-things' arguments: ~s~%" args)
  91. (apply real-build-things store args)))
  92. ;; Add %TOP-SRCDIR to the store with a proper Git predicate so we work
  93. ;; from a clean checkout
  94. (let ((source (add-to-store store "guix-source" #t
  95. "sha256" %top-srcdir
  96. #:select? (git-predicate %top-srcdir))))
  97. (with-directory-excursion source
  98. (save-module-excursion
  99. (lambda ()
  100. (set-current-module %user-module)
  101. (format (current-error-port)
  102. "loading '~a' relative to '~a'...~%"
  103. file source)
  104. (primitive-load file))))
  105. ;; Call the entry point of FILE and print the resulting job sexp.
  106. (pretty-print
  107. (match ((module-ref %user-module
  108. (if (equal? cuirass? "cuirass")
  109. 'cuirass-jobs
  110. 'hydra-jobs))
  111. store `((guix
  112. . ((file-name . ,source)))))
  113. (((names . thunks) ...)
  114. (map (lambda (job thunk)
  115. (format (current-error-port) "evaluating '~a'... " job)
  116. (force-output (current-error-port))
  117. (cons job
  118. (assert-valid-job job
  119. (call-with-time-display thunk))))
  120. names thunks)))
  121. port)))))
  122. ((command _ ...)
  123. (format (current-error-port) "Usage: ~a FILE [cuirass]
  124. Evaluate the Hydra or Cuirass jobs defined in FILE.~%"
  125. command)
  126. (exit 1)))
  127. ;;; Local Variables:
  128. ;;; eval: (put 'call-with-time 'scheme-indent-function 1)
  129. ;;; End: