1 ;;;; stuff which is not specific to any particular build phase, but
2 ;;;; used by most of them
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.
9 ;;;; This software is part of the SBCL system. See the README file for
10 ;;;; more information.
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.
18 ;;; SB-COLD holds stuff used to build the initial SBCL core file
19 ;;; (including not only the final construction of the core file, but
20 ;;; also the preliminary steps like e.g. building the cross-compiler
21 ;;; and running the cross-compiler to produce target FASL files).
22 (defpackage "SB-COLD" (:use "CL"))
24 (in-package "SB-COLD")
26 ;;; prefixes for filename stems when cross-compiling. These are quite arbitrary
27 ;;; (although of course they shouldn't collide with anything we don't want to
28 ;;; write over). In particular, they can be either relative path names (e.g.
29 ;;; "host-objects/" or absolute pathnames (e.g. "/tmp/sbcl-xc-host-objects/").
31 ;;; The cross-compilation process will force the creation of these directories
32 ;;; by executing CL:ENSURE-DIRECTORIES-EXIST (on the xc host Common Lisp).
33 (defvar *host-obj-prefix*)
34 (defvar *target-obj-prefix*)
36 ;;; suffixes for filename stems when cross-compiling
37 (defvar *host-obj-suffix*
39 ;; On some xc hosts, it's impossible to LOAD a fasl file unless it
40 ;; has the same extension that the host uses for COMPILE-FILE
41 ;; output, so we have to be careful to use the xc host's preferred
44 ;; FIXME: This is a little ugly and annoying to maintain. And
45 ;; there's very likely some way to rearrange the build process so
46 ;; that we never explicitly refer to host object file suffixes,
47 ;; only to the result of CL:COMPILE-FILE-PATHNAME.
48 #+lispworks ".ufsl" ; as per Lieven Marchand sbcl-devel 2002-02-01
50 ;; On most xc hosts, any old extension works, so we use an
53 (defvar *target-obj-suffix*
54 ;; Target fasl files are LOADed (actually only quasi-LOADed, in
55 ;; GENESIS) only by SBCL code, and it doesn't care about particular
56 ;; extensions, so we can use something arbitrary.
59 ;;; a function of one functional argument, which calls its functional argument
60 ;;; in an environment suitable for compiling the target. (This environment
61 ;;; includes e.g. a suitable *FEATURES* value.)
62 (defvar *in-target-compilation-mode-fn*)
64 ;;; designator for a function with the same calling convention as
65 ;;; CL:COMPILE-FILE, to be used to translate ordinary Lisp source files into
66 ;;; target object files
67 (defvar *target-compile-file*)
69 ;;; designator for a function with the same calling convention as
70 ;;; SB-C:ASSEMBLE-FILE, to be used to translate assembly files into target
72 (defvar *target-assemble-file*)
76 ;;; Take the file named X and make it into a file named Y. Sorta like
77 ;;; UNIX, and unlike Common Lisp's bare RENAME-FILE, we don't allow
78 ;;; information from the original filename to influence the final
79 ;;; filename. (The reason that it's only sorta like UNIX is that in
80 ;;; UNIX "mv foo bar/" will work, but the analogous
81 ;;; (RENAME-FILE-A-LA-UNIX "foo" "bar/") should fail.)
83 ;;; (This is a workaround for the weird behavior of Debian CMU CL
84 ;;; 2.4.6, where (RENAME-FILE "dir/x" "dir/y") tries to create a file
85 ;;; called "dir/dir/y". If that behavior goes away, then we should be
86 ;;; able to get rid of this function and use plain RENAME-FILE in the
87 ;;; COMPILE-STEM function above. -- WHN 19990321
88 (defun rename-file-a-la-unix (x y)
90 (let ((path ;; (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)
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."
99 (rename-file x path)))
100 (compile 'rename-file-a-la-unix)
102 ;;; a wrapper for compilation/assembly, used mostly to centralize
103 ;;; the procedure for finding full filenames from "stems"
105 ;;; Compile the source file whose basic name is STEM, using some
106 ;;; standard-for-the-SBCL-build-process procedures to generate the
107 ;;; full pathnames of source file and object file. Return the pathname
108 ;;; of the object file for STEM. Several &KEY arguments are accepted:
109 ;;; :SRC-PREFIX, :SRC-SUFFIX =
110 ;;; strings to be concatenated to STEM to produce source filename
111 ;;; :OBJ-PREFIX, :OBJ-SUFFIX =
112 ;;; strings to be concatenated to STEM to produce object filename
113 ;;; :TMP-OBJ-SUFFIX-SUFFIX =
114 ;;; string to be appended to the name of an object file to produce
115 ;;; the name of a temporary object file
116 ;;; :COMPILE-FILE, :IGNORE-FAILURE-P =
117 ;;; :COMPILE-FILE is a function to use for compiling the file
118 ;;; (with the same calling conventions as ANSI CL:COMPILE-FILE).
119 ;;; If the third return value (FAILURE-P) of this function is
120 ;;; true, a continuable error will be signalled, unless
121 ;;; :IGNORE-FAILURE-P is set, in which case only a warning will be
123 (defun compile-stem (stem
126 (obj-suffix (error "missing OBJ-SUFFIX"))
127 (tmp-obj-suffix-suffix "-tmp")
130 (compile-file #'compile-file)
133 (let* (;; KLUDGE: Note that this CONCATENATE 'STRING stuff is not The Common
134 ;; Lisp Way, although it works just fine for common UNIX environments.
135 ;; Should it come to pass that the system is ported to environments
136 ;; where version numbers and so forth become an issue, it might become
137 ;; urgent to rewrite this using the fancy Common Lisp PATHNAME
138 ;; machinery instead of just using strings. In the absence of such a
139 ;; port, it might or might be a good idea to do the rewrite.
141 (src (concatenate 'string src-prefix stem src-suffix))
142 (obj (concatenate 'string obj-prefix stem obj-suffix))
143 (tmp-obj (concatenate 'string obj tmp-obj-suffix-suffix)))
145 (ensure-directories-exist obj :verbose t)
147 ;; We're about to set about building a new object file. First, we
148 ;; delete any preexisting object file in order to avoid confusing
149 ;; ourselves later should we happen to bail out of compilation
151 (when (probe-file obj)
154 ;; Work around a bug in CLISP 1999-01-08 #'COMPILE-FILE: CLISP
155 ;; mangles relative pathnames passed as :OUTPUT-FILE arguments,
156 ;; but works OK with absolute pathnames.
159 ;; (Note that this idiom is taken from the ANSI
160 ;; documentation for TRUENAME.)
161 (with-open-file (stream tmp-obj :direction :output)
165 ;; Try to use the compiler to generate a new temporary object file.
166 (multiple-value-bind (output-truename warnings-p failure-p)
167 (funcall compile-file src :output-file tmp-obj)
168 (declare (ignore warnings-p))
169 (cond ((not output-truename)
170 (error "couldn't compile ~S" src))
173 (warn "ignoring FAILURE-P return value from compilation of ~S"
177 ;; FIXME: This should have another option,
178 ;; redoing compilation.
179 (cerror "Continue, using possibly-bogus ~S."
180 "FAILURE-P was set when creating ~S."
182 (setf failure-p nil))
183 ;; Don't leave failed object files lying around.
184 (when (and failure-p (probe-file tmp-obj))
185 (delete-file tmp-obj)
186 (format t "~&deleted ~S~%" tmp-obj)))))
187 ;; Otherwise: success, just fall through.
190 ;; If we get to here, compilation succeeded, so it's OK to rename
191 ;; the temporary output file to the permanent object file.
192 (rename-file-a-la-unix tmp-obj obj)
194 ;; nice friendly traditional return value
196 (compile 'compile-stem)
198 ;;; other miscellaneous tools
199 (load "src/cold/read-from-file.lisp")
200 (load "src/cold/rename-package-carefully.lisp")
201 (load "src/cold/with-stuff.lisp")
203 ;;; Try to minimize/conceal any non-standardness of the host Common Lisp.
204 (load "src/cold/ansify.lisp")
206 ;;;; special read-macros for building the cold system (and even for
207 ;;;; building some of our tools for building the cold system)
209 (load "src/cold/shebang.lisp")
211 ;;; When cross-compiling, the *FEATURES* set for the target Lisp is
212 ;;; not in general the same as the *FEATURES* set for the host Lisp.
213 ;;; In order to refer to target features specifically, we refer to
214 ;;; *SHEBANG-FEATURES* instead of *FEATURES*, and use the #!+ and #!-
215 ;;; readmacros instead of the ordinary #+ and #- readmacros.
216 (setf *shebang-features*
217 (let* ((default-features
218 (append (read-from-file "base-target-features.lisp-expr")
219 (read-from-file "local-target-features.lisp-expr")))
220 (customizer-file-name "customize-target-features.lisp")
221 (customizer (if (probe-file customizer-file-name)
223 (read-from-file customizer-file-name))
225 (funcall customizer default-features)))
226 (let ((*print-length* nil)
229 "target features *SHEBANG-FEATURES*=~@<~S~:>~%"
232 (defvar *shebang-backend-subfeatures*
233 (let* ((default-subfeatures nil)
234 (customizer-file-name "customize-backend-subfeatures.lisp")
235 (customizer (if (probe-file customizer-file-name)
237 (read-from-file customizer-file-name))
239 (funcall customizer default-subfeatures)))
240 (let ((*print-length* nil)
243 "target backend-subfeatures *SHEBANG-BACKEND-FEATURES*=~@<~S~:>~%"
244 *shebang-backend-subfeatures*))
246 ;;;; cold-init-related PACKAGE and SYMBOL tools
248 ;;; Once we're done with possibly ANSIfying the COMMON-LISP package,
249 ;;; it's probably a mistake if we change it (beyond changing the
250 ;;; values of special variables such as *** and +, anyway). Set up
251 ;;; machinery to warn us when/if we change it.
253 ;;; All code depending on this is itself dependent on #!+SB-SHOW.
256 (load "src/cold/snapshot.lisp")
257 (defvar *cl-snapshot* (take-snapshot "COMMON-LISP")))
259 ;;;; master list of source files and their properties
261 ;;; flags which can be used to describe properties of source files
263 *expected-stem-flags*
264 '(;; meaning: This file is not to be compiled when building the
265 ;; cross-compiler which runs on the host ANSI Lisp.
267 ;; meaning: This file is not to be compiled as part of the target
270 ;; meaning: This file is to be processed with the SBCL assembler,
271 ;; not COMPILE-FILE. (Note that this doesn't make sense unless
272 ;; :NOT-HOST is also set, since the SBCL assembler doesn't exist
273 ;; while the cross-compiler is being built in the host ANSI Lisp.)
275 ;; meaning: The #'COMPILE-STEM argument called :IGNORE-FAILURE-P
276 ;; should be true. (This is a KLUDGE: I'd like to get rid of it.
277 ;; For now, it exists so that compilation can proceed through the
278 ;; legacy warnings in src/compiler/x86/array.lisp, which I've
279 ;; never figured out but which were apparently acceptable in CMU
280 ;; CL. Eventually, it would be great to just get rid of all
281 ;; warnings and remove support for this flag. -- WHN 19990323)
284 (defparameter *stems-and-flags* (read-from-file "build-order.lisp-expr"))
286 (defmacro do-stems-and-flags ((stem flags) &body body)
287 (let ((stem-and-flags (gensym "STEM-AND-FLAGS-")))
288 `(dolist (,stem-and-flags *stems-and-flags*)
289 (let ((,stem (first ,stem-and-flags))
290 (,flags (rest ,stem-and-flags)))
293 ;;; Check for stupid typos in FLAGS list keywords.
294 (let ((stems (make-hash-table :test 'equal)))
295 (do-stems-and-flags (stem flags)
296 (if (gethash stem stems)
297 (error "duplicate stem ~S in *STEMS-AND-FLAGS*" stem)
298 (setf (gethash stem stems) t))
299 (let ((set-difference (set-difference flags *expected-stem-flags*)))
301 (error "found unexpected flag(s) in *STEMS-AND-FLAGS*: ~S"
304 ;;;; tools to compile SBCL sources to create the cross-compiler
306 ;;; Execute function FN in an environment appropriate for compiling the
307 ;;; cross-compiler's source code in the cross-compilation host.
308 (defun in-host-compilation-mode (fn)
309 (let ((*features* (cons :sb-xc-host *features*))
310 ;; the CROSS-FLOAT-INFINITY-KLUDGE, as documented in
311 ;; base-target-features.lisp-expr:
312 (*shebang-features* (set-difference *shebang-features*
313 '(:sb-propagate-float-type
314 :sb-propagate-fun-type))))
315 (with-additional-nickname ("SB-XC" "SB!XC")
317 (compile 'in-host-compilation-mode)
319 ;;; Process a file as source code for the cross-compiler, compiling it
320 ;;; (if necessary) in the appropriate environment, then loading it
321 ;;; into the cross-compilation host Common lisp.
322 (defun host-cload-stem (stem &key ignore-failure-p)
323 (let ((compiled-filename (in-host-compilation-mode
327 :obj-prefix *host-obj-prefix*
328 :obj-suffix *host-obj-suffix*
329 :compile-file #'cl:compile-file
330 :ignore-failure-p ignore-failure-p)))))
331 (load compiled-filename)))
332 (compile 'host-cload-stem)
334 ;;; like HOST-CLOAD-STEM, except that we don't bother to compile
335 (defun host-load-stem (stem &key ignore-failure-p)
336 (declare (ignore ignore-failure-p)) ; (It's only relevant when
337 ;; compiling.) KLUDGE: It's untidy to have the knowledge of how to
338 ;; construct complete filenames from stems in here as well as in
339 ;; COMPILE-STEM. It should probably be factored out somehow. -- WHN
341 (load (concatenate 'simple-string *host-obj-prefix* stem *host-obj-suffix*)))
342 (compile 'host-load-stem)
344 ;;;; tools to compile SBCL sources to create object files which will
345 ;;;; be used to create the target SBCL .core file
347 ;;; Run the cross-compiler on a file in the source directory tree to
348 ;;; produce a corresponding file in the target object directory tree.
349 (defun target-compile-stem (stem &key assem-p ignore-failure-p)
350 (funcall *in-target-compilation-mode-fn*
353 :obj-prefix *target-obj-prefix*
354 :obj-suffix *target-obj-suffix*
355 :ignore-failure-p ignore-failure-p
356 :compile-file (if assem-p
357 *target-assemble-file*
358 *target-compile-file*)))))
359 (compile 'target-compile-stem)
361 ;;; (This function is not used by the build process, but is intended
362 ;;; for interactive use when experimenting with the system. It runs
363 ;;; the cross-compiler on test files with arbitrary filenames, not
364 ;;; necessarily in the source tree, e.g. in "/tmp".)
365 (defun target-compile-file (filename)
366 (funcall *in-target-compilation-mode-fn*
368 (funcall *target-compile-file* filename))))
369 (compile 'target-compile-file)