diff --git a/luprex/Makefile b/luprex/Makefile index 8a526791..7737df4b 100644 --- a/luprex/Makefile +++ b/luprex/Makefile @@ -97,6 +97,7 @@ OBJ_CORE=\ OBJ_DRV=\ + obj/drv/driver.obj\ obj/drv/drvutil.obj\ obj/drv/sslutil.obj\ @@ -112,12 +113,19 @@ ifeq "$(OS)" "linux" OPT=-g -O0 GPP=g++ -Wall -fvisibility=hidden $(OPT) -std=c++17 -MMD +LIB_SSL=-L./ext/openssl-3.0.1/lib/linux -lssl -lcrypto +LIB_OS=-ldl -luprex: $(OBJ_DRV) driver-linux.o luprexlib.so - $(GPP) -o $@ $(OBJ_DRV) driver-linux.o -L./ext/openssl-linux -lssl -lcrypto -ldl +all: luprex luprexlib.so luprexstatic + +luprex: $(OBJ_DRV) luprexlib.so + $(GPP) $(OBJ_DRV) $(LIB_SSL) $(LIB_OS) -o $@ luprexlib.so: $(OBJ_ERIS) $(OBJ_CORE) - $(GPP) -export-dynamic -Wl,--no-allow-shlib-undefined -Wl,-z,defs -shared-o $@ $^ + $(GPP) -export-dynamic -Wl,--no-allow-shlib-undefined -Wl,-z,defs -shared -o $@ $^ + +luprexstatic: $(OBJ_DRV) $(OBJ_ERIS) $(OBJ_CORE) + $(GPP) $(OBJ_DRV) $(OBJ_ERIS) $(OBJ_CORE) $(LIB_SSL) $(LIB_OS) -o $@ obj/eris/%.obj: ext/eris-master/src/%.c $(GPP) -fPIC -DLUA_USE_APICHECK -DLUA_USE_POSIX -o $@ -c $< @@ -178,35 +186,41 @@ OPT=/Od /Zi MMD=g++ -Wall -std=c++17 -MMD -E CL=CL $(OPT) /std:c++17 /EHsc /nologo /MD /TP /c LINK=CL $(OPT) /std:c++17 /EHsc /nologo -OBJ_OS=obj/drv/driver-windows.obj LIB_SSL=ext/openssl-3.1.0/lib/visual/libcrypto.lib ext/openssl-3.1.0/lib/visual/libssl.lib -LIB_WIN=ws2_32.lib crypt32.lib cryptui.lib user32.lib advapi32.lib +LIB_OS=ws2_32.lib crypt32.lib cryptui.lib user32.lib advapi32.lib all: luprex.exe luprexlib.dll luprexstatic.exe -luprex.exe: luprexlib.dll $(OBJ_DRV) $(OBJ_OS) - $(LINK) /Fe: $@ $(OBJ_DRV) $(OBJ_OS) $(LIB_SSL) $(LIB_WIN) +luprex.exe: luprexlib.dll $(OBJ_DRV) + $(LINK) /Fe: $@ $(OBJ_DRV) $(LIB_SSL) $(LIB_OS) luprexlib.dll: $(OBJ_ERIS) $(OBJ_CORE) $(LINK) /LDd /Fe: $@ $^ -luprexstatic.exe: $(OBJ_DRV) $(OBJ_OS) $(OBJ_ERIS) $(OBJ_CORE) - $(LINK) /Fe: $@ $(OBJ_DRV) $(OBJ_OS) $(OBJ_ERIS) $(OBJ_CORE) $(LIB_SSL) $(LIB_WIN) +luprexstatic.exe: $(OBJ_DRV) $(OBJ_ERIS) $(OBJ_CORE) + $(LINK) /Fe: $@ $(OBJ_DRV) $(OBJ_ERIS) $(OBJ_CORE) $(LIB_SSL) $(LIB_OS) + obj/eris/%.obj: ext/eris-master/src/%.c - $(MMD) -DLUA_USE_APICHECK -DLUA_COMPAT_ALL -o $@d $< - $(CL) /DLUA_USE_APICHECK /DLUA_COMPAT_ALL /Fo: $@ $< + $(GPCL) -DLUA_USE_APICHECK -DLUA_COMPAT_ALL -o $@d $< + $(VCCL) /DLUA_USE_APICHECK /DLUA_COMPAT_ALL /Fo: $@ $< obj/core/%.obj: cpp/core/%.cpp - $(MMD) -I./ext/eris-master/src -I./cpp/wrap -I./cpp/core -o $@d $< - $(CL) /I./ext/eris-master/src /I./cpp\wrap /I./cpp/core /Fo: $@ $< + $(GPCL) -I./ext/eris-master/src -I./cpp/wrap -I./cpp/core -o $@d $< + $(VCCL) /I./ext/eris-master/src /I./cpp\wrap /I./cpp/core /Fo: $@ $< obj/drv/%.obj: cpp/drv/%.cpp - $(MMD) -I./ext/openssl-3.1.0/inc -I./src/drv -o $@d $< - $(CL) /I./ext/openssl-3.1.0/inc -I./src/drv /Fo: $@ $< + $(GPCL) -I./ext/openssl-3.1.0/inc -I./src/drv -o $@d $< + $(VCCL) /I./ext/openssl-3.1.0/inc -I./src/drv /Fo: $@ $< endif +####################################################################### +## +## Common Make Rules +## +####################################################################### + ####################################################################### ## diff --git a/luprex/cpp/drv/driver-linux.cpp b/luprex/cpp/drv/driver-linux.cpp index 589e3862..637ef478 100644 --- a/luprex/cpp/drv/driver-linux.cpp +++ b/luprex/cpp/drv/driver-linux.cpp @@ -221,9 +221,12 @@ static int console_read(char *bytes, int nbytes) { static void call_init_engine_wrapper(EngineWrapper *w) { using InitFn = void (*)(EngineWrapper *); - void *dll_handle = dlopen("./luprex.so", RTLD_NOW | RTLD_LOCAL); - assert(dll_handle != nullptr); - InitFn initfn = (InitFn)dlsym(dll_handle, "init_engine_wrapper"); + InitFn initfn = (InitFn)dlsym(nullptr, "init_engine_wrapper"); + if (initfn == nullptr) { + void *dll_handle = dlopen("./luprexlib.so", RTLD_NOW | RTLD_LOCAL); + assert(dll_handle != nullptr); + initfn = (InitFn)dlsym(dll_handle, "init_engine_wrapper"); + } assert(initfn != nullptr); initfn(w); } @@ -242,16 +245,9 @@ static void disable_randomization(int argc, char *argv[]) { } } -#include "driver-common.cpp" - - -int main(int argc, char **argv) -{ +static void os_initialize(int argc, char **argv) { disable_randomization(argc, argv); enable_tty_raw(); - assert(OPENSSL_init_ssl(0, NULL) == 1); - sslutil::clear_all_errors(); - Driver driver; - return driver.drive(argc, argv); } + diff --git a/luprex/cpp/drv/driver-windows.cpp b/luprex/cpp/drv/driver-windows.cpp index 4267167a..b9e3199e 100644 --- a/luprex/cpp/drv/driver-windows.cpp +++ b/luprex/cpp/drv/driver-windows.cpp @@ -291,14 +291,7 @@ static void call_init_engine_wrapper(EngineWrapper *w) { initfn(w); } -#include "driver-common.cpp" - -int main(int argc, char **argv) -{ +void os_initialize(int argc, char **argv) { init_winsock(); - assert(OPENSSL_init_ssl(0, NULL) == 1); - sslutil::clear_all_errors(); - Driver driver; - return driver.drive(argc, argv); } diff --git a/luprex/cpp/drv/driver-common.cpp b/luprex/cpp/drv/driver.cpp similarity index 98% rename from luprex/cpp/drv/driver-common.cpp rename to luprex/cpp/drv/driver.cpp index 816d3073..a01eeb0d 100644 --- a/luprex/cpp/drv/driver-common.cpp +++ b/luprex/cpp/drv/driver.cpp @@ -1,3 +1,10 @@ + +#if defined(__linux__) +#include "driver-linux.cpp" +#elif defined(WIN32) +#include "driver-windows.cpp" +#endif + #define POLLVEC_SIZE (DRV_MAX_CHAN + 1) #define MAX_BIO_BUFFER (128 * 1024) @@ -628,3 +635,12 @@ class Driver { return 0; } }; + +int main(int argc, char **argv) { + os_initialize(argc, argv); + assert(OPENSSL_init_ssl(0, NULL) == 1); + sslutil::clear_all_errors(); + Driver driver; + return driver.drive(argc, argv); +} + diff --git a/luprex/luprexstatic b/luprex/luprexstatic new file mode 100755 index 00000000..4e8ca72b Binary files /dev/null and b/luprex/luprexstatic differ