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>
5 ;;; This file is part of GNU Guix.
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.
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.
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/>.
20 (define-module (guix build compile)
21 #:use-module (srfi srfi-1)
22 #:use-module (ice-9 match)
23 #:use-module (ice-9 format)
24 #:use-module (ice-9 threads)
25 #:use-module (system base target)
26 #:use-module (system base compile)
27 #:use-module (system base message)
28 #:use-module (guix modules)
29 #:use-module (guix build utils)
30 #:use-module (language tree-il optimize)
31 #:use-module (language cps optimize)
32 #:export (compile-files))
36 ;;; Support code to compile Guile code as efficiently as possible (with 2.2).
40 (define optimizations-for-level
41 (or (and=> (false-if-exception
42 (resolve-interface '(system base optimize)))
44 (module-ref iface 'optimizations-for-level))) ;Guile 3.0
46 (define %default-optimizations
47 ;; Default optimization options (equivalent to -O2 on Guile 2.2).
48 (append (tree-il-default-optimization-options)
49 (cps-default-optimization-options)))
51 (define %lightweight-optimizations
52 ;; Lightweight optimizations (like -O0, but with partial evaluation).
53 (let loop ((opts %default-optimizations)
57 ((#:partial-eval? _ rest ...)
58 (loop rest `(#t #:partial-eval? ,@result)))
60 (loop rest `(#f ,kw ,@result))))))
64 %lightweight-optimizations
65 %default-optimizations)))))
67 (define (supported-warning-type? type)
68 "Return true if TYPE, a symbol, denotes a supported warning type."
69 (find (lambda (warning-type)
70 (eq? type (warning-type-name warning-type)))
74 ;; FIXME: 'format' is missing because it reports "non-literal format
75 ;; strings" due to the fact that we use 'G_' instead of '_'. We'll need
76 ;; help from Guile to solve this.
77 (let ((optional (lambda (type)
78 (if (supported-warning-type? type)
81 `(unbound-variable arity-mismatch
82 macro-use-before-definition ;new in 2.2
83 ,@(optional 'shadowed-toplevel)))) ;new in 2.2.5
85 (define (optimization-options file)
86 "Return the default set of optimizations options for FILE."
87 (define (strip-option option lst)
95 (append (reverse result) rest)
96 (loop rest (cons* value kw result)))))))
98 (define (override-option option value lst)
99 `(,option ,value ,@(strip-option option lst)))
101 (cond ((or (string-contains file "gnu/packages/")
102 (string-contains file "gnu/tests/"))
103 ;; Use '-O1' to have partial evaluation and primitive inlining so we
104 ;; can honor the "macro writer's bill of rights".
105 (optimizations-for-level 1))
106 ((string-contains file "gnu/services/")
107 ;; '-O2 -Ono-letrectify' compiles about ~20% faster than '-O2' for
108 ;; large files like gnu/services/mail.scm.
109 (override-option #:letrectify? #f
110 (optimizations-for-level 2)))
112 (optimizations-for-level 3))))
114 (define (scm->go file)
115 "Strip the \".scm\" suffix from FILE, and append \".go\"."
116 (string-append (string-drop-right file 4) ".go"))
118 (define (relative-file directory file)
119 "Return FILE relative to DIRECTORY, if possible."
120 (if (string-prefix? (string-append directory "/") file)
121 (string-drop file (+ 1 (string-length directory)))
124 (define* (load-files directory files
126 (report-load (const #f))
127 (debug-port (%make-void-port "w")))
128 "Load FILES, a list of relative file names, from DIRECTORY."
132 (let loop ((files files)
136 (unless (zero? total)
137 (report-load #f total completed))
140 (let ((file (relative-file directory file)))
141 (report-load file total completed)
142 (format debug-port "~%loading '~a'...~%" file)
144 (resolve-interface (file-name->module-name file))
146 (loop files (+ 1 completed)))))))
148 (define-syntax-rule (with-augmented-search-path path item body ...)
149 "Within the dynamic extent of BODY, augment PATH by adding ITEM to the
151 (let ((initial-value path))
154 (set! path (cons item path)))
158 (set! path initial-value)))))
160 (define (call/exit-on-exception file thunk)
161 "Evaluate THUNK and exit right away if an exception is thrown. Report FILE
162 as the file that was being compiled when the exception was thrown."
168 ;; Duplicate stderr to avoid thread-safety issues.
169 (let* ((port (duplicate-port (current-error-port) "w0"))
170 (stack (make-stack #t))
171 (depth (stack-length stack))
172 (frame (and (> depth 1) (stack-ref stack 1))))
174 (format port "error: failed to compile '~a':~%~%" file)
175 (false-if-exception (display-backtrace stack port))
176 (print-exception port frame key args)))
178 ;; Don't go any further.
179 (primitive-exit 1))))
181 (define-syntax-rule (exit-on-exception file exp ...)
182 "Evaluate EXP and exit if an exception is thrown. Report FILE as the faulty
183 file when an exception is thrown."
184 (call/exit-on-exception file (lambda () exp ...)))
186 (define* (compile-files source-directory build-directory files
189 (workers (current-processor-count))
190 (optimization-options optimization-options)
191 (warning-options `(#:warnings ,%warnings))
192 (report-load (const #f))
193 (report-compilation (const #f))
194 (debug-port (%make-void-port "w")))
195 "Compile FILES, a list of source files taken from SOURCE-DIRECTORY, to
196 BUILD-DIRECTORY, using up to WORKERS parallel workers. The resulting object
197 files are for HOST, a GNU triplet such as \"x86_64-linux-gnu\"."
198 (define progress-lock (make-mutex))
199 (define total (length files))
203 (with-mutex progress-lock
204 (report-compilation file total progress)
205 (set! progress (+ 1 progress)))
207 ;; Exit as soon as something goes wrong.
210 (let ((relative (relative-file source-directory file)))
212 #:output-file (string-append build-directory "/"
214 #:opts (append warning-options
215 (optimization-options relative))))))
217 (with-augmented-search-path %load-path source-directory
218 (with-augmented-search-path %load-compiled-path build-directory
219 (with-fluids ((*current-warning-prefix* ""))
220 ;; Make sure the compiler's modules are loaded before 'with-target'
221 ;; (since 'with-target' influences the .go loader), and before
222 ;; starting to compile files in parallel.
227 ;; FIXME: To work around <https://bugs.gnu.org/15602>, we first
228 ;; load all of FILES.
229 (load-files source-directory files
230 #:report-load report-load
231 #:debug-port debug-port)
233 ;; XXX: Don't use too many workers to work around the insane
234 ;; memory requirements of the compiler in Guile 2.2.2:
235 ;; <https://lists.gnu.org/archive/html/guile-devel/2017-05/msg00033.html>.
236 (n-par-for-each (min workers 8) build files)
238 (unless (zero? total)
239 (report-compilation #f total total))))))))
241 (eval-when (eval load)
242 (when (and (string=? "2" (major-version))
243 (or (string=? "0" (minor-version))
244 (and (string=? (minor-version) "2")
245 (< (string->number (micro-version)) 4))))
246 ;; Work around <https://bugs.gnu.org/31878> on Guile < 2.2.4.
247 ;; Serialize 'try-module-autoload' calls.
248 (set! (@ (guile) try-module-autoload)
249 (let ((mutex (make-mutex 'recursive))
250 (real (@ (guile) try-module-autoload)))
251 (lambda* (module #:optional version)
253 (real module version)))))))
256 ;;; eval: (put 'with-augmented-search-path 'scheme-indent-function 2)
257 ;;; eval: (put 'with-target 'scheme-indent-function 1)