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