compile-all.scm 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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 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. ((_ . files)
  91. (catch #t
  92. (lambda ()
  93. (compile-files srcdir (getcwd)
  94. (filter file-needs-compilation? files)
  95. #:workers (parallel-job-count*)
  96. #:host host
  97. #:report-load (lambda (file total completed)
  98. (when file
  99. (format #t "[~3d%] LOAD ~a~%"
  100. (% (+ 1 completed) (* 2 total))
  101. file)
  102. (force-output)))
  103. #:report-compilation (lambda (file total completed)
  104. (when file
  105. (format #t "[~3d%] GUILEC ~a~%"
  106. (% (+ total completed 1)
  107. (* 2 total))
  108. (scm->go file))
  109. (force-output)))))
  110. (lambda _
  111. (primitive-exit 1))
  112. (lambda args
  113. ;; Try to report the error in an intelligible way.
  114. (let* ((stack (make-stack #t))
  115. (frame (if (> (stack-length stack) 1)
  116. (stack-ref stack 1) ;skip the 'throw' frame
  117. (stack-ref stack 0)))
  118. (ui (false-if-exception
  119. (resolve-module '(guix ui))))
  120. (report (and ui
  121. (false-if-exception
  122. (module-ref ui 'report-load-error)))))
  123. (if report
  124. ;; In Guile <= 2.2.5, 'current-load-port' was not exported.
  125. (let ((load-port ((module-ref (resolve-module '(ice-9 ports))
  126. 'current-load-port))))
  127. (report (or (and=> load-port port-filename) "?.scm")
  128. args frame))
  129. (begin
  130. (print-exception (current-error-port) frame
  131. (car args) (cdr args))
  132. (display-backtrace stack (current-error-port)))))))))