0.6.12.4:
[sbcl.git] / src / cold / shared.lisp
1 ;;;; stuff which is not specific to any particular build phase, but
2 ;;;; used by most of them
3 ;;;;
4 ;;;; Note: It's specifically not used when bootstrapping PCL, because
5 ;;;; we do SAVE-LISP after that, and we don't want to save extraneous
6 ;;;; bootstrapping machinery into the frozen image which will
7 ;;;; subsequently be used as the mother of all Lisp sessions.
8
9 ;;;; This software is part of the SBCL system. See the README file for
10 ;;;; more information.
11 ;;;;
12 ;;;; This software is derived from the CMU CL system, which was
13 ;;;; written at Carnegie Mellon University and released into the
14 ;;;; public domain. The software is in the public domain and is
15 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
16 ;;;; files for more information.
17
18 ;;; GC tuning has little effect on the x86 due to the generational
19 ;;; collector.  For the older stop & copy collector, it assuredly
20 ;;; does.  GC time is proportional to the amount of non-grabage
21 ;;; needing collection and copying; when the application involved is
22 ;;; the SBCL compiler, it doesn't take any longer to collect 20Mb than
23 ;;; 2              -dan, 20000819
24
25 #+sbcl
26 (progn
27   (sb-ext:gc-off)
28   (setf sb-KERNEL::*bytes-consed-between-gcs* (* 20 (expt 10 6)))
29   (sb-ext:gc-on)
30   (sb-ext:gc))
31
32 ;;; FIXME: I'm now inclined to make all the bootstrap stuff run in CL-USER
33 ;;; instead of SB-COLD. If I do so, I should first take care to
34 ;;; UNINTERN any old stuff in CL-USER, since ANSI says (11.1.2.2, "The
35 ;;; COMMON-LISP-USER Package") that CL-USER can have arbitrary symbols in
36 ;;; it. (And of course I should set the USE list to only CL.)
37 (defpackage "SB-COLD" (:use "CL"))
38 (in-package "SB-COLD")
39
40 ;;; prefixes for filename stems when cross-compiling. These are quite arbitrary
41 ;;; (although of course they shouldn't collide with anything we don't want to
42 ;;; write over). In particular, they can be either relative path names (e.g.
43 ;;; "host-objects/" or absolute pathnames (e.g. "/tmp/sbcl-xc-host-objects/").
44 ;;;
45 ;;; The cross-compilation process will force the creation of these directories
46 ;;; by executing CL:ENSURE-DIRECTORIES-EXIST (on the host Common Lisp).
47 (defvar *host-obj-prefix*)
48 (defvar *target-obj-prefix*)
49
50 ;;; suffixes for filename stems when cross-compiling. Everything should work
51 ;;; fine for any arbitrary string values here. With more work maybe we
52 ;;; could cause these automatically to become the traditional extensions for
53 ;;; whatever host and target architectures (e.g. ".x86f" or ".axpf") we're
54 ;;; currently doing. That would make it easier for a human looking at the
55 ;;; temporary files to figure out what they're for, but it's not necessary for
56 ;;; the compilation process to work, so we haven't bothered.
57 (defvar *host-obj-suffix* ".lisp-obj")
58 (defvar *target-obj-suffix* ".lisp-obj")
59
60 ;;; a function of one functional argument, which calls its functional argument
61 ;;; in an environment suitable for compiling the target. (This environment
62 ;;; includes e.g. a suitable *FEATURES* value.)
63 (defvar *in-target-compilation-mode-fn*)
64
65 ;;; designator for a function with the same calling convention as
66 ;;; CL:COMPILE-FILE, to be used to translate ordinary Lisp source files into
67 ;;; target object files
68 (defvar *target-compile-file*)
69
70 ;;; designator for a function with the same calling convention as
71 ;;; SB-C:ASSEMBLE-FILE, to be used to translate assembly files into target
72 ;;; object files
73 (defvar *target-assemble-file*)
74 \f
75 ;;;; some tools
76
77 ;;; Take the file named X and make it into a file named Y. Sorta like
78 ;;; UNIX, and unlike Common Lisp's bare RENAME-FILE, we don't allow
79 ;;; information from the original filename to influence the final
80 ;;; filename. (The reason that it's only sorta like UNIX is that in
81 ;;; UNIX "mv foo bar/" will work, but the analogous
82 ;;; (RENAME-FILE-A-LA-UNIX "foo" "bar/") should fail.)
83 ;;;
84 ;;; (This is a workaround for the weird behavior of Debian CMU CL
85 ;;; 2.4.6, where (RENAME-FILE "dir/x" "dir/y") tries to create a file
86 ;;; called "dir/dir/y". If that behavior goes away, then we should be
87 ;;; able to get rid of this function and use plain RENAME-FILE in the
88 ;;; COMPILE-STEM function above. -- WHN 19990321
89 (defun rename-file-a-la-unix (x y)
90   (rename-file x
91                ;; (Note that the TRUENAME expression here is lifted from an
92                ;; example in the ANSI spec for TRUENAME.)
93                (with-open-file (stream y :direction :output)
94                  (close stream)
95                  ;; From the ANSI spec: "In this case, the file is closed
96                  ;; when the truename is tried, so the truename
97                  ;; information is reliable."
98                  (truename stream))))
99 (compile 'rename-file-a-la-unix)
100
101 ;;; a wrapper for compilation/assembly, used mostly to centralize
102 ;;; the procedure for finding full filenames from "stems"
103 ;;;
104 ;;; Compile the source file whose basic name is STEM, using some
105 ;;; standard-for-the-SBCL-build-process procedures to generate the full
106 ;;; pathnames of source file and object file. Return the pathname of the object
107 ;;; file for STEM. Several &KEY arguments are accepted:
108 ;;;   :SRC-PREFIX, :SRC-SUFFIX =
109 ;;;      strings to be concatenated to STEM to produce source filename
110 ;;;   :OBJ-PREFIX, :OBJ-SUFFIX =
111 ;;;      strings to be concatenated to STEM to produce object filename
112 ;;;   :TMP-OBJ-SUFFIX-SUFFIX =
113 ;;;      string to be appended to the name of an object file to produce 
114 ;;;      the name of a temporary object file
115 ;;;   :COMPILE-FILE, :IGNORE-FAILURE-P =
116 ;;;     :COMPILE-FILE is a function to use for compiling the file (with the
117 ;;;     same calling conventions as ANSI CL:COMPILE-FILE). If the third
118 ;;;     return value (FAILURE-P) of this function is true, a continuable
119 ;;;     error will be signalled, unless :IGNORE-FAILURE-P is set, in which
120 ;;;     case only a warning will be signalled.
121 (defun compile-stem (stem
122                      &key
123                      (obj-prefix "")
124                      (obj-suffix (error "missing OBJ-SUFFIX"))
125                      (tmp-obj-suffix-suffix "-tmp")
126                      (src-prefix "")
127                      (src-suffix ".lisp")
128                      (compile-file #'compile-file)
129                      ignore-failure-p)
130
131  (let* (;; KLUDGE: Note that this CONCATENATE 'STRING stuff is not The Common
132         ;; Lisp Way, although it works just fine for common UNIX environments.
133         ;; Should it come to pass that the system is ported to environments
134         ;; where version numbers and so forth become an issue, it might become
135         ;; urgent to rewrite this using the fancy Common Lisp PATHNAME
136         ;; machinery instead of just using strings. In the absence of such a
137         ;; port, it might or might be a good idea to do the rewrite.
138         ;; -- WHN 19990815
139         (src (concatenate 'string src-prefix stem src-suffix))
140         (obj (concatenate 'string obj-prefix stem obj-suffix))
141         (tmp-obj (concatenate 'string obj tmp-obj-suffix-suffix)))
142
143    (ensure-directories-exist obj :verbose t)
144
145    ;; We're about to set about building a new object file. First, we
146    ;; delete any preexisting object file in order to avoid confusing
147    ;; ourselves later should we happen to bail out of compilation with an
148    ;; error.
149    (when (probe-file obj)
150      (delete-file obj))
151
152    ;; Work around a bug in CLISP 1999-01-08 #'COMPILE-FILE: CLISP mangles
153    ;; relative pathnames passed as :OUTPUT-FILE arguments, but works OK
154    ;; with absolute pathnames.
155    #+clisp
156    (setf tmp-obj
157          ;; (Note that this idiom is taken from the ANSI documentation
158          ;; for TRUENAME.)
159          (with-open-file (stream tmp-obj :direction :output)
160            (close stream)
161            (truename stream)))
162
163    ;; Try to use the compiler to generate a new temporary object file.
164    (multiple-value-bind (output-truename warnings-p failure-p)
165        (funcall compile-file src :output-file tmp-obj)
166      (declare (ignore warnings-p))
167      (cond ((not output-truename)
168             (error "couldn't compile ~S" src))
169            (failure-p
170             (if ignore-failure-p
171                 (warn "ignoring FAILURE-P return value from compilation of ~S"
172                       src)
173                 (unwind-protect
174                     (progn
175                       ;; FIXME: This should have another option, redoing
176                       ;; compilation.
177                       (cerror "Continue, using possibly-bogus ~S."
178                               "FAILURE-P was set when creating ~S."
179                               obj)
180                       (setf failure-p nil))
181                   ;; Don't leave failed object files lying around.
182                   (when (and failure-p (probe-file tmp-obj))
183                     (delete-file tmp-obj)
184                     (format t "~&deleted ~S~%" tmp-obj)))))
185            ;; Otherwise: success, just fall through.
186            (t nil)))
187
188    ;; If we get to here, compilation succeeded, so it's OK to rename the
189    ;; temporary output file to the permanent object file.
190    (rename-file-a-la-unix tmp-obj obj)
191
192    ;; nice friendly traditional return value
193    (pathname obj)))
194 (compile 'compile-stem)
195
196 ;;; other miscellaneous tools
197 (load "src/cold/read-from-file.lisp")
198 (load "src/cold/rename-package-carefully.lisp")
199 (load "src/cold/with-stuff.lisp")
200
201 ;;; Try to minimize/conceal any non-standardness of the host Common Lisp.
202 (load "src/cold/ansify.lisp")
203 \f
204 ;;;; special read-macros for building the cold system (and even for
205 ;;;; building some of our tools for building the cold system)
206
207 (load "src/cold/shebang.lisp")
208
209 ;;; When cross-compiling, the *FEATURES* set for the target Lisp is
210 ;;; not in general the same as the *FEATURES* set for the host Lisp.
211 ;;; In order to refer to target features specifically, we refer to
212 ;;; *SHEBANG-FEATURES* instead of *FEATURES*, and use the #!+ and #!-
213 ;;; readmacros instead of the ordinary #+ and #- readmacros.
214 (setf *shebang-features*
215       (let* ((default-features
216                (append (read-from-file "base-target-features.lisp-expr")
217                        (read-from-file "local-target-features.lisp-expr")))
218              (customizer-file-name "customize-target-features.lisp")
219              (customizer (if (probe-file customizer-file-name)
220                              (compile nil 
221                                       (read-from-file customizer-file-name))
222                              #'identity)))
223         (funcall customizer default-features)))
224 (let ((*print-length* nil)
225       (*print-level* nil))
226   (format t
227           "target features *SHEBANG-FEATURES*=~@<~S~:>~%"
228           *shebang-features*))
229 \f
230 ;;;; cold-init-related PACKAGE and SYMBOL tools
231
232 ;;; Once we're done with possibly ANSIfying the COMMON-LISP package,
233 ;;; it's probably a mistake if we change it (beyond changing the
234 ;;; values of special variables such as *** and +, anyway). Set up
235 ;;; machinery to warn us when/if we change it.
236 ;;;
237 ;;; FIXME: All this machinery should probably be conditional on
238 ;;; #!+SB-SHOW, i.e. we should be able to wrap #!+SB-SHOW around both
239 ;;; the LOAD and the DEFVAR here. 
240 (load "src/cold/snapshot.lisp")
241 (defvar *cl-snapshot* (take-snapshot "COMMON-LISP"))
242 \f
243 ;;;; master list of source files and their properties
244
245 ;;; flags which can be used to describe properties of source files
246 (defparameter
247   *expected-stem-flags*
248   '(;; meaning: This file is not to be compiled when building the
249     ;; cross-compiler which runs on the host ANSI Lisp.
250     :not-host
251     ;; meaning: This file is not to be compiled as part of the target
252     ;; SBCL.
253     :not-target
254     ;; meaning: This file is to be processed with the SBCL assembler,
255     ;; not COMPILE-FILE. (Note that this doesn't make sense unless
256     ;; :NOT-HOST is also set, since the SBCL assembler doesn't exist
257     ;; while the cross-compiler is being built in the host ANSI Lisp.)
258     :assem
259     ;; meaning: The #'COMPILE-STEM argument called :IGNORE-FAILURE-P
260     ;; should be true. (This is a KLUDGE: I'd like to get rid of it.
261     ;; For now, it exists so that compilation can proceed through the
262     ;; legacy warnings in src/compiler/x86/array.lisp, which I've
263     ;; never figured out but which were apparently acceptable in CMU
264     ;; CL. Eventually, it would be great to just get rid of all
265     ;; warnings and remove support for this flag. -- WHN 19990323)
266     :ignore-failure-p))
267
268 (defparameter *stems-and-flags* (read-from-file "stems-and-flags.lisp-expr"))
269
270 (defmacro for-stems-and-flags ((stem flags) &body body)
271   (let ((stem-and-flags (gensym "STEM-AND-FLAGS-")))
272     `(dolist (,stem-and-flags *stems-and-flags*)
273        (let ((,stem (first ,stem-and-flags))
274              (,flags (rest ,stem-and-flags)))
275          ,@body))))
276
277 ;;; Check for stupid typos in FLAGS list keywords.
278 (let ((stems (make-hash-table :test 'equal)))
279   (for-stems-and-flags (stem flags)
280     (if (gethash stem stems)
281       (error "duplicate stem ~S in stems-and-flags data" stem)
282       (setf (gethash stem stems) t))
283     (let ((set-difference (set-difference flags *expected-stem-flags*)))
284       (when set-difference
285         (error "found unexpected flag(s) in *STEMS-AND-FLAGS*: ~S"
286                set-difference)))))
287 \f
288 ;;;; tools to compile SBCL sources to create the cross-compiler
289
290 ;;; Execute function FN in an environment appropriate for compiling the
291 ;;; cross-compiler's source code in the cross-compilation host.
292 (defun in-host-compilation-mode (fn)
293   (let ((*features* (cons :sb-xc-host *features*))
294         ;; the CROSS-FLOAT-INFINITY-KLUDGE, as documented in
295         ;; base-target-features.lisp-expr:
296         (*shebang-features* (set-difference *shebang-features*
297                                             '(:sb-propagate-float-type
298                                               :sb-propagate-fun-type))))
299     (with-additional-nickname ("SB-XC" "SB!XC")
300       (funcall fn))))
301 ;;; FIXME: This COMPILE caused problems in sbcl-0.6.11.26. (bug 93)
302 ;;;(compile 'in-host-compilation-mode)
303
304 ;;; Process a file as source code for the cross-compiler, compiling it
305 ;;; (if necessary) in the appropriate environment, then loading it
306 ;;; into the cross-compilation host Common lisp.
307 (defun host-cload-stem (stem &key ignore-failure-p)
308   (load (in-host-compilation-mode
309           (lambda ()
310             (compile-stem stem
311                           :obj-prefix *host-obj-prefix*
312                           :obj-suffix *host-obj-suffix*
313                           :compile-file #'cl:compile-file
314                           :ignore-failure-p ignore-failure-p)))))
315 (compile 'host-cload-stem)
316
317 ;;; Like HOST-CLOAD-STEM, except that we don't bother to compile.
318 (defun host-load-stem (stem &key ignore-failure-p)
319   (declare (ignore ignore-failure-p)) ; (It's only relevant when
320   ;; compiling.) KLUDGE: It's untidy to have the knowledge of how to
321   ;; construct complete filenames from stems in here as well as in
322   ;; COMPILE-STEM. It should probably be factored out somehow. -- WHN
323   ;; 19990815
324   (load (concatenate 'simple-string *host-obj-prefix* stem *host-obj-suffix*)))
325 (compile 'host-load-stem)
326 \f
327 ;;;; tools to compile SBCL sources to create object files which will
328 ;;;; be used to create the target SBCL .core file
329
330 ;;; Run the cross-compiler on a file in the source directory tree to
331 ;;; produce a corresponding file in the target object directory tree.
332 (defun target-compile-stem (stem &key assem-p ignore-failure-p)
333   (funcall *in-target-compilation-mode-fn*
334            (lambda ()
335              (compile-stem stem
336                            :obj-prefix *target-obj-prefix*
337                            :obj-suffix *target-obj-suffix*
338                            :ignore-failure-p ignore-failure-p
339                            :compile-file (if assem-p
340                                              *target-assemble-file*
341                                              *target-compile-file*)))))
342 (compile 'target-compile-stem)
343
344 ;;; (This function is not used by the build process, but is intended
345 ;;; for interactive use when experimenting with the system. It runs
346 ;;; the cross-compiler on test files with arbitrary filenames, not
347 ;;; necessarily in the source tree, e.g. in "/tmp".)
348 (defun target-compile-file (filename)
349   (funcall *in-target-compilation-mode-fn*
350            (lambda ()
351              (funcall *target-compile-file* filename))))
352 (compile 'target-compile-file)