Disallow both a :translator and :expander for any type name.
[sbcl.git] / src / cold / shared.lisp
blob2def2dc02f06592da7a4b6612211312bc691a864
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 (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.
40 ".lisp-obj")
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.
49 ".assem-obj")
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
64 ;;; object files
65 (defvar *target-assemble-file*)
67 ;;;; some tools
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.)
75 ;;;
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)
86 (close stream)
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."
90 (truename stream))))
91 (delete-file path)
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 (funcall (compile
117 (read-from-file "local-target-features.lisp-expr"))
118 (read-from-file "base-target-features.lisp-expr")))
119 (customizer-file-name "customize-target-features.lisp")
120 (customizer (if (probe-file customizer-file-name)
121 (compile nil
122 (read-from-file customizer-file-name))
123 #'identity)))
124 (funcall customizer default-features)))
125 (let ((*print-length* nil)
126 (*print-level* nil))
127 (format t
128 "target features *SHEBANG-FEATURES*=~@<~S~:>~%"
129 *shebang-features*))
131 (defvar *shebang-backend-subfeatures*
132 (let* ((default-subfeatures nil)
133 (customizer-file-name "customize-backend-subfeatures.lisp")
134 (customizer (if (probe-file customizer-file-name)
135 (compile nil
136 (read-from-file customizer-file-name))
137 #'identity)))
138 (funcall customizer default-subfeatures)))
139 (let ((*print-length* nil)
140 (*print-level* nil))
141 (format t
142 "target backend-subfeatures *SHEBANG-BACKEND-FEATURES*=~@<~S~:>~%"
143 *shebang-backend-subfeatures*))
145 (let ((arch (intersection '(:alpha :arm :hppa :mips :ppc :sparc :x86 :x86-64)
146 *shebang-features*)))
147 (cond ((not arch) (error "No architecture selected"))
148 ((> (length arch) 1) (error "More than one architecture selected"))))
150 ;;; Some feature combinations simply don't work, and sometimes don't
151 ;;; fail until quite a ways into the build. Pick off the more obvious
152 ;;; combinations now, and provide a description of what the actual
153 ;;; failure is (not always obvious from when the build fails).
154 (let ((feature-compatibility-tests
155 '(("(and sb-thread (not gencgc))"
156 ":SB-THREAD requires :GENCGC")
157 ("(and sb-thread (not (or ppc x86 x86-64)))"
158 ":SB-THREAD not supported on selected architecture")
159 ("(and gencgc cheneygc)"
160 ":GENCGC and :CHENEYGC are incompatible")
161 ("(and cheneygc (not (or alpha arm hppa mips ppc sparc)))"
162 ":CHENEYGC not supported on selected architecture")
163 ("(and gencgc (not (or sparc ppc x86 x86-64 arm)))"
164 ":GENCGC not supported on selected architecture")
165 ("(not (or gencgc cheneygc))"
166 "One of :GENCGC or :CHENEYGC must be enabled")
167 ("(and win32 (not (and sb-thread
168 sb-safepoint sb-thruption sb-wtimer
169 sb-dynamic-core)))"
170 ":SB-WIN32 requires :SB-THREAD and related features")
171 ("(and sb-dynamic-core (not (and linkage-table sb-thread)))"
172 ;; Subtle memory corruption follows when sb-dynamic-core is
173 ;; active, and non-threaded allocation routines have not been
174 ;; updated to take the additional indirection into account.
175 ;; Let's avoid this unusual combination.
176 ":SB-DYNAMIC-CORE requires :LINKAGE-TABLE and :SB-THREAD")
177 ;; There is still hope to make multithreading on DragonFly x86-64
178 ("(and sb-thread x86 dragonfly)"
179 ":SB-THREAD not supported on selected architecture")))
180 (failed-test-descriptions nil))
181 (dolist (test feature-compatibility-tests)
182 (let ((*features* *shebang-features*))
183 (when (read-from-string (concatenate 'string "#+" (first test) "T NIL"))
184 (push (second test) failed-test-descriptions))))
185 (when failed-test-descriptions
186 (error "Feature compatibility check failed, ~S"
187 failed-test-descriptions)))
189 ;;;; cold-init-related PACKAGE and SYMBOL tools
191 ;;; Once we're done with possibly ANSIfying the COMMON-LISP package,
192 ;;; it's probably a mistake if we change it (beyond changing the
193 ;;; values of special variables such as *** and +, anyway). Set up
194 ;;; machinery to warn us when/if we change it.
196 ;;; All code depending on this is itself dependent on #!+SB-SHOW.
197 #!+sb-show
198 (progn
199 (load "src/cold/snapshot.lisp")
200 (defvar *cl-snapshot* (take-snapshot "COMMON-LISP")))
202 ;;;; master list of source files and their properties
204 ;;; flags which can be used to describe properties of source files
205 (defparameter
206 *expected-stem-flags*
207 '(;; meaning: This file is not to be compiled when building the
208 ;; cross-compiler which runs on the host ANSI Lisp. ("not host
209 ;; code", i.e. does not execute on host -- but may still be
210 ;; cross-compiled by the host, so that it executes on the target)
211 :not-host
212 ;; meaning: This file is not to be compiled as part of the target
213 ;; SBCL. ("not target code" -- but still presumably host code,
214 ;; used to support the cross-compilation process)
215 :not-target
216 ;; meaning: The #'COMPILE-STEM argument :TRACE-FILE should be T.
217 ;; When the compiler is SBCL's COMPILE-FILE or something like it,
218 ;; compiling "foo.lisp" will generate "foo.trace" which contains lots
219 ;; of exciting low-level information about representation selection,
220 ;; VOPs used by the compiler, and bits of assembly.
221 :trace-file
222 ;; meaning: This file is to be processed with the SBCL assembler,
223 ;; not COMPILE-FILE. (Note that this doesn't make sense unless
224 ;; :NOT-HOST is also set, since the SBCL assembler doesn't exist
225 ;; while the cross-compiler is being built in the host ANSI Lisp.)
226 :assem
227 ;; meaning: The #'COMPILE-STEM argument called :IGNORE-FAILURE-P
228 ;; should be true. (This is a KLUDGE: I'd like to get rid of it.
229 ;; For now, it exists so that compilation can proceed through the
230 ;; legacy warnings in src/compiler/x86/array.lisp, which I've
231 ;; never figured out but which were apparently acceptable in CMU
232 ;; CL. Eventually, it would be great to just get rid of all
233 ;; warnings and remove support for this flag. -- WHN 19990323)
234 :ignore-failure-p))
236 (defparameter *stems-and-flags* (read-from-file "build-order.lisp-expr"))
238 (defvar *array-to-specialization* (make-hash-table :test #'eq))
240 (defmacro do-stems-and-flags ((stem flags) &body body)
241 (let ((stem-and-flags (gensym "STEM-AND-FLAGS")))
242 `(dolist (,stem-and-flags *stems-and-flags*)
243 (let ((,stem (first ,stem-and-flags))
244 (,flags (rest ,stem-and-flags)))
245 ,@body
246 (clrhash *array-to-specialization*)))))
248 ;;; Given a STEM, remap the path component "/target/" to a suitable
249 ;;; target directory.
250 (defun stem-remap-target (stem)
251 (let ((position (search "/target/" stem)))
252 (if position
253 (concatenate 'string
254 (subseq stem 0 (1+ position))
255 #!+x86 "x86"
256 #!+x86-64 "x86-64"
257 #!+sparc "sparc"
258 #!+ppc "ppc"
259 #!+mips "mips"
260 #!+alpha "alpha"
261 #!+hppa "hppa"
262 #!+arm "arm"
263 (subseq stem (+ position 7)))
264 stem)))
265 (compile 'stem-remap-target)
267 ;;; Determine the source path for a stem.
268 (defun stem-source-path (stem)
269 (concatenate 'string "" (stem-remap-target stem) ".lisp"))
270 (compile 'stem-source-path)
272 ;;; Determine the object path for a stem/flags/mode combination.
273 (defun stem-object-path (stem flags mode)
274 (multiple-value-bind
275 (obj-prefix obj-suffix)
276 (ecase mode
277 (:host-compile
278 ;; On some xc hosts, it's impossible to LOAD a fasl file unless it
279 ;; has the same extension that the host uses for COMPILE-FILE
280 ;; output, so we have to be careful to use the xc host's preferred
281 ;; extension.
282 (values *host-obj-prefix*
283 (concatenate 'string "."
284 (pathname-type (compile-file-pathname stem)))))
285 (:target-compile (values *target-obj-prefix*
286 (if (find :assem flags)
287 *target-assem-obj-suffix*
288 *target-obj-suffix*))))
289 (concatenate 'string obj-prefix (stem-remap-target stem) obj-suffix)))
290 (compile 'stem-object-path)
292 ;;; Check for stupid typos in FLAGS list keywords.
293 (let ((stems (make-hash-table :test 'equal)))
294 (do-stems-and-flags (stem flags)
295 ;; We do duplicate stem comparison based on the object path in
296 ;; order to cover the case of stems with an :assem flag, which
297 ;; have two entries but separate object paths for each. KLUDGE:
298 ;; We have to bind *target-obj-prefix* here because it's normally
299 ;; set up later in the build process and we don't actually care
300 ;; what it is so long as it doesn't change while we're checking
301 ;; for duplicate stems.
302 (let* ((*target-obj-prefix* "")
303 (object-path (stem-object-path stem flags :target-compile)))
304 (if (gethash object-path stems)
305 (error "duplicate stem ~S in *STEMS-AND-FLAGS*" stem)
306 (setf (gethash object-path stems) t)))
307 ;; FIXME: We should make sure that the :assem flag is only used
308 ;; when paired with :not-host.
309 (let ((set-difference (set-difference flags *expected-stem-flags*)))
310 (when set-difference
311 (error "found unexpected flag(s) in *STEMS-AND-FLAGS*: ~S"
312 set-difference)))))
314 ;;;; tools to compile SBCL sources to create the cross-compiler
316 ;;; a wrapper for compilation/assembly, used mostly to centralize
317 ;;; the procedure for finding full filenames from "stems"
319 ;;; Compile the source file whose basic name is STEM, using some
320 ;;; standard-for-the-SBCL-build-process procedures to generate the
321 ;;; full pathnames of source file and object file. Return the pathname
322 ;;; of the object file for STEM.
324 ;;; STEM and FLAGS are as per DO-STEMS-AND-FLAGS. MODE is one of
325 ;;; :HOST-COMPILE and :TARGET-COMPILE.
326 (defun compile-stem (stem flags mode)
328 (let* (;; KLUDGE: Note that this CONCATENATE 'STRING stuff is not The Common
329 ;; Lisp Way, although it works just fine for common UNIX environments.
330 ;; Should it come to pass that the system is ported to environments
331 ;; where version numbers and so forth become an issue, it might become
332 ;; urgent to rewrite this using the fancy Common Lisp PATHNAME
333 ;; machinery instead of just using strings. In the absence of such a
334 ;; port, it might or might be a good idea to do the rewrite.
335 ;; -- WHN 19990815
336 (src (stem-source-path stem))
337 (obj (stem-object-path stem flags mode))
338 (tmp-obj (concatenate 'string obj "-tmp"))
340 (compile-file (ecase mode
341 (:host-compile #'compile-file)
342 (:target-compile (if (find :assem flags)
343 *target-assemble-file*
344 *target-compile-file*))))
345 (trace-file (find :trace-file flags))
346 (ignore-failure-p (find :ignore-failure-p flags)))
347 (declare (type function compile-file))
349 (ensure-directories-exist obj :verbose t)
351 ;; We're about to set about building a new object file. First, we
352 ;; delete any preexisting object file in order to avoid confusing
353 ;; ourselves later should we happen to bail out of compilation
354 ;; with an error.
355 (when (probe-file obj)
356 (delete-file obj))
358 ;; Original comment:
360 ;; Work around a bug in CLISP 1999-01-08 #'COMPILE-FILE: CLISP
361 ;; mangles relative pathnames passed as :OUTPUT-FILE arguments,
362 ;; but works OK with absolute pathnames.
364 ;; following discussion on cmucl-imp 2002-07
365 ;; "COMPILE-FILE-PATHNAME", it would seem safer to deal with
366 ;; absolute pathnames all the time; it is no longer clear that the
367 ;; original behaviour in CLISP was wrong or that the current
368 ;; behaviour is right; and in any case absolutifying the pathname
369 ;; insulates us against changes of behaviour. -- CSR, 2002-08-09
370 (setf tmp-obj
371 ;; (Note that this idiom is taken from the ANSI
372 ;; documentation for TRUENAME.)
373 (with-open-file (stream tmp-obj
374 :direction :output
375 ;; Compilation would overwrite the
376 ;; temporary object anyway and overly
377 ;; strict implementations default
378 ;; to :ERROR.
379 :if-exists :supersede)
380 (close stream)
381 (truename stream)))
382 ;; and some compilers (e.g. OpenMCL) will complain if they're
383 ;; asked to write over a file that exists already (and isn't
384 ;; recognizeably a fasl file), so
385 (when (probe-file tmp-obj)
386 (delete-file tmp-obj))
388 ;; Try to use the compiler to generate a new temporary object file.
389 (flet ((report-recompile-restart (stream)
390 (format stream "Recompile file ~S" src))
391 (report-continue-restart (stream)
392 (format stream "Continue, using possibly bogus file ~S" obj)))
393 (tagbody
394 retry-compile-file
395 (multiple-value-bind (output-truename warnings-p failure-p)
396 (if trace-file
397 (funcall compile-file src :output-file tmp-obj
398 :trace-file t :allow-other-keys t)
399 (funcall compile-file src :output-file tmp-obj))
400 (declare (ignore warnings-p))
401 (cond ((not output-truename)
402 (error "couldn't compile ~S" src))
403 (failure-p
404 (if ignore-failure-p
405 (warn "ignoring FAILURE-P return value from compilation of ~S"
406 src)
407 (unwind-protect
408 (restart-case
409 (error "FAILURE-P was set when creating ~S."
410 obj)
411 (recompile ()
412 :report report-recompile-restart
413 (go retry-compile-file))
414 (continue ()
415 :report report-continue-restart
416 (setf failure-p nil)))
417 ;; Don't leave failed object files lying around.
418 (when (and failure-p (probe-file tmp-obj))
419 (delete-file tmp-obj)
420 (format t "~&deleted ~S~%" tmp-obj)))))
421 ;; Otherwise: success, just fall through.
422 (t nil)))))
424 ;; If we get to here, compilation succeeded, so it's OK to rename
425 ;; the temporary output file to the permanent object file.
426 (rename-file-a-la-unix tmp-obj obj)
428 ;; nice friendly traditional return value
429 (pathname obj)))
430 (compile 'compile-stem)
432 ;;; Execute function FN in an environment appropriate for compiling the
433 ;;; cross-compiler's source code in the cross-compilation host.
434 (defun in-host-compilation-mode (fn)
435 (declare (type function fn))
436 (let ((*features* (cons :sb-xc-host *features*))
437 ;; the CROSS-FLOAT-INFINITY-KLUDGE, as documented in
438 ;; base-target-features.lisp-expr:
439 (*shebang-features* (set-difference *shebang-features*
440 '(:sb-propagate-float-type
441 :sb-propagate-fun-type))))
442 (with-additional-nickname ("SB-XC" "SB!XC")
443 (funcall fn))))
444 (compile 'in-host-compilation-mode)
446 ;;; Process a file as source code for the cross-compiler, compiling it
447 ;;; (if necessary) in the appropriate environment, then loading it
448 ;;; into the cross-compilation host Common lisp.
449 (defun host-cload-stem (stem flags)
450 (let ((compiled-filename (in-host-compilation-mode
451 (lambda ()
452 (compile-stem stem flags :host-compile)))))
453 (load compiled-filename)))
454 (compile 'host-cload-stem)
456 ;;; like HOST-CLOAD-STEM, except that we don't bother to compile
457 (defun host-load-stem (stem flags)
458 (load (stem-object-path stem flags :host-compile)))
459 (compile 'host-load-stem)
461 ;;;; tools to compile SBCL sources to create object files which will
462 ;;;; be used to create the target SBCL .core file
464 ;;; Run the cross-compiler on a file in the source directory tree to
465 ;;; produce a corresponding file in the target object directory tree.
466 (defun target-compile-stem (stem flags)
467 (funcall *in-target-compilation-mode-fn*
468 (lambda ()
469 (compile-stem stem flags :target-compile))))
470 (compile 'target-compile-stem)
472 ;;; (This function is not used by the build process, but is intended
473 ;;; for interactive use when experimenting with the system. It runs
474 ;;; the cross-compiler on test files with arbitrary filenames, not
475 ;;; necessarily in the source tree, e.g. in "/tmp".)
476 (defun target-compile-file (filename)
477 (funcall *in-target-compilation-mode-fn*
478 (lambda ()
479 (funcall *target-compile-file* filename))))
480 (compile 'target-compile-file)