pspdev / psp-packages

https://pspdev.github.io/psp-packages/
The Unlicense
24 stars 13 forks source link

[WIP] Add initial version of luasocket #139

Closed tpimh closed 2 months ago

tpimh commented 4 months ago

Do not merge this yet, this is still WIP. I am now building with Lua 5.1, and it seems to work fine.

tpimh commented 4 months ago

@sharkwouter my "AR-in-place-of-LD hack" is not very reliable with the latest toolchain. I can remember it worked every time about 3 months ago, and now it only works every other time. Can't figure out why it is doing this, and what the success depends on.

Also now I started thinking, why am I building it as a static library anyway? I should try to build prx instead and load it dynamically.

sharkwouter commented 4 months ago

I'm going to need a bit more context for the "AR-in-place-of-LD hack" then I can help look into it. What is the problem you're running into?

I do think having luasocket as a library would be really cool.

sharkwouter commented 4 months ago

You might want to rebase on the latest commit to main, then the automated build should work again.

tpimh commented 4 months ago

The whole hack looks like this:

LDFLAGS_psp=$(LUALIB) && mv templib.a 
LD_psp=psp-ar rcs templib.a

When building dynamic libraries, LD is fine with the last argument being the output file name. When building static libraries, the output file name must be the first argument to ar after rcs flags. So for every static library to be built, I create a temporary file, then move it to the correct file name, because mv accepts the target file name as the last argument.

The order of arguments is $(LD) $(OBJECTS) $(LDFLAGS) $(OUTFILE) which expands to gcc obj1.o obj2.o -llua -o libluasocket.so (when LD is gcc and LDFLAGS is -llua -o), but with the variables above it expands to ar rcs templib.a obj1.o obj2.o /path/to/lua.a && mv templib.a libluasocket.a. Ideally I'd like to just have ar rcs libluasocket.a obj1.o obj2.o /path/to/lua.a without the extra mv call, but it's not possible to implement without seriously rewriting the makefile.

Not sure why it was failing, but after updating, the reason is obvious: https://github.com/pspdev/pspsdk/pull/205 (I'll fix it).

tpimh commented 4 months ago

Quick overview of the patch:

The other patch just fixes the incorrect version number reported.

tpimh commented 4 months ago

Here is a simple script that can simplify using AR-in-place-of-LD:

#!/bin/sh

for last; do true; done
args="$last $(echo $@ | sed 's@ '$last'$@@')"

psp-ar rcs $args
tpimh commented 2 months ago

Alright, now it's somewhat usable. In order to use it, do this:

#include "luasocket.h"
#include "mime.h" // (optional)

// init Lua and load all libraries
lua_State *L = luaL_newstate();
luaL_openlibs(L);

// Load LuaSocket into Lua core
lua_getglobal(L, "package");
lua_getfield(L, -1, "preload");
lua_pushcfunction(L, luaopen_socket_core);
lua_setfield(L, -2, "socket.core");
lua_pushcfunction(L, luaopen_mime_core); // (optional)
lua_setfield(L, -2, "mime.core"); // (optional)
lua_pop(L, 2);

Then files copied from psp/share/lua/5.1 can be used from lua. A bit cumbersome, but I don't really know how can I make it any better at the moment.

sharkwouter commented 2 months ago

Thanks for working on this. I'm mostly happy with this. I'm a little bit worried about the compat.h and mime.h files being in $PSPDEV/psp/include, though. Those are very generic names. Would it be possible to give them their own directory or give luasocket an own directory for now?

tpimh commented 2 months ago

I'm a little bit worried about the compat.h and mime.h files being in $PSPDEV/psp/include, though. Those are very generic names. Would it be possible to give them their own directory or give luasocket an own directory for now?

Yes, I was also thinking about this. I don't really like the "compat" header, and it is only included, because it's referenced from luasocket.h, so maybe the two can be merged? mime.h can be renamed to luasocket_mime.h, maybe? Or just move all the headers to $PSPDEV/psp/include/luasocket. There's no standard way of handling this as far as I know, this library is only supposed to be compiled dynamically (which is really cool, and I want to do it, but unfortunately, there's no way to load dynamic libraries at runtime from Lua at the moment), however a handful of projects link luasocket statically and they all just invent their own way. I wish something like this actually went upstream, I might break my PSP support patch into smaller pieces, and submit them separately.

sharkwouter commented 2 months ago

Such an odd library, I think putting it in lib/luasocket probably makes the most sense.

tpimh commented 2 months ago

I am sure lib/libluasocket.a and include/luasocket.h (a single combined header) wouldn't collide with anything.

sharkwouter commented 2 months ago

Yeah, that would be great

sharkwouter commented 2 months ago

Thanks for the work on this, this was an impressive feat. You should probably update your pull request to upstream to have more information on what was changed. Maybe they'll look into it then.