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