From 769764836e62861b844a5c46fa73d560c59e112f Mon Sep 17 00:00:00 2001
From: Matt Birkholz <matt@birchwood-abbey.net>
Date: Thu, 2 Mar 2017 17:49:37 -0700
Subject: [PATCH] First load runtime/host-adapter when building a
 cross-compiler.

---
 src/Makefile.tools.in        |   4 ++
 src/compiler/base/utils.scm  |   7 ---
 src/cref/make.scm            |  50 ------------------
 src/runtime/host-adapter.scm | 100 +++++++++++++++++++++++++++++++++++
 4 files changed, 104 insertions(+), 57 deletions(-)
 create mode 100644 src/runtime/host-adapter.scm

diff --git a/src/Makefile.tools.in b/src/Makefile.tools.in
index 8e4a3a93a..565df7372 100644
--- a/src/Makefile.tools.in
+++ b/src/Makefile.tools.in
@@ -124,6 +124,8 @@ tools/compiler.com: cross-cref
 tools/compiler.com: cross-sf
 tools/compiler.com: kludgerous-star-parser
 	(echo '(begin' && \
+	 echo '  (with-working-directory-pathname "runtime"' && \
+	 echo '    (lambda () (load "host-adapter")))' && \
 	 echo '  (with-working-directory-pathname "cref"' && \
 	 echo '    (lambda () (load "make")))' && \
 	 echo '  (with-working-directory-pathname "sf"' && \
@@ -139,6 +141,8 @@ tools/syntaxer.com: cross-cref
 tools/syntaxer.com: cross-sf
 tools/syntaxer.com: kludgerous-star-parser
 	(echo '(begin' && \
+	 echo '  (with-working-directory-pathname "runtime"' && \
+	 echo '    (lambda () (load "host-adapter")))' && \
 	 echo '  (with-working-directory-pathname "cref"' && \
 	 echo '    (lambda () (load "make")))' && \
 	 echo '  (with-working-directory-pathname "sf"' && \
diff --git a/src/compiler/base/utils.scm b/src/compiler/base/utils.scm
index 1f0813b61..f5e0a36cd 100644
--- a/src/compiler/base/utils.scm
+++ b/src/compiler/base/utils.scm
@@ -31,13 +31,6 @@ USA.
 
 ;;;; Miscellaneous
 
-;; Temporary definition, for 9.2 hosts.
-(define (random-bytevector n #!optional state)
-  (let ((env (->environment '(runtime random-number))))
-    ((if (environment-bound? env 'random-byte-vector)
-	 (access random-byte-vector env)
-	 (access random-bytevector env)) n state)))
-
 (define (three-way-sort = set set* receiver)
   (let ((member? (member-procedure =)))
     (define (loop set set* receiver)
diff --git a/src/cref/make.scm b/src/cref/make.scm
index e4ff75665..797c64e88 100644
--- a/src/cref/make.scm
+++ b/src/cref/make.scm
@@ -32,54 +32,4 @@ USA.
   (lambda ()
     (load-package-set "cref")))
 
-;;; Patch the package loader in 9.2 host runtimes.
-(if (string-prefix? "9.2" (get-subsystem-version-string "Release"))
-    (eval
-     '(begin
-	(define (link-description? object)
-	  (and (vector? object)
-	       (cond ((fix:= (vector-length object) 2)
-		      (and (symbol? (vector-ref object 0))
-			   (package-name? (vector-ref object 1))))
-		     ((fix:= (vector-length object) 3)
-		      (and (symbol? (vector-ref object 0))
-			   (package-name? (vector-ref object 1))
-			   (symbol? (vector-ref object 2))))
-		     ((fix:= (vector-length object) 4)
-		      (and (symbol? (vector-ref object 0))
-			   (package-name? (vector-ref object 1))
-			   (symbol? (vector-ref object 2))
-			   (or (eq? #f (vector-ref object 3))
-			       (eq? 'deprecated (vector-ref object 3)))))
-		     (else #f))))
-	(define (create-links-from-description description)
-	  (let ((environment
-		 (find-package-environment (package-description/name description))))
-	    (let ((bindings (package-description/exports description)))
-	      (let ((n (vector-length bindings)))
-		(do ((i 0 (fix:+ i 1)))
-		    ((fix:= i n))
-		  (let ((binding (vector-ref bindings i)))
-		    (link-variables (find-package-environment (vector-ref binding 1))
-				    (if (fix:= (vector-length binding) 3)
-					(vector-ref binding 2)
-					(vector-ref binding 0))
-				    environment
-				    (vector-ref binding 0))))))
-	    (let ((bindings (package-description/imports description)))
-	      (let ((n (vector-length bindings)))
-		(do ((i 0 (fix:+ i 1)))
-		    ((fix:= i n))
-		  (let ((binding (vector-ref bindings i)))
-		    (let ((source-environment
-			   (find-package-environment (vector-ref binding 1)))
-			  (source-name
-			   (if (fix:>= (vector-length binding) 3)
-			       (vector-ref binding 2)
-			       (vector-ref binding 0))))
-		      (guarantee-binding source-environment source-name)
-		      (link-variables environment (vector-ref binding 0)
-				      source-environment source-name)))))))))
-     (->environment '(package))))
-
 (add-subsystem-identification! "CREF" '(2 4))
\ No newline at end of file
diff --git a/src/runtime/host-adapter.scm b/src/runtime/host-adapter.scm
new file mode 100644
index 000000000..c2f4d560d
--- /dev/null
+++ b/src/runtime/host-adapter.scm
@@ -0,0 +1,100 @@
+#| -*-Scheme-*-
+
+Copyright (C) 1986, 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994,
+    1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
+    2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016,
+    2017 Massachusetts Institute of Technology
+
+This file is part of MIT/GNU Scheme.
+
+MIT/GNU Scheme is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2 of the License, or (at
+your option) any later version.
+
+MIT/GNU Scheme is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with MIT/GNU Scheme; if not, write to the Free Software
+Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301,
+USA.
+
+|#
+
+;;;; Adapt host system.
+;;; package: ()
+
+(declare (usual-integrations))
+
+;;; This is loaded by the cross-syntaxer and cross-compiler to hack
+;;; the host runtime.
+
+(let ((env (->environment '())))
+
+  (if (not (environment-bound? env 'random-bytevector))
+      (eval '
+(define random-bytevector random-byte-vector)
+       env))
+
+  (if (not (environment-bound? env 'guarantee))
+      (eval '
+(define (guarantee predicate object #!optional caller)
+  (if (not (predicate object))
+      (error "Not a:" predicate object)))
+       env)))
+
+(let ((env (->environment '(package))))
+
+  (if (not ((access link-description? env) #(???)))
+      (begin
+	(eval '
+(define (link-description? object)
+  (and (vector? object)
+       (cond ((fix:= (vector-length object) 2)
+	      (and (symbol? (vector-ref object 0))
+		   (package-name? (vector-ref object 1))))
+	     ((fix:= (vector-length object) 3)
+	      (and (symbol? (vector-ref object 0))
+		   (package-name? (vector-ref object 1))
+		   (symbol? (vector-ref object 2))))
+	     ((fix:= (vector-length object) 4)
+	      (and (symbol? (vector-ref object 0))
+		   (package-name? (vector-ref object 1))
+		   (symbol? (vector-ref object 2))
+		   (or (eq? #f (vector-ref object 3))
+		       (eq? 'deprecated (vector-ref object 3)))))
+	     (else #f))))
+	 env)
+	(eval '
+(define (create-links-from-description description)
+  (let ((environment
+	 (find-package-environment (package-description/name description))))
+    (let ((bindings (package-description/exports description)))
+      (let ((n (vector-length bindings)))
+	(do ((i 0 (fix:+ i 1)))
+	    ((fix:= i n))
+	  (let ((binding (vector-ref bindings i)))
+	    (link-variables (find-package-environment (vector-ref binding 1))
+			    (if (fix:= (vector-length binding) 3)
+				(vector-ref binding 2)
+				(vector-ref binding 0))
+			    environment
+			    (vector-ref binding 0))))))
+    (let ((bindings (package-description/imports description)))
+      (let ((n (vector-length bindings)))
+	(do ((i 0 (fix:+ i 1)))
+	    ((fix:= i n))
+	  (let ((binding (vector-ref bindings i)))
+	    (let ((source-environment
+		   (find-package-environment (vector-ref binding 1)))
+		  (source-name
+		   (if (fix:>= (vector-length binding) 3)
+		       (vector-ref binding 2)
+		       (vector-ref binding 0))))
+	      (guarantee-binding source-environment source-name)
+	      (link-variables environment (vector-ref binding 0)
+			      source-environment source-name))))))))
+	 env))))
\ No newline at end of file
-- 
2.25.1