Hi,

I committed this patch to trunk. It makes toolwrapper.c use dlopen to load the libjvm library, instead of linking to it directly. This is how the tool wrappers work in the reference implementation, and allows libjvm.so to be looked up based on the value of JAVA_HOME. The native tool wrapper binaries are built when the --enable-tool-wrappers configure option is specified.

Tested on cacao.

Tom

2006-08-18  Thomas Fitzsimmons  <[EMAIL PROTECTED]>

        * configure.ac (tool-wrappers): Check for ltdl support when tool
        wrapper binaries are enabled.
        * tools/Makefile.am (LIBJVM): Remove variable.
        (AM_CPPFLAGS): Add LIBJVM define.
        (gappletviewer_LDFLAGS, gjarsigner_LDFLAGS, gkeytool_LDFLAGS,
        gjar_LDFLAGS, gnative2ascii_LDFLAGS, gserialver_LDFLAGS,
        gjavah_LDFLAGS): Remove variables.
        * tools/toolwrapper.c (main): Use dlopen to load libjvm library.
Index: configure.ac
===================================================================
RCS file: /sources/classpath/classpath/configure.ac,v
retrieving revision 1.178
diff -u -r1.178 configure.ac
--- configure.ac	14 Aug 2006 20:31:20 -0000	1.178
+++ configure.ac	18 Aug 2006 19:08:10 -0000
@@ -287,7 +287,11 @@
 AC_ARG_ENABLE([tool-wrappers],
               [AS_HELP_STRING(--enable-tool-wrappers,create tool wrapper binaries [default=no])],
               [case x"${enableval}" in
-                xyes) COMPILE_WRAPPERS=yes ;;
+                xyes)
+                  COMPILE_WRAPPERS=yes;
+                  AC_CHECK_HEADERS([ltdl.h],, [AC_MSG_ERROR(cannot find ltdl.h)])
+                  AC_CHECK_LIB(ltdl, lt_dlopen,, [AC_MSG_ERROR(cannot find libltdl)])
+                  ;;
                 xno) COMPILE_WRAPPERS=no ;;
 		x) COMPILE_WRAPPERS=yes ;;
                 *) COMPILE_WRAPPERS=yes ;;
Index: tools/Makefile.am
===================================================================
RCS file: /sources/classpath/classpath/tools/Makefile.am,v
retrieving revision 1.22
diff -u -r1.22 Makefile.am
--- tools/Makefile.am	28 Jul 2006 20:09:52 -0000	1.22
+++ tools/Makefile.am	18 Aug 2006 19:08:22 -0000
@@ -30,62 +30,46 @@
 bin_PROGRAMS = gappletviewer gjarsigner gkeytool \
 	gjar gnative2ascii gserialver $(javah)
 
-if FOUND_GCJ
-LIBJVM = -lgcj
-else
-if FOUND_CACAO
-LIBJVM = -ljvm
-else
-LIBJVM =
-endif
-endif
-
 AM_CPPFLAGS = -Wall \
 	-I$(top_srcdir)/include \
+	-DLIBJVM="\"$(libdir)/libjvm\"" \
 	-DTOOLS_ZIP="\"$(TOOLSdir)/$(TOOLS_ZIP)\""
 
 gappletviewer_SOURCES = toolwrapper.c
 gappletviewer_CFLAGS = \
 	-DTOOLPACKAGE="\"appletviewer\"" \
 	-DTOOLNAME="\"gappletviewer\""
-gappletviewer_LDFLAGS = -L$(libdir) $(LIBJVM)
 
 gjarsigner_SOURCES = toolwrapper.c
 gjarsigner_CFLAGS = \
 	-DTOOLPACKAGE="\"jarsigner\"" \
 	-DTOOLNAME="\"gjarsigner\""
-gjarsigner_LDFLAGS = -L$(libdir) $(LIBJVM)
 
 gkeytool_SOURCES = toolwrapper.c
 gkeytool_CFLAGS = \
 	-DTOOLPACKAGE="\"keytool\"" \
 	-DTOOLNAME="\"gkeytool\""
-gkeytool_LDFLAGS = -L$(libdir) $(LIBJVM)
 
 gjar_SOURCES = toolwrapper.c
 gjar_CFLAGS = \
 	-DTOOLPACKAGE="\"jar\"" \
 	-DTOOLNAME="\"gjar\""
-gjar_LDFLAGS = -L$(libdir) $(LIBJVM)
 
 gnative2ascii_SOURCES = toolwrapper.c
 gnative2ascii_CFLAGS = \
 	-DTOOLPACKAGE="\"native2ascii\"" \
 	-DTOOLNAME="\"gnative2ascii\""
-gnative2ascii_LDFLAGS = -L$(libdir) $(LIBJVM)
 
 gserialver_SOURCES = toolwrapper.c
 gserialver_CFLAGS = \
 	-DTOOLPACKAGE="\"serialver\"" \
 	-DTOOLNAME="\"gserialver\""
-gserialver_LDFLAGS = -L$(libdir) $(LIBJVM)
 
 if USE_ASM
 gjavah_SOURCES = toolwrapper.c
 gjavah_CFLAGS = \
 	-DTOOLPACKAGE="\"javah\"" \
 	-DTOOLNAME="\"gjavah\""
-gjavah_LDFLAGS = -L$(libdir) $(LIBJVM)
 endif
 
 else
Index: tools/toolwrapper.c
===================================================================
RCS file: /sources/classpath/classpath/tools/toolwrapper.c,v
retrieving revision 1.2
diff -u -r1.2 toolwrapper.c
--- tools/toolwrapper.c	15 Jun 2006 23:08:49 -0000	1.2
+++ tools/toolwrapper.c	18 Aug 2006 19:08:22 -0000
@@ -37,6 +37,7 @@
 exception statement from your version. */
 
 #include <jni.h>
+#include <ltdl.h>
 #include <string.h>
 #include <stdlib.h>
 #include "config.h"
@@ -51,6 +52,9 @@
   JNIEnv *jni_env;
 };
 
+/* Typedef for JNI_CreateJavaVM dlopen call. */
+typedef jint createVM (JavaVM **, void **, void *);
+
 int
 main (int argc, const char** argv)
 {
@@ -68,6 +72,10 @@
   int non_vm_argc;
   int i;
   int classpath_found = 0;
+  /* Variables for JNI_CreateJavaVM dlopen call. */
+  lt_dlhandle libjvm_handle = NULL;
+  createVM* libjvm_create = NULL;
+  int libjvm_error = 0;
 
   env = NULL;
   jvm = NULL;
@@ -152,7 +160,27 @@
   vm_args.version = JNI_VERSION_1_2;
   vm_args.ignoreUnrecognized = JNI_TRUE;
 
-  result = JNI_CreateJavaVM (&jvm, &tmp.void_env, &vm_args);
+  /* dlopen libjvm.so */
+  libjvm_error = lt_dlinit ();
+  if (libjvm_error)
+    {
+      fprintf (stderr, TOOLNAME ": lt_dlinit failed.\n");
+      goto destroy;
+    }
+
+  libjvm_handle = lt_dlopenext (LIBJVM);
+  if (!libjvm_handle)
+    {
+      fprintf (stderr, TOOLNAME ": failed to open " LIBJVM "\n");
+      goto destroy;
+    }
+  libjvm_create = (createVM*) lt_dlsym (libjvm_handle, "JNI_CreateJavaVM");
+  if (!libjvm_create)
+    {
+      fprintf (stderr, TOOLNAME ": failed to load JNI_CreateJavaVM symbol from " LIBJVM "\n");
+      goto destroy;
+    }
+  result = (*libjvm_create) (&jvm, &tmp.void_env, &vm_args);
 
   if (result < 0)
     {
@@ -216,5 +244,15 @@
 	(*jvm)->DestroyJavaVM (jvm);
     }
 
+  /* libltdl cleanup */
+  if (libjvm_handle)
+    {
+      if (lt_dlclose (libjvm_handle) != 0)
+        fprintf (stderr, TOOLNAME ": failed to close " LIBJVM "\n");
+    }
+
+  if (lt_dlexit () != 0)
+    fprintf (stderr, TOOLNAME ": lt_dlexit failed.\n");
+
   return 1;
 }

Reply via email to