2017-10-13 12:07:41 -04:00
|
|
|
|
;;; GNU Guix --- Functional package management for GNU
|
2022-01-14 09:59:37 -05:00
|
|
|
|
;;; Copyright © 2013-2014, 2016-2020, 2022 Ludovic Courtès <ludo@gnu.org>
|
2017-10-13 12:07:41 -04:00
|
|
|
|
;;; Copyright © 2015 Taylan Ulrich Bayırlı/Kammer <taylanbayirli@gmail.com>
|
|
|
|
|
;;;
|
|
|
|
|
;;; This file is part of GNU Guix.
|
|
|
|
|
;;;
|
|
|
|
|
;;; GNU Guix is free software; you can redistribute it and/or modify it
|
|
|
|
|
;;; under the terms of the GNU General Public License as published by
|
|
|
|
|
;;; the Free Software Foundation; either version 3 of the License, or (at
|
|
|
|
|
;;; your option) any later version.
|
|
|
|
|
;;;
|
|
|
|
|
;;; GNU Guix is distributed in the hope that it will be useful, but
|
|
|
|
|
;;; WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
;;; GNU General Public License for more details.
|
|
|
|
|
;;;
|
|
|
|
|
;;; You should have received a copy of the GNU General Public License
|
|
|
|
|
;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
|
|
(define-module (guix build compile)
|
2019-07-12 16:38:03 -04:00
|
|
|
|
#:use-module (srfi srfi-1)
|
2017-10-13 12:07:41 -04:00
|
|
|
|
#:use-module (ice-9 match)
|
|
|
|
|
#:use-module (ice-9 format)
|
|
|
|
|
#:use-module (ice-9 threads)
|
|
|
|
|
#:use-module (system base target)
|
|
|
|
|
#:use-module (system base compile)
|
|
|
|
|
#:use-module (system base message)
|
2017-10-13 16:16:46 -04:00
|
|
|
|
#:use-module (guix modules)
|
2017-10-13 12:07:41 -04:00
|
|
|
|
#:use-module (guix build utils)
|
2019-01-06 16:02:40 -05:00
|
|
|
|
#:use-module (language tree-il optimize)
|
|
|
|
|
#:use-module (language cps optimize)
|
2020-01-23 09:34:41 -05:00
|
|
|
|
#:export (compile-files))
|
2017-10-13 12:07:41 -04:00
|
|
|
|
|
|
|
|
|
;;; Commentary:
|
|
|
|
|
;;;
|
2019-01-06 16:02:40 -05:00
|
|
|
|
;;; Support code to compile Guile code as efficiently as possible (with 2.2).
|
2017-10-13 12:07:41 -04:00
|
|
|
|
;;;
|
|
|
|
|
;;; Code:
|
|
|
|
|
|
2022-01-21 10:32:44 -05:00
|
|
|
|
(define (clear-keyword-arguments keywords args)
|
|
|
|
|
"Set to #f the value associated with each of the KEYWORDS in ARGS."
|
2022-01-14 09:59:37 -05:00
|
|
|
|
(let loop ((args args)
|
|
|
|
|
(result '()))
|
|
|
|
|
(match args
|
|
|
|
|
(()
|
|
|
|
|
(reverse result))
|
|
|
|
|
(((? keyword? kw) arg . rest)
|
|
|
|
|
(loop rest
|
|
|
|
|
(if (memq kw keywords)
|
2022-01-21 10:32:44 -05:00
|
|
|
|
(cons* #f kw result)
|
2022-01-14 09:59:37 -05:00
|
|
|
|
(cons* arg kw result))))
|
|
|
|
|
((head . tail)
|
|
|
|
|
(loop tail (cons head result))))))
|
|
|
|
|
|
2019-11-23 16:43:40 -05:00
|
|
|
|
(define optimizations-for-level
|
|
|
|
|
(or (and=> (false-if-exception
|
|
|
|
|
(resolve-interface '(system base optimize)))
|
|
|
|
|
(lambda (iface)
|
|
|
|
|
(module-ref iface 'optimizations-for-level))) ;Guile 3.0
|
|
|
|
|
(let () ;Guile 2.2
|
|
|
|
|
(define %default-optimizations
|
|
|
|
|
;; Default optimization options (equivalent to -O2 on Guile 2.2).
|
|
|
|
|
(append (tree-il-default-optimization-options)
|
|
|
|
|
(cps-default-optimization-options)))
|
2017-10-13 12:07:41 -04:00
|
|
|
|
|
2019-11-23 16:43:40 -05:00
|
|
|
|
(define %lightweight-optimizations
|
|
|
|
|
;; Lightweight optimizations (like -O0, but with partial evaluation).
|
|
|
|
|
(let loop ((opts %default-optimizations)
|
|
|
|
|
(result '()))
|
|
|
|
|
(match opts
|
|
|
|
|
(() (reverse result))
|
|
|
|
|
((#:partial-eval? _ rest ...)
|
|
|
|
|
(loop rest `(#t #:partial-eval? ,@result)))
|
|
|
|
|
((kw _ rest ...)
|
|
|
|
|
(loop rest `(#f ,kw ,@result))))))
|
|
|
|
|
|
|
|
|
|
(lambda (level)
|
2022-01-14 09:59:37 -05:00
|
|
|
|
;; In the upcoming Guile 3.0.8, .go files include code of their
|
|
|
|
|
;; inlinable exports and free variables are resolved at compile time
|
|
|
|
|
;; (both are enabled at -O1) to permit cross-module inlining
|
|
|
|
|
;; (enabled at -O2). Unfortunately, this currently leads to
|
|
|
|
|
;; non-reproducible and more expensive builds, so we turn it off
|
|
|
|
|
;; here:
|
|
|
|
|
;; <https://wingolog.org/archives/2021/05/13/cross-module-inlining-in-guile>.
|
2022-01-21 10:32:44 -05:00
|
|
|
|
(clear-keyword-arguments '(#:inlinable-exports? #:resolve-free-vars?
|
2022-01-14 09:59:37 -05:00
|
|
|
|
#:cross-module-inlining?)
|
|
|
|
|
(if (<= level 1)
|
|
|
|
|
%lightweight-optimizations
|
|
|
|
|
%default-optimizations))))))
|
2017-10-13 12:07:41 -04:00
|
|
|
|
|
2019-07-12 16:38:03 -04:00
|
|
|
|
(define (supported-warning-type? type)
|
|
|
|
|
"Return true if TYPE, a symbol, denotes a supported warning type."
|
|
|
|
|
(find (lambda (warning-type)
|
|
|
|
|
(eq? type (warning-type-name warning-type)))
|
|
|
|
|
%warning-types))
|
|
|
|
|
|
2017-10-13 12:07:41 -04:00
|
|
|
|
(define %warnings
|
|
|
|
|
;; FIXME: 'format' is missing because it reports "non-literal format
|
|
|
|
|
;; strings" due to the fact that we use 'G_' instead of '_'. We'll need
|
|
|
|
|
;; help from Guile to solve this.
|
2019-07-12 16:38:03 -04:00
|
|
|
|
(let ((optional (lambda (type)
|
|
|
|
|
(if (supported-warning-type? type)
|
|
|
|
|
(list type)
|
|
|
|
|
'()))))
|
|
|
|
|
`(unbound-variable arity-mismatch
|
|
|
|
|
macro-use-before-definition ;new in 2.2
|
|
|
|
|
,@(optional 'shadowed-toplevel)))) ;new in 2.2.5
|
2017-10-13 12:07:41 -04:00
|
|
|
|
|
|
|
|
|
(define (optimization-options file)
|
|
|
|
|
"Return the default set of optimizations options for FILE."
|
2020-05-13 17:17:49 -04:00
|
|
|
|
(define (strip-option option lst)
|
|
|
|
|
(let loop ((lst lst)
|
|
|
|
|
(result '()))
|
|
|
|
|
(match lst
|
|
|
|
|
(()
|
|
|
|
|
(reverse result))
|
|
|
|
|
((kw value rest ...)
|
|
|
|
|
(if (eq? kw option)
|
|
|
|
|
(append (reverse result) rest)
|
|
|
|
|
(loop rest (cons* value kw result)))))))
|
|
|
|
|
|
|
|
|
|
(define (override-option option value lst)
|
|
|
|
|
`(,option ,value ,@(strip-option option lst)))
|
|
|
|
|
|
2020-05-25 16:59:22 -04:00
|
|
|
|
(cond ((or (string-contains file "gnu/packages/")
|
|
|
|
|
(string-contains file "gnu/tests/"))
|
2020-06-22 03:39:06 -04:00
|
|
|
|
;; Use '-O1' to have partial evaluation and primitive inlining so we
|
|
|
|
|
;; can honor the "macro writer's bill of rights".
|
|
|
|
|
(optimizations-for-level 1))
|
2020-05-13 17:17:49 -04:00
|
|
|
|
((string-contains file "gnu/services/")
|
|
|
|
|
;; '-O2 -Ono-letrectify' compiles about ~20% faster than '-O2' for
|
|
|
|
|
;; large files like gnu/services/mail.scm.
|
|
|
|
|
(override-option #:letrectify? #f
|
|
|
|
|
(optimizations-for-level 2)))
|
|
|
|
|
(else
|
|
|
|
|
(optimizations-for-level 3))))
|
2017-10-13 12:07:41 -04:00
|
|
|
|
|
|
|
|
|
(define (scm->go file)
|
|
|
|
|
"Strip the \".scm\" suffix from FILE, and append \".go\"."
|
|
|
|
|
(string-append (string-drop-right file 4) ".go"))
|
|
|
|
|
|
2017-11-05 06:49:57 -05:00
|
|
|
|
(define (relative-file directory file)
|
|
|
|
|
"Return FILE relative to DIRECTORY, if possible."
|
|
|
|
|
(if (string-prefix? (string-append directory "/") file)
|
|
|
|
|
(string-drop file (+ 1 (string-length directory)))
|
|
|
|
|
file))
|
|
|
|
|
|
2017-10-13 12:07:41 -04:00
|
|
|
|
(define* (load-files directory files
|
|
|
|
|
#:key
|
|
|
|
|
(report-load (const #f))
|
|
|
|
|
(debug-port (%make-void-port "w")))
|
|
|
|
|
"Load FILES, a list of relative file names, from DIRECTORY."
|
|
|
|
|
(define total
|
|
|
|
|
(length files))
|
|
|
|
|
|
|
|
|
|
(let loop ((files files)
|
|
|
|
|
(completed 0))
|
|
|
|
|
(match files
|
|
|
|
|
(()
|
|
|
|
|
(unless (zero? total)
|
|
|
|
|
(report-load #f total completed))
|
|
|
|
|
*unspecified*)
|
|
|
|
|
((file files ...)
|
2017-11-05 06:49:57 -05:00
|
|
|
|
(let ((file (relative-file directory file)))
|
|
|
|
|
(report-load file total completed)
|
|
|
|
|
(format debug-port "~%loading '~a'...~%" file)
|
2017-10-13 12:07:41 -04:00
|
|
|
|
|
2019-01-22 06:01:49 -05:00
|
|
|
|
(resolve-interface (file-name->module-name file))
|
2017-10-13 12:07:41 -04:00
|
|
|
|
|
2017-11-05 06:49:57 -05:00
|
|
|
|
(loop files (+ 1 completed)))))))
|
2017-10-13 12:07:41 -04:00
|
|
|
|
|
|
|
|
|
(define-syntax-rule (with-augmented-search-path path item body ...)
|
|
|
|
|
"Within the dynamic extent of BODY, augment PATH by adding ITEM to the
|
|
|
|
|
front."
|
|
|
|
|
(let ((initial-value path))
|
|
|
|
|
(dynamic-wind
|
|
|
|
|
(lambda ()
|
|
|
|
|
(set! path (cons item path)))
|
|
|
|
|
(lambda ()
|
|
|
|
|
body ...)
|
|
|
|
|
(lambda ()
|
|
|
|
|
(set! path initial-value)))))
|
|
|
|
|
|
2019-07-14 11:07:09 -04:00
|
|
|
|
(define (call/exit-on-exception file thunk)
|
|
|
|
|
"Evaluate THUNK and exit right away if an exception is thrown. Report FILE
|
|
|
|
|
as the file that was being compiled when the exception was thrown."
|
2018-05-01 09:26:16 -04:00
|
|
|
|
(catch #t
|
|
|
|
|
thunk
|
|
|
|
|
(const #f)
|
|
|
|
|
(lambda (key . args)
|
|
|
|
|
(false-if-exception
|
|
|
|
|
;; Duplicate stderr to avoid thread-safety issues.
|
|
|
|
|
(let* ((port (duplicate-port (current-error-port) "w0"))
|
|
|
|
|
(stack (make-stack #t))
|
|
|
|
|
(depth (stack-length stack))
|
|
|
|
|
(frame (and (> depth 1) (stack-ref stack 1))))
|
2019-07-14 11:07:09 -04:00
|
|
|
|
(newline port)
|
|
|
|
|
(format port "error: failed to compile '~a':~%~%" file)
|
2018-05-01 09:26:16 -04:00
|
|
|
|
(false-if-exception (display-backtrace stack port))
|
|
|
|
|
(print-exception port frame key args)))
|
|
|
|
|
|
|
|
|
|
;; Don't go any further.
|
|
|
|
|
(primitive-exit 1))))
|
|
|
|
|
|
2019-07-14 11:07:09 -04:00
|
|
|
|
(define-syntax-rule (exit-on-exception file exp ...)
|
|
|
|
|
"Evaluate EXP and exit if an exception is thrown. Report FILE as the faulty
|
|
|
|
|
file when an exception is thrown."
|
|
|
|
|
(call/exit-on-exception file (lambda () exp ...)))
|
2018-05-01 09:26:16 -04:00
|
|
|
|
|
2017-10-13 12:07:41 -04:00
|
|
|
|
(define* (compile-files source-directory build-directory files
|
|
|
|
|
#:key
|
|
|
|
|
(host %host-type)
|
|
|
|
|
(workers (current-processor-count))
|
|
|
|
|
(optimization-options optimization-options)
|
|
|
|
|
(warning-options `(#:warnings ,%warnings))
|
|
|
|
|
(report-load (const #f))
|
|
|
|
|
(report-compilation (const #f))
|
|
|
|
|
(debug-port (%make-void-port "w")))
|
|
|
|
|
"Compile FILES, a list of source files taken from SOURCE-DIRECTORY, to
|
|
|
|
|
BUILD-DIRECTORY, using up to WORKERS parallel workers. The resulting object
|
|
|
|
|
files are for HOST, a GNU triplet such as \"x86_64-linux-gnu\"."
|
|
|
|
|
(define progress-lock (make-mutex))
|
|
|
|
|
(define total (length files))
|
2019-09-24 21:57:34 -04:00
|
|
|
|
(define progress 0)
|
2017-10-13 12:07:41 -04:00
|
|
|
|
|
|
|
|
|
(define (build file)
|
|
|
|
|
(with-mutex progress-lock
|
2019-09-24 21:57:34 -04:00
|
|
|
|
(report-compilation file total progress)
|
|
|
|
|
(set! progress (+ 1 progress)))
|
2018-05-01 09:26:16 -04:00
|
|
|
|
|
|
|
|
|
;; Exit as soon as something goes wrong.
|
|
|
|
|
(exit-on-exception
|
2019-07-14 11:07:09 -04:00
|
|
|
|
file
|
2020-04-07 17:48:54 -04:00
|
|
|
|
(let ((relative (relative-file source-directory file)))
|
|
|
|
|
(compile-file file
|
|
|
|
|
#:output-file (string-append build-directory "/"
|
|
|
|
|
(scm->go relative))
|
|
|
|
|
#:opts (append warning-options
|
|
|
|
|
(optimization-options relative))))))
|
2017-10-13 12:07:41 -04:00
|
|
|
|
|
|
|
|
|
(with-augmented-search-path %load-path source-directory
|
|
|
|
|
(with-augmented-search-path %load-compiled-path build-directory
|
2019-01-22 06:01:49 -05:00
|
|
|
|
(with-fluids ((*current-warning-prefix* ""))
|
2020-04-23 17:10:19 -04:00
|
|
|
|
;; Make sure the compiler's modules are loaded before 'with-target'
|
|
|
|
|
;; (since 'with-target' influences the .go loader), and before
|
|
|
|
|
;; starting to compile files in parallel.
|
|
|
|
|
(compile #f)
|
|
|
|
|
|
2020-04-07 17:48:54 -04:00
|
|
|
|
(with-target host
|
|
|
|
|
(lambda ()
|
|
|
|
|
;; FIXME: To work around <https://bugs.gnu.org/15602>, we first
|
|
|
|
|
;; load all of FILES.
|
|
|
|
|
(load-files source-directory files
|
|
|
|
|
#:report-load report-load
|
|
|
|
|
#:debug-port debug-port)
|
|
|
|
|
|
|
|
|
|
;; XXX: Don't use too many workers to work around the insane
|
|
|
|
|
;; memory requirements of the compiler in Guile 2.2.2:
|
|
|
|
|
;; <https://lists.gnu.org/archive/html/guile-devel/2017-05/msg00033.html>.
|
|
|
|
|
(n-par-for-each (min workers 8) build files)
|
|
|
|
|
|
|
|
|
|
(unless (zero? total)
|
|
|
|
|
(report-compilation #f total total))))))))
|
2017-10-13 12:07:41 -04:00
|
|
|
|
|
2018-06-18 09:16:40 -04:00
|
|
|
|
(eval-when (eval load)
|
|
|
|
|
(when (and (string=? "2" (major-version))
|
|
|
|
|
(or (string=? "0" (minor-version))
|
|
|
|
|
(and (string=? (minor-version) "2")
|
|
|
|
|
(< (string->number (micro-version)) 4))))
|
|
|
|
|
;; Work around <https://bugs.gnu.org/31878> on Guile < 2.2.4.
|
|
|
|
|
;; Serialize 'try-module-autoload' calls.
|
|
|
|
|
(set! (@ (guile) try-module-autoload)
|
|
|
|
|
(let ((mutex (make-mutex 'recursive))
|
|
|
|
|
(real (@ (guile) try-module-autoload)))
|
|
|
|
|
(lambda* (module #:optional version)
|
|
|
|
|
(with-mutex mutex
|
|
|
|
|
(real module version)))))))
|
|
|
|
|
|
2017-10-13 12:07:41 -04:00
|
|
|
|
;;; Local Variables:
|
|
|
|
|
;;; eval: (put 'with-augmented-search-path 'scheme-indent-function 2)
|
|
|
|
|
;;; eval: (put 'with-target 'scheme-indent-function 1)
|
|
|
|
|
;;; End:
|