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