tilkinsc / Lua.NET

[C# .NET Core 8.0] [Cross-Platform] Lua.NET contains full bindings to Lua5.1.5, Lua5.2.4, Lua5.3.6, Lua.5.4.6 and LuaJIT
MIT License
43 stars 4 forks source link

Linux Native Lua Binaries Needed #16

Closed tilkinsc closed 7 months ago

tilkinsc commented 9 months ago

Its time we support Linux (I will add x86_64 support)!

If you have a Linux on ARM64 cpu, your assistance is needed (Read Below)!

This is how you download and build lua.

curl -L -R -O https://www.lua.org/ftp/lua-all.tar.gz
tar zxf lua-all.tar.gz
cd lua-all
make -j $(nproc) all

Each sub folder in lua-all will have a .so file associated inside of it resulting from the build.

This is how you download and build luajit.

git clone https://luajit.org/git/luajit.git
cd luajit
make -j $(nproc) all

Collect each minor versions highest patch number (5.4.6, 5.3.6, 5.2.4, 5.1.5) and rename the so appropriately (lua546.so, lua536.so, lua524.so lua515.so) For luajit, retain the name of lua51.so.

Move these so's to runtimes/linux-arm64/native. example About RIDs

McJazzy commented 7 months ago

HI @tilkinsc , is there a branch available where we can test the linux x64 binding?

tilkinsc commented 7 months ago

When you build luajit, you need to have all the symbols available. I was trying to build these .so's for Linux, but ran into issues with the lua headers themselves. Building .so's is hacked on top of lua when it comes to linux because on linux puc-lua expects you to just suck it up and use .a files instead. If you look at lua.h there is no reference to dlls for anything other than windows. I was going to experiment to see if I could create either a custom configuration or if I could jerry rig the .a (which should have everything necessary) into a dll as the path of least resistance. Missing symbols means things will stop working.

require("socket"). 

I've compiled luasocket with luarocks, but getting following error:

error loading module 'socket.core' from file '/usr/local/lib/lua/5.1/socket/core.so':
        /usr/local/lib/lua/5.1/socket/core.so: undefined symbol: lua_gettop

I've trying loading these modules:
lua_State L = luaL_newstate();
if (L == 0)
{
Console.WriteLine("Unable to create context!");
}
luaopen_base(L);
luaL_openlibs(L);
luaopen_jit(L);
but the same error appears.

I've tried to dlopen luajit, but still the same error appears.
IntPtr moduleHandle = dlopen("lua51.so", RTLD_GLOBAL | RTLD_NOW);
Console.WriteLine($"handle: {moduleHandle}");

As per dlopen api, I think its the fact that the file isn't found 'lua51.so' is not a valid path. This would definitely return a null pointer. LuaRocks is a layer that sits somewhere between lua and lua libraries. You may get around this by manually linking your luajit dll with the luasocket library. Let me know how that goes.

tilkinsc commented 7 months ago

I am on archlinux. To be transparent, this is the setup I am using for figuring out how to handle this.

yay -Syu
yay -S dotnet-runtime dotnet-sdk
git clone https://github.com/tilkinsc/Lua.NET
cd Lua.NET
git clone https://luajit.org/git/luajit.git
curl -L -R -O https://www.lua.org/ftp/lua-all.tar.gz > lua-all.tar.gz
tar zxf lua-all.tar.gz

Hmm it seems that the .so is building out of the box now. Thats nice.

tilkinsc commented 7 months ago

luaso.tar.gz

I was able to drive the library with these .so's. However, I had to dump these next to the binary. For some reason, its looking relative to it (makes sense due to windows), rather than from runtimes/** which I expect to always be on the search path as well. This is local testing, however, and I havent been able to test it due to nuget being a required step in this process. Perhaps I should just release a -dev suffixed nuget package for this purpose.

I'll check out getting luarocks support in after this.

You may beta test this through this package reference <PackageReference Include="Lua.NET" Version="5.0.0" />

tilkinsc commented 7 months ago

Lua.NET works on Linux now. <PackageReference Include="Lua.NET" Version="5.0.3" /> Moving LuaRocks discussion to #21