From 552861d3926ced1a512019f376347ec716201e5a Mon Sep 17 00:00:00 2001
From: Nicolas Pope <nwpope@utu.fi>
Date: Mon, 9 Nov 2020 16:59:46 +0200
Subject: [PATCH] Load correct runtime library in linux

---
 SDK/CPP/CMakeLists.txt   | 18 ++++++++++++++++++
 SDK/CPP/public/voltu.cpp | 35 ++++++++++++++++++++++++++++++++++-
 2 files changed, 52 insertions(+), 1 deletion(-)

diff --git a/SDK/CPP/CMakeLists.txt b/SDK/CPP/CMakeLists.txt
index b64eae3d9..f66f714b8 100644
--- a/SDK/CPP/CMakeLists.txt
+++ b/SDK/CPP/CMakeLists.txt
@@ -11,6 +11,24 @@ add_library(voltu SHARED
 	private/property_impl.cpp
 )
 
+file(READ "public/include/voltu/voltu.hpp" VOLVER)
+
+string(REGEX MATCH "VOLTU_VERSION_MAJOR ([0-9]*)" _ ${VOLVER})
+set(VOLTU_MAJOR ${CMAKE_MATCH_1})
+
+string(REGEX MATCH "VOLTU_VERSION_MINOR ([0-9]*)" _ ${VOLVER})
+set(VOLTU_MINOR ${CMAKE_MATCH_1})
+
+string(REGEX MATCH "VOLTU_VERSION_PATCH ([0-9]*)" _ ${VOLVER})
+set(VOLTU_PATCH ${CMAKE_MATCH_1})
+
+message("VolTu SDK version: ${VOLTU_MAJOR}.${VOLTU_MINOR}.${VOLTU_PATCH}")
+
+set_target_properties( voltu  PROPERTIES
+	VERSION "${VOLTU_MAJOR}.${VOLTU_MINOR}"
+	SOVERSION "${VOLTU_MAJOR}.${VOLTU_MINOR}"
+)
+
 target_include_directories(voltu
 	PUBLIC public/include
 	PRIVATE src)
diff --git a/SDK/CPP/public/voltu.cpp b/SDK/CPP/public/voltu.cpp
index bf63ed6b4..47ddb4007 100644
--- a/SDK/CPP/public/voltu.cpp
+++ b/SDK/CPP/public/voltu.cpp
@@ -10,10 +10,16 @@
 
 #if defined(WIN32)
 #include <windows.h>
+#pragma comment(lib, "User32.lib")
 #else
 #include <dlfcn.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <unistd.h>
 #endif
 
+#include <iostream>
+
 static bool g_init = false;
 
 typedef void* Library;
@@ -48,13 +54,39 @@ static void unloadLibrary(Library lib)
 #endif
 }
 
+static bool is_file(const std::string &path) {
+#ifdef WIN32
+	WIN32_FIND_DATA ffd;
+	HANDLE hFind = FindFirstFile(path.c_str(), &ffd);
+
+	if (hFind == INVALID_HANDLE_VALUE) return false;
+	FindClose(hFind);
+	return true;
+#else
+	struct stat s;
+	if (::stat(path.c_str(), &s) == 0) {
+		return true;
+	} else {
+		return false;
+	}
+#endif
+}
+
 static std::string locateLibrary()
 {
 	// TODO: Use full paths and find correct versions
 #if defined(WIN32)
 	return "voltu.dll";
 #else
-	return "libvoltu.so";
+	std::string name = "libvoltu.so";
+	std::string vname = name + std::string(".") + std::to_string(VOLTU_VERSION_MAJOR) + std::string(".") + std::to_string(VOLTU_VERSION_MINOR);
+	if (is_file(std::string("./") + vname)) return std::string("./") + vname;
+	else if (is_file(std::string("./") + name)) return std::string("./") + name;
+	else if (is_file(std::string("../") + vname)) return std::string("../") + vname;
+	else if (is_file(std::string("../") + name)) return std::string("../") + name;
+	else if (is_file(std::string("/usr/local/lib/") + vname)) return std::string("/usr/local/lib/") + vname;
+	else if (is_file(std::string("/usr/local/lib/") + name)) return std::string("/usr/local/lib/") + name;
+	return name;
 #endif
 }
 
@@ -63,6 +95,7 @@ std::shared_ptr<voltu::System> voltu::instance()
 	if (g_init) return nullptr;
 	
 	std::string name = locateLibrary();
+	std::cout << "Loading VolTu Runtime: " << name << std::endl;
 	Library handle = loadLibrary(name.c_str());
 
 	if (handle)
-- 
GitLab