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