compile-all.scm 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2016 Taylan Ulrich Bayırlı/Kammer <taylanbayirli@gmail.com>
  3. ;;; Copyright © 2016, 2017, 2019, 2020, 2021 Ludovic Courtès <ludo@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. (use-modules (ice-9 format)
  20. (ice-9 match)
  21. (ice-9 threads)
  22. (srfi srfi-1)
  23. (guix build compile)
  24. (guix build utils))
  25. (define host (getenv "host"))
  26. (define srcdir (getenv "srcdir"))
  27. (define (relative-file file)
  28. (if (string-prefix? (string-append srcdir "/") file)
  29. (string-drop file (+ 1 (string-length srcdir)))
  30. file))
  31. (define (file-mtime<? f1 f2)
  32. (< (stat:mtime (stat f1))
  33. (stat:mtime (stat f2))))
  34. (define (scm->go file)
  35. (let* ((relative (relative-file file))
  36. (without-extension (string-drop-right relative 4)))
  37. (string-append without-extension ".go")))
  38. (define (file-needs-compilation? file)
  39. (let ((go (scm->go file)))
  40. (or (not (file-exists? go))
  41. (file-mtime<? go file))))
  42. (define* (parallel-job-count #:optional (flags (getenv "MAKEFLAGS")))
  43. "Return the number of parallel jobs as determined by FLAGS, the flags passed
  44. to 'make'."
  45. (match flags
  46. (#f (current-processor-count))
  47. (flags
  48. (let ((initial-flags (string-tokenize flags)))
  49. (let loop ((flags initial-flags))
  50. (match flags
  51. (()
  52. ;; Note: GNU make prior to version 4.2 would hide "-j" flags from
  53. ;; $MAKEFLAGS. Thus, check for a "--jobserver" flag here and
  54. ;; assume we're using all cores if specified.
  55. (if (any (lambda (flag)
  56. (string-prefix? "--jobserver" flag))
  57. initial-flags)
  58. (current-processor-count) ;GNU make < 4.2
  59. 1)) ;sequential make
  60. (("-j" (= string->number count) _ ...)
  61. (if (integer? count)
  62. count
  63. (current-processor-count)))
  64. ((head tail ...)
  65. (if (string-prefix? "-j" head)
  66. (match (string-drop head 2)
  67. (""
  68. (current-processor-count))
  69. ((= string->number count)
  70. (if (integer? count)
  71. count
  72. (current-processor-count))))
  73. (loop tail)))))))))
  74. (define (parallel-job-count*)
  75. ;; XXX: Work around memory requirements not sustainable on i686 above '-j4'
  76. ;; or so: <https://bugs.gnu.org/40522>.
  77. (let ((count (parallel-job-count)))
  78. (if (string-prefix? "i686" %host-type)
  79. (min count 4)
  80. count)))
  81. (define (% completed total)
  82. "Return the completion percentage of COMPLETED over TOTAL as an integer."
  83. (inexact->exact (round (* 100. (/ completed total)))))
  84. ;; Install a SIGINT handler to give unwind handlers in 'compile-file' an
  85. ;; opportunity to run upon SIGINT and to remove temporary output files.
  86. (sigaction SIGINT
  87. (lambda args
  88. (exit 1)))
  89. (match (command-line)
  90. ((_ "--total" (= string->number grand-total)
  91. "--completed" (= string->number processed)
  92. . files)
  93. ;; GRAND-TOTAL is the total number of .scm files in the project; PROCESSED
  94. ;; is the total number of .scm files already compiled in previous
  95. ;; invocations of this script.
  96. (catch #t
  97. (lambda ()
  98. (let* ((to-build (filter file-needs-compilation? files))
  99. (processed (+ processed
  100. (- (length files) (length to-build)))))
  101. (compile-files srcdir (getcwd) to-build
  102. #:workers (parallel-job-count*)
  103. #:host host
  104. #:report-load (lambda (file total completed)
  105. (when file
  106. (format #t "[~3d%] LOAD ~a~%"
  107. (% (+ 1 completed
  108. (* 2 processed))
  109. (* 2 grand-total))
  110. file)
  111. (force-output)))
  112. #:report-compilation (lambda (file total completed)
  113. (when file
  114. (format #t "[~3d%] GUILEC ~a~%"
  115. (% (+ total completed 1
  116. (* 2 processed))
  117. (* 2 grand-total))
  118. (scm->go file))
  119. (force-output))))))
  120. (lambda _
  121. (primitive-exit 1))
  122. (lambda args
  123. ;; Try to report the error in an intelligible way.
  124. (let* ((stack (make-stack #t))
  125. (frame (if (> (stack-length stack) 1)
  126. (stack-ref stack 1) ;skip the 'throw' frame
  127. (stack-ref stack 0)))
  128. (ui (false-if-exception
  129. (resolve-module '(guix ui))))
  130. (report (and ui
  131. (false-if-exception
  132. (module-ref ui 'report-load-error)))))
  133. (if report
  134. (report (or (and=> (current-load-port) port-filename) "?.scm")
  135. args frame)
  136. (begin
  137. (print-exception (current-error-port) frame
  138. (car args) (cdr args))
  139. (display-backtrace stack (current-error-port)))))))))