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 win32 (not (and sb-thread
205 sb-safepoint sb-thruption sb-wtimer
207 ":SB-WIN32 requires :SB-THREAD and related features")
208 ("(and sb-dynamic-core (not (and linkage-table sb-thread)))"
209 ;; Subtle memory corruption follows when sb-dynamic-core is
210 ;; active, and non-threaded allocation routines have not been
211 ;; updated to take the additional indirection into account.
212 ;; Let's avoid this unusual combination.
213 ":SB-DYNAMIC-CORE requires :LINKAGE-TABLE and :SB-THREAD")
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 ;; There is still hope to make multithreading on DragonFly x86-64
219 ("(and sb-thread x86 dragonfly)"
220 ":SB-THREAD not supported on selected architecture")))
221 (failed-test-descriptions nil
))
222 (dolist (test feature-compatibility-tests
)
223 (let ((*features
* *shebang-features
*))
224 (when (read-from-string (concatenate 'string
"#+" (first test
) "T NIL"))
225 (push (second test
) failed-test-descriptions
))))
226 (when failed-test-descriptions
227 (error "Feature compatibility check failed, ~S"
228 failed-test-descriptions
)))
230 ;;;; cold-init-related PACKAGE and SYMBOL tools
232 ;;; Once we're done with possibly ANSIfying the COMMON-LISP package,
233 ;;; it's probably a mistake if we change it (beyond changing the
234 ;;; values of special variables such as *** and +, anyway). Set up
235 ;;; machinery to warn us when/if we change it.
237 ;;; All code depending on this is itself dependent on #!+SB-SHOW.
240 (load "src/cold/snapshot.lisp")
241 (defvar *cl-snapshot
* (take-snapshot "COMMON-LISP")))
243 ;;;; master list of source files and their properties
245 ;;; flags which can be used to describe properties of source files
247 *expected-stem-flags
*
248 '(;; meaning: This file is not to be compiled when building the
249 ;; cross-compiler which runs on the host ANSI Lisp. ("not host
250 ;; code", i.e. does not execute on host -- but may still be
251 ;; cross-compiled by the host, so that it executes on the target)
253 ;; meaning: This file is not to be compiled as part of the target
254 ;; SBCL. ("not target code" -- but still presumably host code,
255 ;; used to support the cross-compilation process)
257 ;; meaning: This file must always be compiled by 'slam.lisp' even if
258 ;; the object is not out of date with respect to its source.
259 ;; Necessary if there are compile-time-too effects that are not
260 ;; reflected into make-host-2 by load-time actions of make-host-1.
262 ;; meaning: The #'COMPILE-STEM argument :TRACE-FILE should be T.
263 ;; When the compiler is SBCL's COMPILE-FILE or something like it,
264 ;; compiling "foo.lisp" will generate "foo.trace" which contains lots
265 ;; of exciting low-level information about representation selection,
266 ;; VOPs used by the compiler, and bits of assembly.
268 ;; meaning: This file is to be processed with the SBCL assembler,
269 ;; not COMPILE-FILE. (Note that this doesn't make sense unless
270 ;; :NOT-HOST is also set, since the SBCL assembler doesn't exist
271 ;; while the cross-compiler is being built in the host ANSI Lisp.)
273 ;; meaning: The #'COMPILE-STEM argument called :IGNORE-FAILURE-P
274 ;; should be true. (This is a KLUDGE: I'd like to get rid of it.
275 ;; For now, it exists so that compilation can proceed through the
276 ;; legacy warnings in src/compiler/x86/array.lisp, which I've
277 ;; never figured out but which were apparently acceptable in CMU
278 ;; CL. Eventually, it would be great to just get rid of all
279 ;; warnings and remove support for this flag. -- WHN 19990323)
281 ;; meaning: Build this file, but don't put it on the list for
282 ;; genesis to include in the cold core.
285 (defparameter *stems-and-flags
* (read-from-file "build-order.lisp-expr"))
287 (defvar *array-to-specialization
* (make-hash-table :test
#'eq
))
289 (defmacro do-stems-and-flags
((stem flags
) &body body
)
290 (let ((stem-and-flags (gensym "STEM-AND-FLAGS")))
291 `(dolist (,stem-and-flags
*stems-and-flags
*)
292 (let ((,stem
(first ,stem-and-flags
))
293 (,flags
(rest ,stem-and-flags
)))
295 (clrhash *array-to-specialization
*)))))
297 ;;; Given a STEM, remap the path component "/target/" to a suitable
298 ;;; target directory.
299 (defun stem-remap-target (stem)
300 (let ((position (search "/target/" stem
)))
303 (subseq stem
0 (1+ position
))
304 (target-platform-name)
305 (subseq stem
(+ position
7)))
307 (compile 'stem-remap-target
)
309 ;;; Determine the source path for a stem.
310 (defun stem-source-path (stem)
311 (concatenate 'string
"" (stem-remap-target stem
) ".lisp"))
312 (compile 'stem-source-path
)
314 ;;; Determine the object path for a stem/flags/mode combination.
315 (defun stem-object-path (stem flags mode
)
317 (obj-prefix obj-suffix
)
320 ;; On some xc hosts, it's impossible to LOAD a fasl file unless it
321 ;; has the same extension that the host uses for COMPILE-FILE
322 ;; output, so we have to be careful to use the xc host's preferred
324 (values *host-obj-prefix
*
325 (concatenate 'string
"."
326 (pathname-type (compile-file-pathname stem
)))))
327 (:target-compile
(values *target-obj-prefix
*
328 (if (find :assem flags
)
329 *target-assem-obj-suffix
*
330 *target-obj-suffix
*))))
331 (concatenate 'string obj-prefix
(stem-remap-target stem
) obj-suffix
)))
332 (compile 'stem-object-path
)
334 ;;; Check for stupid typos in FLAGS list keywords.
335 (let ((stems (make-hash-table :test
'equal
)))
336 (do-stems-and-flags (stem flags
)
337 ;; We do duplicate stem comparison based on the object path in
338 ;; order to cover the case of stems with an :assem flag, which
339 ;; have two entries but separate object paths for each. KLUDGE:
340 ;; We have to bind *target-obj-prefix* here because it's normally
341 ;; set up later in the build process and we don't actually care
342 ;; what it is so long as it doesn't change while we're checking
343 ;; for duplicate stems.
344 (let* ((*target-obj-prefix
* "")
345 (object-path (stem-object-path stem flags
:target-compile
)))
346 (if (gethash object-path stems
)
347 (error "duplicate stem ~S in *STEMS-AND-FLAGS*" stem
)
348 (setf (gethash object-path stems
) t
)))
349 ;; FIXME: We should make sure that the :assem flag is only used
350 ;; when paired with :not-host.
351 (let ((set-difference (set-difference flags
*expected-stem-flags
*)))
353 (error "found unexpected flag(s) in *STEMS-AND-FLAGS*: ~S"
356 ;;;; tools to compile SBCL sources to create the cross-compiler
358 ;;; a wrapper for compilation/assembly, used mostly to centralize
359 ;;; the procedure for finding full filenames from "stems"
361 ;;; Compile the source file whose basic name is STEM, using some
362 ;;; standard-for-the-SBCL-build-process procedures to generate the
363 ;;; full pathnames of source file and object file. Return the pathname
364 ;;; of the object file for STEM.
366 ;;; STEM and FLAGS are as per DO-STEMS-AND-FLAGS. MODE is one of
367 ;;; :HOST-COMPILE and :TARGET-COMPILE.
368 (defun compile-stem (stem flags mode
)
370 (let* (;; KLUDGE: Note that this CONCATENATE 'STRING stuff is not The Common
371 ;; Lisp Way, although it works just fine for common UNIX environments.
372 ;; Should it come to pass that the system is ported to environments
373 ;; where version numbers and so forth become an issue, it might become
374 ;; urgent to rewrite this using the fancy Common Lisp PATHNAME
375 ;; machinery instead of just using strings. In the absence of such a
376 ;; port, it might or might be a good idea to do the rewrite.
378 (src (stem-source-path stem
))
379 (obj (stem-object-path stem flags mode
))
380 ;; Compile-for-effect happens simultaneously with a forked compile,
381 ;; so we need the for-effect output not to stomp on the real output.
383 (concatenate 'string obj
384 (if *compile-for-effect-only
* "-scratch" "-tmp")))
386 (compile-file (ecase mode
387 (:host-compile
#'compile-file
)
388 (:target-compile
(if (find :assem flags
)
389 *target-assemble-file
*
390 *target-compile-file
*))))
391 (trace-file (find :trace-file flags
))
392 (ignore-failure-p (find :ignore-failure-p flags
)))
393 (declare (type function compile-file
))
395 (ensure-directories-exist obj
:verbose t
)
397 ;; We're about to set about building a new object file. First, we
398 ;; delete any preexisting object file in order to avoid confusing
399 ;; ourselves later should we happen to bail out of compilation
401 (when (and (not *compile-for-effect-only
*) (probe-file obj
))
406 ;; Work around a bug in CLISP 1999-01-08 #'COMPILE-FILE: CLISP
407 ;; mangles relative pathnames passed as :OUTPUT-FILE arguments,
408 ;; but works OK with absolute pathnames.
410 ;; following discussion on cmucl-imp 2002-07
411 ;; "COMPILE-FILE-PATHNAME", it would seem safer to deal with
412 ;; absolute pathnames all the time; it is no longer clear that the
413 ;; original behaviour in CLISP was wrong or that the current
414 ;; behaviour is right; and in any case absolutifying the pathname
415 ;; insulates us against changes of behaviour. -- CSR, 2002-08-09
417 ;; (Note that this idiom is taken from the ANSI
418 ;; documentation for TRUENAME.)
419 (with-open-file (stream tmp-obj
421 ;; Compilation would overwrite the
422 ;; temporary object anyway and overly
423 ;; strict implementations default
425 :if-exists
:supersede
)
428 ;; and some compilers (e.g. OpenMCL) will complain if they're
429 ;; asked to write over a file that exists already (and isn't
430 ;; recognizeably a fasl file), so
431 (when (probe-file tmp-obj
)
432 (delete-file tmp-obj
))
434 ;; Try to use the compiler to generate a new temporary object file.
435 (flet ((report-recompile-restart (stream)
436 (format stream
"Recompile file ~S" src
))
437 (report-continue-restart (stream)
438 (format stream
"Continue, using possibly bogus file ~S" obj
)))
441 (multiple-value-bind (output-truename warnings-p failure-p
)
443 (funcall compile-file src
:output-file tmp-obj
444 :trace-file t
:allow-other-keys t
)
445 (funcall compile-file src
:output-file tmp-obj
))
446 (declare (ignore warnings-p
))
447 (cond ((not output-truename
)
448 (error "couldn't compile ~S" src
))
451 (warn "ignoring FAILURE-P return value from compilation of ~S"
455 (error "FAILURE-P was set when creating ~S."
458 :report report-recompile-restart
459 (go retry-compile-file
))
461 :report report-continue-restart
462 (setf failure-p nil
)))
463 ;; Don't leave failed object files lying around.
464 (when (and failure-p
(probe-file tmp-obj
))
465 (delete-file tmp-obj
)
466 (format t
"~&deleted ~S~%" tmp-obj
)))))
467 ;; Otherwise: success, just fall through.
470 ;; If we get to here, compilation succeeded, so it's OK to rename
471 ;; the temporary output file to the permanent object file.
472 (cond ((not *compile-for-effect-only
*)
473 (rename-file-a-la-unix tmp-obj obj
))
474 ((probe-file tmp-obj
)
475 (delete-file tmp-obj
))) ; clean up the trash
477 ;; nice friendly traditional return value
479 (compile 'compile-stem
)
481 ;;; Execute function FN in an environment appropriate for compiling the
482 ;;; cross-compiler's source code in the cross-compilation host.
483 (defun in-host-compilation-mode (fn)
484 (declare (type function fn
))
485 (let ((*features
* (cons :sb-xc-host
*features
*))
486 ;; the CROSS-FLOAT-INFINITY-KLUDGE, as documented in
487 ;; base-target-features.lisp-expr:
488 (*shebang-features
* (set-difference *shebang-features
*
489 '(:sb-propagate-float-type
490 :sb-propagate-fun-type
))))
491 (with-additional-nickname ("SB-XC" "SB!XC")
493 (compile 'in-host-compilation-mode
)
495 ;;; Process a file as source code for the cross-compiler, compiling it
496 ;;; (if necessary) in the appropriate environment, then loading it
497 ;;; into the cross-compilation host Common lisp.
498 (defun host-cload-stem (stem flags
)
499 (let ((compiled-filename (in-host-compilation-mode
501 (compile-stem stem flags
:host-compile
)))))
502 (load compiled-filename
)))
503 (compile 'host-cload-stem
)
505 ;;; like HOST-CLOAD-STEM, except that we don't bother to compile
506 (defun host-load-stem (stem flags
)
507 (load (stem-object-path stem flags
:host-compile
)))
508 (compile 'host-load-stem
)
510 ;;;; tools to compile SBCL sources to create object files which will
511 ;;;; be used to create the target SBCL .core file
513 ;;; Run the cross-compiler on a file in the source directory tree to
514 ;;; produce a corresponding file in the target object directory tree.
515 (defun target-compile-stem (stem flags
)
516 (funcall *in-target-compilation-mode-fn
*
518 (compile-stem stem flags
:target-compile
))))
519 (compile 'target-compile-stem
)
521 ;;; (This function is not used by the build process, but is intended
522 ;;; for interactive use when experimenting with the system. It runs
523 ;;; the cross-compiler on test files with arbitrary filenames, not
524 ;;; necessarily in the source tree, e.g. in "/tmp".)
525 (defun target-compile-file (filename)
526 (funcall *in-target-compilation-mode-fn
*
528 (funcall *target-compile-file
* filename
))))
529 (compile 'target-compile-file
)
531 (defun make-assembler-package (pkg-name)
532 (when (find-package pkg-name
)
533 (delete-package pkg-name
))
534 (let ((pkg (make-package pkg-name
535 :use
'("CL" "SB!INT" "SB!EXT" "SB!KERNEL" "SB!VM"
536 "SB!SYS" ; for SAP accessors
537 ;; Dependence of the assembler on the compiler
538 ;; feels a bit backwards, but assembly needs
539 ;; TN-SC, TN-OFFSET, etc. because the compiler
540 ;; doesn't speak the assembler's language.
541 ;; Rather vice-versa.
543 ;; Both SB-ASSEM and SB-DISASSEM export these two symbols.
544 ;; Neither is shadowing-imported. If you need one, package-qualify it.
545 (shadow '("SEGMENT" "MAKE-SEGMENT") pkg
)
546 (use-package '("SB!ASSEM" "SB!DISASSEM") pkg
)