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 (defun parse-make-host-parallelism (str)
27 (multiple-value-bind (value1 end
) (parse-integer str
:junk-allowed t
)
29 (let ((value2 (if (and value1
30 (< end
(1- (length str
))) ; ~ /,[\d]+/
31 (eql (char str end
) #\
,))
32 (parse-integer str
:start
(1+ end
)))))
33 ;; If only 1 integer, assume same parallelism for both passes.
36 ;; 0 means no parallelism. 1 means use at most one subjob,
37 ;; just in case you want to test the controlling loop.
38 (when (eql value1
0) (setq value1 nil
))
39 (when (eql value2
0) (setq value2 nil
))
40 ;; Parallelism on pass 1 works only if LOAD does not compile.
41 ;; Otherwise it's slower than compiling serially.
42 ;; (And this has only been tested with sb-fasteval, not sb-eval.)
43 (cons (and (find-package "SB-INTERPRETER") value1
)
46 (defvar *make-host-parallelism
*
48 (let ((envvar (sb-ext:posix-getenv
"SBCL_MAKE_PARALLEL")))
50 (parse-make-host-parallelism envvar
)))))
52 (defun make-host-1-parallelism () (car *make-host-parallelism
*))
53 (defun make-host-2-parallelism () (cdr *make-host-parallelism
*))
55 ;;; If TRUE, then COMPILE-FILE is being invoked only to process
56 ;;; :COMPILE-TOPLEVEL forms, not to produce an output file.
57 ;;; This is part of the implementation of parallelized make-host-2.
58 (defvar *compile-for-effect-only
* nil
)
60 ;;; prefixes for filename stems when cross-compiling. These are quite arbitrary
61 ;;; (although of course they shouldn't collide with anything we don't want to
62 ;;; write over). In particular, they can be either relative path names (e.g.
63 ;;; "host-objects/" or absolute pathnames (e.g. "/tmp/sbcl-xc-host-objects/").
65 ;;; The cross-compilation process will force the creation of these directories
66 ;;; by executing CL:ENSURE-DIRECTORIES-EXIST (on the xc host Common Lisp).
67 (defvar *host-obj-prefix
*)
68 (defvar *target-obj-prefix
*)
70 (defvar *target-obj-suffix
*
71 ;; Target fasl files are LOADed (actually only quasi-LOADed, in
72 ;; GENESIS) only by SBCL code, and it doesn't care about particular
73 ;; extensions, so we can use something arbitrary.
75 (defvar *target-assem-obj-suffix
*
76 ;; Target fasl files from SB!C:ASSEMBLE-FILE are LOADed via GENESIS.
77 ;; The source files are compiled once as assembly files and once as
78 ;; normal lisp files. In the past, they were kept separate by
79 ;; clever symlinking in the source tree, but that became less clean
80 ;; as ports to host environments without symlinks started appearing.
81 ;; In order to keep them separate, we have the assembled versions
82 ;; with a separate suffix.
85 ;;; a function of one functional argument, which calls its functional argument
86 ;;; in an environment suitable for compiling the target. (This environment
87 ;;; includes e.g. a suitable *FEATURES* value.)
88 (declaim (type function
*in-target-compilation-mode-fn
*))
89 (defvar *in-target-compilation-mode-fn
*)
91 ;;; a function with the same calling convention as CL:COMPILE-FILE, to be
92 ;;; used to translate ordinary Lisp source files into target object files
93 (declaim (type function
*target-compile-file
*))
94 (defvar *target-compile-file
*)
96 ;;; designator for a function with the same calling convention as
97 ;;; SB-C:ASSEMBLE-FILE, to be used to translate assembly files into target
99 (defvar *target-assemble-file
*)
103 ;;; Take the file named X and make it into a file named Y. Sorta like
104 ;;; UNIX, and unlike Common Lisp's bare RENAME-FILE, we don't allow
105 ;;; information from the original filename to influence the final
106 ;;; filename. (The reason that it's only sorta like UNIX is that in
107 ;;; UNIX "mv foo bar/" will work, but the analogous
108 ;;; (RENAME-FILE-A-LA-UNIX "foo" "bar/") should fail.)
110 ;;; (This is a workaround for the weird behavior of Debian CMU CL
111 ;;; 2.4.6, where (RENAME-FILE "dir/x" "dir/y") tries to create a file
112 ;;; called "dir/dir/y". If that behavior goes away, then we should be
113 ;;; able to get rid of this function and use plain RENAME-FILE in the
114 ;;; COMPILE-STEM function above. -- WHN 19990321
115 (defun rename-file-a-la-unix (x y
)
117 (let ((path ;; (Note that the TRUENAME expression here is lifted from an
118 ;; example in the ANSI spec for TRUENAME.)
119 (with-open-file (stream y
:direction
:output
)
121 ;; From the ANSI spec: "In this case, the file is closed
122 ;; when the truename is tried, so the truename
123 ;; information is reliable."
126 (rename-file x path
)))
127 (compile 'rename-file-a-la-unix
)
129 ;;; other miscellaneous tools
130 (load "src/cold/read-from-file.lisp")
131 (load "src/cold/rename-package-carefully.lisp")
132 (load "src/cold/with-stuff.lisp")
134 ;;; Try to minimize/conceal any non-standardness of the host Common Lisp.
135 (load "src/cold/ansify.lisp")
137 ;;;; special read-macros for building the cold system (and even for
138 ;;;; building some of our tools for building the cold system)
140 (load "src/cold/shebang.lisp")
142 ;;; When cross-compiling, the *FEATURES* set for the target Lisp is
143 ;;; not in general the same as the *FEATURES* set for the host Lisp.
144 ;;; In order to refer to target features specifically, we refer to
145 ;;; *SHEBANG-FEATURES* instead of *FEATURES*, and use the #!+ and #!-
146 ;;; readmacros instead of the ordinary #+ and #- readmacros.
147 (setf *shebang-features
*
148 (let* ((default-features
151 (read-from-file "local-target-features.lisp-expr"))
152 (read-from-file "base-target-features.lisp-expr")))
153 (customizer-file-name "customize-target-features.lisp")
154 (customizer (if (probe-file customizer-file-name
)
156 (read-from-file customizer-file-name
))
158 (funcall customizer default-features
)))
159 (let ((*print-length
* nil
)
162 "target features *SHEBANG-FEATURES*=~%~@<~S~:>~%"
165 (defvar *shebang-backend-subfeatures
*
166 (let* ((default-subfeatures nil
)
167 (customizer-file-name "customize-backend-subfeatures.lisp")
168 (customizer (if (probe-file customizer-file-name
)
170 (read-from-file customizer-file-name
))
172 (funcall customizer default-subfeatures
)))
173 (let ((*print-length
* nil
)
176 "target backend-subfeatures *SHEBANG-BACKEND-FEATURES*=~@<~S~:>~%"
177 *shebang-backend-subfeatures
*))
179 ;;; Call for effect of signaling an error if no target picked.
180 (target-platform-name)
182 ;;; You can get all the way through make-host-1 without either one of these
183 ;;; features, but then 'bit-bash' will fail to cross-compile.
184 (unless (intersection '(:big-endian
:little-endian
) *shebang-features
*)
185 (warn "You'll have bad time without either endian-ness defined"))
187 ;;; Some feature combinations simply don't work, and sometimes don't
188 ;;; fail until quite a ways into the build. Pick off the more obvious
189 ;;; combinations now, and provide a description of what the actual
190 ;;; failure is (not always obvious from when the build fails).
191 (let ((feature-compatibility-tests
192 '(("(and sb-thread (not gencgc))"
193 ":SB-THREAD requires :GENCGC")
194 ("(and sb-thread (not (or ppc x86 x86-64 arm64)))"
195 ":SB-THREAD not supported on selected architecture")
196 ("(and gencgc cheneygc)"
197 ":GENCGC and :CHENEYGC are incompatible")
198 ("(and cheneygc (not (or alpha arm hppa mips ppc sparc)))"
199 ":CHENEYGC not supported on selected architecture")
200 ("(and gencgc (not (or sparc ppc x86 x86-64 arm arm64)))"
201 ":GENCGC not supported on selected architecture")
202 ("(not (or gencgc cheneygc))"
203 "One of :GENCGC or :CHENEYGC must be enabled")
204 ("(and sb-dynamic-core (not linkage-table))"
205 ":SB-DYNAMIC-CORE requires :LINKAGE-TABLE")
206 ("(and relocatable-heap (or cheneygc win32))"
207 "Relocatable heap requires gencgc + not win32")
208 ("(and sb-linkable-runtime (not sb-dynamic-core))"
209 ":SB-LINKABLE-RUNTIME requires :SB-DYNAMIC-CORE")
210 ("(and sb-linkable-runtime (not (or x86 x86-64)))"
211 ":SB-LINKABLE-RUNTIME not supported on selected architecture")
212 ("(and sb-linkable-runtime (not (or darwin linux win32)))"
213 ":SB-LINKABLE-RUNTIME not supported on selected operating system")
214 ("(and sb-eval sb-fasteval)"
215 ;; It sorta kinda works to have both, but there should be no need,
216 ;; and it's not really supported.
217 "At most one interpreter can be selected")
218 ("(and immobile-space (not x86-64))"
219 ":IMMOBILE-SPACE is supported only on x86-64")
220 ("(and compact-instance-header (not immobile-space))"
221 ":COMPACT-INSTANCE-HEADER requires :IMMOBILE-SPACE feature")
222 ("(and immobile-code (not immobile-space))"
223 ":IMMOBILE-CODE requires :IMMOBILE-SPACE feature")
224 ("(and immobile-symbols (not immobile-space))"
225 ":IMMOBILE-SYMBOLS requires :IMMOBILE-SPACE feature")
226 ;; There is still hope to make multithreading on DragonFly x86-64
227 ("(and sb-thread x86 dragonfly)"
228 ":SB-THREAD not supported on selected architecture")))
229 (failed-test-descriptions nil
))
230 (dolist (test feature-compatibility-tests
)
231 (let ((*features
* *shebang-features
*))
232 (when (read-from-string (concatenate 'string
"#+" (first test
) "T NIL"))
233 (push (second test
) failed-test-descriptions
))))
234 (when failed-test-descriptions
235 (error "Feature compatibility check failed, ~S"
236 failed-test-descriptions
)))
238 ;;;; cold-init-related PACKAGE and SYMBOL tools
240 ;;; Once we're done with possibly ANSIfying the COMMON-LISP package,
241 ;;; it's probably a mistake if we change it (beyond changing the
242 ;;; values of special variables such as *** and +, anyway). Set up
243 ;;; machinery to warn us when/if we change it.
245 ;;; All code depending on this is itself dependent on #!+SB-SHOW.
248 (load "src/cold/snapshot.lisp")
249 (defvar *cl-snapshot
* (take-snapshot "COMMON-LISP")))
251 ;;;; master list of source files and their properties
253 ;;; flags which can be used to describe properties of source files
255 *expected-stem-flags
*
256 '(;; meaning: This file is not to be compiled when building the
257 ;; cross-compiler which runs on the host ANSI Lisp. ("not host
258 ;; code", i.e. does not execute on host -- but may still be
259 ;; cross-compiled by the host, so that it executes on the target)
261 ;; meaning: This file is not to be compiled as part of the target
262 ;; SBCL. ("not target code" -- but still presumably host code,
263 ;; used to support the cross-compilation process)
265 ;; meaning: This file must always be compiled by 'slam.lisp' even if
266 ;; the object is not out of date with respect to its source.
267 ;; Necessary if there are compile-time-too effects that are not
268 ;; reflected into make-host-2 by load-time actions of make-host-1.
270 ;; meaning: The #'COMPILE-STEM argument :TRACE-FILE should be T.
271 ;; When the compiler is SBCL's COMPILE-FILE or something like it,
272 ;; compiling "foo.lisp" will generate "foo.trace" which contains lots
273 ;; of exciting low-level information about representation selection,
274 ;; VOPs used by the compiler, and bits of assembly.
276 ;; meaning: This file is to be processed with the SBCL assembler,
277 ;; not COMPILE-FILE. (Note that this doesn't make sense unless
278 ;; :NOT-HOST is also set, since the SBCL assembler doesn't exist
279 ;; while the cross-compiler is being built in the host ANSI Lisp.)
281 ;; meaning: The #'COMPILE-STEM argument called :IGNORE-FAILURE-P
282 ;; should be true. (This is a KLUDGE: I'd like to get rid of it.
283 ;; For now, it exists so that compilation can proceed through the
284 ;; legacy warnings in src/compiler/x86/array.lisp, which I've
285 ;; never figured out but which were apparently acceptable in CMU
286 ;; CL. Eventually, it would be great to just get rid of all
287 ;; warnings and remove support for this flag. -- WHN 19990323)
289 ;; meaning: Build this file, but don't put it on the list for
290 ;; genesis to include in the cold core.
293 (defvar *array-to-specialization
* (make-hash-table :test
#'eq
))
295 (defmacro do-stems-and-flags
((stem flags
) &body body
)
296 (let ((stem-and-flags (gensym "STEM-AND-FLAGS")))
297 `(dolist (,stem-and-flags
(get-stems-and-flags))
298 (let ((,stem
(first ,stem-and-flags
))
299 (,flags
(rest ,stem-and-flags
)))
301 (clrhash *array-to-specialization
*)))))
303 ;;; Given a STEM, remap the path component "/target/" to a suitable
304 ;;; target directory.
305 (defun stem-remap-target (stem)
306 (let ((position (search "/target/" stem
)))
309 (subseq stem
0 (1+ position
))
310 (target-platform-name)
311 (subseq stem
(+ position
7)))
313 (compile 'stem-remap-target
)
315 ;;; Determine the source path for a stem.
316 (defun stem-source-path (stem)
317 (concatenate 'string
"" (stem-remap-target stem
) ".lisp"))
318 (compile 'stem-source-path
)
320 ;;; Determine the object path for a stem/flags/mode combination.
321 (defun stem-object-path (stem flags mode
)
323 (obj-prefix obj-suffix
)
326 ;; On some xc hosts, it's impossible to LOAD a fasl file unless it
327 ;; has the same extension that the host uses for COMPILE-FILE
328 ;; output, so we have to be careful to use the xc host's preferred
330 (values *host-obj-prefix
*
331 (concatenate 'string
"."
332 (pathname-type (compile-file-pathname stem
)))))
333 (:target-compile
(values *target-obj-prefix
*
334 (if (find :assem flags
)
335 *target-assem-obj-suffix
*
336 *target-obj-suffix
*))))
337 (concatenate 'string obj-prefix
(stem-remap-target stem
) obj-suffix
)))
338 (compile 'stem-object-path
)
340 (defvar *stems-and-flags
* nil
)
341 ;;; Check for stupid typos in FLAGS list keywords.
342 (defun get-stems-and-flags ()
343 (when *stems-and-flags
*
344 (return-from get-stems-and-flags
*stems-and-flags
*))
345 (setf *stems-and-flags
* (read-from-file "build-order.lisp-expr"))
346 (let ((stems (make-hash-table :test
'equal
)))
347 (do-stems-and-flags (stem flags
)
348 ;; We do duplicate stem comparison based on the object path in
349 ;; order to cover the case of stems with an :assem flag, which
350 ;; have two entries but separate object paths for each. KLUDGE:
351 ;; We have to bind *target-obj-prefix* here because it's normally
352 ;; set up later in the build process and we don't actually care
353 ;; what it is so long as it doesn't change while we're checking
354 ;; for duplicate stems.
355 (let* ((*target-obj-prefix
* "")
356 (object-path (stem-object-path stem flags
:target-compile
)))
357 (if (gethash object-path stems
)
358 (error "duplicate stem ~S in *STEMS-AND-FLAGS*" stem
)
359 (setf (gethash object-path stems
) t
)))
360 ;; FIXME: We should make sure that the :assem flag is only used
361 ;; when paired with :not-host.
362 (let ((set-difference (set-difference flags
*expected-stem-flags
*)))
364 (error "found unexpected flag(s) in *STEMS-AND-FLAGS*: ~S"
368 ;;;; tools to compile SBCL sources to create the cross-compiler
370 ;;; a wrapper for compilation/assembly, used mostly to centralize
371 ;;; the procedure for finding full filenames from "stems"
373 ;;; Compile the source file whose basic name is STEM, using some
374 ;;; standard-for-the-SBCL-build-process procedures to generate the
375 ;;; full pathnames of source file and object file. Return the pathname
376 ;;; of the object file for STEM.
378 ;;; STEM and FLAGS are as per DO-STEMS-AND-FLAGS. MODE is one of
379 ;;; :HOST-COMPILE and :TARGET-COMPILE.
380 (defun compile-stem (stem flags mode
)
382 (let* (;; KLUDGE: Note that this CONCATENATE 'STRING stuff is not The Common
383 ;; Lisp Way, although it works just fine for common UNIX environments.
384 ;; Should it come to pass that the system is ported to environments
385 ;; where version numbers and so forth become an issue, it might become
386 ;; urgent to rewrite this using the fancy Common Lisp PATHNAME
387 ;; machinery instead of just using strings. In the absence of such a
388 ;; port, it might or might be a good idea to do the rewrite.
390 (src (stem-source-path stem
))
391 (obj (stem-object-path stem flags mode
))
392 ;; Compile-for-effect happens simultaneously with a forked compile,
393 ;; so we need the for-effect output not to stomp on the real output.
395 (concatenate 'string obj
396 (if *compile-for-effect-only
* "-scratch" "-tmp")))
398 (compile-file (ecase mode
399 (:host-compile
#'compile-file
)
400 (:target-compile
(if (find :assem flags
)
401 *target-assemble-file
*
402 *target-compile-file
*))))
403 (trace-file (find :trace-file flags
))
404 (ignore-failure-p (find :ignore-failure-p flags
)))
405 (declare (type function compile-file
))
407 (ensure-directories-exist obj
:verbose t
)
409 ;; We're about to set about building a new object file. First, we
410 ;; delete any preexisting object file in order to avoid confusing
411 ;; ourselves later should we happen to bail out of compilation
413 (when (and (not *compile-for-effect-only
*) (probe-file obj
))
418 ;; Work around a bug in CLISP 1999-01-08 #'COMPILE-FILE: CLISP
419 ;; mangles relative pathnames passed as :OUTPUT-FILE arguments,
420 ;; but works OK with absolute pathnames.
422 ;; following discussion on cmucl-imp 2002-07
423 ;; "COMPILE-FILE-PATHNAME", it would seem safer to deal with
424 ;; absolute pathnames all the time; it is no longer clear that the
425 ;; original behaviour in CLISP was wrong or that the current
426 ;; behaviour is right; and in any case absolutifying the pathname
427 ;; insulates us against changes of behaviour. -- CSR, 2002-08-09
429 ;; (Note that this idiom is taken from the ANSI
430 ;; documentation for TRUENAME.)
431 (with-open-file (stream tmp-obj
433 ;; Compilation would overwrite the
434 ;; temporary object anyway and overly
435 ;; strict implementations default
437 :if-exists
:supersede
)
440 ;; and some compilers (e.g. OpenMCL) will complain if they're
441 ;; asked to write over a file that exists already (and isn't
442 ;; recognizeably a fasl file), so
443 (when (probe-file tmp-obj
)
444 (delete-file tmp-obj
))
446 ;; Try to use the compiler to generate a new temporary object file.
447 (flet ((report-recompile-restart (stream)
448 (format stream
"Recompile file ~S" src
))
449 (report-continue-restart (stream)
450 (format stream
"Continue, using possibly bogus file ~S" obj
)))
453 (multiple-value-bind (output-truename warnings-p failure-p
)
456 (funcall compile-file src
:output-file tmp-obj
457 :trace-file t
:allow-other-keys t
)
458 (funcall compile-file src
:output-file tmp-obj
))
460 :report report-recompile-restart
461 (go retry-compile-file
)))
462 (declare (ignore warnings-p
))
463 (cond ((not output-truename
)
464 (error "couldn't compile ~S" src
))
467 (warn "ignoring FAILURE-P return value from compilation of ~S"
471 (error "FAILURE-P was set when creating ~S."
474 :report report-recompile-restart
475 (go retry-compile-file
))
477 :report report-continue-restart
478 (setf failure-p nil
)))
479 ;; Don't leave failed object files lying around.
480 (when (and failure-p
(probe-file tmp-obj
))
481 (delete-file tmp-obj
)
482 (format t
"~&deleted ~S~%" tmp-obj
)))))
483 ;; Otherwise: success, just fall through.
486 ;; If we get to here, compilation succeeded, so it's OK to rename
487 ;; the temporary output file to the permanent object file.
488 (cond ((not *compile-for-effect-only
*)
489 (rename-file-a-la-unix tmp-obj obj
))
490 ((probe-file tmp-obj
)
491 (delete-file tmp-obj
))) ; clean up the trash
493 ;; nice friendly traditional return value
495 (compile 'compile-stem
)
497 ;;; Execute function FN in an environment appropriate for compiling the
498 ;;; cross-compiler's source code in the cross-compilation host.
499 (defun in-host-compilation-mode (fn)
500 (declare (type function fn
))
501 (let ((*features
* (cons :sb-xc-host
*features
*)))
502 (with-additional-nickname ("SB-XC" "SB!XC")
504 (compile 'in-host-compilation-mode
)
506 ;;; Process a file as source code for the cross-compiler, compiling it
507 ;;; (if necessary) in the appropriate environment, then loading it
508 ;;; into the cross-compilation host Common lisp.
509 (defun host-cload-stem (stem flags
)
511 (with-simple-restart (recompile "Recompile")
512 (let ((compiled-filename (in-host-compilation-mode
514 (compile-stem stem flags
:host-compile
)))))
516 (load compiled-filename
))))))
517 (compile 'host-cload-stem
)
519 ;;; like HOST-CLOAD-STEM, except that we don't bother to compile
520 (defun host-load-stem (stem flags
)
522 (with-simple-restart (recompile "Reload")
523 (return (load (stem-object-path stem flags
:host-compile
))))))
524 (compile 'host-load-stem
)
526 ;;;; tools to compile SBCL sources to create object files which will
527 ;;;; be used to create the target SBCL .core file
529 ;;; Run the cross-compiler on a file in the source directory tree to
530 ;;; produce a corresponding file in the target object directory tree.
531 (defun target-compile-stem (stem flags
)
532 (funcall *in-target-compilation-mode-fn
*
534 (compile-stem stem flags
:target-compile
))))
535 (compile 'target-compile-stem
)
537 ;;; (This function is not used by the build process, but is intended
538 ;;; for interactive use when experimenting with the system. It runs
539 ;;; the cross-compiler on test files with arbitrary filenames, not
540 ;;; necessarily in the source tree, e.g. in "/tmp".)
541 (defun target-compile-file (filename)
542 (funcall *in-target-compilation-mode-fn
*
544 (funcall *target-compile-file
* filename
))))
545 (compile 'target-compile-file
)