8fc7be03ce9d2822c3cdb0615cb36986e068657d
[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 ;;;; $Header$
19
20 ;;; TO DO: Might it be possible to increase the efficiency of CMU CL's garbage
21 ;;; collection on my large (256Mb) machine by doing larger incremental GC steps
22 ;;; than the default 2 Mb of CMU CL 2.4.9? A quick test 19990729, setting this
23 ;;; to 5E6 showed no significant improvement, but it's possible that more
24 ;;; cleverness might help..
25 ;#+cmu (setf ext:*bytes-consed-between-gcs* (* 5 (expt 10 6)))
26
27 ;;; FIXME: I'm now inclined to make all the bootstrap stuff run in CL-USER
28 ;;; instead of SB-COLD. If I do so, I should first take care to
29 ;;; UNINTERN any old stuff in CL-USER, since ANSI says (11.1.2.2, "The
30 ;;; COMMON-LISP-USER Package") that CL-USER can have arbitrary symbols in
31 ;;; it. (And of course I should set the USE list to only CL.)
32 (defpackage "SB-COLD" (:use "CL"))
33 (in-package "SB-COLD")
34
35 ;;; prefix for source filename stems when cross-compiling
36 (defvar *src-prefix* "src/")
37 ;;; (We don't bother to specify the source suffix here because ".lisp" is such
38 ;;; a good default value that we never have to specify it explicitly.)
39
40 ;;; prefixes for filename stems when cross-compiling. These are quite arbitrary
41 ;;; (although of course they shouldn't collide with anything we don't want to
42 ;;; write over). In particular, they can be either relative path names (e.g.
43 ;;; "host-objects/" or absolute pathnames (e.g. "/tmp/sbcl-xc-host-objects/").
44 ;;;
45 ;;; The cross-compilation process will force the creation of these directories
46 ;;; by executing CL:ENSURE-DIRECTORIES-EXIST (on the host Common Lisp).
47 (defvar *host-obj-prefix*)
48 (defvar *target-obj-prefix*)
49
50 ;;; suffixes for filename stems when cross-compiling. Everything should work
51 ;;; fine for any arbitrary string values here. With more work maybe we
52 ;;; could cause these automatically to become the traditional extensions for
53 ;;; whatever host and target architectures (e.g. ".x86f" or ".axpf") we're
54 ;;; currently doing. That would make it easier for a human looking at the
55 ;;; temporary files to figure out what they're for, but it's not necessary for
56 ;;; the compilation process to work, so we haven't bothered.
57 (defvar *host-obj-suffix* ".lisp-obj")
58 (defvar *target-obj-suffix* ".lisp-obj")
59
60 ;;; a function of one functional argument, which calls its functional argument
61 ;;; in an environment suitable for compiling the target. (This environment
62 ;;; includes e.g. a suitable *FEATURES* value.)
63 (defvar *in-target-compilation-mode-fn*)
64
65 ;;; designator for a function with the same calling convention as
66 ;;; CL:COMPILE-FILE, to be used to translate ordinary Lisp source files into
67 ;;; target object files
68 (defvar *target-compile-file*)
69
70 ;;; designator for a function with the same calling convention as
71 ;;; SB-C:ASSEMBLE-FILE, to be used to translate assembly files into target
72 ;;; object files
73 (defvar *target-assemble-file*)
74 \f
75 ;;;; some tools
76
77 ;;; Take the file named X and make it into a file named Y. Sorta like UNIX, and
78 ;;; unlike Common Lisp's bare RENAME-FILE, we don't allow information
79 ;;; from the original filename to influence the final filename. (The reason
80 ;;; that it's only sorta like UNIX is that in UNIX "mv foo bar/" will work,
81 ;;; but the analogous (RENAME-FILE-A-LA-UNIX "foo" "bar/") should fail.)
82 ;;;
83 ;;; (This is a workaround for the weird behavior of Debian CMU CL 2.4.6, where
84 ;;; (RENAME-FILE "dir/x" "dir/y") tries to create a file called "dir/dir/y".
85 ;;; If that behavior goes away, then we should be able to get rid of this
86 ;;; function and use plain RENAME-FILE in the COMPILE-STEM function
87 ;;; above. -- WHN 19990321
88 (defun rename-file-a-la-unix (x y)
89   (rename-file x
90                ;; (Note that the TRUENAME expression here is lifted from an
91                ;; example in the ANSI spec for TRUENAME.)
92                (with-open-file (stream y :direction :output)
93                  (close stream)
94                  ;; From the ANSI spec: "In this case, the file is closed
95                  ;; when the truename is tried, so the truename
96                  ;; information is reliable."
97                  (truename stream))))
98 (compile 'rename-file-a-la-unix)
99
100 ;;; a wrapper for compilation/assembly, used mostly to centralize
101 ;;; the procedure for finding full filenames from "stems"
102 ;;;
103 ;;; Compile the source file whose basic name is STEM, using some
104 ;;; standard-for-the-SBCL-build-process procedures to generate the full
105 ;;; pathnames of source file and object file. Return the pathname of the object
106 ;;; file for STEM. Several keyword arguments are accepted:
107 ;;;   SRC-PREFIX, SRC-SUFFIX =
108 ;;; strings to be concatenated to STEM to produce source filename
109 ;;;   OBJ-PREFIX, OBJ-SUFFIX =
110 ;;; strings to be concatenated to STEM to produce object filename
111 ;;;   TMP-OBJ-SUFFIX-SUFFIX
112 ;;; string to be appended to the name of an object file to produce the
113 ;;; name of a temporary object file
114 ;;;   COMPILE-FILE, IGNORE-FAILURE-P =
115 ;;; COMPILE-FILE is a function to use for compiling the file (with the
116 ;;; same calling conventions as ANSI CL:COMPILE-FILE). If the third
117 ;;; return value (FAILURE-P) of this function is true, a continuable
118 ;;; error will be signalled, unless IGNORE-FAILURE-P is set, in which
119 ;;; case only a warning will be signalled.
120 (defun compile-stem (stem
121                      &key
122                      (obj-prefix "")
123                      (obj-suffix (error "missing OBJ-SUFFIX"))
124                      (tmp-obj-suffix-suffix "-tmp")
125                      (src-prefix "")
126                      (src-suffix ".lisp")
127                      (compile-file #'compile-file)
128                      ignore-failure-p)
129
130  (let* (;; KLUDGE: Note that this CONCATENATE 'STRING stuff is not The Common
131         ;; Lisp Way, although it works just fine for common UNIX environments.
132         ;; Should it come to pass that the system is ported to environments
133         ;; where version numbers and so forth become an issue, it might become
134         ;; urgent to rewrite this using the fancy Common Lisp PATHNAME
135         ;; machinery instead of just using strings. In the absence of such a
136         ;; port, it might or might be a good idea to do the rewrite.
137         ;; -- WHN 19990815
138         (src (concatenate 'string src-prefix stem src-suffix))
139         (obj (concatenate 'string obj-prefix stem obj-suffix))
140         (tmp-obj (concatenate 'string obj tmp-obj-suffix-suffix)))
141
142    (ensure-directories-exist obj :verbose t)
143
144    ;; We're about to set about building a new object file. First, we
145    ;; delete any preexisting object file in order to avoid confusing
146    ;; ourselves later should we happen to bail out of compilation with an
147    ;; error.
148    (when (probe-file obj)
149      (delete-file obj))
150
151    ;; Work around a bug in CLISP 1999-01-08 #'COMPILE-FILE: CLISP mangles
152    ;; relative pathnames passed as :OUTPUT-FILE arguments, but works OK
153    ;; with absolute pathnames.
154    #+clisp
155    (setf tmp-obj
156          ;; (Note that this idiom is taken from the ANSI documentation
157          ;; for TRUENAME.)
158          (with-open-file (stream tmp-obj :direction :output)
159            (close stream)
160            (truename stream)))
161
162    ;; Try to use the compiler to generate a new temporary object file.
163    (multiple-value-bind (output-truename warnings-p failure-p)
164        (funcall compile-file src :output-file tmp-obj)
165      (declare (ignore warnings-p))
166      (cond ((not output-truename)
167             (error "couldn't compile ~S" src))
168            (failure-p
169             (if ignore-failure-p
170                 (warn "ignoring FAILURE-P return value from compilation of ~S"
171                       src)
172                 (unwind-protect
173                     (progn
174                       ;; FIXME: This should have another option, redoing
175                       ;; compilation.
176                       (cerror "Continue, using possibly-bogus ~S."
177                               "FAILURE-P was set when creating ~S."
178                               obj)
179                       (setf failure-p nil))
180                   ;; Don't leave failed object files lying around.
181                   (when (and failure-p (probe-file tmp-obj))
182                     (delete-file tmp-obj)
183                     (format t "~&deleted ~S~%" tmp-obj)))))
184            ;; Otherwise: success, just fall through.
185            (t nil)))
186
187    ;; If we get to here, compilation succeeded, so it's OK to rename the
188    ;; temporary output file to the permanent object file.
189    (rename-file-a-la-unix tmp-obj obj)
190
191    ;; nice friendly traditional return value
192    (pathname obj)))
193 (compile 'compile-stem)
194
195 ;;; basic tool for building other tools
196 #+nil
197 (defun tool-cload-stem (stem)
198   (load (compile-stem stem
199                       :src-prefix *src-prefix*
200                       :obj-prefix *host-obj-prefix*
201                       :obj-suffix *host-obj-suffix*
202                       :compile-file #'compile-file))
203   (values))
204 #+nil (compile 'tool-cload-stem)
205
206 ;;; other miscellaneous tools
207 (load "src/cold/read-from-file.lisp")
208 (load "src/cold/rename-package-carefully.lisp")
209 (load "src/cold/with-stuff.lisp")
210
211 ;;; Try to minimize/conceal any non-standardness of the host Common Lisp.
212 (load "src/cold/ansify.lisp")
213 \f
214 ;;;; special read-macros for building the cold system (and even for
215 ;;;; building some of our tools for building the cold system)
216
217 (load "src/cold/shebang.lisp")
218
219 ;;; When cross-compiling, the *FEATURES* set for the target Lisp is
220 ;;; not in general the same as the *FEATURES* set for the host Lisp.
221 ;;; In order to refer to target features specifically, we refer to
222 ;;; *SHEBANG-FEATURES* instead of *FEATURES*, and use the #!+ and #!-
223 ;;; readmacros instead of the ordinary #+ and #- readmacros.
224 (setf *shebang-features*
225       (append (read-from-file "base-target-features.lisp-expr")
226               (read-from-file "local-target-features.lisp-expr")))
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)