Aesthetic tweaks
[sbcl/simd.git] / src / cold / shared.lisp
blob7a77c6d338cf02eb0995aadb896f1e8c11626bf7
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.
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.
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/").
30 ;;;
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*
38 (or
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
42 ;; extension.
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
52 ;; arbitrary one.
53 ".lisp-obj"))
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.
58 ".lisp-obj")
60 ;;; a function of one functional argument, which calls its functional argument
61 ;;; in an environment suitable for compiling the target. (This environment
62 ;;; includes e.g. a suitable *FEATURES* value.)
63 (declaim (type function *in-target-compilation-mode-fn*))
64 (defvar *in-target-compilation-mode-fn*)
66 ;;; a function with the same calling convention as CL:COMPILE-FILE, to be
67 ;;; used to translate ordinary Lisp source files into target object files
68 (declaim (type function *target-compile-file*))
69 (defvar *target-compile-file*)
71 ;;; designator for a function with the same calling convention as
72 ;;; SB-C:ASSEMBLE-FILE, to be used to translate assembly files into target
73 ;;; object files
74 (defvar *target-assemble-file*)
76 ;;;; some tools
78 ;;; Take the file named X and make it into a file named Y. Sorta like
79 ;;; UNIX, and unlike Common Lisp's bare RENAME-FILE, we don't allow
80 ;;; information from the original filename to influence the final
81 ;;; filename. (The reason that it's only sorta like UNIX is that in
82 ;;; UNIX "mv foo bar/" will work, but the analogous
83 ;;; (RENAME-FILE-A-LA-UNIX "foo" "bar/") should fail.)
84 ;;;
85 ;;; (This is a workaround for the weird behavior of Debian CMU CL
86 ;;; 2.4.6, where (RENAME-FILE "dir/x" "dir/y") tries to create a file
87 ;;; called "dir/dir/y". If that behavior goes away, then we should be
88 ;;; able to get rid of this function and use plain RENAME-FILE in the
89 ;;; COMPILE-STEM function above. -- WHN 19990321
90 (defun rename-file-a-la-unix (x y)
92 (let ((path ;; (Note that the TRUENAME expression here is lifted from an
93 ;; example in the ANSI spec for TRUENAME.)
94 (with-open-file (stream y :direction :output)
95 (close stream)
96 ;; From the ANSI spec: "In this case, the file is closed
97 ;; when the truename is tried, so the truename
98 ;; information is reliable."
99 (truename stream))))
100 (delete-file path)
101 (rename-file x path)))
102 (compile 'rename-file-a-la-unix)
104 ;;; a wrapper for compilation/assembly, used mostly to centralize
105 ;;; the procedure for finding full filenames from "stems"
107 ;;; Compile the source file whose basic name is STEM, using some
108 ;;; standard-for-the-SBCL-build-process procedures to generate the
109 ;;; full pathnames of source file and object file. Return the pathname
110 ;;; of the object file for STEM. Several &KEY arguments are accepted:
111 ;;; :SRC-PREFIX, :SRC-SUFFIX =
112 ;;; strings to be concatenated to STEM to produce source filename
113 ;;; :OBJ-PREFIX, :OBJ-SUFFIX =
114 ;;; strings to be concatenated to STEM to produce object filename
115 ;;; :TMP-OBJ-SUFFIX-SUFFIX =
116 ;;; string to be appended to the name of an object file to produce
117 ;;; the name of a temporary object file
118 ;;; :COMPILE-FILE, :IGNORE-FAILURE-P =
119 ;;; :COMPILE-FILE is a function to use for compiling the file
120 ;;; (with the same calling conventions as ANSI CL:COMPILE-FILE).
121 ;;; If the third return value (FAILURE-P) of this function is
122 ;;; true, a continuable error will be signalled, unless
123 ;;; :IGNORE-FAILURE-P is set, in which case only a warning will be
124 ;;; signalled.
125 (defun compile-stem (stem
126 &key
127 (obj-prefix "")
128 (obj-suffix (error "missing OBJ-SUFFIX"))
129 (tmp-obj-suffix-suffix "-tmp")
130 (src-prefix "")
131 (src-suffix ".lisp")
132 (compile-file #'compile-file)
133 trace-file
134 ignore-failure-p)
136 (declare (type function compile-file))
138 (let* (;; KLUDGE: Note that this CONCATENATE 'STRING stuff is not The Common
139 ;; Lisp Way, although it works just fine for common UNIX environments.
140 ;; Should it come to pass that the system is ported to environments
141 ;; where version numbers and so forth become an issue, it might become
142 ;; urgent to rewrite this using the fancy Common Lisp PATHNAME
143 ;; machinery instead of just using strings. In the absence of such a
144 ;; port, it might or might be a good idea to do the rewrite.
145 ;; -- WHN 19990815
146 (src (concatenate 'string src-prefix stem src-suffix))
147 (obj (concatenate 'string obj-prefix stem obj-suffix))
148 (tmp-obj (concatenate 'string obj tmp-obj-suffix-suffix)))
150 (ensure-directories-exist obj :verbose t)
152 ;; We're about to set about building a new object file. First, we
153 ;; delete any preexisting object file in order to avoid confusing
154 ;; ourselves later should we happen to bail out of compilation
155 ;; with an error.
156 (when (probe-file obj)
157 (delete-file obj))
159 ;; Original comment:
161 ;; Work around a bug in CLISP 1999-01-08 #'COMPILE-FILE: CLISP
162 ;; mangles relative pathnames passed as :OUTPUT-FILE arguments,
163 ;; but works OK with absolute pathnames.
165 ;; following discussion on cmucl-imp 2002-07
166 ;; "COMPILE-FILE-PATHNAME", it would seem safer to deal with
167 ;; absolute pathnames all the time; it is no longer clear that the
168 ;; original behaviour in CLISP was wrong or that the current
169 ;; behaviour is right; and in any case absolutifying the pathname
170 ;; insulates us against changes of behaviour. -- CSR, 2002-08-09
171 (setf tmp-obj
172 ;; (Note that this idiom is taken from the ANSI
173 ;; documentation for TRUENAME.)
174 (with-open-file (stream tmp-obj
175 :direction :output
176 ;; Compilation would overwrite the
177 ;; temporary object anyway and overly
178 ;; strict implementations default
179 ;; to :ERROR.
180 :if-exists :supersede)
181 (close stream)
182 (truename stream)))
183 ;; and some compilers (e.g. OpenMCL) will complain if they're
184 ;; asked to write over a file that exists already (and isn't
185 ;; recognizeably a fasl file), so
186 (when (probe-file tmp-obj)
187 (delete-file tmp-obj))
189 ;; Try to use the compiler to generate a new temporary object file.
190 (flet ((report-recompile-restart (stream)
191 (format stream "Recompile file ~S" src))
192 (report-continue-restart (stream)
193 (format stream "Continue, using possibly bogus file ~S" obj)))
194 (tagbody
195 retry-compile-file
196 (multiple-value-bind (output-truename warnings-p failure-p)
197 (if trace-file
198 (funcall compile-file src :output-file tmp-obj
199 :trace-file t)
200 (funcall compile-file src :output-file tmp-obj ))
201 (declare (ignore warnings-p))
202 (cond ((not output-truename)
203 (error "couldn't compile ~S" src))
204 (failure-p
205 (if ignore-failure-p
206 (warn "ignoring FAILURE-P return value from compilation of ~S"
207 src)
208 (unwind-protect
209 (restart-case
210 (error "FAILURE-P was set when creating ~S."
211 obj)
212 (recompile ()
213 :report report-recompile-restart
214 (go retry-compile-file))
215 (continue ()
216 :report report-continue-restart
217 (setf failure-p nil)))
218 ;; Don't leave failed object files lying around.
219 (when (and failure-p (probe-file tmp-obj))
220 (delete-file tmp-obj)
221 (format t "~&deleted ~S~%" tmp-obj)))))
222 ;; Otherwise: success, just fall through.
223 (t nil)))))
225 ;; If we get to here, compilation succeeded, so it's OK to rename
226 ;; the temporary output file to the permanent object file.
227 (rename-file-a-la-unix tmp-obj obj)
229 ;; nice friendly traditional return value
230 (pathname obj)))
231 (compile 'compile-stem)
233 ;;; other miscellaneous tools
234 (load "src/cold/read-from-file.lisp")
235 (load "src/cold/rename-package-carefully.lisp")
236 (load "src/cold/with-stuff.lisp")
238 ;;; Try to minimize/conceal any non-standardness of the host Common Lisp.
239 (load "src/cold/ansify.lisp")
241 ;;;; special read-macros for building the cold system (and even for
242 ;;;; building some of our tools for building the cold system)
244 (load "src/cold/shebang.lisp")
246 ;;; When cross-compiling, the *FEATURES* set for the target Lisp is
247 ;;; not in general the same as the *FEATURES* set for the host Lisp.
248 ;;; In order to refer to target features specifically, we refer to
249 ;;; *SHEBANG-FEATURES* instead of *FEATURES*, and use the #!+ and #!-
250 ;;; readmacros instead of the ordinary #+ and #- readmacros.
251 (setf *shebang-features*
252 (let* ((default-features
253 (append (read-from-file "base-target-features.lisp-expr")
254 (read-from-file "local-target-features.lisp-expr")))
255 (customizer-file-name "customize-target-features.lisp")
256 (customizer (if (probe-file customizer-file-name)
257 (compile nil
258 (read-from-file customizer-file-name))
259 #'identity)))
260 (funcall customizer default-features)))
261 (let ((*print-length* nil)
262 (*print-level* nil))
263 (format t
264 "target features *SHEBANG-FEATURES*=~@<~S~:>~%"
265 *shebang-features*))
267 (defvar *shebang-backend-subfeatures*
268 (let* ((default-subfeatures nil)
269 (customizer-file-name "customize-backend-subfeatures.lisp")
270 (customizer (if (probe-file customizer-file-name)
271 (compile nil
272 (read-from-file customizer-file-name))
273 #'identity)))
274 (funcall customizer default-subfeatures)))
275 (let ((*print-length* nil)
276 (*print-level* nil))
277 (format t
278 "target backend-subfeatures *SHEBANG-BACKEND-FEATURES*=~@<~S~:>~%"
279 *shebang-backend-subfeatures*))
281 ;;;; cold-init-related PACKAGE and SYMBOL tools
283 ;;; Once we're done with possibly ANSIfying the COMMON-LISP package,
284 ;;; it's probably a mistake if we change it (beyond changing the
285 ;;; values of special variables such as *** and +, anyway). Set up
286 ;;; machinery to warn us when/if we change it.
288 ;;; All code depending on this is itself dependent on #!+SB-SHOW.
289 #!+sb-show
290 (progn
291 (load "src/cold/snapshot.lisp")
292 (defvar *cl-snapshot* (take-snapshot "COMMON-LISP")))
294 ;;;; master list of source files and their properties
296 ;;; flags which can be used to describe properties of source files
297 (defparameter
298 *expected-stem-flags*
299 '(;; meaning: This file is not to be compiled when building the
300 ;; cross-compiler which runs on the host ANSI Lisp. ("not host
301 ;; code", i.e. does not execute on host -- but may still be
302 ;; cross-compiled by the host, so that it executes on the target)
303 :not-host
304 ;; meaning: This file is not to be compiled as part of the target
305 ;; SBCL. ("not target code" -- but still presumably host code,
306 ;; used to support the cross-compilation process)
307 :not-target
308 ;; meaning: The #'COMPILE-STEM argument :TRACE-FILE should be T.
309 ;; When the compiler is SBCL's COMPILE-FILE or something like it,
310 ;; compiling "foo.lisp" will generate "foo.trace" which contains lots
311 ;; of exciting low-level information about representation selection,
312 ;; VOPs used by the compiler, and bits of assembly.
313 :trace-file
314 ;; meaning: This file is to be processed with the SBCL assembler,
315 ;; not COMPILE-FILE. (Note that this doesn't make sense unless
316 ;; :NOT-HOST is also set, since the SBCL assembler doesn't exist
317 ;; while the cross-compiler is being built in the host ANSI Lisp.)
318 :assem
319 ;; meaning: The #'COMPILE-STEM argument called :IGNORE-FAILURE-P
320 ;; should be true. (This is a KLUDGE: I'd like to get rid of it.
321 ;; For now, it exists so that compilation can proceed through the
322 ;; legacy warnings in src/compiler/x86/array.lisp, which I've
323 ;; never figured out but which were apparently acceptable in CMU
324 ;; CL. Eventually, it would be great to just get rid of all
325 ;; warnings and remove support for this flag. -- WHN 19990323)
326 :ignore-failure-p))
328 (defparameter *stems-and-flags* (read-from-file "build-order.lisp-expr"))
330 (defmacro do-stems-and-flags ((stem flags) &body body)
331 (let ((stem-and-flags (gensym "STEM-AND-FLAGS")))
332 `(dolist (,stem-and-flags *stems-and-flags*)
333 (let ((,stem (first ,stem-and-flags))
334 (,flags (rest ,stem-and-flags)))
335 ,@body))))
337 ;;; Check for stupid typos in FLAGS list keywords.
338 (let ((stems (make-hash-table :test 'equal)))
339 (do-stems-and-flags (stem flags)
340 (if (gethash stem stems)
341 (error "duplicate stem ~S in *STEMS-AND-FLAGS*" stem)
342 (setf (gethash stem stems) t))
343 (let ((set-difference (set-difference flags *expected-stem-flags*)))
344 (when set-difference
345 (error "found unexpected flag(s) in *STEMS-AND-FLAGS*: ~S"
346 set-difference)))))
348 ;;;; tools to compile SBCL sources to create the cross-compiler
350 ;;; Execute function FN in an environment appropriate for compiling the
351 ;;; cross-compiler's source code in the cross-compilation host.
352 (defun in-host-compilation-mode (fn)
353 (declare (type function fn))
354 (let ((*features* (cons :sb-xc-host *features*))
355 ;; the CROSS-FLOAT-INFINITY-KLUDGE, as documented in
356 ;; base-target-features.lisp-expr:
357 (*shebang-features* (set-difference *shebang-features*
358 '(:sb-propagate-float-type
359 :sb-propagate-fun-type))))
360 (with-additional-nickname ("SB-XC" "SB!XC")
361 (funcall fn))))
362 (compile 'in-host-compilation-mode)
364 ;;; Process a file as source code for the cross-compiler, compiling it
365 ;;; (if necessary) in the appropriate environment, then loading it
366 ;;; into the cross-compilation host Common lisp.
367 (defun host-cload-stem (stem &key ignore-failure-p)
368 (let ((compiled-filename (in-host-compilation-mode
369 (lambda ()
370 (compile-stem
371 stem
372 :obj-prefix *host-obj-prefix*
373 :obj-suffix *host-obj-suffix*
374 :compile-file #'cl:compile-file
375 :ignore-failure-p ignore-failure-p)))))
376 (load compiled-filename)))
377 (compile 'host-cload-stem)
379 ;;; like HOST-CLOAD-STEM, except that we don't bother to compile
380 (defun host-load-stem (stem &key ignore-failure-p)
381 (declare (ignore ignore-failure-p)) ; (It's only relevant when
382 ;; compiling.) KLUDGE: It's untidy to have the knowledge of how to
383 ;; construct complete filenames from stems in here as well as in
384 ;; COMPILE-STEM. It should probably be factored out somehow. -- WHN
385 ;; 19990815
386 (load (concatenate 'simple-string *host-obj-prefix* stem *host-obj-suffix*)))
387 (compile 'host-load-stem)
389 ;;;; tools to compile SBCL sources to create object files which will
390 ;;;; be used to create the target SBCL .core file
392 ;;; Run the cross-compiler on a file in the source directory tree to
393 ;;; produce a corresponding file in the target object directory tree.
394 (defun target-compile-stem (stem &key assem-p ignore-failure-p trace-file)
395 (funcall *in-target-compilation-mode-fn*
396 (lambda ()
397 (compile-stem stem
398 :obj-prefix *target-obj-prefix*
399 :obj-suffix *target-obj-suffix*
400 :trace-file trace-file
401 :ignore-failure-p ignore-failure-p
402 :compile-file (if assem-p
403 *target-assemble-file*
404 *target-compile-file*)))))
405 (compile 'target-compile-stem)
407 ;;; (This function is not used by the build process, but is intended
408 ;;; for interactive use when experimenting with the system. It runs
409 ;;; the cross-compiler on test files with arbitrary filenames, not
410 ;;; necessarily in the source tree, e.g. in "/tmp".)
411 (defun target-compile-file (filename)
412 (funcall *in-target-compilation-mode-fn*
413 (lambda ()
414 (funcall *target-compile-file* filename))))
415 (compile 'target-compile-file)