compile-all.scm 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2016 Taylan Ulrich Bayırlı/Kammer <taylanbayirli@gmail.com>
  3. ;;; Copyright © 2016, 2017 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 (system base target)
  20. (system base message)
  21. (ice-9 match)
  22. (ice-9 threads)
  23. (guix build utils))
  24. (define warnings
  25. ;; FIXME: 'format' is missing because it reports "non-literal format
  26. ;; strings" due to the fact that we use 'G_' instead of '_'. We'll need
  27. ;; help from Guile to solve this.
  28. '(unsupported-warning unbound-variable arity-mismatch
  29. macro-use-before-definition)) ;new in 2.2
  30. (define host (getenv "host"))
  31. (define srcdir (getenv "srcdir"))
  32. (define (relative-file file)
  33. (if (string-prefix? (string-append srcdir "/") file)
  34. (string-drop file (+ 1 (string-length srcdir)))
  35. file))
  36. (define (file-mtime<? f1 f2)
  37. (< (stat:mtime (stat f1))
  38. (stat:mtime (stat f2))))
  39. (define (scm->go file)
  40. (let* ((relative (relative-file file))
  41. (without-extension (string-drop-right relative 4)))
  42. (string-append without-extension ".go")))
  43. (define (file-needs-compilation? file)
  44. (let ((go (scm->go file)))
  45. (or (not (file-exists? go))
  46. (file-mtime<? go file))))
  47. (define (file->module file)
  48. (let* ((relative (relative-file file))
  49. (module-path (string-drop-right relative 4)))
  50. (map string->symbol
  51. (string-split module-path #\/))))
  52. ;;; To work around <http://bugs.gnu.org/15602> (FIXME), we want to load all
  53. ;;; files to be compiled first. We do this via resolve-interface so that the
  54. ;;; top-level of each file (module) is only executed once.
  55. (define (load-module-file file)
  56. (let ((module (file->module file)))
  57. (format #t " LOAD ~a~%" module)
  58. (resolve-interface module)))
  59. (cond-expand
  60. (guile-2.2 (use-modules (language tree-il optimize)
  61. (language cps optimize)))
  62. (else #f))
  63. (define %default-optimizations
  64. ;; Default optimization options (equivalent to -O2 on Guile 2.2).
  65. (cond-expand
  66. (guile-2.2 (append (tree-il-default-optimization-options)
  67. (cps-default-optimization-options)))
  68. (else '())))
  69. (define %lightweight-optimizations
  70. ;; Lightweight optimizations (like -O0, but with partial evaluation).
  71. (let loop ((opts %default-optimizations)
  72. (result '()))
  73. (match opts
  74. (() (reverse result))
  75. ((#:partial-eval? _ rest ...)
  76. (loop rest `(#t #:partial-eval? ,@result)))
  77. ((kw _ rest ...)
  78. (loop rest `(#f ,kw ,@result))))))
  79. (define (optimization-options file)
  80. (if (string-contains file "gnu/packages/")
  81. %lightweight-optimizations ;build faster
  82. '()))
  83. (define (compile-file* file output-mutex)
  84. (let ((go (scm->go file)))
  85. (with-mutex output-mutex
  86. (format #t " GUILEC ~a~%" go)
  87. (force-output))
  88. (mkdir-p (dirname go))
  89. (with-fluids ((*current-warning-prefix* ""))
  90. (with-target host
  91. (lambda ()
  92. (compile-file file
  93. #:output-file go
  94. #:opts `(#:warnings ,warnings
  95. ,@(optimization-options file))))))))
  96. ;; Install a SIGINT handler to give unwind handlers in 'compile-file' an
  97. ;; opportunity to run upon SIGINT and to remove temporary output files.
  98. (sigaction SIGINT
  99. (lambda args
  100. (exit 1)))
  101. (match (command-line)
  102. ((_ . files)
  103. (let ((files (filter file-needs-compilation? files)))
  104. (for-each load-module-file files)
  105. (let ((mutex (make-mutex)))
  106. ;; Make sure compilation related modules are loaded before starting to
  107. ;; compile files in parallel.
  108. (compile #f)
  109. (par-for-each (lambda (file)
  110. (compile-file* file mutex))
  111. files)))))
  112. ;;; Local Variables:
  113. ;;; eval: (put 'with-target 'scheme-indent-function 1)
  114. ;;; End: