Search code examples
lualuajitluarocks

How to integrate LuaJIT with LuaRocks on Windows?


I downloaded the source of LuaJIT and compiled it with msvc120.dll (VS 2013 x64). When I run it from the command line I have no problems executing some basic lua. Now the LuaJIT installation guide mentions moving luajit.exe and lua51.dll into their own folder. From there it says to create a lua folder and under that a jit folder with the contents of src/jit moved underneath the newly created jit folder.

From my understanding my folder should look like and contain:

luajit.exe
lua51.dll
/lua
   /jit
       bc.lua
       [rest of jit files]
       vmdef.lua

Is this correct or am I missing files?

Now after I built my luajit I tried to wire it up into my luarocks to act as my interpreter using

install.bat /LUA C:\LuaJIT\2.0.3\[folder with above content]

However this cannot find the header files. I then copied over what are the header files into the folder above and that wires it up, but I can never actually get anything to compile when pointed over to LuaJIT. Edit: The error I get is the following,

C:\LuaJIT\2.0.3\bin\lua51.dll : fatal error LNK1107: invalid or corrupt file: cannot read at 0x2D0

Error: Failed installing dependency: https://rocks.moonscript.org/luafilesystem-1.6.2-2.src.rock - Build error: Failed compiling module lfs.dll

Is the correct way to handle this to simply point to my lua binaries and from there leverage LuaJIT to run my files or am I doing something wrong with wiring up LuaJIT and luarocks? The former seems to work for the most part, since I only ran into one library compilation issue, lua-cjson.


Solution

  • I've run on exactly the same problem, but they've found a solution right here:

    https://github.com/keplerproject/luafilesystem/issues/22

    I knew that for "linking DLLs statically" there is a so-called "export" .lib file, which is passed to the linker (and not the DLL itself).

    So, for example, when compiling, LuaRocks was doing this:

    cl /nologo /MD /O2 -c -Fosrc/mime.obj -ID:/LuaJIT-2.0.4/include/ src/mime.c -DLUA_COMPAT_APIINTCASTS -DLUASOCKET_DEBUG -DNDEBUG -DLUASOCKET_API=__declspec(dllexport) -DMIME_API=__declspec(dllexport) mime.c
    link -dll -def:core.def -out:mime/core.dll D:/LuaJIT-2.0.4/bin/lua51.dll src/mime.obj
    

    My LuaJIT was compiled from source in D:\LuaJIT-2.0.4\src, but I made two folders myself: D:\LuaJIT-2.0.4\include with all *.h files copied from src and D:\LuaJIT-2.0.4\bin with luajit.exe, lua51.dll, and then later lua51.exp and lua51.lib. Still same error, but this was the right track.

    Fix

    Now, check where your LuaRocks configs are:

    luarocks.bat help
    

    Scroll down to a section like:

    CONFIGURATION
        Lua version: 5.1
        Configuration files:
                System: D:/luarocks/config-5.1.lua (ok)
                User  : (... snip ...)
    

    Edit the System configuration file, specifically see the part:

    variables = {
        MSVCRT = 'VCRUNTIME140',
        LUALIB = 'lua51.dll' 
    }
    

    Here! LUALIB should be the .lib file. If your export lib is alongside the DLL, you just need to change to:

    variables = {
        MSVCRT = 'VCRUNTIME140',
        LUALIB = 'lua51.lib' -- here!
    }
    

    Verification

    And now:

    luarocks.bat install luasocket
    (...)
    link -dll -def:core.def -out:socket/core.dll D:/LuaJIT-2.0.4/bin/lua51.lib src/luasocket.obj (...)
    (...)
    luasocket 3.0rc1-2 is now built and installed in D:\luarocks\systree (license: MIT)
    

    Note the first argument passed to the linker.