lua-users home
lua-l archive

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


Hi,

Adam D. Moss wrote:
> While this topic is under discussion... could there please
> be an obvious and standard method of statically linking /
> pre-initializing a module under this system?  At the moment
> it's not clear to me how this would work.

That's what the registry._PRELOAD (aliased to package.preload) table is
good for. Even incremental builds are doable:

Building an extension:

- Instead of building a shared library (gcc -shared -o module.so a.o b.o ...)
  you want to do a relative link: ld -r -o module.o a.o b.o ...

- You may want to strip all public symbols from module.o except for
  "luaopen_"..modulename

Installing an extension:

- Copy module.o to a common directory, probably something like:
  /usr/local/lib/lua-static/5.1/

- Create a C array initializer for all module init functions by listing
  the contents of the above directory and put it in lua_glue.h
    static const luaL_reg extlibs[] = {
      {"foo",     luaopen_foo},
      {"bar.baz", luaopen_bar_baz},
      {NULL,      NULL}
    }

- Recompile lua_glue.c. This simple C file should include the above header
  file. It has a single function that goes through the generated list and
  stuffs the open functions into the registry._PRELOAD table.
  The implementation is left as an exercise for the reader. :-)

- Now link the Lua stand-alone loader (src/lua/lua.c), the Lua core, the
  Lua base libraries, lua_glue.o and all extension module.o files together
  to form a new lua binary.

Of course you can defer the last three steps for a final relink phase
to save time when rebuilding many modules.

To build a Lua core suitable for static linking you need to modify the
lua_userstateopen(L) define in luaconf.h to refer to the function name
from lua_glue.c. It's probably helpful to do a relative link for the loader,
the core and the base libraries and strip all symbols except for the public
Lua API and main().

So far so simple ...

But the real problem behind this is that you need to go through the makefiles
of every 3rd party extension module and change a few things. This is similar
to what you have to do to build an extension module with a non-standard 
build system. This can quickly get annoying.

See my next mail in a new thread for more on this.

Bye,
     Mike