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