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