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 (defvar *target-obj-suffix*
37 ;; Target fasl files are LOADed (actually only quasi-LOADed, in
38 ;; GENESIS) only by SBCL code, and it doesn't care about particular
39 ;; extensions, so we can use something arbitrary.
41 (defvar *target-assem-obj-suffix*
42 ;; Target fasl files from SB!C:ASSEMBLE-FILE are LOADed via GENESIS.
43 ;; The source files are compiled once as assembly files and once as
44 ;; normal lisp files. In the past, they were kept separate by
45 ;; clever symlinking in the source tree, but that became less clean
46 ;; as ports to host environments without symlinks started appearing.
47 ;; In order to keep them separate, we have the assembled versions
48 ;; with a separate suffix.
51 ;;; a function of one functional argument, which calls its functional argument
52 ;;; in an environment suitable for compiling the target. (This environment
53 ;;; includes e.g. a suitable *FEATURES* value.)
54 (declaim (type function *in-target-compilation-mode-fn*))
55 (defvar *in-target-compilation-mode-fn*)
57 ;;; a function with the same calling convention as CL:COMPILE-FILE, to be
58 ;;; used to translate ordinary Lisp source files into target object files
59 (declaim (type function *target-compile-file*))
60 (defvar *target-compile-file*)
62 ;;; designator for a function with the same calling convention as
63 ;;; SB-C:ASSEMBLE-FILE, to be used to translate assembly files into target
65 (defvar *target-assemble-file*)
69 ;;; Take the file named X and make it into a file named Y. Sorta like
70 ;;; UNIX, and unlike Common Lisp's bare RENAME-FILE, we don't allow
71 ;;; information from the original filename to influence the final
72 ;;; filename. (The reason that it's only sorta like UNIX is that in
73 ;;; UNIX "mv foo bar/" will work, but the analogous
74 ;;; (RENAME-FILE-A-LA-UNIX "foo" "bar/") should fail.)
76 ;;; (This is a workaround for the weird behavior of Debian CMU CL
77 ;;; 2.4.6, where (RENAME-FILE "dir/x" "dir/y") tries to create a file
78 ;;; called "dir/dir/y". If that behavior goes away, then we should be
79 ;;; able to get rid of this function and use plain RENAME-FILE in the
80 ;;; COMPILE-STEM function above. -- WHN 19990321
81 (defun rename-file-a-la-unix (x y)
83 (let ((path ;; (Note that the TRUENAME expression here is lifted from an
84 ;; example in the ANSI spec for TRUENAME.)
85 (with-open-file (stream y :direction :output)
87 ;; From the ANSI spec: "In this case, the file is closed
88 ;; when the truename is tried, so the truename
89 ;; information is reliable."
92 (rename-file x path)))
93 (compile 'rename-file-a-la-unix)
95 ;;; other miscellaneous tools
96 (load "src/cold/read-from-file.lisp")
97 (load "src/cold/rename-package-carefully.lisp")
98 (load "src/cold/with-stuff.lisp")
100 ;;; Try to minimize/conceal any non-standardness of the host Common Lisp.
101 (load "src/cold/ansify.lisp")
103 ;;;; special read-macros for building the cold system (and even for
104 ;;;; building some of our tools for building the cold system)
106 (load "src/cold/shebang.lisp")
108 ;;; When cross-compiling, the *FEATURES* set for the target Lisp is
109 ;;; not in general the same as the *FEATURES* set for the host Lisp.
110 ;;; In order to refer to target features specifically, we refer to
111 ;;; *SHEBANG-FEATURES* instead of *FEATURES*, and use the #!+ and #!-
112 ;;; readmacros instead of the ordinary #+ and #- readmacros.
113 (setf *shebang-features*
114 (let* ((default-features
115 (append (read-from-file "base-target-features.lisp-expr")
116 (eval (read-from-file "local-target-features.lisp-expr"))))
117 (customizer-file-name "customize-target-features.lisp")
118 (customizer (if (probe-file customizer-file-name)
120 (read-from-file customizer-file-name))
122 (funcall customizer default-features)))
123 (let ((*print-length* nil)
126 "target features *SHEBANG-FEATURES*=~@<~S~:>~%"
129 (defvar *shebang-backend-subfeatures*
130 (let* ((default-subfeatures nil)
131 (customizer-file-name "customize-backend-subfeatures.lisp")
132 (customizer (if (probe-file customizer-file-name)
134 (read-from-file customizer-file-name))
136 (funcall customizer default-subfeatures)))
137 (let ((*print-length* nil)
140 "target backend-subfeatures *SHEBANG-BACKEND-FEATURES*=~@<~S~:>~%"
141 *shebang-backend-subfeatures*))
143 ;;; Some feature combinations simply don't work, and sometimes don't
144 ;;; fail until quite a ways into the build. Pick off the more obvious
145 ;;; combinations now, and provide a description of what the actual
146 ;;; failure is (not always obvious from when the build fails).
147 (let ((feature-compatability-tests
148 '(("(and sb-thread (not gencgc))"
149 ":SB-THREAD requires :GENCGC")
150 ("(and sb-thread (not (or ppc x86 x86-64)))"
151 ":SB-THREAD not supported on selected architecture")
152 ("(and gencgc cheneygc)"
153 ":GENCGC and :CHENEYGC are incompatible")
154 ("(and cheneygc (not (or alpha hppa mips ppc sparc)))"
155 ":CHENEYGC not supported on selected architecture")
156 ("(and gencgc (not (or sparc ppc x86 x86-64)))"
157 ":GENCGC not supported on selected architecture")
158 ("(not (or gencgc cheneygc))"
159 "One of :GENCGC or :CHENEYGC must be enabled")
160 ("(and win32 (not (and sb-thread
161 sb-safepoint sb-thruption sb-wtimer
163 ":SB-WIN32 requires :SB-THREAD and related features")
164 ("(and sb-dynamic-core (not (and linkage-table sb-thread)))"
165 ;; Subtle memory corruption follows when sb-dynamic-core is
166 ;; active, and non-threaded allocation routines have not been
167 ;; updated to take the additional indirection into account.
168 ;; Let's avoid this unusual combination.
169 ":SB-DYNAMIC-CORE requires :LINKAGE-TABLE and :SB-THREAD")
170 ("(or (and alpha (or hppa mips ppc sparc x86 x86-64))
171 (and hppa (or mips ppc sparc x86 x86-64))
172 (and mips (or ppc sparc x86 x86-64))
173 (and ppc (or sparc x86 x86-64))
174 (and sparc (or x86 x86-64))
176 "More than one architecture selected")))
177 (failed-test-descriptions nil))
178 (dolist (test feature-compatability-tests)
179 (let ((*features* *shebang-features*))
180 (when (read-from-string (concatenate 'string "#+" (first test) "T NIL"))
181 (push (second test) failed-test-descriptions))))
182 (when failed-test-descriptions
183 (error "Feature compatibility check failed, ~S"
184 failed-test-descriptions)))
186 ;;;; cold-init-related PACKAGE and SYMBOL tools
188 ;;; Once we're done with possibly ANSIfying the COMMON-LISP package,
189 ;;; it's probably a mistake if we change it (beyond changing the
190 ;;; values of special variables such as *** and +, anyway). Set up
191 ;;; machinery to warn us when/if we change it.
193 ;;; All code depending on this is itself dependent on #!+SB-SHOW.
196 (load "src/cold/snapshot.lisp")
197 (defvar *cl-snapshot* (take-snapshot "COMMON-LISP")))
199 ;;;; master list of source files and their properties
201 ;;; flags which can be used to describe properties of source files
203 *expected-stem-flags*
204 '(;; meaning: This file is not to be compiled when building the
205 ;; cross-compiler which runs on the host ANSI Lisp. ("not host
206 ;; code", i.e. does not execute on host -- but may still be
207 ;; cross-compiled by the host, so that it executes on the target)
209 ;; meaning: This file is not to be compiled as part of the target
210 ;; SBCL. ("not target code" -- but still presumably host code,
211 ;; used to support the cross-compilation process)
213 ;; meaning: The #'COMPILE-STEM argument :TRACE-FILE should be T.
214 ;; When the compiler is SBCL's COMPILE-FILE or something like it,
215 ;; compiling "foo.lisp" will generate "foo.trace" which contains lots
216 ;; of exciting low-level information about representation selection,
217 ;; VOPs used by the compiler, and bits of assembly.
219 ;; meaning: This file is to be processed with the SBCL assembler,
220 ;; not COMPILE-FILE. (Note that this doesn't make sense unless
221 ;; :NOT-HOST is also set, since the SBCL assembler doesn't exist
222 ;; while the cross-compiler is being built in the host ANSI Lisp.)
224 ;; meaning: The #'COMPILE-STEM argument called :IGNORE-FAILURE-P
225 ;; should be true. (This is a KLUDGE: I'd like to get rid of it.
226 ;; For now, it exists so that compilation can proceed through the
227 ;; legacy warnings in src/compiler/x86/array.lisp, which I've
228 ;; never figured out but which were apparently acceptable in CMU
229 ;; CL. Eventually, it would be great to just get rid of all
230 ;; warnings and remove support for this flag. -- WHN 19990323)
233 (defparameter *stems-and-flags* (read-from-file "build-order.lisp-expr"))
235 (defmacro do-stems-and-flags ((stem flags) &body body)
236 (let ((stem-and-flags (gensym "STEM-AND-FLAGS")))
237 `(dolist (,stem-and-flags *stems-and-flags*)
238 (let ((,stem (first ,stem-and-flags))
239 (,flags (rest ,stem-and-flags)))
242 ;;; Given a STEM, remap the path component "/target/" to a suitable
243 ;;; target directory.
244 (defun stem-remap-target (stem)
245 (let ((position (search "/target/" stem)))
248 (subseq stem 0 (1+ position))
256 (subseq stem (+ position 7)))
258 (compile 'stem-remap-target)
260 ;;; Determine the source path for a stem.
261 (defun stem-source-path (stem)
262 (concatenate 'string "" (stem-remap-target stem) ".lisp"))
263 (compile 'stem-source-path)
265 ;;; Determine the object path for a stem/flags/mode combination.
266 (defun stem-object-path (stem flags mode)
268 (obj-prefix obj-suffix)
271 ;; On some xc hosts, it's impossible to LOAD a fasl file unless it
272 ;; has the same extension that the host uses for COMPILE-FILE
273 ;; output, so we have to be careful to use the xc host's preferred
275 (values *host-obj-prefix*
276 (concatenate 'string "."
277 (pathname-type (compile-file-pathname stem)))))
278 (:target-compile (values *target-obj-prefix*
279 (if (find :assem flags)
280 *target-assem-obj-suffix*
281 *target-obj-suffix*))))
282 (concatenate 'string obj-prefix (stem-remap-target stem) obj-suffix)))
283 (compile 'stem-object-path)
285 ;;; Check for stupid typos in FLAGS list keywords.
286 (let ((stems (make-hash-table :test 'equal)))
287 (do-stems-and-flags (stem flags)
288 ;; We do duplicate stem comparison based on the object path in
289 ;; order to cover the case of stems with an :assem flag, which
290 ;; have two entries but separate object paths for each. KLUDGE:
291 ;; We have to bind *target-obj-prefix* here because it's normally
292 ;; set up later in the build process and we don't actually care
293 ;; what it is so long as it doesn't change while we're checking
294 ;; for duplicate stems.
295 (let* ((*target-obj-prefix* "")
296 (object-path (stem-object-path stem flags :target-compile)))
297 (if (gethash object-path stems)
298 (error "duplicate stem ~S in *STEMS-AND-FLAGS*" stem)
299 (setf (gethash object-path stems) t)))
300 ;; FIXME: We should make sure that the :assem flag is only used
301 ;; when paired with :not-host.
302 (let ((set-difference (set-difference flags *expected-stem-flags*)))
304 (error "found unexpected flag(s) in *STEMS-AND-FLAGS*: ~S"
307 ;;;; tools to compile SBCL sources to create the cross-compiler
309 ;;; a wrapper for compilation/assembly, used mostly to centralize
310 ;;; the procedure for finding full filenames from "stems"
312 ;;; Compile the source file whose basic name is STEM, using some
313 ;;; standard-for-the-SBCL-build-process procedures to generate the
314 ;;; full pathnames of source file and object file. Return the pathname
315 ;;; of the object file for STEM.
317 ;;; STEM and FLAGS are as per DO-STEMS-AND-FLAGS. MODE is one of
318 ;;; :HOST-COMPILE and :TARGET-COMPILE.
319 (defun compile-stem (stem flags mode)
321 (let* (;; KLUDGE: Note that this CONCATENATE 'STRING stuff is not The Common
322 ;; Lisp Way, although it works just fine for common UNIX environments.
323 ;; Should it come to pass that the system is ported to environments
324 ;; where version numbers and so forth become an issue, it might become
325 ;; urgent to rewrite this using the fancy Common Lisp PATHNAME
326 ;; machinery instead of just using strings. In the absence of such a
327 ;; port, it might or might be a good idea to do the rewrite.
329 (src (stem-source-path stem))
330 (obj (stem-object-path stem flags mode))
331 (tmp-obj (concatenate 'string obj "-tmp"))
333 (compile-file (ecase mode
334 (:host-compile #'compile-file)
335 (:target-compile (if (find :assem flags)
336 *target-assemble-file*
337 *target-compile-file*))))
338 (trace-file (find :trace-file flags))
339 (ignore-failure-p (find :ignore-failure-p flags)))
340 (declare (type function compile-file))
342 (ensure-directories-exist obj :verbose t)
344 ;; We're about to set about building a new object file. First, we
345 ;; delete any preexisting object file in order to avoid confusing
346 ;; ourselves later should we happen to bail out of compilation
348 (when (probe-file obj)
353 ;; Work around a bug in CLISP 1999-01-08 #'COMPILE-FILE: CLISP
354 ;; mangles relative pathnames passed as :OUTPUT-FILE arguments,
355 ;; but works OK with absolute pathnames.
357 ;; following discussion on cmucl-imp 2002-07
358 ;; "COMPILE-FILE-PATHNAME", it would seem safer to deal with
359 ;; absolute pathnames all the time; it is no longer clear that the
360 ;; original behaviour in CLISP was wrong or that the current
361 ;; behaviour is right; and in any case absolutifying the pathname
362 ;; insulates us against changes of behaviour. -- CSR, 2002-08-09
364 ;; (Note that this idiom is taken from the ANSI
365 ;; documentation for TRUENAME.)
366 (with-open-file (stream tmp-obj
368 ;; Compilation would overwrite the
369 ;; temporary object anyway and overly
370 ;; strict implementations default
372 :if-exists :supersede)
375 ;; and some compilers (e.g. OpenMCL) will complain if they're
376 ;; asked to write over a file that exists already (and isn't
377 ;; recognizeably a fasl file), so
378 (when (probe-file tmp-obj)
379 (delete-file tmp-obj))
381 ;; Try to use the compiler to generate a new temporary object file.
382 (flet ((report-recompile-restart (stream)
383 (format stream "Recompile file ~S" src))
384 (report-continue-restart (stream)
385 (format stream "Continue, using possibly bogus file ~S" obj)))
388 (multiple-value-bind (output-truename warnings-p failure-p)
390 (funcall compile-file src :output-file tmp-obj
391 :trace-file t :allow-other-keys t)
392 (funcall compile-file src :output-file tmp-obj))
393 (declare (ignore warnings-p))
394 (cond ((not output-truename)
395 (error "couldn't compile ~S" src))
398 (warn "ignoring FAILURE-P return value from compilation of ~S"
402 (error "FAILURE-P was set when creating ~S."
405 :report report-recompile-restart
406 (go retry-compile-file))
408 :report report-continue-restart
409 (setf failure-p nil)))
410 ;; Don't leave failed object files lying around.
411 (when (and failure-p (probe-file tmp-obj))
412 (delete-file tmp-obj)
413 (format t "~&deleted ~S~%" tmp-obj)))))
414 ;; Otherwise: success, just fall through.
417 ;; If we get to here, compilation succeeded, so it's OK to rename
418 ;; the temporary output file to the permanent object file.
419 (rename-file-a-la-unix tmp-obj obj)
421 ;; nice friendly traditional return value
423 (compile 'compile-stem)
425 ;;; Execute function FN in an environment appropriate for compiling the
426 ;;; cross-compiler's source code in the cross-compilation host.
427 (defun in-host-compilation-mode (fn)
428 (declare (type function fn))
429 (let ((*features* (cons :sb-xc-host *features*))
430 ;; the CROSS-FLOAT-INFINITY-KLUDGE, as documented in
431 ;; base-target-features.lisp-expr:
432 (*shebang-features* (set-difference *shebang-features*
433 '(:sb-propagate-float-type
434 :sb-propagate-fun-type))))
435 (with-additional-nickname ("SB-XC" "SB!XC")
437 (compile 'in-host-compilation-mode)
439 ;;; Process a file as source code for the cross-compiler, compiling it
440 ;;; (if necessary) in the appropriate environment, then loading it
441 ;;; into the cross-compilation host Common lisp.
442 (defun host-cload-stem (stem flags)
443 (let ((compiled-filename (in-host-compilation-mode
445 (compile-stem stem flags :host-compile)))))
446 (load compiled-filename)))
447 (compile 'host-cload-stem)
449 ;;; like HOST-CLOAD-STEM, except that we don't bother to compile
450 (defun host-load-stem (stem flags)
451 (load (stem-object-path stem flags :host-compile)))
452 (compile 'host-load-stem)
454 ;;;; tools to compile SBCL sources to create object files which will
455 ;;;; be used to create the target SBCL .core file
457 ;;; Run the cross-compiler on a file in the source directory tree to
458 ;;; produce a corresponding file in the target object directory tree.
459 (defun target-compile-stem (stem flags)
460 (funcall *in-target-compilation-mode-fn*
462 (compile-stem stem flags :target-compile))))
463 (compile 'target-compile-stem)
465 ;;; (This function is not used by the build process, but is intended
466 ;;; for interactive use when experimenting with the system. It runs
467 ;;; the cross-compiler on test files with arbitrary filenames, not
468 ;;; necessarily in the source tree, e.g. in "/tmp".)
469 (defun target-compile-file (filename)
470 (funcall *in-target-compilation-mode-fn*
472 (funcall *target-compile-file* filename))))
473 (compile 'target-compile-file)