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