evaluate.scm 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2016, 2017, 2018, 2020 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. ((guix ui) #:select (build-notifier))
  26. (srfi srfi-19)
  27. (ice-9 match)
  28. (ice-9 pretty-print)
  29. (ice-9 format))
  30. (define %top-srcdir
  31. (and=> (assq-ref (current-source-location) 'filename)
  32. (lambda (file)
  33. (canonicalize-path
  34. (string-append (dirname file) "/../..")))))
  35. (define %user-module
  36. ;; Hydra user module.
  37. (let ((m (make-module)))
  38. (beautify-user-module! m)
  39. m))
  40. (define (call-with-time thunk kont)
  41. "Call THUNK and pass KONT the elapsed time followed by THUNK's return
  42. values."
  43. (let* ((start (current-time time-monotonic))
  44. (result (call-with-values thunk list))
  45. (end (current-time time-monotonic)))
  46. (apply kont (time-difference end start) result)))
  47. (define (call-with-time-display thunk)
  48. "Call THUNK and write to the current output port its duration."
  49. (call-with-time thunk
  50. (lambda (time . results)
  51. (format #t "~,3f seconds~%"
  52. (+ (time-second time)
  53. (/ (time-nanosecond time) 1e9)))
  54. (apply values results))))
  55. (define (assert-valid-job job thing)
  56. "Raise an error if THING is not an alist with a valid 'derivation' entry.
  57. Otherwise return THING."
  58. (unless (and (list? thing)
  59. (and=> (assoc-ref thing 'derivation)
  60. (lambda (value)
  61. (and (string? value)
  62. (string-suffix? ".drv" value)))))
  63. (error "job did not produce a valid alist" job thing))
  64. thing)
  65. ;; Without further ado...
  66. (match (command-line)
  67. ((command file cuirass? ...)
  68. ;; Load FILE, a Scheme file that defines Hydra jobs.
  69. (let ((port (current-output-port))
  70. (real-build-things build-things))
  71. (with-store store
  72. ;; Make sure we don't resort to substitutes.
  73. (set-build-options store
  74. #:use-substitutes? #f
  75. #:substitute-urls '())
  76. ;; The evaluation of Guix itself requires building a "trampoline"
  77. ;; program, and possibly everything it depends on. Thus, allow builds
  78. ;; but print a notification.
  79. (with-build-handler (build-notifier #:use-substitutes? #f)
  80. ;; Add %TOP-SRCDIR to the store with a proper Git predicate so we work
  81. ;; from a clean checkout
  82. (let ((source (add-to-store store "guix-source" #t
  83. "sha256" %top-srcdir
  84. #:select? (git-predicate %top-srcdir))))
  85. (with-directory-excursion source
  86. (save-module-excursion
  87. (lambda ()
  88. (set-current-module %user-module)
  89. (format (current-error-port)
  90. "loading '~a' relative to '~a'...~%"
  91. file source)
  92. (primitive-load file))))
  93. ;; Call the entry point of FILE and print the resulting job sexp.
  94. (pretty-print
  95. (match ((module-ref %user-module
  96. (if (equal? cuirass? "cuirass")
  97. 'cuirass-jobs
  98. 'hydra-jobs))
  99. store `((guix
  100. . ((file-name . ,source)))))
  101. (((names . thunks) ...)
  102. (map (lambda (job thunk)
  103. (format (current-error-port) "evaluating '~a'... " job)
  104. (force-output (current-error-port))
  105. (cons job
  106. (assert-valid-job job
  107. (call-with-time-display thunk))))
  108. names thunks)))
  109. port))))))
  110. ((command _ ...)
  111. (format (current-error-port) "Usage: ~a FILE [cuirass]
  112. Evaluate the Hydra or Cuirass jobs defined in FILE.~%"
  113. command)
  114. (exit 1)))
  115. ;;; Local Variables:
  116. ;;; eval: (put 'call-with-time 'scheme-indent-function 1)
  117. ;;; End: