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
49 #+(and openmcl (not darwin)) ".pfsl"
50 #+(and openmcl darwin) ".dfsl"
51 ;; On most xc hosts, any old extension works, so we use an
54 (defvar *target-obj-suffix*
55 ;; Target fasl files are LOADed (actually only quasi-LOADed, in
56 ;; GENESIS) only by SBCL code, and it doesn't care about particular
57 ;; extensions, so we can use something arbitrary.
59 (defvar *target-assem-obj-suffix*
60 ;; Target fasl files from SB!C:ASSEMBLE-FILE are LOADed via GENESIS.
61 ;; The source files are compiled once as assembly files and once as
62 ;; normal lisp files. In the past, they were kept separate by
63 ;; clever symlinking in the source tree, but that became less clean
64 ;; as ports to host environments without symlinks started appearing.
65 ;; In order to keep them separate, we have the assembled versions
66 ;; with a separate suffix.
69 ;;; a function of one functional argument, which calls its functional argument
70 ;;; in an environment suitable for compiling the target. (This environment
71 ;;; includes e.g. a suitable *FEATURES* value.)
72 (declaim (type function *in-target-compilation-mode-fn*))
73 (defvar *in-target-compilation-mode-fn*)
75 ;;; a function with the same calling convention as CL:COMPILE-FILE, to be
76 ;;; used to translate ordinary Lisp source files into target object files
77 (declaim (type function *target-compile-file*))
78 (defvar *target-compile-file*)
80 ;;; designator for a function with the same calling convention as
81 ;;; SB-C:ASSEMBLE-FILE, to be used to translate assembly files into target
83 (defvar *target-assemble-file*)
87 ;;; Take the file named X and make it into a file named Y. Sorta like
88 ;;; UNIX, and unlike Common Lisp's bare RENAME-FILE, we don't allow
89 ;;; information from the original filename to influence the final
90 ;;; filename. (The reason that it's only sorta like UNIX is that in
91 ;;; UNIX "mv foo bar/" will work, but the analogous
92 ;;; (RENAME-FILE-A-LA-UNIX "foo" "bar/") should fail.)
94 ;;; (This is a workaround for the weird behavior of Debian CMU CL
95 ;;; 2.4.6, where (RENAME-FILE "dir/x" "dir/y") tries to create a file
96 ;;; called "dir/dir/y". If that behavior goes away, then we should be
97 ;;; able to get rid of this function and use plain RENAME-FILE in the
98 ;;; COMPILE-STEM function above. -- WHN 19990321
99 (defun rename-file-a-la-unix (x y)
101 (let ((path ;; (Note that the TRUENAME expression here is lifted from an
102 ;; example in the ANSI spec for TRUENAME.)
103 (with-open-file (stream y :direction :output)
105 ;; From the ANSI spec: "In this case, the file is closed
106 ;; when the truename is tried, so the truename
107 ;; information is reliable."
110 (rename-file x path)))
111 (compile 'rename-file-a-la-unix)
113 ;;; other miscellaneous tools
114 (load "src/cold/read-from-file.lisp")
115 (load "src/cold/rename-package-carefully.lisp")
116 (load "src/cold/with-stuff.lisp")
118 ;;; Try to minimize/conceal any non-standardness of the host Common Lisp.
119 (load "src/cold/ansify.lisp")
121 ;;;; special read-macros for building the cold system (and even for
122 ;;;; building some of our tools for building the cold system)
124 (load "src/cold/shebang.lisp")
126 ;;; When cross-compiling, the *FEATURES* set for the target Lisp is
127 ;;; not in general the same as the *FEATURES* set for the host Lisp.
128 ;;; In order to refer to target features specifically, we refer to
129 ;;; *SHEBANG-FEATURES* instead of *FEATURES*, and use the #!+ and #!-
130 ;;; readmacros instead of the ordinary #+ and #- readmacros.
131 (setf *shebang-features*
132 (let* ((default-features
133 (append (read-from-file "base-target-features.lisp-expr")
134 (read-from-file "local-target-features.lisp-expr")))
135 (customizer-file-name "customize-target-features.lisp")
136 (customizer (if (probe-file customizer-file-name)
138 (read-from-file customizer-file-name))
140 (funcall customizer default-features)))
141 (let ((*print-length* nil)
144 "target features *SHEBANG-FEATURES*=~@<~S~:>~%"
147 (defvar *shebang-backend-subfeatures*
148 (let* ((default-subfeatures nil)
149 (customizer-file-name "customize-backend-subfeatures.lisp")
150 (customizer (if (probe-file customizer-file-name)
152 (read-from-file customizer-file-name))
154 (funcall customizer default-subfeatures)))
155 (let ((*print-length* nil)
158 "target backend-subfeatures *SHEBANG-BACKEND-FEATURES*=~@<~S~:>~%"
159 *shebang-backend-subfeatures*))
161 ;;;; cold-init-related PACKAGE and SYMBOL tools
163 ;;; Once we're done with possibly ANSIfying the COMMON-LISP package,
164 ;;; it's probably a mistake if we change it (beyond changing the
165 ;;; values of special variables such as *** and +, anyway). Set up
166 ;;; machinery to warn us when/if we change it.
168 ;;; All code depending on this is itself dependent on #!+SB-SHOW.
171 (load "src/cold/snapshot.lisp")
172 (defvar *cl-snapshot* (take-snapshot "COMMON-LISP")))
174 ;;;; master list of source files and their properties
176 ;;; flags which can be used to describe properties of source files
178 *expected-stem-flags*
179 '(;; meaning: This file is not to be compiled when building the
180 ;; cross-compiler which runs on the host ANSI Lisp. ("not host
181 ;; code", i.e. does not execute on host -- but may still be
182 ;; cross-compiled by the host, so that it executes on the target)
184 ;; meaning: This file is not to be compiled as part of the target
185 ;; SBCL. ("not target code" -- but still presumably host code,
186 ;; used to support the cross-compilation process)
188 ;; meaning: The #'COMPILE-STEM argument :TRACE-FILE should be T.
189 ;; When the compiler is SBCL's COMPILE-FILE or something like it,
190 ;; compiling "foo.lisp" will generate "foo.trace" which contains lots
191 ;; of exciting low-level information about representation selection,
192 ;; VOPs used by the compiler, and bits of assembly.
194 ;; meaning: This file is to be processed with the SBCL assembler,
195 ;; not COMPILE-FILE. (Note that this doesn't make sense unless
196 ;; :NOT-HOST is also set, since the SBCL assembler doesn't exist
197 ;; while the cross-compiler is being built in the host ANSI Lisp.)
199 ;; meaning: The #'COMPILE-STEM argument called :IGNORE-FAILURE-P
200 ;; should be true. (This is a KLUDGE: I'd like to get rid of it.
201 ;; For now, it exists so that compilation can proceed through the
202 ;; legacy warnings in src/compiler/x86/array.lisp, which I've
203 ;; never figured out but which were apparently acceptable in CMU
204 ;; CL. Eventually, it would be great to just get rid of all
205 ;; warnings and remove support for this flag. -- WHN 19990323)
208 (defparameter *stems-and-flags* (read-from-file "build-order.lisp-expr"))
210 (defmacro do-stems-and-flags ((stem flags) &body body)
211 (let ((stem-and-flags (gensym "STEM-AND-FLAGS")))
212 `(dolist (,stem-and-flags *stems-and-flags*)
213 (let ((,stem (first ,stem-and-flags))
214 (,flags (rest ,stem-and-flags)))
217 ;;; Given a STEM, remap the path component "/target/" to a suitable
218 ;;; target directory.
219 (defun stem-remap-target (stem)
220 (let ((position (search "/target/" stem)))
223 (subseq stem 0 (1+ position))
231 (subseq stem (+ position 7)))
233 (compile 'stem-remap-target)
235 ;;; Determine the source path for a stem.
236 (defun stem-source-path (stem)
237 (concatenate 'string "" (stem-remap-target stem) ".lisp"))
238 (compile 'stem-source-path)
240 ;;; Determine the object path for a stem/flags/mode combination.
241 (defun stem-object-path (stem flags mode)
243 (obj-prefix obj-suffix)
245 (:host-compile (values *host-obj-prefix* *host-obj-suffix*))
246 (:target-compile (values *target-obj-prefix*
247 (if (find :assem flags)
248 *target-assem-obj-suffix*
249 *target-obj-suffix*))))
250 (concatenate 'string obj-prefix (stem-remap-target stem) obj-suffix)))
251 (compile 'stem-object-path)
253 ;;; Check for stupid typos in FLAGS list keywords.
254 (let ((stems (make-hash-table :test 'equal)))
255 (do-stems-and-flags (stem flags)
256 ;; We do duplicate stem comparison based on the object path in
257 ;; order to cover the case of stems with an :assem flag, which
258 ;; have two entries but separate object paths for each. KLUDGE:
259 ;; We have to bind *target-obj-prefix* here because it's normally
260 ;; set up later in the build process and we don't actually care
261 ;; what it is so long as it doesn't change while we're checking
262 ;; for duplicate stems.
263 (let* ((*target-obj-prefix* "")
264 (object-path (stem-object-path stem flags :target-compile)))
265 (if (gethash object-path stems)
266 (error "duplicate stem ~S in *STEMS-AND-FLAGS*" stem)
267 (setf (gethash object-path stems) t)))
268 ;; FIXME: We should make sure that the :assem flag is only used
269 ;; when paired with :not-host.
270 (let ((set-difference (set-difference flags *expected-stem-flags*)))
272 (error "found unexpected flag(s) in *STEMS-AND-FLAGS*: ~S"
275 ;;;; tools to compile SBCL sources to create the cross-compiler
277 ;;; a wrapper for compilation/assembly, used mostly to centralize
278 ;;; the procedure for finding full filenames from "stems"
280 ;;; Compile the source file whose basic name is STEM, using some
281 ;;; standard-for-the-SBCL-build-process procedures to generate the
282 ;;; full pathnames of source file and object file. Return the pathname
283 ;;; of the object file for STEM.
285 ;;; STEM and FLAGS are as per DO-STEMS-AND-FLAGS. MODE is one of
286 ;;; :HOST-COMPILE and :TARGET-COMPILE.
287 (defun compile-stem (stem flags mode)
289 (let* (;; KLUDGE: Note that this CONCATENATE 'STRING stuff is not The Common
290 ;; Lisp Way, although it works just fine for common UNIX environments.
291 ;; Should it come to pass that the system is ported to environments
292 ;; where version numbers and so forth become an issue, it might become
293 ;; urgent to rewrite this using the fancy Common Lisp PATHNAME
294 ;; machinery instead of just using strings. In the absence of such a
295 ;; port, it might or might be a good idea to do the rewrite.
297 (src (stem-source-path stem))
298 (obj (stem-object-path stem flags mode))
299 (tmp-obj (concatenate 'string obj "-tmp"))
301 (compile-file (ecase mode
302 (:host-compile #'compile-file)
303 (:target-compile (if (find :assem flags)
304 *target-assemble-file*
305 *target-compile-file*))))
306 (trace-file (find :trace-file flags))
307 (ignore-failure-p (find :ignore-failure-p flags)))
308 (declare (type function compile-file))
310 (ensure-directories-exist obj :verbose t)
312 ;; We're about to set about building a new object file. First, we
313 ;; delete any preexisting object file in order to avoid confusing
314 ;; ourselves later should we happen to bail out of compilation
316 (when (probe-file obj)
321 ;; Work around a bug in CLISP 1999-01-08 #'COMPILE-FILE: CLISP
322 ;; mangles relative pathnames passed as :OUTPUT-FILE arguments,
323 ;; but works OK with absolute pathnames.
325 ;; following discussion on cmucl-imp 2002-07
326 ;; "COMPILE-FILE-PATHNAME", it would seem safer to deal with
327 ;; absolute pathnames all the time; it is no longer clear that the
328 ;; original behaviour in CLISP was wrong or that the current
329 ;; behaviour is right; and in any case absolutifying the pathname
330 ;; insulates us against changes of behaviour. -- CSR, 2002-08-09
332 ;; (Note that this idiom is taken from the ANSI
333 ;; documentation for TRUENAME.)
334 (with-open-file (stream tmp-obj
336 ;; Compilation would overwrite the
337 ;; temporary object anyway and overly
338 ;; strict implementations default
340 :if-exists :supersede)
343 ;; and some compilers (e.g. OpenMCL) will complain if they're
344 ;; asked to write over a file that exists already (and isn't
345 ;; recognizeably a fasl file), so
346 (when (probe-file tmp-obj)
347 (delete-file tmp-obj))
349 ;; Try to use the compiler to generate a new temporary object file.
350 (flet ((report-recompile-restart (stream)
351 (format stream "Recompile file ~S" src))
352 (report-continue-restart (stream)
353 (format stream "Continue, using possibly bogus file ~S" obj)))
356 (multiple-value-bind (output-truename warnings-p failure-p)
358 (funcall compile-file src :output-file tmp-obj
360 (funcall compile-file src :output-file tmp-obj ))
361 (declare (ignore warnings-p))
362 (cond ((not output-truename)
363 (error "couldn't compile ~S" src))
366 (warn "ignoring FAILURE-P return value from compilation of ~S"
370 (error "FAILURE-P was set when creating ~S."
373 :report report-recompile-restart
374 (go retry-compile-file))
376 :report report-continue-restart
377 (setf failure-p nil)))
378 ;; Don't leave failed object files lying around.
379 (when (and failure-p (probe-file tmp-obj))
380 (delete-file tmp-obj)
381 (format t "~&deleted ~S~%" tmp-obj)))))
382 ;; Otherwise: success, just fall through.
385 ;; If we get to here, compilation succeeded, so it's OK to rename
386 ;; the temporary output file to the permanent object file.
387 (rename-file-a-la-unix tmp-obj obj)
389 ;; nice friendly traditional return value
391 (compile 'compile-stem)
393 ;;; Execute function FN in an environment appropriate for compiling the
394 ;;; cross-compiler's source code in the cross-compilation host.
395 (defun in-host-compilation-mode (fn)
396 (declare (type function fn))
397 (let ((*features* (cons :sb-xc-host *features*))
398 ;; the CROSS-FLOAT-INFINITY-KLUDGE, as documented in
399 ;; base-target-features.lisp-expr:
400 (*shebang-features* (set-difference *shebang-features*
401 '(:sb-propagate-float-type
402 :sb-propagate-fun-type))))
403 (with-additional-nickname ("SB-XC" "SB!XC")
405 (compile 'in-host-compilation-mode)
407 ;;; Process a file as source code for the cross-compiler, compiling it
408 ;;; (if necessary) in the appropriate environment, then loading it
409 ;;; into the cross-compilation host Common lisp.
410 (defun host-cload-stem (stem flags)
411 (let ((compiled-filename (in-host-compilation-mode
413 (compile-stem stem flags :host-compile)))))
414 (load compiled-filename)))
415 (compile 'host-cload-stem)
417 ;;; like HOST-CLOAD-STEM, except that we don't bother to compile
418 (defun host-load-stem (stem flags)
419 (load (stem-object-path stem flags :host-compile)))
420 (compile 'host-load-stem)
422 ;;;; tools to compile SBCL sources to create object files which will
423 ;;;; be used to create the target SBCL .core file
425 ;;; Run the cross-compiler on a file in the source directory tree to
426 ;;; produce a corresponding file in the target object directory tree.
427 (defun target-compile-stem (stem flags)
428 (funcall *in-target-compilation-mode-fn*
430 (compile-stem stem flags :target-compile))))
431 (compile 'target-compile-stem)
433 ;;; (This function is not used by the build process, but is intended
434 ;;; for interactive use when experimenting with the system. It runs
435 ;;; the cross-compiler on test files with arbitrary filenames, not
436 ;;; necessarily in the source tree, e.g. in "/tmp".)
437 (defun target-compile-file (filename)
438 (funcall *in-target-compilation-mode-fn*
440 (funcall *target-compile-file* filename))))
441 (compile 'target-compile-file)