Skip to content

Commit f4ea4cd

Browse files
dschoGit for Windows Build Agent
authored andcommitted
http: support lazy-loading libcurl also on Windows
This implements the Windows-specific support code, because everything is slightly different on Windows, even loading shared libraries. Note: I specifically do _not_ use the code from `compat/win32/lazyload.h` here because that code is optimized for loading individual functions from various system DLLs, while we specifically want to load _many_ functions from _one_ DLL here, and distinctly not a system DLL (we expect libcurl to be located outside `C:\Windows\system32`, something `INIT_PROC_ADDR` refuses to work with). Also, the `curl_easy_getinfo()`/`curl_easy_setopt()` functions are declared as vararg functions, which `lazyload.h` cannot handle. Finally, we are about to optionally override the exact file name that is to be loaded, which is a goal contrary to `lazyload.h`'s design. Signed-off-by: Johannes Schindelin <[email protected]>
1 parent 77be653 commit f4ea4cd

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

Makefile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1610,7 +1610,11 @@ else
16101610
# The `CURL_STATICLIB` constant must be defined to avoid seeing the functions
16111611
# declared as DLL imports
16121612
CURL_CFLAGS = -DCURL_STATICLIB
1613+
ifneq ($(uname_S),MINGW)
1614+
ifneq ($(uname_S),Windows)
16131615
CURL_LIBCURL = -ldl
1616+
endif
1617+
endif
16141618
else
16151619
ifndef CURL_LDFLAGS
16161620
CURL_LDFLAGS = $(eval CURL_LDFLAGS := $$(shell $$(CURL_CONFIG) --libs))$(CURL_LDFLAGS)

compat/lazyload-curl.c

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#include "../git-compat-util.h"
22
#include "../git-curl-compat.h"
3+
#ifndef WIN32
34
#include <dlfcn.h>
5+
#endif
46

57
/*
68
* The ABI version of libcurl is encoded in its shared libraries' file names.
@@ -11,6 +13,7 @@
1113

1214
typedef void (*func_t)(void);
1315

16+
#ifndef WIN32
1417
#ifdef __APPLE__
1518
#define LIBCURL_FILE_NAME(base) base "." #LIBCURL_ABI_VERSION ".dylib"
1619
#else
@@ -35,6 +38,39 @@ static func_t load_function(void *handle, const char *name)
3538
*(void **)&f = dlsym(handle, name);
3639
return f;
3740
}
41+
#else
42+
#define LIBCURL_FILE_NAME(base) base "-" #LIBCURL_ABI_VERSION ".dll"
43+
44+
static void *load_library(const char *name)
45+
{
46+
size_t name_size = strlen(name) + 1;
47+
const char *path = getenv("PATH");
48+
char dll_path[MAX_PATH];
49+
50+
while (path && *path) {
51+
const char *sep = strchrnul(path, ';');
52+
size_t len = sep - path;
53+
54+
if (len && len + name_size < sizeof(dll_path)) {
55+
memcpy(dll_path, path, len);
56+
dll_path[len] = '/';
57+
memcpy(dll_path + len + 1, name, name_size);
58+
59+
if (!access(dll_path, R_OK))
60+
return (void *)LoadLibraryExA(dll_path, NULL, 0);
61+
}
62+
63+
path = *sep ? sep + 1 : NULL;
64+
}
65+
66+
return NULL;
67+
}
68+
69+
static func_t load_function(void *handle, const char *name)
70+
{
71+
return (func_t)GetProcAddress((HANDLE)handle, name);
72+
}
73+
#endif
3874

3975
typedef char *(*curl_easy_escape_type)(CURL *handle, const char *string, int length);
4076
static curl_easy_escape_type curl_easy_escape_func;

0 commit comments

Comments
 (0)