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 ("(or (and alpha (or hppa mips ppc sparc x86 x86-64))
161 (and hppa (or mips ppc sparc x86 x86-64))
162 (and mips (or ppc sparc x86 x86-64))
163 (and ppc (or sparc x86 x86-64))
164 (and sparc (or x86 x86-64))
166 "More than one architecture selected")))
167 (failed-test-descriptions nil))
168 (dolist (test feature-compatability-tests)
169 (let ((*features* *shebang-features*))
170 (when (read-from-string (concatenate 'string "#+" (first test) "T NIL"))
171 (push (second test) failed-test-descriptions))))
172 (when failed-test-descriptions
173 (error "Feature compatability check failed, ~S"
174 failed-test-descriptions)))
176 ;;;; cold-init-related PACKAGE and SYMBOL tools
178 ;;; Once we're done with possibly ANSIfying the COMMON-LISP package,
179 ;;; it's probably a mistake if we change it (beyond changing the
180 ;;; values of special variables such as *** and +, anyway). Set up
181 ;;; machinery to warn us when/if we change it.
183 ;;; All code depending on this is itself dependent on #!+SB-SHOW.
186 (load "src/cold/snapshot.lisp")
187 (defvar *cl-snapshot* (take-snapshot "COMMON-LISP")))
189 ;;;; master list of source files and their properties
191 ;;; flags which can be used to describe properties of source files
193 *expected-stem-flags*
194 '(;; meaning: This file is not to be compiled when building the
195 ;; cross-compiler which runs on the host ANSI Lisp. ("not host
196 ;; code", i.e. does not execute on host -- but may still be
197 ;; cross-compiled by the host, so that it executes on the target)
199 ;; meaning: This file is not to be compiled as part of the target
200 ;; SBCL. ("not target code" -- but still presumably host code,
201 ;; used to support the cross-compilation process)
203 ;; meaning: The #'COMPILE-STEM argument :TRACE-FILE should be T.
204 ;; When the compiler is SBCL's COMPILE-FILE or something like it,
205 ;; compiling "foo.lisp" will generate "foo.trace" which contains lots
206 ;; of exciting low-level information about representation selection,
207 ;; VOPs used by the compiler, and bits of assembly.
209 ;; meaning: This file is to be processed with the SBCL assembler,
210 ;; not COMPILE-FILE. (Note that this doesn't make sense unless
211 ;; :NOT-HOST is also set, since the SBCL assembler doesn't exist
212 ;; while the cross-compiler is being built in the host ANSI Lisp.)
214 ;; meaning: The #'COMPILE-STEM argument called :IGNORE-FAILURE-P
215 ;; should be true. (This is a KLUDGE: I'd like to get rid of it.
216 ;; For now, it exists so that compilation can proceed through the
217 ;; legacy warnings in src/compiler/x86/array.lisp, which I've
218 ;; never figured out but which were apparently acceptable in CMU
219 ;; CL. Eventually, it would be great to just get rid of all
220 ;; warnings and remove support for this flag. -- WHN 19990323)
223 (defparameter *stems-and-flags* (read-from-file "build-order.lisp-expr"))
225 (defmacro do-stems-and-flags ((stem flags) &body body)
226 (let ((stem-and-flags (gensym "STEM-AND-FLAGS")))
227 `(dolist (,stem-and-flags *stems-and-flags*)
228 (let ((,stem (first ,stem-and-flags))
229 (,flags (rest ,stem-and-flags)))
232 ;;; Given a STEM, remap the path component "/target/" to a suitable
233 ;;; target directory.
234 (defun stem-remap-target (stem)
235 (let ((position (search "/target/" stem)))
238 (subseq stem 0 (1+ position))
246 (subseq stem (+ position 7)))
248 (compile 'stem-remap-target)
250 ;;; Determine the source path for a stem.
251 (defun stem-source-path (stem)
252 (concatenate 'string "" (stem-remap-target stem) ".lisp"))
253 (compile 'stem-source-path)
255 ;;; Determine the object path for a stem/flags/mode combination.
256 (defun stem-object-path (stem flags mode)
258 (obj-prefix obj-suffix)
261 ;; On some xc hosts, it's impossible to LOAD a fasl file unless it
262 ;; has the same extension that the host uses for COMPILE-FILE
263 ;; output, so we have to be careful to use the xc host's preferred
265 (values *host-obj-prefix*
266 (concatenate 'string "."
267 (pathname-type (compile-file-pathname stem)))))
268 (:target-compile (values *target-obj-prefix*
269 (if (find :assem flags)
270 *target-assem-obj-suffix*
271 *target-obj-suffix*))))
272 (concatenate 'string obj-prefix (stem-remap-target stem) obj-suffix)))
273 (compile 'stem-object-path)
275 ;;; Check for stupid typos in FLAGS list keywords.
276 (let ((stems (make-hash-table :test 'equal)))
277 (do-stems-and-flags (stem flags)
278 ;; We do duplicate stem comparison based on the object path in
279 ;; order to cover the case of stems with an :assem flag, which
280 ;; have two entries but separate object paths for each. KLUDGE:
281 ;; We have to bind *target-obj-prefix* here because it's normally
282 ;; set up later in the build process and we don't actually care
283 ;; what it is so long as it doesn't change while we're checking
284 ;; for duplicate stems.
285 (let* ((*target-obj-prefix* "")
286 (object-path (stem-object-path stem flags :target-compile)))
287 (if (gethash object-path stems)
288 (error "duplicate stem ~S in *STEMS-AND-FLAGS*" stem)
289 (setf (gethash object-path stems) t)))
290 ;; FIXME: We should make sure that the :assem flag is only used
291 ;; when paired with :not-host.
292 (let ((set-difference (set-difference flags *expected-stem-flags*)))
294 (error "found unexpected flag(s) in *STEMS-AND-FLAGS*: ~S"
297 ;;;; tools to compile SBCL sources to create the cross-compiler
299 ;;; a wrapper for compilation/assembly, used mostly to centralize
300 ;;; the procedure for finding full filenames from "stems"
302 ;;; Compile the source file whose basic name is STEM, using some
303 ;;; standard-for-the-SBCL-build-process procedures to generate the
304 ;;; full pathnames of source file and object file. Return the pathname
305 ;;; of the object file for STEM.
307 ;;; STEM and FLAGS are as per DO-STEMS-AND-FLAGS. MODE is one of
308 ;;; :HOST-COMPILE and :TARGET-COMPILE.
309 (defun compile-stem (stem flags mode)
311 (let* (;; KLUDGE: Note that this CONCATENATE 'STRING stuff is not The Common
312 ;; Lisp Way, although it works just fine for common UNIX environments.
313 ;; Should it come to pass that the system is ported to environments
314 ;; where version numbers and so forth become an issue, it might become
315 ;; urgent to rewrite this using the fancy Common Lisp PATHNAME
316 ;; machinery instead of just using strings. In the absence of such a
317 ;; port, it might or might be a good idea to do the rewrite.
319 (src (stem-source-path stem))
320 (obj (stem-object-path stem flags mode))
321 (tmp-obj (concatenate 'string obj "-tmp"))
323 (compile-file (ecase mode
324 (:host-compile #'compile-file)
325 (:target-compile (if (find :assem flags)
326 *target-assemble-file*
327 *target-compile-file*))))
328 (trace-file (find :trace-file flags))
329 (ignore-failure-p (find :ignore-failure-p flags)))
330 (declare (type function compile-file))
332 (ensure-directories-exist obj :verbose t)
334 ;; We're about to set about building a new object file. First, we
335 ;; delete any preexisting object file in order to avoid confusing
336 ;; ourselves later should we happen to bail out of compilation
338 (when (probe-file obj)
343 ;; Work around a bug in CLISP 1999-01-08 #'COMPILE-FILE: CLISP
344 ;; mangles relative pathnames passed as :OUTPUT-FILE arguments,
345 ;; but works OK with absolute pathnames.
347 ;; following discussion on cmucl-imp 2002-07
348 ;; "COMPILE-FILE-PATHNAME", it would seem safer to deal with
349 ;; absolute pathnames all the time; it is no longer clear that the
350 ;; original behaviour in CLISP was wrong or that the current
351 ;; behaviour is right; and in any case absolutifying the pathname
352 ;; insulates us against changes of behaviour. -- CSR, 2002-08-09
354 ;; (Note that this idiom is taken from the ANSI
355 ;; documentation for TRUENAME.)
356 (with-open-file (stream tmp-obj
358 ;; Compilation would overwrite the
359 ;; temporary object anyway and overly
360 ;; strict implementations default
362 :if-exists :supersede)
365 ;; and some compilers (e.g. OpenMCL) will complain if they're
366 ;; asked to write over a file that exists already (and isn't
367 ;; recognizeably a fasl file), so
368 (when (probe-file tmp-obj)
369 (delete-file tmp-obj))
371 ;; Try to use the compiler to generate a new temporary object file.
372 (flet ((report-recompile-restart (stream)
373 (format stream "Recompile file ~S" src))
374 (report-continue-restart (stream)
375 (format stream "Continue, using possibly bogus file ~S" obj)))
378 (multiple-value-bind (output-truename warnings-p failure-p)
380 (funcall compile-file src :output-file tmp-obj
381 :trace-file t :allow-other-keys t)
382 (funcall compile-file src :output-file tmp-obj))
383 (declare (ignore warnings-p))
384 (cond ((not output-truename)
385 (error "couldn't compile ~S" src))
388 (warn "ignoring FAILURE-P return value from compilation of ~S"
392 (error "FAILURE-P was set when creating ~S."
395 :report report-recompile-restart
396 (go retry-compile-file))
398 :report report-continue-restart
399 (setf failure-p nil)))
400 ;; Don't leave failed object files lying around.
401 (when (and failure-p (probe-file tmp-obj))
402 (delete-file tmp-obj)
403 (format t "~&deleted ~S~%" tmp-obj)))))
404 ;; Otherwise: success, just fall through.
407 ;; If we get to here, compilation succeeded, so it's OK to rename
408 ;; the temporary output file to the permanent object file.
409 (rename-file-a-la-unix tmp-obj obj)
411 ;; nice friendly traditional return value
413 (compile 'compile-stem)
415 ;;; Execute function FN in an environment appropriate for compiling the
416 ;;; cross-compiler's source code in the cross-compilation host.
417 (defun in-host-compilation-mode (fn)
418 (declare (type function fn))
419 (let ((*features* (cons :sb-xc-host *features*))
420 ;; the CROSS-FLOAT-INFINITY-KLUDGE, as documented in
421 ;; base-target-features.lisp-expr:
422 (*shebang-features* (set-difference *shebang-features*
423 '(:sb-propagate-float-type
424 :sb-propagate-fun-type))))
425 (with-additional-nickname ("SB-XC" "SB!XC")
427 (compile 'in-host-compilation-mode)
429 ;;; Process a file as source code for the cross-compiler, compiling it
430 ;;; (if necessary) in the appropriate environment, then loading it
431 ;;; into the cross-compilation host Common lisp.
432 (defun host-cload-stem (stem flags)
433 (let ((compiled-filename (in-host-compilation-mode
435 (compile-stem stem flags :host-compile)))))
436 (load compiled-filename)))
437 (compile 'host-cload-stem)
439 ;;; like HOST-CLOAD-STEM, except that we don't bother to compile
440 (defun host-load-stem (stem flags)
441 (load (stem-object-path stem flags :host-compile)))
442 (compile 'host-load-stem)
444 ;;;; tools to compile SBCL sources to create object files which will
445 ;;;; be used to create the target SBCL .core file
447 ;;; Run the cross-compiler on a file in the source directory tree to
448 ;;; produce a corresponding file in the target object directory tree.
449 (defun target-compile-stem (stem flags)
450 (funcall *in-target-compilation-mode-fn*
452 (compile-stem stem flags :target-compile))))
453 (compile 'target-compile-stem)
455 ;;; (This function is not used by the build process, but is intended
456 ;;; for interactive use when experimenting with the system. It runs
457 ;;; the cross-compiler on test files with arbitrary filenames, not
458 ;;; necessarily in the source tree, e.g. in "/tmp".)
459 (defun target-compile-file (filename)
460 (funcall *in-target-compilation-mode-fn*
462 (funcall *target-compile-file* filename))))
463 (compile 'target-compile-file)