Skip to content

Commit ded519c

Browse files
committed
Make library loader selectable
1 parent 0668b0f commit ded519c

File tree

5 files changed

+69
-25
lines changed

5 files changed

+69
-25
lines changed

src/CMakeLists.txt

+24-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,14 @@ add_library (https-common STATIC
4141
common/HTTPRequest.cpp
4242
common/HTTPSClient.cpp
4343
common/PlaintextConnection.cpp
44-
common/LibraryLoader.cpp
44+
)
45+
46+
add_library (https-windows-libraryloader STATIC EXCLUDE_FROM_ALL
47+
windows/WindowsLibraryLoader.cpp
48+
)
49+
50+
add_library (https-unix-libraryloader STATIC EXCLUDE_FROM_ALL
51+
generic/UnixLibraryLoader.cpp
4552
)
4653

4754
add_library (https-curl STATIC EXCLUDE_FROM_ALL
@@ -69,6 +76,8 @@ add_library (https-wininet STATIC EXCLUDE_FROM_ALL
6976
)
7077

7178
### Flags
79+
set (LIBRARY_LOADER_DEFAULT "unix")
80+
7281
if (${CMAKE_SYSTEM_NAME} STREQUAL "Linux")
7382
option (USE_CURL_BACKEND "Use the libcurl backend" ON)
7483
option (USE_OPENSSL_BACKEND "Use the openssl backend" ON)
@@ -93,6 +102,8 @@ elseif (WIN32)
93102

94103
option (USE_WINSOCK "Use winsock instead of BSD sockets (windows-only)" ON)
95104

105+
set (LIBRARY_LOADER_DEFAULT "windows")
106+
96107
# Windows needs to link with Lua libraries
97108
target_link_libraries(https ${LUA_LIBRARIES})
98109
elseif (${CMAKE_SYSTEM_NAME} STREQUAL "Darwin")
@@ -125,6 +136,8 @@ elseif (ANDROID)
125136
target_link_libraries(https ${LUA_LIBRARIES})
126137
endif ()
127138
option (DEBUG_SCHANNEL "Enable debug output in schannel backend" OFF)
139+
set (LIBRARY_LOADER ${LIBRARY_LOADER_DEFAULT} CACHE STRING "Which method to use to dynamically load libraries")
140+
set_property (CACHE LIBRARY_LOADER PROPERTY STRINGS "unix;windows")
128141

129142
set_target_properties(https PROPERTIES PREFIX "")
130143

@@ -176,6 +189,16 @@ if (USE_WINSOCK)
176189
set(HTTPS_USE_WINSOCK ON)
177190
endif ()
178191

192+
if ("${LIBRARY_LOADER}" STREQUAL "unix")
193+
set(HTTPS_LIBRARY_LOADER_UNIX ON)
194+
target_link_libraries (https https-unix-libraryloader)
195+
elseif ("${LIBRARY_LOADER}" STREQUAL "windows")
196+
set(HTTPS_LIBRARY_LOADER_WINDOWS ON)
197+
target_link_libraries (https https-windows-libraryloader)
198+
else ()
199+
message(WARNING "No library loader selected, backends that depend on dynamic loading will be broken")
200+
endif ()
201+
179202
### Generate config-generated.h
180203
add_compile_definitions(HTTPS_HAVE_CONFIG_GENERATED_H)
181204
configure_file (

src/common/config-generated.h.in

+2
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,5 @@
66
#cmakedefine HTTPS_BACKEND_WININET
77
#cmakedefine HTTPS_USE_WINSOCK
88
#cmakedefine DEBUG_SCHANNEL
9+
#cmakedefine HTTPS_LIBRARY_LOADER_WINDOWS
10+
#cmakedefine HTTPS_LIBRARY_LOADER_UNIX

src/common/config.h

+5
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#elif defined(WIN32) || defined(_WIN32)
1111
#define HTTPS_BACKEND_SCHANNEL
1212
#define HTTPS_USE_WINSOCK
13+
#define HTTPS_LIBRARY_LOADER_WINDOWS
1314
#include <winapifamily.h>
1415
#if !defined(WINAPI_FAMILY) || (WINAPI_FAMILY == WINAPI_FAMILY_DESKTOP_APP)
1516
// WinINet is only supported on desktop.
@@ -23,9 +24,13 @@
2324
#endif
2425
#elif defined(__ANDROID__)
2526
#define HTTPS_BACKEND_ANDROID
27+
#define HTTPS_LIBRARY_LOADER_UNIX
2628
#elif defined(__APPLE__)
2729
#define HTTPS_BACKEND_NSURL
30+
#define HTTPS_LIBRARY_LOADER_UNIX
2831
#elif defined(linux) || defined(__linux) || defined(__linux__)
32+
#define HTTPS_LIBRARY_LOADER_UNIX
33+
2934
#if defined __has_include
3035
#if __has_include(<curl/curl.h>)
3136
#define HTTPS_BACKEND_CURL

src/generic/UnixLibraryLoader.cpp

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#include "../common/config.h"
2+
#include "../common/LibraryLoader.h"
3+
4+
#ifdef HTTPS_LIBRARY_LOADER_UNIX
5+
6+
#include <dlfcn.h>
7+
8+
namespace LibraryLoader
9+
{
10+
handle *OpenLibrary(const char *name)
11+
{
12+
return dlopen(name, RTLD_LAZY);
13+
}
14+
15+
void CloseLibrary(handle *handle)
16+
{
17+
if (handle)
18+
dlclose(handle);
19+
}
20+
21+
handle* GetCurrentProcessHandle()
22+
{
23+
return RTLD_DEFAULT;
24+
}
25+
26+
function *GetFunction(handle *handle, const char *name)
27+
{
28+
return reinterpret_cast<function *>(dlsym(handle, name));
29+
}
30+
}
31+
32+
#endif // HTTPS_LIBRARY_LOADER_UNIX
33+
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,35 @@
1-
#include "config.h"
2-
#include "LibraryLoader.h"
1+
#include "../common/config.h"
2+
#include "../common/LibraryLoader.h"
33

4-
#ifdef _WIN32
4+
#ifdef HTTPS_LIBRARY_LOADER_WINDOWS
55
#define NOMINMAX
66
#define WIN32_LEAN_AND_MEAN
77

88
#include <windows.h>
9-
#else
10-
#include <dlfcn.h>
11-
#endif
129

1310
namespace LibraryLoader
1411
{
1512
handle *OpenLibrary(const char *name)
1613
{
17-
#ifdef _WIN32
1814
return reinterpret_cast<handle *>(LoadLibraryA(name));
19-
#else
20-
return dlopen(name, RTLD_LAZY);
21-
#endif
2215
}
2316

2417
void CloseLibrary(handle *handle)
2518
{
2619
if (handle)
27-
{
28-
#ifdef _WIN32
2920
FreeLibrary(handle);
30-
#else
31-
dlclose(handle);
32-
#endif
33-
}
3421
}
3522

3623
handle* GetCurrentProcessHandle()
3724
{
38-
#ifdef _WIN32
3925
return reinterpret_cast<handle *>(GetModuleHandle(nullptr));
40-
#else
41-
return RTLD_DEFAULT;
42-
#endif
4326
}
4427

4528
function *GetFunction(handle *handle, const char *name)
4629
{
47-
#ifdef _WIN32
4830
HMODULE nativeHandle = reinterpret_cast<HMODULE>(handle);
4931
return reinterpret_cast<function *>(GetProcAddress(nativeHandle, name));
50-
#else
51-
return reinterpret_cast<function *>(dlsym(handle, name));
52-
#endif
5332
}
5433
}
34+
35+
#endif // HTTPS_LIBRARY_LOADER_WINDOWS

0 commit comments

Comments
 (0)