From 2e6d2c5611d6a246c84af4659dfb03b04ad7c34c Mon Sep 17 00:00:00 2001
From: Carl Philipp Klemm <philipp@uvos.xyz>
Date: Mon, 22 May 2023 13:09:02 +0200
Subject: [PATCH] Make win32 version build again

---
 .gitmodules    |  3 +++
 CMakeLists.txt | 42 +++++++++++++++++++++++++-------
 compile.cpp    | 65 --------------------------------------------------
 compile.h      | 33 -------------------------
 crossW64.cmake |  1 +
 dlfcn-win32    |  1 +
 model.cpp      |  5 ++--
 7 files changed, 41 insertions(+), 109 deletions(-)
 create mode 100644 .gitmodules
 create mode 160000 dlfcn-win32

diff --git a/.gitmodules b/.gitmodules
new file mode 100644
index 0000000..d1e4404
--- /dev/null
+++ b/.gitmodules
@@ -0,0 +1,3 @@
+[submodule "dlfcn-win32"]
+	path = dlfcn-win32
+	url = https://github.com/dlfcn-win32/dlfcn-win32.git
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 70cd170..1873500 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -2,6 +2,8 @@ cmake_minimum_required(VERSION 3.19)
 
 project(eisgenerator LANGUAGES C CXX)
 
+set (CMAKE_CXX_STANDARD 20)
+
 set(SRC_FILES
 	componant.cpp
 	cap.cpp
@@ -22,7 +24,7 @@ set(SRC_FILES
 	strops.cpp
 	translators.cpp
 	randomgen.cpp
-	compile.cpp
+	compcache.cpp
 )
 
 set(API_HEADERS_CPP_DIR eisgenerator/)
@@ -53,15 +55,37 @@ if(WIN32)
 	file(GLOB_RECURSE ARGP_SRC ${PROJECT_SOURCE_DIR}/argp/*.*)
 	set(SRC_FILES ${SRC_FILES} ${ARGP_SRC})
 	include_directories(./argp)
+	include_directories(./dlfcn-win32/src/)
+
+	set(SRC_FILES ${SRC_FILES}
+		compile-win.cpp
+		./dlfcn-win32/src/dlfcn.c
+	)
+
+	set(COMMON_LINK_FLAGS "-flto -dl")
 else()
 	message("Building on UNIX")
+
+	set(SRC_FILES ${SRC_FILES}
+		compile.cpp
+	)
+	set(COMMON_LINK_FLAGS "-flto -ltbb -pthread")
 endif(WIN32)
 
 
-add_library(${PROJECT_NAME} SHARED ${SRC_FILES} ${API_HEADERS_CPP})
-target_link_libraries(${PROJECT_NAME} ${LIBS})
-target_include_directories(${PROJECT_NAME} PUBLIC eisgenerator)
-set_target_properties(${PROJECT_NAME} PROPERTIES COMPILE_FLAGS "-std=c++2a -Wall -O2 -march=native -g" LINK_FLAGS "-flto -ltbb -pthread")
+add_library(${PROJECT_NAME}_obj OBJECT ${SRC_FILES} ${API_HEADERS_CPP})
+target_link_libraries(${PROJECT_NAME}_obj ${LIBS})
+target_include_directories(${PROJECT_NAME}_obj PUBLIC eisgenerator)
+set_target_properties(${PROJECT_NAME}_obj PROPERTIES POSITION_INDEPENDENT_CODE 1)
+set_target_properties(${PROJECT_NAME}_obj PROPERTIES COMPILE_FLAGS "-Wall -O2 -march=native -g" LINK_FLAGS ${COMMON_LINK_FLAGS})
+add_library(${PROJECT_NAME} SHARED $<TARGET_OBJECTS:${PROJECT_NAME}_obj>)
+add_library(${PROJECT_NAME}_static STATIC $<TARGET_OBJECTS:${PROJECT_NAME}_obj>)
+
+if(WIN32)
+	set(EISGEN_LINK_FLAG ${PROJECT_NAME}_static)
+else()
+	set(EISGEN_LINK_FLAG ${PROJECT_NAME})
+endif(WIN32)
 
 if(CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT)
 	set(CMAKE_INSTALL_PREFIX "/usr" CACHE PATH "..." FORCE)
@@ -82,20 +106,20 @@ GROUP_READ GROUP_EXECUTE )
 
 link_directories(${CMAKE_CURRENT_BINARY_DIR})
 set(SRC_FILES_TEST_APP test.cpp)
-set(LIBS_TEST -L. -l${PROJECT_NAME})
+set(LIBS_TEST -L. -l${EISGEN_LINK_FLAG})
 add_executable(${PROJECT_NAME}_test ${SRC_FILES_TEST_APP})
 add_dependencies(${PROJECT_NAME}_test ${PROJECT_NAME})
 target_link_libraries(${PROJECT_NAME}_test ${LIBS_TEST})
 target_include_directories(${PROJECT_NAME}_test PUBLIC eisgenerator)
-set_target_properties(${PROJECT_NAME}_test PROPERTIES COMPILE_FLAGS "-std=c++2a -Wall -O2 -march=native -g" LINK_FLAGS "-flto")
+set_target_properties(${PROJECT_NAME}_test PROPERTIES COMPILE_FLAGS "-Wall -O2 -march=native -g" LINK_FLAGS ${COMMON_LINK_FLAGS})
 install(TARGETS ${PROJECT_NAME}_test DESTINATION bin)
 
 link_directories(${CMAKE_CURRENT_BINARY_DIR})
 set(SRC_FILES_TEST_APP main.cpp)
-set(LIBS_TEST -L. -l${PROJECT_NAME})
+set(LIBS_TEST -L. -l${EISGEN_LINK_FLAG})
 add_executable(${PROJECT_NAME}_export ${SRC_FILES_TEST_APP})
 add_dependencies(${PROJECT_NAME}_export ${PROJECT_NAME})
 target_link_libraries(${PROJECT_NAME}_export ${LIBS_TEST})
 target_include_directories(${PROJECT_NAME}_export PUBLIC eisgenerator)
-set_target_properties(${PROJECT_NAME}_export PROPERTIES COMPILE_FLAGS "-std=c++2a -Wall -O2 -march=native -g" LINK_FLAGS "-flto")
+set_target_properties(${PROJECT_NAME}_export PROPERTIES COMPILE_FLAGS "-Wall -O2 -march=native -g" LINK_FLAGS ${COMMON_LINK_FLAGS})
 install(TARGETS ${PROJECT_NAME}_export DESTINATION bin)
diff --git a/compile.cpp b/compile.cpp
index 00b8629..5134a88 100644
--- a/compile.cpp
+++ b/compile.cpp
@@ -7,7 +7,6 @@
 #include <errno.h>
 #include <stdexcept>
 #include <sys/wait.h>
-#include <dlfcn.h>
 
 #include "log.h"
 
@@ -83,67 +82,3 @@ int eis::compile_code(const std::string& code, const std::string& outputName)
 	return -1;
 }
 
-std::string eis::getTempdir()
-{
-	char* tmpEnv = getenv("TMP");
-	char* tempEnv = getenv("TEMP");
-	char* tempDirEnv = getenv("TEMPDIR");
-
-	std::filesystem::path path;
-	if(tmpEnv && std::string(tmpEnv).length() > 1)
-		path = tmpEnv;
-	else if(tempEnv && std::string(tempEnv).length() > 1)
-		path = tempEnv;
-	else if(tempDirEnv && std::string(tempDirEnv).length() > 1)
-		path = tempDirEnv;
-	else
-		path = "/tmp";
-	path = path/"eis_models";
-
-	if(!std::filesystem::is_directory(path))
-	{
-		if(!std::filesystem::create_directory(path))
-			throw std::runtime_error(path.string() +
-				"is not a directory and a directory can not be created at this locaion");
-	}
-
-	return path;
-}
-
-
-CompCache* CompCache::getInstance()
-{
-	if(!instance)
-		instance = new CompCache();
-	return instance;
-}
-
-bool CompCache::addObject(size_t uuid, const CompiledObject& object)
-{
-	CompiledObject* foundobject = getObject(uuid);
-	if(foundobject)
-		return false;
-
-	objects.insert({uuid, new CompiledObject(object)});
-	return true;
-}
-
-CompiledObject* CompCache::getObject(size_t uuid)
-{
-	auto search = objects.find(uuid);
-	if(search == objects.end())
-		return nullptr;
-	else
-		return search->second;
-}
-
-void CompCache::dropAllObjects()
-{
-	for(std::pair<size_t, CompiledObject*> object : objects)
-	{
-		dlclose(object.second->objectCode);
-		delete object.second;
-	}
-
-	objects.clear();
-}
diff --git a/compile.h b/compile.h
index ce0abab..8c9b763 100644
--- a/compile.h
+++ b/compile.h
@@ -1,41 +1,8 @@
 #include <string>
-#include <vector>
-#include <map>
-#include <complex>
-
-#include "eistype.h"
 
 namespace eis
 {
 
 int compile_code(const std::string& code, const std::string& outputName);
 
-std::string getTempdir();
-
-struct CompiledObject
-{
-	void* objectCode;
-	std::vector<std::complex<fvalue>>(*symbol)(const std::vector<fvalue>&, const std::vector<fvalue>&);
-};
-
-class CompCache
-{
-public:
-
-private:
-
-	inline static CompCache* instance = nullptr;
-	std::map<size_t, CompiledObject*> objects;
-	CompCache() {};
-
-public:
-
-	static CompCache* getInstance();
-	CompCache(const CompCache&) = delete;
-	CompCache& operator=(const CompCache&) = delete;
-	bool addObject(size_t uuid, const CompiledObject& object);
-	CompiledObject* getObject(size_t uuid);
-	void dropAllObjects();
-};
-
 }
diff --git a/crossW64.cmake b/crossW64.cmake
index 22d72b5..37c1214 100644
--- a/crossW64.cmake
+++ b/crossW64.cmake
@@ -5,3 +5,4 @@ set(CMAKE_FIND_ROOT_PATH /usr/i686-w64-mingw32)
 set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
 set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
 set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
+set(CMAKE_EXE_LINKER_FLAGS "-static")
diff --git a/dlfcn-win32 b/dlfcn-win32
new file mode 160000
index 0000000..6444294
--- /dev/null
+++ b/dlfcn-win32
@@ -0,0 +1 @@
+Subproject commit 6444294ee354796536c34b54e154c0538a4d1eaf
diff --git a/model.cpp b/model.cpp
index fd9d1cc..84f11e9 100644
--- a/model.cpp
+++ b/model.cpp
@@ -27,6 +27,7 @@
 #include "basicmath.h"
 #include "randomgen.h"
 #include "compile.h"
+#include "compcache.h"
 
 using namespace eis;
 
@@ -582,7 +583,7 @@ bool Model::compile()
 		size_t uuid = getUuid();
 
 		std::filesystem::path path = tmp/(std::to_string(getUuid())+".so");
-		int ret = compile_code(getCode(), path);
+		int ret = compile_code(getCode(), path.string());
 		if(ret != 0)
 		{
 			Log(Log::WARN)<<"Unable to compile model!! expect performance degredation";
@@ -590,7 +591,7 @@ bool Model::compile()
 		}
 
 		CompiledObject object;
-		object.objectCode = dlopen(path.c_str(), RTLD_NOW);
+		object.objectCode = dlopen(path.string().c_str(), RTLD_NOW);
 		if(!object.objectCode)
 			throw std::runtime_error("Unable to dlopen compiled model " + std::string(dlerror()));
 
-- 
GitLab