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