compile.scm 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2013, 2014, 2016, 2017, 2018, 2019, 2020 Ludovic Courtès <ludo@gnu.org>
  3. ;;; Copyright © 2015 Taylan Ulrich Bayırlı/Kammer <taylanbayirli@gmail.com>
  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. (define-module (guix build compile)
  20. #:use-module (srfi srfi-1)
  21. #:use-module (ice-9 match)
  22. #:use-module (ice-9 format)
  23. #:use-module (ice-9 threads)
  24. #:use-module (system base target)
  25. #:use-module (system base compile)
  26. #:use-module (system base message)
  27. #:use-module (guix modules)
  28. #:use-module (guix build utils)
  29. #:use-module (language tree-il optimize)
  30. #:use-module (language cps optimize)
  31. #:export (compile-files))
  32. ;;; Commentary:
  33. ;;;
  34. ;;; Support code to compile Guile code as efficiently as possible (with 2.2).
  35. ;;;
  36. ;;; Code:
  37. (define optimizations-for-level
  38. (or (and=> (false-if-exception
  39. (resolve-interface '(system base optimize)))
  40. (lambda (iface)
  41. (module-ref iface 'optimizations-for-level))) ;Guile 3.0
  42. (let () ;Guile 2.2
  43. (define %default-optimizations
  44. ;; Default optimization options (equivalent to -O2 on Guile 2.2).
  45. (append (tree-il-default-optimization-options)
  46. (cps-default-optimization-options)))
  47. (define %lightweight-optimizations
  48. ;; Lightweight optimizations (like -O0, but with partial evaluation).
  49. (let loop ((opts %default-optimizations)
  50. (result '()))
  51. (match opts
  52. (() (reverse result))
  53. ((#:partial-eval? _ rest ...)
  54. (loop rest `(#t #:partial-eval? ,@result)))
  55. ((kw _ rest ...)
  56. (loop rest `(#f ,kw ,@result))))))
  57. (lambda (level)
  58. (if (<= level 1)
  59. %lightweight-optimizations
  60. %default-optimizations)))))
  61. (define (supported-warning-type? type)
  62. "Return true if TYPE, a symbol, denotes a supported warning type."
  63. (find (lambda (warning-type)
  64. (eq? type (warning-type-name warning-type)))
  65. %warning-types))
  66. (define %warnings
  67. ;; FIXME: 'format' is missing because it reports "non-literal format
  68. ;; strings" due to the fact that we use 'G_' instead of '_'. We'll need
  69. ;; help from Guile to solve this.
  70. (let ((optional (lambda (type)
  71. (if (supported-warning-type? type)
  72. (list type)
  73. '()))))
  74. `(unbound-variable arity-mismatch
  75. macro-use-before-definition ;new in 2.2
  76. ,@(optional 'shadowed-toplevel)))) ;new in 2.2.5
  77. (define (optimization-options file)
  78. "Return the default set of optimizations options for FILE."
  79. (define (strip-option option lst)
  80. (let loop ((lst lst)
  81. (result '()))
  82. (match lst
  83. (()
  84. (reverse result))
  85. ((kw value rest ...)
  86. (if (eq? kw option)
  87. (append (reverse result) rest)
  88. (loop rest (cons* value kw result)))))))
  89. (define (override-option option value lst)
  90. `(,option ,value ,@(strip-option option lst)))
  91. (cond ((or (string-contains file "gnu/packages/")
  92. (string-contains file "gnu/tests/"))
  93. ;; Use '-O1' to have partial evaluation and primitive inlining so we
  94. ;; can honor the "macro writer's bill of rights".
  95. (optimizations-for-level 1))
  96. ((string-contains file "gnu/services/")
  97. ;; '-O2 -Ono-letrectify' compiles about ~20% faster than '-O2' for
  98. ;; large files like gnu/services/mail.scm.
  99. (override-option #:letrectify? #f
  100. (optimizations-for-level 2)))
  101. (else
  102. (optimizations-for-level 3))))
  103. (define (scm->go file)
  104. "Strip the \".scm\" suffix from FILE, and append \".go\"."
  105. (string-append (string-drop-right file 4) ".go"))
  106. (define (relative-file directory file)
  107. "Return FILE relative to DIRECTORY, if possible."
  108. (if (string-prefix? (string-append directory "/") file)
  109. (string-drop file (+ 1 (string-length directory)))
  110. file))
  111. (define* (load-files directory files
  112. #:key
  113. (report-load (const #f))
  114. (debug-port (%make-void-port "w")))
  115. "Load FILES, a list of relative file names, from DIRECTORY."
  116. (define total
  117. (length files))
  118. (let loop ((files files)
  119. (completed 0))
  120. (match files
  121. (()
  122. (unless (zero? total)
  123. (report-load #f total completed))
  124. *unspecified*)
  125. ((file files ...)
  126. (let ((file (relative-file directory file)))
  127. (report-load file total completed)
  128. (format debug-port "~%loading '~a'...~%" file)
  129. (resolve-interface (file-name->module-name file))
  130. (loop files (+ 1 completed)))))))
  131. (define-syntax-rule (with-augmented-search-path path item body ...)
  132. "Within the dynamic extent of BODY, augment PATH by adding ITEM to the
  133. front."
  134. (let ((initial-value path))
  135. (dynamic-wind
  136. (lambda ()
  137. (set! path (cons item path)))
  138. (lambda ()
  139. body ...)
  140. (lambda ()
  141. (set! path initial-value)))))
  142. (define (call/exit-on-exception file thunk)
  143. "Evaluate THUNK and exit right away if an exception is thrown. Report FILE
  144. as the file that was being compiled when the exception was thrown."
  145. (catch #t
  146. thunk
  147. (const #f)
  148. (lambda (key . args)
  149. (false-if-exception
  150. ;; Duplicate stderr to avoid thread-safety issues.
  151. (let* ((port (duplicate-port (current-error-port) "w0"))
  152. (stack (make-stack #t))
  153. (depth (stack-length stack))
  154. (frame (and (> depth 1) (stack-ref stack 1))))
  155. (newline port)
  156. (format port "error: failed to compile '~a':~%~%" file)
  157. (false-if-exception (display-backtrace stack port))
  158. (print-exception port frame key args)))
  159. ;; Don't go any further.
  160. (primitive-exit 1))))
  161. (define-syntax-rule (exit-on-exception file exp ...)
  162. "Evaluate EXP and exit if an exception is thrown. Report FILE as the faulty
  163. file when an exception is thrown."
  164. (call/exit-on-exception file (lambda () exp ...)))
  165. (define* (compile-files source-directory build-directory files
  166. #:key
  167. (host %host-type)
  168. (workers (current-processor-count))
  169. (optimization-options optimization-options)
  170. (warning-options `(#:warnings ,%warnings))
  171. (report-load (const #f))
  172. (report-compilation (const #f))
  173. (debug-port (%make-void-port "w")))
  174. "Compile FILES, a list of source files taken from SOURCE-DIRECTORY, to
  175. BUILD-DIRECTORY, using up to WORKERS parallel workers. The resulting object
  176. files are for HOST, a GNU triplet such as \"x86_64-linux-gnu\"."
  177. (define progress-lock (make-mutex))
  178. (define total (length files))
  179. (define progress 0)
  180. (define (build file)
  181. (with-mutex progress-lock
  182. (report-compilation file total progress)
  183. (set! progress (+ 1 progress)))
  184. ;; Exit as soon as something goes wrong.
  185. (exit-on-exception
  186. file
  187. (let ((relative (relative-file source-directory file)))
  188. (compile-file file
  189. #:output-file (string-append build-directory "/"
  190. (scm->go relative))
  191. #:opts (append warning-options
  192. (optimization-options relative))))))
  193. (with-augmented-search-path %load-path source-directory
  194. (with-augmented-search-path %load-compiled-path build-directory
  195. (with-fluids ((*current-warning-prefix* ""))
  196. ;; Make sure the compiler's modules are loaded before 'with-target'
  197. ;; (since 'with-target' influences the .go loader), and before
  198. ;; starting to compile files in parallel.
  199. (compile #f)
  200. (with-target host
  201. (lambda ()
  202. ;; FIXME: To work around <https://bugs.gnu.org/15602>, we first
  203. ;; load all of FILES.
  204. (load-files source-directory files
  205. #:report-load report-load
  206. #:debug-port debug-port)
  207. ;; XXX: Don't use too many workers to work around the insane
  208. ;; memory requirements of the compiler in Guile 2.2.2:
  209. ;; <https://lists.gnu.org/archive/html/guile-devel/2017-05/msg00033.html>.
  210. (n-par-for-each (min workers 8) build files)
  211. (unless (zero? total)
  212. (report-compilation #f total total))))))))
  213. (eval-when (eval load)
  214. (when (and (string=? "2" (major-version))
  215. (or (string=? "0" (minor-version))
  216. (and (string=? (minor-version) "2")
  217. (< (string->number (micro-version)) 4))))
  218. ;; Work around <https://bugs.gnu.org/31878> on Guile < 2.2.4.
  219. ;; Serialize 'try-module-autoload' calls.
  220. (set! (@ (guile) try-module-autoload)
  221. (let ((mutex (make-mutex 'recursive))
  222. (real (@ (guile) try-module-autoload)))
  223. (lambda* (module #:optional version)
  224. (with-mutex mutex
  225. (real module version)))))))
  226. ;;; Local Variables:
  227. ;;; eval: (put 'with-augmented-search-path 'scheme-indent-function 2)
  228. ;;; eval: (put 'with-target 'scheme-indent-function 1)
  229. ;;; End: