From 98c1b2d599c8acc363dd41ca2ec983ed7f8597d6 Mon Sep 17 00:00:00 2001 From: jyelon Date: Tue, 21 Feb 2023 15:28:45 -0500 Subject: [PATCH] Convert to a true DLL engine --- luprex/.gitignore | 6 ++- luprex/Makefile | 67 ++++++++++++++++++++++++++------- luprex/cpp/core/luastack.hpp | 2 - luprex/cpp/drv/driver-linux.cpp | 12 ++---- 4 files changed, 62 insertions(+), 25 deletions(-) diff --git a/luprex/.gitignore b/luprex/.gitignore index 872781f8..273f56b6 100644 --- a/luprex/.gitignore +++ b/luprex/.gitignore @@ -1,5 +1,8 @@ +a.out gprof.out gmon.out +luprex + *~ \#*# .#* @@ -7,8 +10,9 @@ gmon.out *.o *.dll *.exe +*.so *.a -main + obj/** .vscode/** ext/eris-master/src/lua diff --git a/luprex/Makefile b/luprex/Makefile index c70b6214..85715670 100644 --- a/luprex/Makefile +++ b/luprex/Makefile @@ -1,23 +1,42 @@ +####################################################################### +## +## Auto detect Operating System +## +####################################################################### ifneq "" "$(findstring -linux-,$(MAKE_HOST))" + OS=linux - EXE=main - LIBS=-L./ext/openssl-linux -lssl -lcrypto -ldl + EXE=luprex + DLL=luprex.so + LIBS=-L./ext/openssl-mingw -lssl -lcrypto -ldl LUAFLAGS=-DLUA_USE_APICHECK -DLUA_USE_POSIX OPT=-g -O0 + else ifneq "" "$(findstring cmd.exe,$(COMSPEC))" + OS=mingw - EXE=main.exe + EXE=luprex.exe + DLL=luprex.dll LIBS=-L./ext/openssl-mingw -lssl -lcrypto -lws2_32 -lcrypt32 -lcryptui LUAFLAGS=-DLUA_USE_APICHECK -DLUA_COMPAT_ALL OPT=-g -O0 + else + $(error Cannot figure out whether to build the linux or mingw version) + endif - - $(info Building for $(OS)...) + + +####################################################################### +## +## List of all OBJ files +## +####################################################################### + OBJ_ERIS=\ obj/eris/lapi.o \ obj/eris/lcode.o \ @@ -91,25 +110,47 @@ OBJ_CORE=\ OBJ_DRV=\ obj/drv/drvutil.o\ obj/drv/sslutil.o\ - obj/drv/driver-$(OS).o + obj/drv/driver-$(OS).o\ -$(EXE): $(OBJ_ERIS) $(OBJ_CORE) $(OBJ_DRV) - g++ $(OPT) -std=c++17 -export-dynamic -Wall -o $@ $(OBJ_ERIS) $(OBJ_CORE) $(OBJ_DRV) $(LIBS) +####################################################################### +## +## Make rules +## +####################################################################### + +GPP=g++ -Wall -fvisibility=hidden $(OPT) -std=c++17 -MMD + +all: $(EXE) $(DLL) + +$(EXE): $(OBJ_DRV) + $(GPP) -o $@ $(OBJ_DRV) -L./ext/openssl-linux -lssl -lcrypto -ldl + +$(DLL): $(OBJ_ERIS) $(OBJ_CORE) + $(GPP) -export-dynamic -Wl,--no-allow-shlib-undefined -Wl,-z,defs -shared -o $@ $^ obj/eris/%.o: ext/eris-master/src/%.c - gcc $(OPT) -Wall -fvisibility=hidden $(LUAFLAGS) -c -MMD $< -o $@ + $(GPP) -fPIC $(LUAFLAGS) -o $@ -c $< obj/core/%.o: cpp/core/%.cpp - g++ $(OPT) -Wall -fvisibility=hidden -std=c++17 -I./ext/eris-master/src -I./cpp/wrap -I./cpp/core -c -MMD $< -o $@ + $(GPP) -fPIC -I./ext/eris-master/src -I./cpp/wrap -I./cpp/core -o $@ -c $< obj/drv/%.o: cpp/drv/%.cpp - g++ $(OPT) -Wall -fvisibility=hidden -std=c++17 -I./ext -I./src/drv -c -MMD $< -o $@ + $(GPP) -I./ext -I./src/drv -o $@ -c $< clean: - rm -f main.exe main obj/core/*.* obj/drv/*.* obj/eris/*.* + rm -f luprex luprex.exe luprex.so luprex.dll obj/core/*.* obj/drv/*.* obj/eris/*.* + + + +####################################################################### +## +## Automatically generated Make Dependencies +## +####################################################################### -include $(OBJ_ERIS:%.o=%.d) --include $(OBJ_ERIS:%.o=%.d) +-include $(OBJ_CORE:%.o=%.d) +-include $(OBJ_DRV:%.o=%.d) diff --git a/luprex/cpp/core/luastack.hpp b/luprex/cpp/core/luastack.hpp index 321d5c24..6600e14e 100644 --- a/luprex/cpp/core/luastack.hpp +++ b/luprex/cpp/core/luastack.hpp @@ -158,12 +158,10 @@ #include "wrap-set.hpp" #include -extern "C" { #include "lua.h" #include "lauxlib.h" #include "lualib.h" #include "eris.h" -} class LuaSlot : public eng::nevernew { protected: diff --git a/luprex/cpp/drv/driver-linux.cpp b/luprex/cpp/drv/driver-linux.cpp index 559e22bf..55663724 100644 --- a/luprex/cpp/drv/driver-linux.cpp +++ b/luprex/cpp/drv/driver-linux.cpp @@ -217,17 +217,11 @@ static int console_read(char *bytes, int nbytes) { return read(0, bytes, nbytes); } -// Load the DLL if it's not already loaded. Stores -// the handle in a global variable. -static void load_engine_dll() { - // Not actually implemented yet. Currently, the engine - // is linked right into the executable. -} - static void call_init_engine_wrapper(EngineWrapper *w) { - load_engine_dll(); using InitFn = void (*)(EngineWrapper *); - InitFn initfn = (InitFn)dlsym(RTLD_DEFAULT, "init_engine_wrapper"); + void *dll_handle = dlopen("./luprex.so", RTLD_NOW | RTLD_LOCAL); + assert(dll_handle != nullptr); + InitFn initfn = (InitFn)dlsym(dll_handle, "init_engine_wrapper"); assert(initfn != nullptr); initfn(w); }