From 29e40b62585e77dd8841717ce3c6f8111b91d646 Mon Sep 17 00:00:00 2001
From: Matt Birkholz <matt@birkholz.chandler.az.us>
Date: Mon, 9 Sep 2013 12:57:16 -0700
Subject: [PATCH] ffi: generate-shim no longer produces -const.bin.

Some -const.c files require library-specific cc and ld flags, so
generate-shim cannot work the whole process.  Re-introduced the "4
tedious little rules" that compile, link and execute the -const
program, and fasdump the resulting -const.scm.
---
 src/blowfish/Makefile.in | 26 ++++++++++++++++++-------
 src/ffi/build.scm.in     | 11 -----------
 src/gdbm/Makefile        | 23 ++++++++++++++++------
 src/md5/Makefile.in      | 28 ++++++++++++++++++---------
 src/mhash/Makefile.in    | 28 ++++++++++++++++++---------
 src/mhash/README         | 41 ++++++++++++++++++++++------------------
 src/runtime/ffi.scm      |  2 +-
 src/runtime/runtime.pkg  |  4 +---
 8 files changed, 99 insertions(+), 64 deletions(-)

diff --git a/src/blowfish/Makefile.in b/src/blowfish/Makefile.in
index 5e98b8cbc..63629147c 100644
--- a/src/blowfish/Makefile.in
+++ b/src/blowfish/Makefile.in
@@ -24,25 +24,24 @@ MIT_SCHEME_EXE = mit-scheme
 EXE = '$(MIT_SCHEME_EXE)' --batch-mode
 
 CFLAGS = @CFLAGS@
-CPPFLAGS = @CPPFLAGS@ -I. -I$(srcdir)
+CPPFLAGS = @CPPFLAGS@
 LDFLAGS = @LDFLAGS@
 LIBS = @LIBS@
 
-all: blowfish-shim.so
+all: blowfish-shim.so blowfish-types.bin blowfish-const.bin
 	echo '(load "compile")' | $(EXE)
 
-check: all
+check:
 	echo '(load "check")' | $(EXE)
 
-install: all
+install:
 	echo '(install-shim "blowfish")' | $(EXE) -- *.com *.bci *.pkd make.scm
 
 clean:
 	rm -f blowfish-const.scm blowfish-const blowfish-const.c
 	rm -f blowfish-shim.c
 	rm -f blowfish-*.crf blowfish-*.fre blowfish-*.pkd
-	rm -f *.o *.so *.bin *.ext *.com *.bci
-	rm -f *.moc *.fni *-init.c *-init.h *-init.o
+	rm -f *.o *.so *.bin *.ext *.com *.bci *.moc *.fni
 	rm -f test
 
 distclean: clean
@@ -61,8 +60,21 @@ blowfish-adapter.o: blowfish-adapter.c blowfish-shim.h
 blowfish-shim.o: blowfish-shim.c blowfish-shim.h
 	echo '(compile-shim)' | $(EXE) -- $(CPPFLAGS) $(CFLAGS) -c $<
 
-blowfish-shim.c: blowfish.cdecl blowfish-shim.h
+blowfish-shim.c blowfish-const.c blowfish-types.bin: \
+  blowfish.cdecl blowfish-shim.h
 	echo '(generate-shim "blowfish" "#include \"blowfish-shim.h\"")' \
 	| $(EXE)
 
+blowfish-const.bin: blowfish-const.scm
+	echo '(sf "blowfish-const")' | $(EXE)
+
+blowfish-const.scm: blowfish-const
+	./blowfish-const
+
+blowfish-const: blowfish-const.o
+	$(CC) $(LDFLAGS) -o $@ $^ $(LIBS)
+
+blowfish-const.o: blowfish-const.c blowfish-shim.h
+	$(CC) $(CPPFLAGS) $(CFLAGS) -c $<
+
 .PHONY: all check install clean distclean maintainer-clean
diff --git a/src/ffi/build.scm.in b/src/ffi/build.scm.in
index 4229edeeb..36f36d623 100644
--- a/src/ffi/build.scm.in
+++ b/src/ffi/build.scm.in
@@ -53,17 +53,6 @@ USA.
 			 library-dir
 			 (except-last-pair
 			  (pathname-directory library-dir))))))))
-
-(define (generate-shim library #!optional prefix)
-  (let ((-const (string-append library"-const"))
-	(-const.c (string-append library"-const.c"))
-	(-const.o (string-append library"-const.o")))
-    (c-generate library prefix)
-    (run-command (append CC CFLAGS (list "-c" -const.c)))
-    (run-command (append CC LDFLAGS (list "-o" -const -const.o)))
-    (run-command (list (->namestring
-			(merge-pathnames -const (working-directory-pathname)))))
-    (sf -const)))
 
 (define (parse-words string)
   (burst-string string char-set:whitespace #t))
diff --git a/src/gdbm/Makefile b/src/gdbm/Makefile
index f5fe3d3aa..874937749 100644
--- a/src/gdbm/Makefile
+++ b/src/gdbm/Makefile
@@ -23,20 +23,19 @@
 MIT_SCHEME_EXE = mit-scheme
 EXE = '$(MIT_SCHEME_EXE)' --batch-mode
 
-all: gdbm-shim.so
+all: gdbm-shim.so gdbm-types.bin gdbm-const.bin
 	echo '(load "compile")' | $(EXE)
 
-check: all
+check:
 	echo '(load "check")' | $(EXE)
 
-install: all
+install:
 	echo '(install-shim "gdbm")' | $(EXE) -- *.com *.bci *.pkd make.scm
 
 clean distclean maintainer-clean:
 	rm -f gdbm-const.scm gdbm-const gdbm-const.c gdbm-shim.c
 	rm -f gdbm-*.crf gdbm-*.fre gdbm-*.pkd
-	rm -f *.o *.so *.bin *.ext *.com *.bci
-	rm -f *.moc *.fni *-init.c *-init.h *-init.o
+	rm -f *.o *.so *.bin *.ext *.com *.bci *.moc *.fni
 	rm -f gdbm-check.db
 
 gdbm-shim.so: gdbm-shim.o gdbm-adapter.o
@@ -48,7 +47,19 @@ gdbm-adapter.o: gdbm-adapter.c gdbm-shim.h
 gdbm-shim.o: gdbm-shim.c gdbm-shim.h
 	echo '(compile-shim)' | $(EXE) -- -c $<
 
-gdbm-shim.c: gdbm.cdecl gdbm-shim.h
+gdbm-shim.c gdbm-const.c gdbm-types.bin: gdbm.cdecl gdbm-shim.h
 	echo '(generate-shim "gdbm" "#include \"gdbm-shim.h\"")' | $(EXE)
 
+gdbm-const.bin: gdbm-const.scm
+	echo '(sf "gdbm-const")' | $(EXE)
+
+gdbm-const.scm: gdbm-const
+	./gdbm-const
+
+gdbm-const: gdbm-const.o
+	$(CC) $(LDFLAGS) -o $@ $^ $(LIBS)
+
+gdbm-const.o: gdbm-const.c gdbm-shim.h
+	$(CC) $(CPPFLAGS) $(CFLAGS) -c $<
+
 .PHONY: all check install clean distclean maintainer-clean
diff --git a/src/md5/Makefile.in b/src/md5/Makefile.in
index 30009f989..dd0f845dc 100644
--- a/src/md5/Makefile.in
+++ b/src/md5/Makefile.in
@@ -24,24 +24,23 @@ MIT_SCHEME_EXE = mit-scheme
 EXE = '$(MIT_SCHEME_EXE)' --batch-mode
 
 CFLAGS = @CFLAGS@
-CPPFLAGS = @CPPFLAGS@ -I. -I$(srcdir)
+CPPFLAGS = @CPPFLAGS@
 LDFLAGS = @LDFLAGS@
 LIBS = @LIBS@
 
-all: md5-shim.so
+all: md5-shim.so md5-types.bin md5-const.bin
 	echo '(load "compile")' | $(EXE)
 
-check: all
+check:
 	echo '(load "check")' | $(EXE)
 
-install: all
+install:
 	echo '(install-shim "md5")' | $(EXE) -- *.com *.bci *.pkd make.scm
 
 clean:
 	rm -f md5-const.scm md5-const md5-const.c md5-shim.c
 	rm -f md5-*.crf md5-*.fre md5-*.pkd
-	rm -f *.o *.so *.bin *.ext *.com *.bci
-	rm -f *.moc *.fni *-init.c *-init.h *-init.o
+	rm -f *.o *.so *.bin *.ext *.com *.bci *.moc *.fni
 	rm -f sample
 
 distclean: clean
@@ -60,8 +59,19 @@ md5-adapter.o: md5-adapter.c md5-shim.h
 md5-shim.o: md5-shim.c md5-shim.h
 	echo '(compile-shim)' | $(EXE) -- $(CPPFLAGS) $(CFLAGS) -c $<
 
-md5-shim.c: md5.cdecl md5-shim.h
-	echo '(generate-shim "md5" "#include \"md5-shim.h\"")' \
-	| $(EXE)
+md5-shim.c md5-const.c md5-types.bin: md5.cdecl md5-shim.h
+	echo '(generate-shim "md5" "#include \"md5-shim.h\"")' | $(EXE)
+
+md5-const.bin: md5-const.scm
+	echo '(sf "md5-const")' | $(EXE)
+
+md5-const.scm: md5-const
+	./md5-const
+
+md5-const: md5-const.o
+	$(CC) $(LDFLAGS) -o $@ $^ $(LIBS)
+
+md5-const.o: md5-const.c md5-shim.h
+	$(CC) $(CPPFLAGS) $(CFLAGS) -c $<
 
 .PHONY: all check install clean distclean maintainer-clean
diff --git a/src/mhash/Makefile.in b/src/mhash/Makefile.in
index 9f8171239..0a82d9acf 100644
--- a/src/mhash/Makefile.in
+++ b/src/mhash/Makefile.in
@@ -24,24 +24,23 @@ MIT_SCHEME_EXE = mit-scheme
 EXE = '$(MIT_SCHEME_EXE)' --batch-mode
 
 CFLAGS = @CFLAGS@
-CPPFLAGS = @CPPFLAGS@ -I. -I$(srcdir)
+CPPFLAGS = @CPPFLAGS@
 LDFLAGS = @LDFLAGS@
 LIBS = @LIBS@
 
-all: mhash-shim.so
+all: mhash-shim.so mhash-types.bin mhash-const.bin
 	echo '(load "compile")' | $(EXE)
 
-check: all
+check:
 	echo '(load "check")' | $(EXE)
 
-install: all
+install:
 	echo '(install-shim "mhash")' | $(EXE) -- *.com *.bci *.pkd make.scm
 
 clean:
 	rm -f mhash-const.scm mhash-const mhash-const.c mhash-shim.c
 	rm -f mhash-*.crf mhash-*.fre mhash-*.pkd
-	rm -f *.o *.so *.bin *.ext *.com *.bci
-	rm -f *.moc *.fni *-init.c *-init.h *-init.o
+	rm -f *.o *.so *.bin *.ext *.com *.bci *.moc *.fni
 	rm -f sample
 
 distclean: clean
@@ -60,8 +59,19 @@ mhash-adapter.o: mhash-adapter.c mhash-shim.h
 mhash-shim.o: mhash-shim.c mhash-shim.h
 	echo '(compile-shim)' | $(EXE) -- $(CPPFLAGS) $(CFLAGS) -c $<
 
-mhash-shim.c: mhash.cdecl mhash-shim.h
-	echo '(generate-shim "mhash" "#include \"mhash-shim.h\"")' \
-	| $(EXE)
+mhash-shim.c mhash-const.c mhash-types.bin: mhash.cdecl mhash-shim.h
+	echo '(generate-shim "mhash" "#include \"mhash-shim.h\"")' | $(EXE)
+
+mhash-const.bin: mhash-const.scm
+	echo '(sf "mhash-const")' | $(EXE)
+
+mhash-const.scm: mhash-const
+	./mhash-const
+
+mhash-const: mhash-const.o
+	$(CC) $(LDFLAGS) -o $@ $^ $(LIBS)
+
+mhash-const.o: mhash-const.c mhash-shim.h
+	$(CC) $(CPPFLAGS) $(CFLAGS) -c $<
 
 .PHONY: all check install clean distclean maintainer-clean
diff --git a/src/mhash/README b/src/mhash/README
index 72d4a4db0..528bd107a 100644
--- a/src/mhash/README
+++ b/src/mhash/README
@@ -1,18 +1,23 @@
-mhash/ (prmhash.c):
-mhash 4
-mhash_count 0
-mhash_end 1
-mhash_get_block_size 1
-mhash_get_hash_name 1
-mhash_get_hash_pblock 1
-mhash_get_keygen_max_key_size 1
-mhash_get_keygen_name 1
-mhash_get_keygen_salt_size 1
-mhash_hmac_end 1
-mhash_hmac_init 3
-mhash_init 1
-mhash_keygen 4
-mhash_keygen_count 0
-mhash_keygen_uses_count 1
-mhash_keygen_uses_hash_algorithm 1
-mhash_keygen_uses_salt 1
+The mhash wrapper.
+
+This is a drop-in replacement for the mhash microcode module and the
+mhash-* procedures in runtime/crypto.scm.  It is not part of the core
+build and can be built outside the core build tree in the customary
+way:
+
+    ./configure [--with-mhash=directory]...
+    make all check install
+
+The install target copies a shared library shim and compiled Scheme
+files into the system library path.  You can override the default
+command name "mit-scheme" (and thus the system library path) by
+setting MIT_SCHEME_EXE.
+
+To load via load-option, install the following in your optiondb.scm:
+
+    (define-load-option 'MHASH
+      (guarded-system-loader '(mhash) "mhash"))
+
+You will need to import the bindings you want to use.  They are not
+exported to the global environment because they would conflict with
+the exports from (runtime crypto).
diff --git a/src/runtime/ffi.scm b/src/runtime/ffi.scm
index 056ef55a4..87f2d4e0f 100644
--- a/src/runtime/ffi.scm
+++ b/src/runtime/ffi.scm
@@ -527,7 +527,7 @@ USA.
 
 (define (generate-shim library #!optional prefix)
   (load-ffi-quietly)
-  ((environment-lookup (->environment '(ffi)) 'generate-shim) library prefix))
+  ((environment-lookup (->environment '(ffi)) 'c-generate) library prefix))
 
 (define (compile-shim)
   (load-ffi-quietly)
diff --git a/src/runtime/runtime.pkg b/src/runtime/runtime.pkg
index b2052c63b..cf4d0d243 100644
--- a/src/runtime/runtime.pkg
+++ b/src/runtime/runtime.pkg
@@ -3336,9 +3336,7 @@ USA.
 	  generate-shim
 	  compile-shim
 	  link-shim
-	  install-shim
-	  compile-bundle
-	  install-bundle)
+	  install-shim)
   (initialization (initialize-package!)))
 
 (define-package (runtime program-copier)
-- 
2.25.1