[commit] r2114 - simplify plugin & FFI library search

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

[commit] r2114 - simplify plugin & FFI library search

commits-3
 
Author: piumarta
Date: 2009-08-30 16:46:00 -0700 (Sun, 30 Aug 2009)
New Revision: 2114

Modified:
   trunk/platforms/unix/ChangeLog
   trunk/platforms/unix/plugins/SqueakFFIPrims/config.cmake
   trunk/platforms/unix/vm/config.cmake
   trunk/platforms/unix/vm/sqUnixExternalPrims.c
   trunk/platforms/unix/vm/sqUnixMain.c
Log:
simplify plugin & FFI library search

Modified: trunk/platforms/unix/ChangeLog
===================================================================
--- trunk/platforms/unix/ChangeLog 2009-08-28 05:25:58 UTC (rev 2113)
+++ trunk/platforms/unix/ChangeLog 2009-08-30 23:46:00 UTC (rev 2114)
@@ -1,3 +1,22 @@
+2009-08-30    <[hidden email]>
+
+ * vm/sqUnixMain.c (main): squeakPlugin path defaults to VM
+ executable directory.
+
+ * plugins/SqueakFFIPrims/config.cmake: Explicitly include
+ generated source in plugin_sources.
+
+ * vm/sqUnixExternalPrims.c (ioLoadModule): squeakPlugins can
+ contain ':'-separated paths.  Distinguish module and library
+ prefix and suffix.  Try to load a module from each squeakPlugins
+ path with module prefix/suffix, then try dlopen() with no explicit
+ path and platoform's library prefix/suffix.
+
+ * vm/config.cmake: Configure {MODULE,LIBRARY}_{PREFIX,SUFFIX}.
+ Change default module prefix to "so.", suffix to "".
+
+ * vm/sqUnixMain.c (main): Default plugin location is VM directory.
+
 2009-08-27  Ian Piumarta  <com -dot- gmail -at- piumarta (backwards)>
 
  * cmake/squeak.in: Path and text encodings are UTF-8 unless

Modified: trunk/platforms/unix/plugins/SqueakFFIPrims/config.cmake
===================================================================
--- trunk/platforms/unix/plugins/SqueakFFIPrims/config.cmake 2009-08-28 05:25:58 UTC (rev 2113)
+++ trunk/platforms/unix/plugins/SqueakFFIPrims/config.cmake 2009-08-30 23:46:00 UTC (rev 2114)
@@ -36,7 +36,14 @@
       ENDIF ()
     ENDIF ()
   ENDIF ()
-  PLUGIN_SOURCES ("${unix}/plugins/${plugin}/${cpu}-${abi}.c ${unix}/plugins/${plugin}/${cpu}-${abi}-asm.S")
+  IF (EXISTS    "${src}/plugins/${plugin}/${plugin}.c")
+    SET (SQFFIC "${src}/plugins/${plugin}/${plugin}.c")
+  ELSEIF (EXISTS "${src}/vm/intplugins/${plugin}/${plugin}.c")
+    SET (SQFFIC  "${src}/vm/intplugins/${plugin}/${plugin}.c")
+  ELSE ()
+    PLUGIN_DISABLE ()
+  ENDIF ()
+    PLUGIN_SOURCES ("${SQFFIC} ${unix}/plugins/${plugin}/${cpu}-${abi}.c ${unix}/plugins/${plugin}/${cpu}-${abi}-asm.S")
 ENDIF ()
 
 CONFIG_DEFINE (HAVE_FFI_H)

Modified: trunk/platforms/unix/vm/config.cmake
===================================================================
--- trunk/platforms/unix/vm/config.cmake 2009-08-28 05:25:58 UTC (rev 2113)
+++ trunk/platforms/unix/vm/config.cmake 2009-08-30 23:46:00 UTC (rev 2114)
@@ -168,6 +168,19 @@
 
 CONFIG_DEFINE (VM_MODULE_PREFIX)
 
+SET (CMAKE_SHARED_MODULE_PREFIX "so.")
+SET (CMAKE_SHARED_MODULE_SUFFIX "")
+
+SET (MODULE_PREFIX  \"${CMAKE_SHARED_MODULE_PREFIX}\")
+SET (MODULE_SUFFIX  \"${CMAKE_SHARED_MODULE_SUFFIX}\")
+SET (LIBRARY_PREFIX \"${CMAKE_SHARED_LIBRARY_PREFIX}\")
+SET (LIBRARY_SUFFIX \"${CMAKE_SHARED_LIBRARY_SUFFIX}\")
+
+CONFIG_DEFINE (MODULE_PREFIX)
+CONFIG_DEFINE (MODULE_SUFFIX)
+CONFIG_DEFINE (LIBRARY_PREFIX)
+CONFIG_DEFINE (LIBRARY_SUFFIX)
+
 # sqUnixMain.c
 
 SET (VM_BUILD_STRING "\"Unix built on \"__DATE__ \" \"__TIME__\" Compiler: \"__VERSION__")

Modified: trunk/platforms/unix/vm/sqUnixExternalPrims.c
===================================================================
--- trunk/platforms/unix/vm/sqUnixExternalPrims.c 2009-08-28 05:25:58 UTC (rev 2113)
+++ trunk/platforms/unix/vm/sqUnixExternalPrims.c 2009-08-30 23:46:00 UTC (rev 2114)
@@ -1,7 +1,6 @@
 /* sqUnixExternalPrims.c -- Unix named primitives and loadable modules
  *
- *   Copyright (C) 1996-2007 by Ian Piumarta and other authors/contributors
- *                              listed elsewhere in this file.
+ *   Copyright (C) 1996-2009 by Ian Piumarta
  *   All rights reserved.
  *  
  *   This file is part of Unix Squeak.
@@ -25,9 +24,7 @@
  *   SOFTWARE.
  */
 
-/* Author: [hidden email]
- *
- * Last edited: 2009-08-19 04:16:18 by piumarta on emilia-2.local
+/* Last edited: 2009-08-30 16:40:06 by piumarta on ubuntu.piumarta.com
  */
 
 #define DEBUG 0
@@ -114,7 +111,89 @@
 
 /*** local functions ***/
 
+#if 1 /* simplified plugin logic */
 
+
+static void *tryLoadModule(char *in, char *name)
+{
+  char path[PATH_MAX], *out= path;
+  void *handle= 0;
+  int c;
+  while ((c= *in++) && ':' != c) { /* copy next plugin path to path[] */
+    switch (c) {
+    case '%':
+      if ('n' == *in || 'N' == *in) { /* replace %n with name of plugin */
+ ++in;
+ strcpy(out, name);
+ out += strlen(name);
+ continue;
+      }
+      if ('%' == *in) {
+ ++in;
+ *out++= '%';
+ continue;
+      }
+      /* fall through... */
+    default:
+      *out++= c;
+      continue;
+    }
+  }
+  sprintf(out, "/" MODULE_PREFIX "%s" MODULE_SUFFIX, name);
+  handle= dlopen(path, RTLD_NOW | RTLD_GLOBAL);
+  fdebugf((stderr, "tryLoading(%s) = %p\n", path, handle));
+  if (!handle) {
+    struct stat buf;
+    if ((0 == stat(path, &buf)) && ! S_ISDIR(buf.st_mode))
+      fprintf(stderr, "%s\n", dlerror());
+  }
+  return handle;
+}
+
+
+void *ioLoadModule(char *pluginName)
+{
+  char  path[PATH_MAX];
+  char *dir= squeakPlugins;
+  void *handle= 0;
+
+  if ((0 == pluginName) || ('\0' == pluginName[0])) { /* find module in main program */
+    handle= dlopen(0, RTLD_NOW | RTLD_GLOBAL);
+    if (handle == 0) {
+      fprintf(stderr, "ioLoadModule(<intrinsic>): %s\n", dlerror());
+    }
+    else {
+      fdebugf((stderr, "loaded: <intrinsic>\n"));
+    }
+    return handle;
+  }
+
+  /* try loading {pluginPaths}/MODULE_PREFIX<name>MODULE_SUFFIX */
+
+  while (*dir) {
+    if ((handle= tryLoadModule(dir, pluginName)))
+      return handle;
+    while (*dir && ':' != *dir++)
+      ;
+  }
+
+  /* try dlopen()ing LIBRARY_PREFIX<name>LIBRARY_SUFFIX searching only the default locations modulo LD_LIBRARY_PATH et al */
+
+# if defined(HAVE_SNPRINTF)
+  snprintf(path, sizeof(path), "%s%s%s", LIBRARY_PREFIX, pluginName, LIBRARY_SUFFIX);
+# else
+  sprintf(path, "%s%s%s", LIBRARY_PREFIX, pluginName, LIBRARY_SUFFIX);
+# endif
+
+  handle= dlopen(path, RTLD_NOW | RTLD_GLOBAL);
+  fdebugf((stderr, "ioLoadModule(%s) = %p\n", path, handle));
+
+  return handle;
+}
+
+
+#else /* obsolete plugin logic */
+
 /*  Attempt to load the shared library named by the concatenation of prefix,
  *  moduleName and suffix.  Answer the new module entry, or 0 if the shared
  *  library could not be loaded.
@@ -278,7 +357,7 @@
   return handle;
       }
   }
-#endif
+#endif /* DARWIN */
 
   /* finally (for VM hackers) try the pre-install build location */
   {
@@ -305,39 +384,26 @@
   return 0;
 }
 
+#endif /* obsolete plugin logic */
 
 /*  Find a function in a loaded module.  Answer 0 if not found (do NOT
  *  fail the primitive!).
  */
 void *ioFindExternalFunctionIn(char *lookupName, void *moduleHandle)
 {
-  char buf[256];
-  void *fn;
+  void *fn= dlsym(moduleHandle, lookupName);
+  fdebugf((stderr, "ioFindExternalFunctionIn(%s, %p) = %p\n", lookupName, moduleHandle, fn));
 
-#ifdef HAVE_SNPRINTF
-  snprintf(buf, sizeof(buf), "%s", lookupName);
-#else
-  sprintf(buf, "%s", lookupName);
-#endif
-
-  fn= dlsym(moduleHandle, buf);
-
-  fdebugf((stderr, "ioFindExternalFunctionIn(%s, %d)\n",
-   lookupName, moduleHandle));
-
   if ((fn == 0) && (!sqIgnorePluginErrors)
       && strcmp(lookupName, "initialiseModule")
       && strcmp(lookupName, "shutdownModule")
       && strcmp(lookupName, "setInterpreter")
       && strcmp(lookupName, "getModuleName"))
-    fprintf(stderr, "ioFindExternalFunctionIn(%s, %p):\n  %s\n",
-    lookupName, moduleHandle, dlerror());
+    fprintf(stderr, "ioFindExternalFunctionIn(%s, %p):\n  %s\n", lookupName, moduleHandle, dlerror());
 
   return fn;
 }
 
-
-
 /*  Free the module with the associated handle.  Answer 0 on error (do
  *  NOT fail the primitive!).
 */

Modified: trunk/platforms/unix/vm/sqUnixMain.c
===================================================================
--- trunk/platforms/unix/vm/sqUnixMain.c 2009-08-28 05:25:58 UTC (rev 2113)
+++ trunk/platforms/unix/vm/sqUnixMain.c 2009-08-30 23:46:00 UTC (rev 2114)
@@ -27,7 +27,7 @@
 
 /* Author: Ian Piumarta <[hidden email]>
  *
- * Last edited: 2009-08-16 09:22:27 by piumarta on emilia-2.local
+ * Last edited: 2009-08-30 15:24:00 by piumarta on ubuntu.piumarta.com
  */
 
 #include "sq.h"
@@ -1148,13 +1148,15 @@
   sprintf(info+strlen(info), " XShm");
 #endif
   sprintf(info+strlen(info), " %s %s\n", vm_date, cc_version);
+#if 0
   if (verbose)
     sprintf(info+strlen(info), "Built from: ");
   sprintf(info+strlen(info), "%s\n", interpreterVersion);
+#endif
   if (verbose)
     sprintf(info+strlen(info), "Build host: ");
   sprintf(info+strlen(info), "%s\n", ux_version);
-  sprintf(info+strlen(info), "default plugin location: %s/*.so\n", vmLibDir);
+  sprintf(info+strlen(info), "plugin path: %s [default: %s]\n", squeakPlugins, vmPath);
   return info;
 }
 
@@ -1344,7 +1346,8 @@
   tzset(); /* should _not_ be necessary! */
 #endif
 
-  recordFullPathForVmName(argv[0]); /* full vm path */
+  recordFullPathForVmName(argv[0]); /* full vm path */
+  squeakPlugins= vmPath; /* default plugin location is VM directory */
 
   sqIgnorePluginErrors= 1;
   if (!modules)