abort failed builds instead of soldiering on
[jscl.git] / jscl.lisp
1 ;;; jscl.lisp ---
2
3 ;; Copyright (C) 2012, 2013 David Vazquez
4 ;; Copyright (C) 2012 Raimon Grau
5
6 ;; This program is free software: you can redistribute it and/or
7 ;; modify it under the terms of the GNU General Public License as
8 ;; published by the Free Software Foundation, either version 3 of the
9 ;; License, or (at your option) any later version.
10 ;;
11 ;; This program is distributed in the hope that it will be useful, but
12 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
13 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 ;; General Public License for more details.
15 ;;
16 ;; You should have received a copy of the GNU General Public License
17 ;; along with this program.  If not, see <http://www.gnu.org/licenses/>.
18
19 (defvar *source*
20   '(("boot"      :target)
21     ("compat"    :host)
22     ("utils"     :both)
23     ("print"     :target)
24     ("read"      :both)
25     ("compiler"  :both)
26     ("toplevel"  :target)))
27
28 (defun source-pathname
29     (filename &key (directory '(:relative "src")) (type nil) (defaults filename))
30   (if type
31       (make-pathname :type type :directory directory :defaults defaults)
32       (make-pathname            :directory directory :defaults defaults)))
33
34 ;;; Compile jscl into the host
35 (with-compilation-unit ()
36   (dolist (input *source*)
37     (when (member (cadr input) '(:host :both))
38       (let ((fname (source-pathname (car input))))
39         (multiple-value-bind (fasl warn fail) (compile-file fname)
40           (declare (ignore fasl warn))
41           (when fail
42             (error "Compilation of ~A failed." fname)))))))
43
44 ;;; Load jscl into the host
45 (dolist (input *source*)
46   (when (member (cadr input) '(:host :both))
47     (load (source-pathname (car input)))))
48
49 (defun read-whole-file (filename)
50   (with-open-file (in filename)
51     (let ((seq (make-array (file-length in) :element-type 'character)))
52       (read-sequence seq in)
53       seq)))
54
55 (defun ls-compile-file (filename out &key print)
56   (let ((*compiling-file* t)
57         (*compile-print-toplevels* print))
58     (let* ((source (read-whole-file filename))
59            (in (make-string-stream source)))
60       (format t "Compiling ~a...~%" filename)
61       (loop
62          with eof-mark = (gensym)
63          for x = (ls-read in nil eof-mark)
64          until (eq x eof-mark)
65          for compilation = (ls-compile-toplevel x)
66          when (plusp (length compilation))
67          do (write-string compilation out)))))
68
69 (defun bootstrap ()
70   (setq *environment* (make-lexenv))
71   (setq *literal-table* nil)
72   (setq *variable-counter* 0
73         *gensym-counter* 0
74         *literal-counter* 0
75         *block-counter* 0)
76   (with-open-file (out "jscl.js" :direction :output :if-exists :supersede)
77     (write-string (read-whole-file (source-pathname "prelude.js")) out)
78     (dolist (input *source*)
79       (when (member (cadr input) '(:target :both))
80         (ls-compile-file (source-pathname (car input) :type "lisp") out))))
81   ;; Tests
82   (with-open-file (out "tests.js" :direction :output :if-exists :supersede)
83     (dolist (input (append (directory "tests.lisp")
84                            (directory "tests/*.lisp")
85                            (directory "tests-report.lisp"))) 
86       (ls-compile-file input out))))
87
88
89 ;;; Run the tests in the host Lisp implementation. It is a quick way
90 ;;; to improve the level of trust of the tests.
91 (defun run-tests-in-host ()
92   (dolist (input (append (directory "tests.lisp")
93                          (directory "tests/*.lisp")
94                          (directory "tests-report.lisp")))
95     (load input)))