lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


On 21 February 2011 11:13, T T <t34www@googlemail.com> wrote:
> I actually like the Windows way in this case :)  Are there patches to
> do that on Linux?

The following patch applies to LuaJIT 2 head and is based on the patch
to the base Lua interpreter Steve Donovan did in lua4linux. The built
executable will search for libraries in ../lib relative to the
directory it was placed in.

diff --git a/src/lib_package.c b/src/lib_package.c
--- a/src/lib_package.c
+++ b/src/lib_package.c
@@ -31,6 +31,30 @@

 #include <dlfcn.h>

+#ifdef LUA_DL_EXE_PATH
+
+#include <unistd.h>
+
+#undef setprogdir
+
+#define MAX_PATH 1024
+
+static void setprogdir (lua_State *L) {
+  char buff[MAX_PATH + 1];
+  int n;
+  char *lb;
+  n = readlink("/proc/self/exe", buff, sizeof(buff));
+  if (n == 0 || n == -1 || (lb = strrchr(buff, '/')) == NULL) {
+    perror("readlink");
+    luaL_error(L, "unable to get link to executable");
+  } else {
+    *lb = '\0';
+    luaL_gsub(L, lua_tostring(L, -1), LUA_EXECDIR, buff);
+    lua_remove(L, -2);  /* remove original string */
+  }
+}
+#endif
+
 static void ll_unloadlib(void *lib)
 {
   dlclose(lib);
diff --git a/src/luaconf.h b/src/luaconf.h
index 330253e..704be6d 100644
--- a/src/luaconf.h
+++ b/src/luaconf.h
@@ -9,6 +9,11 @@
 #include <limits.h>
 #include <stddef.h>

+/* @@ LUA_DL_EXE_PATH allows for the Lua executable to base
+*  its module path on the executable path for Linux systems.
+*/
+#define LUA_DL_EXE_PATH
+
 /* Default path for loading Lua and C modules with require(). */
 #if defined(_WIN32)
 /*
@@ -21,6 +26,14 @@
   ".\\?.lua;" LUA_LDIR"?.lua;" LUA_LDIR"?\\init.lua;"
 #define LUA_CPATH_DEFAULT \
   ".\\?.dll;" LUA_CDIR"?.dll;" LUA_CDIR"loadall.dll"
+#elif defined(LUA_DL_EXE_PATH)
+#define LUA_LDIR       "!/../lib/lua/"
+#define LUA_CDIR       "!/../lib/lua/"
+#define LUA_PATH_DEFAULT  \
+  "./?.lua;"  LUA_LDIR"?.lua;"  LUA_LDIR"?/init.lua"
+#define LUA_CPATH_DEFAULT \
+  "./?.so;"  LUA_CDIR"?.so;" LUA_CDIR"loadall.so"
+
 #else
 #define LUA_ROOT	"/usr/local/"
 #define LUA_LDIR	LUA_ROOT "share/lua/5.1/"