0.pre7.14.flaky4.3:
[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   (let* (;; KLUDGE: Note that this CONCATENATE 'STRING stuff is not The Common
140         ;; Lisp Way, although it works just fine for common UNIX environments.
141         ;; Should it come to pass that the system is ported to environments
142         ;; where version numbers and so forth become an issue, it might become
143         ;; urgent to rewrite this using the fancy Common Lisp PATHNAME
144         ;; machinery instead of just using strings. In the absence of such a
145         ;; port, it might or might be a good idea to do the rewrite.
146         ;; -- WHN 19990815
147         (src (concatenate 'string src-prefix stem src-suffix))
148         (obj (concatenate 'string obj-prefix stem obj-suffix))
149         (tmp-obj (concatenate 'string obj tmp-obj-suffix-suffix)))
150
151     (ensure-directories-exist obj :verbose t)
152
153     ;; We're about to set about building a new object file. First, we
154     ;; delete any preexisting object file in order to avoid confusing
155     ;; ourselves later should we happen to bail out of compilation
156     ;; with an error.
157     (when (probe-file obj)
158       (delete-file obj))
159
160     ;; Work around a bug in CLISP 1999-01-08 #'COMPILE-FILE: CLISP
161     ;; mangles relative pathnames passed as :OUTPUT-FILE arguments,
162     ;; but works OK with absolute pathnames.
163     #+clisp
164     (setf tmp-obj
165           ;; (Note that this idiom is taken from the ANSI
166           ;; documentation for TRUENAME.)
167           (with-open-file (stream tmp-obj :direction :output)
168             (close stream)
169             (truename stream)))
170
171     ;; Try to use the compiler to generate a new temporary object file.
172     (multiple-value-bind (output-truename warnings-p failure-p)
173         (funcall compile-file src :output-file tmp-obj)
174       (declare (ignore warnings-p))
175       (cond ((not output-truename)
176              (error "couldn't compile ~S" src))
177             (failure-p
178              (if ignore-failure-p
179                  (warn "ignoring FAILURE-P return value from compilation of ~S"
180                        src)
181                  (unwind-protect
182                      (progn
183                        ;; FIXME: This should have another option,
184                        ;; redoing compilation.
185                        (cerror "Continue, using possibly-bogus ~S."
186                                "FAILURE-P was set when creating ~S."
187                                obj)
188                        (setf failure-p nil))
189                    ;; Don't leave failed object files lying around.
190                    (when (and failure-p (probe-file tmp-obj))
191                      (delete-file tmp-obj)
192                      (format t "~&deleted ~S~%" tmp-obj)))))
193             ;; Otherwise: success, just fall through.
194             (t nil)))
195
196     ;; If we get to here, compilation succeeded, so it's OK to rename
197     ;; the temporary output file to the permanent object file.
198     (rename-file-a-la-unix tmp-obj obj)
199
200     ;; nice friendly traditional return value
201     (pathname obj)))
202 (compile 'compile-stem)
203
204 ;;; other miscellaneous tools
205 (load "src/cold/read-from-file.lisp")
206 (load "src/cold/rename-package-carefully.lisp")
207 (load "src/cold/with-stuff.lisp")
208
209 ;;; Try to minimize/conceal any non-standardness of the host Common Lisp.
210 (load "src/cold/ansify.lisp")
211 \f
212 ;;;; special read-macros for building the cold system (and even for
213 ;;;; building some of our tools for building the cold system)
214
215 (load "src/cold/shebang.lisp")
216
217 ;;; When cross-compiling, the *FEATURES* set for the target Lisp is
218 ;;; not in general the same as the *FEATURES* set for the host Lisp.
219 ;;; In order to refer to target features specifically, we refer to
220 ;;; *SHEBANG-FEATURES* instead of *FEATURES*, and use the #!+ and #!-
221 ;;; readmacros instead of the ordinary #+ and #- readmacros.
222 (setf *shebang-features*
223       (let* ((default-features
224                (append (read-from-file "base-target-features.lisp-expr")
225                        (read-from-file "local-target-features.lisp-expr")))
226              (customizer-file-name "customize-target-features.lisp")
227              (customizer (if (probe-file customizer-file-name)
228                              (compile nil 
229                                       (read-from-file customizer-file-name))
230                              #'identity)))
231         (funcall customizer default-features)))
232 (let ((*print-length* nil)
233       (*print-level* nil))
234   (format t
235           "target features *SHEBANG-FEATURES*=~@<~S~:>~%"
236           *shebang-features*))
237 \f
238 ;;;; cold-init-related PACKAGE and SYMBOL tools
239
240 ;;; Once we're done with possibly ANSIfying the COMMON-LISP package,
241 ;;; it's probably a mistake if we change it (beyond changing the
242 ;;; values of special variables such as *** and +, anyway). Set up
243 ;;; machinery to warn us when/if we change it.
244 ;;;
245 ;;; FIXME: All this machinery should probably be conditional on
246 ;;; #!+SB-SHOW, i.e. we should be able to wrap #!+SB-SHOW around both
247 ;;; the LOAD and the DEFVAR here. 
248 (load "src/cold/snapshot.lisp")
249 (defvar *cl-snapshot* (take-snapshot "COMMON-LISP"))
250 \f
251 ;;;; master list of source files and their properties
252
253 ;;; flags which can be used to describe properties of source files
254 (defparameter
255   *expected-stem-flags*
256   '(;; meaning: This file is not to be compiled when building the
257     ;; cross-compiler which runs on the host ANSI Lisp.
258     :not-host
259     ;; meaning: This file is not to be compiled as part of the target
260     ;; SBCL.
261     :not-target
262     ;; meaning: This file is to be processed with the SBCL assembler,
263     ;; not COMPILE-FILE. (Note that this doesn't make sense unless
264     ;; :NOT-HOST is also set, since the SBCL assembler doesn't exist
265     ;; while the cross-compiler is being built in the host ANSI Lisp.)
266     :assem
267     ;; meaning: The #'COMPILE-STEM argument called :IGNORE-FAILURE-P
268     ;; should be true. (This is a KLUDGE: I'd like to get rid of it.
269     ;; For now, it exists so that compilation can proceed through the
270     ;; legacy warnings in src/compiler/x86/array.lisp, which I've
271     ;; never figured out but which were apparently acceptable in CMU
272     ;; CL. Eventually, it would be great to just get rid of all
273     ;; warnings and remove support for this flag. -- WHN 19990323)
274     :ignore-failure-p))
275
276 (defparameter *stems-and-flags* (read-from-file "stems-and-flags.lisp-expr"))
277
278 (defmacro do-stems-and-flags ((stem flags) &body body)
279   (let ((stem-and-flags (gensym "STEM-AND-FLAGS-")))
280     `(dolist (,stem-and-flags *stems-and-flags*)
281        (let ((,stem (first ,stem-and-flags))
282              (,flags (rest ,stem-and-flags)))
283          ,@body))))
284
285 ;;; Check for stupid typos in FLAGS list keywords.
286 (let ((stems (make-hash-table :test 'equal)))
287   (do-stems-and-flags (stem flags)
288     (if (gethash stem stems)
289       (error "duplicate stem ~S in stems-and-flags data" stem)
290       (setf (gethash stem stems) t))
291     (let ((set-difference (set-difference flags *expected-stem-flags*)))
292       (when set-difference
293         (error "found unexpected flag(s) in *STEMS-AND-FLAGS*: ~S"
294                set-difference)))))
295 \f
296 ;;;; tools to compile SBCL sources to create the cross-compiler
297
298 ;;; Execute function FN in an environment appropriate for compiling the
299 ;;; cross-compiler's source code in the cross-compilation host.
300 (defun in-host-compilation-mode (fn)
301   (let ((*features* (cons :sb-xc-host *features*))
302         ;; the CROSS-FLOAT-INFINITY-KLUDGE, as documented in
303         ;; base-target-features.lisp-expr:
304         (*shebang-features* (set-difference *shebang-features*
305                                             '(:sb-propagate-float-type
306                                               :sb-propagate-fun-type))))
307     (with-additional-nickname ("SB-XC" "SB!XC")
308       (funcall fn))))
309 (compile 'in-host-compilation-mode)
310
311 ;;; Process a file as source code for the cross-compiler, compiling it
312 ;;; (if necessary) in the appropriate environment, then loading it
313 ;;; into the cross-compilation host Common lisp.
314 (defun host-cload-stem (stem &key ignore-failure-p)
315   (let ((compiled-filename (in-host-compilation-mode
316                             (lambda ()
317                               (compile-stem
318                                stem
319                                :obj-prefix *host-obj-prefix*
320                                :obj-suffix *host-obj-suffix*
321                                :compile-file #'cl:compile-file
322                                :ignore-failure-p ignore-failure-p)))))
323     (load compiled-filename)))
324 (compile 'host-cload-stem)
325
326 ;;; Like HOST-CLOAD-STEM, except that we don't bother to compile.
327 (defun host-load-stem (stem &key ignore-failure-p)
328   (declare (ignore ignore-failure-p)) ; (It's only relevant when
329   ;; compiling.) KLUDGE: It's untidy to have the knowledge of how to
330   ;; construct complete filenames from stems in here as well as in
331   ;; COMPILE-STEM. It should probably be factored out somehow. -- WHN
332   ;; 19990815
333   (load (concatenate 'simple-string *host-obj-prefix* stem *host-obj-suffix*)))
334 (compile 'host-load-stem)
335 \f
336 ;;;; tools to compile SBCL sources to create object files which will
337 ;;;; be used to create the target SBCL .core file
338
339 ;;; Run the cross-compiler on a file in the source directory tree to
340 ;;; produce a corresponding file in the target object directory tree.
341 (defun target-compile-stem (stem &key assem-p ignore-failure-p)
342   (funcall *in-target-compilation-mode-fn*
343            (lambda ()
344              (compile-stem stem
345                            :obj-prefix *target-obj-prefix*
346                            :obj-suffix *target-obj-suffix*
347                            :ignore-failure-p ignore-failure-p
348                            :compile-file (if assem-p
349                                              *target-assemble-file*
350                                              *target-compile-file*)))))
351 (compile 'target-compile-stem)
352
353 ;;; (This function is not used by the build process, but is intended
354 ;;; for interactive use when experimenting with the system. It runs
355 ;;; the cross-compiler on test files with arbitrary filenames, not
356 ;;; necessarily in the source tree, e.g. in "/tmp".)
357 (defun target-compile-file (filename)
358   (funcall *in-target-compilation-mode-fn*
359            (lambda ()
360              (funcall *target-compile-file* filename))))
361 (compile 'target-compile-file)