Bootstrap from any working directory
[jscl.git] / jscl.lisp
1 ;;; jscl.lisp ---
2
3 ;; Copyright (C) 2012, 2013 David Vazquez
4 ;; Copyright (C) 2012 Raimon Grau
5
6 ;; JSCL 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 ;; JSCL 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 JSCL.  If not, see <http://www.gnu.org/licenses/>.
18
19 (defpackage :jscl
20   (:use :cl)
21   (:export #:bootstrap #:run-tests-in-host))
22
23 (in-package :jscl)
24
25 (defvar *base-directory*
26   (or #.*load-pathname* *default-pathname-defaults*))
27
28 ;;; List of all the source files that need to be compiled, and whether they
29 ;;; are to be compiled just by the host, by the target JSCL, or by both.
30 ;;; All files have a `.lisp' extension, and
31 ;;; are relative to src/
32 ;;; Subdirectories are indicated by the presence of a list rather than a
33 ;;; keyword in the second element of the list. For example, this list:
34 ;;;  (("foo"    :target)
35 ;;;   ("bar"
36 ;;;     ("baz"  :host)
37 ;;;     ("quux" :both)))
38 ;;; Means that src/foo.lisp and src/bar/quux.lisp need to be compiled in the
39 ;;; target, and that src/bar/baz.lisp and src/bar/quux.lisp need to be
40 ;;; compiled in the host
41 (defvar *source*
42   '(("boot"          :target)
43     ("compat"        :host)
44     ("utils"         :both)
45     ("numbers"       :target)
46     ("char"          :target)
47     ("list"          :target)
48     ("array"         :target)
49     ("string"        :target)
50     ("sequence"      :target)
51     ("stream"        :target)
52     ("print"         :target)
53     ("documentation" :target)
54     ("misc"          :target)
55     ("ffi"           :target)
56     ("package"       :target)
57
58     ("read"          :both)
59     ("defstruct"     :both)
60     ("lambda-list"   :both)
61     ("backquote"     :both)
62     ("compiler"
63      ("codegen"      :both)
64      ("compiler"     :both))
65     ("toplevel"      :target)))
66
67 (defun get-files (file-list type dir)
68   "Traverse FILE-LIST and retrieve a list of the files within which match
69    either TYPE or :BOTH, processing subdirectories."
70   (let ((file (car file-list)))
71     (cond
72       ((null file-list)
73        ())
74       ((listp (cadr file))
75        (append
76          (get-files (cdr file)      type (append dir (list (car file))))
77          (get-files (cdr file-list) type dir)))
78       ((member (cadr file) (list type :both))
79        (cons (source-pathname (car file) :directory dir :type "lisp")
80              (get-files (cdr file-list) type dir)))
81       (t
82        (get-files (cdr file-list) type dir)))))
83
84 (defmacro do-source (name type &body body)
85   "Iterate over all the source files that need to be compiled in the host or
86    the target, depending on the TYPE argument."
87   (unless (member type '(:host :target))
88     (error "TYPE must be one of :HOST or :TARGET, not ~S" type))
89   `(dolist (,name (get-files *source* ,type '(:relative "src")))
90      ,@body))
91
92 (defun source-pathname (filename &key (directory '(:relative "src")) (type nil) (defaults filename))
93   (merge-pathnames
94    (if type
95        (make-pathname :type type :directory directory :defaults defaults)
96        (make-pathname            :directory directory :defaults defaults))
97    *base-directory*))
98
99 ;;; Compile jscl into the host
100 (with-compilation-unit ()
101   (do-source input :host
102     (multiple-value-bind (fasl warn fail) (compile-file input)
103       (declare (ignore fasl warn))
104       (when fail
105         (error "Compilation of ~A failed." input)))))
106
107 ;;; Load jscl into the host
108 (do-source input :host
109   (load input))
110
111 (defun read-whole-file (filename)
112   (with-open-file (in filename)
113     (let ((seq (make-array (file-length in) :element-type 'character)))
114       (read-sequence seq in)
115       seq)))
116
117 (defun !compile-file (filename out &key print)
118   (let ((*compiling-file* t)
119         (*compile-print-toplevels* print))
120     (let* ((source (read-whole-file filename))
121            (in (make-string-stream source)))
122       (format t "Compiling ~a...~%" (enough-namestring filename))
123       (loop
124          with eof-mark = (gensym)
125          for x = (ls-read in nil eof-mark)
126          until (eq x eof-mark)
127          do (let ((compilation (compile-toplevel x)))
128               (when (plusp (length compilation))
129                 (write-string compilation out)))))))
130
131 (defun dump-global-environment (stream)
132   (flet ((late-compile (form)
133            (let ((*standard-output* stream))
134              (write-string (compile-toplevel form)))))
135     ;; We assume that environments have a friendly list representation
136     ;; for the compiler and it can be dumped.
137     (dolist (b (lexenv-function *environment*))
138       (when (eq (binding-type b) 'macro)
139         (setf (binding-value b) `(,*magic-unquote-marker* ,(binding-value b)))))
140     (late-compile `(setq *environment* ',*environment*))
141     ;; Set some counter variable properly, so user compiled code will
142     ;; not collide with the compiler itself.
143     (late-compile
144      `(progn
145         (progn ,@(mapcar (lambda (s) `(%intern-symbol (%js-vref ,(string (cdr s)))))
146                          (remove-if-not #'symbolp *literal-table* :key #'car)))
147         (setq *literal-table* ',*literal-table*)
148         (setq *variable-counter* ,*variable-counter*)
149         (setq *gensym-counter* ,*gensym-counter*)))
150     (late-compile `(setq *literal-counter* ,*literal-counter*))))
151
152
153 (defun bootstrap ()
154   (let ((*features* (cons :jscl *features*))
155         (*package* (find-package "JSCL")))
156     (setq *environment* (make-lexenv))
157     (setq *literal-table* nil)
158     (setq *variable-counter* 0
159           *gensym-counter* 0
160           *literal-counter* 0)
161     (with-open-file (out (merge-pathnames "jscl.js" *base-directory*) :direction :output :if-exists :supersede)
162       (write-string (read-whole-file (source-pathname "prelude.js")) out)
163       (do-source input :target
164         (!compile-file input out))
165       (dump-global-environment out))
166     ;; Tests
167     (with-open-file (out (merge-pathnames "tests.js" *base-directory*) :direction :output :if-exists :supersede)
168       (dolist (input (append (directory "tests.lisp")
169                              (directory "tests/*.lisp")
170                              (directory "tests-report.lisp")))
171         (!compile-file input out)))))
172
173
174 ;;; Run the tests in the host Lisp implementation. It is a quick way
175 ;;; to improve the level of trust of the tests.
176 (defun run-tests-in-host ()
177   (let ((*package* (find-package "JSCL")))
178     (load "tests.lisp")
179     (let ((*use-html-output-p* nil))
180       (declare (special *use-html-output-p*))
181       (dolist (input (directory "tests/*.lisp"))
182         (load input)))
183     (load "tests-report.lisp")))