瀏覽代碼

Make library loader selectable

Bart van Strien 1 年之前
父節點
當前提交
ded519c7ef
共有 5 個文件被更改,包括 69 次插入25 次删除
  1. 24 1
      src/CMakeLists.txt
  2. 2 0
      src/common/config-generated.h.in
  3. 5 0
      src/common/config.h
  4. 33 0
      src/generic/UnixLibraryLoader.cpp
  5. 5 24
      src/windows/WindowsLibraryLoader.cpp

+ 24 - 1
src/CMakeLists.txt

@@ -41,7 +41,14 @@ add_library (https-common STATIC
 	common/HTTPRequest.cpp
 	common/HTTPSClient.cpp
 	common/PlaintextConnection.cpp
-	common/LibraryLoader.cpp
+)
+
+add_library (https-windows-libraryloader STATIC EXCLUDE_FROM_ALL
+	windows/WindowsLibraryLoader.cpp
+)
+
+add_library (https-unix-libraryloader STATIC EXCLUDE_FROM_ALL
+	generic/UnixLibraryLoader.cpp
 )
 
 add_library (https-curl STATIC EXCLUDE_FROM_ALL
@@ -69,6 +76,8 @@ add_library (https-wininet STATIC EXCLUDE_FROM_ALL
 )
 
 ### Flags
+set (LIBRARY_LOADER_DEFAULT "unix")
+
 if (${CMAKE_SYSTEM_NAME} STREQUAL "Linux")
 	option (USE_CURL_BACKEND "Use the libcurl backend" ON)
 	option (USE_OPENSSL_BACKEND "Use the openssl backend" ON)
@@ -93,6 +102,8 @@ elseif (WIN32)
 
 	option (USE_WINSOCK "Use winsock instead of BSD sockets (windows-only)" ON)
 
+	set (LIBRARY_LOADER_DEFAULT "windows")
+
 	# Windows needs to link with Lua libraries
 	target_link_libraries(https ${LUA_LIBRARIES})
 elseif (${CMAKE_SYSTEM_NAME} STREQUAL "Darwin")
@@ -125,6 +136,8 @@ elseif (ANDROID)
 	target_link_libraries(https ${LUA_LIBRARIES})
 endif ()
 option (DEBUG_SCHANNEL "Enable debug output in schannel backend" OFF)
+set (LIBRARY_LOADER ${LIBRARY_LOADER_DEFAULT} CACHE STRING "Which method to use to dynamically load libraries")
+set_property (CACHE LIBRARY_LOADER PROPERTY STRINGS "unix;windows")
 
 set_target_properties(https PROPERTIES PREFIX "")
 
@@ -176,6 +189,16 @@ if (USE_WINSOCK)
 	set(HTTPS_USE_WINSOCK ON)
 endif ()
 
+if ("${LIBRARY_LOADER}" STREQUAL "unix")
+	set(HTTPS_LIBRARY_LOADER_UNIX ON)
+	target_link_libraries (https https-unix-libraryloader)
+elseif ("${LIBRARY_LOADER}" STREQUAL "windows")
+	set(HTTPS_LIBRARY_LOADER_WINDOWS ON)
+	target_link_libraries (https https-windows-libraryloader)
+else ()
+	message(WARNING "No library loader selected, backends that depend on dynamic loading will be broken")
+endif ()
+
 ### Generate config-generated.h
 add_compile_definitions(HTTPS_HAVE_CONFIG_GENERATED_H)
 configure_file (

+ 2 - 0
src/common/config-generated.h.in

@@ -6,3 +6,5 @@
 #cmakedefine HTTPS_BACKEND_WININET
 #cmakedefine HTTPS_USE_WINSOCK
 #cmakedefine DEBUG_SCHANNEL
+#cmakedefine HTTPS_LIBRARY_LOADER_WINDOWS
+#cmakedefine HTTPS_LIBRARY_LOADER_UNIX

+ 5 - 0
src/common/config.h

@@ -10,6 +10,7 @@
 #elif defined(WIN32) || defined(_WIN32)
 	#define HTTPS_BACKEND_SCHANNEL
 	#define HTTPS_USE_WINSOCK
+	#define HTTPS_LIBRARY_LOADER_WINDOWS
 	#include <winapifamily.h>
 	#if !defined(WINAPI_FAMILY) || (WINAPI_FAMILY == WINAPI_FAMILY_DESKTOP_APP)
 		// WinINet is only supported on desktop.
@@ -23,9 +24,13 @@
 	#endif
 #elif defined(__ANDROID__)
 	#define HTTPS_BACKEND_ANDROID
+	#define HTTPS_LIBRARY_LOADER_UNIX
 #elif defined(__APPLE__)
 	#define HTTPS_BACKEND_NSURL
+	#define HTTPS_LIBRARY_LOADER_UNIX
 #elif defined(linux) || defined(__linux) || defined(__linux__)
+	#define HTTPS_LIBRARY_LOADER_UNIX
+
 	#if defined __has_include
 		#if __has_include(<curl/curl.h>)
 			#define HTTPS_BACKEND_CURL

+ 33 - 0
src/generic/UnixLibraryLoader.cpp

@@ -0,0 +1,33 @@
+#include "../common/config.h"
+#include "../common/LibraryLoader.h"
+
+#ifdef HTTPS_LIBRARY_LOADER_UNIX
+
+#include <dlfcn.h>
+
+namespace LibraryLoader
+{
+	handle *OpenLibrary(const char *name)
+	{
+		return dlopen(name, RTLD_LAZY);
+	}
+
+	void CloseLibrary(handle *handle)
+	{
+		if (handle)
+			dlclose(handle);
+	}
+
+	handle* GetCurrentProcessHandle()
+	{
+		return RTLD_DEFAULT;
+	}
+
+	function *GetFunction(handle *handle, const char *name)
+	{
+		return reinterpret_cast<function *>(dlsym(handle, name));
+	}
+}
+
+#endif // HTTPS_LIBRARY_LOADER_UNIX
+

+ 5 - 24
src/common/LibraryLoader.cpp → src/windows/WindowsLibraryLoader.cpp

@@ -1,54 +1,35 @@
-#include "config.h"
-#include "LibraryLoader.h"
+#include "../common/config.h"
+#include "../common/LibraryLoader.h"
 
-#ifdef _WIN32
+#ifdef HTTPS_LIBRARY_LOADER_WINDOWS
 #define NOMINMAX
 #define WIN32_LEAN_AND_MEAN
 
 #include <windows.h>
-#else
-#include <dlfcn.h>
-#endif
 
 namespace LibraryLoader
 {
 	handle *OpenLibrary(const char *name)
 	{
-#ifdef _WIN32
 		return reinterpret_cast<handle *>(LoadLibraryA(name));
-#else
-		return dlopen(name, RTLD_LAZY);
-#endif
 	}
 
 	void CloseLibrary(handle *handle)
 	{
 		if (handle)
-		{
-#ifdef _WIN32
 			FreeLibrary(handle);
-#else
-			dlclose(handle);
-#endif
-		}
 	}
 
 	handle* GetCurrentProcessHandle()
 	{
-#ifdef _WIN32
 		return reinterpret_cast<handle *>(GetModuleHandle(nullptr));
-#else
-		return RTLD_DEFAULT;
-#endif
 	}
 
 	function *GetFunction(handle *handle, const char *name)
 	{
-#ifdef _WIN32
 		HMODULE nativeHandle = reinterpret_cast<HMODULE>(handle);
 		return reinterpret_cast<function *>(GetProcAddress(nativeHandle, name));
-#else
-		return reinterpret_cast<function *>(dlsym(handle, name));
-#endif
 	}
 }
+
+#endif // HTTPS_LIBRARY_LOADER_WINDOWS