-
-
Notifications
You must be signed in to change notification settings - Fork 745
dynamically load libcurl #3009
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
dynamically load libcurl #3009
Conversation
9d563d4
to
fd5465e
Compare
Briefly looked over it, the overall implementation seems OK. Don't have time right now to look at it in detail, though. Hopefully other reviewers will chime in... |
else | ||
static assert(0, "unimplemented"); | ||
|
||
enforce!CurlException(handle !is null, "Failed to load libcurl."); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm worried that this error will be thrown at people currently linking with curl libraries with non-standard or fringe names, and they won't know which names are tried without looking at the source code. Maybe it's a good idea to include the list of tried names in the exception message? If it's going to be a breaking change for them, we should at least make it as comfortable as possible.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Mmh, I spent some effort trying to find all relevant names. How would someone rename their libcurl.
I might look through a few more implementations, e.g. perl to see what works.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I added one name libcurl-nss.so.4
which is one of the libcurl
flavors on ubuntu, couldn't find anything relevant besides this.
fd5465e
to
090aaad
Compare
I shortly enabled the std.net.curl tests to see whether I was missing something, but they pass on all platforms. |
090aaad
to
2b1b2ba
Compare
{ | ||
// initialize early to prevent thread races | ||
enforce!CurlException(!curl_global_init(CurlGlobal.all), | ||
"Couldn't initialize libcurl"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We no longer call curl_global_init
anywhere explicitly, is this OK?
It is called above: _api.global_init
Not 100% sure about this... this change means that it will be impossible to statically link libcurl by substituting the import library with a static one. |
I could try to load the symbols from the binary itself, but for that to work, you need to use the export-dynamic switch, not sure if that works on windows. |
Lazy loading has its disadvantages (sometimes it's better to fail fast).
This is the real reason for this change as far as I can see. Which platforms are affected? |
Does this export all public symbols or something? If so, that would conflict with one of the use cases for why you'd want to statically link libcurl (self-contained closed-source application). |
We could add weak undefined references to curl and check whether those are defined. |
I could try to use undefined weak references and initialize pointers from that when curl is statically linked, not sure if that'd work on Windows. |
I don't know. How important is the versioned symbol problem? Can we do this only for affected systems? |
We also have a link order issue, because dmd currently doesn't allow to add libraries after phobos. |
There is no easy way to get weakly linked but undefined symbols in D. Function declarations in templates are strong undefined. So we'll have to add a C file, hope that dmc supports weak linkage. |
Can we go ahead with this in the meantime? LDC is hit pretty hard with the linker order issue right now, and I'd rather not add -lcurl as a default dependency for every built executable. I might actually merge this into the LDC Phobos fork for the next release. |
This is the only sensible solution until e.g. dlang/phobos#3009 is merged, which makes Phobos load libcurl dynamically. The issue with the current situation is is that the user cannot even manually link libcurl if linker errors occur, because it will appear before libphobos2-ldc in the linker command line. GitHub: Fixes ldc-developers#906.
ODBC will have the same issue once we have D version of |
Not sure if dmc actually supports weak symbols. I think the simpler solution is to try to dynamically load the curl symbols from the executable. |
2b1b2ba
to
fcfe3a7
Compare
done, now also with support for linking curl |
da0e762
to
0e1a04e
Compare
Let's hope we can prevent the mistake to add ODBC. |
std.net.curl tests pass though there is an unrelated and already existing 32-bit bug. |
@MartinNowak - what needs to be done to be able to import from the executable itself? Static linking wouldn't suffice I guess? |
It should work right away when linking with an import library for a curl.dll or with libcurl.so on Posix, b/c loading a symbol from the exe does search for symbols in dependent libraries (not 100% sure if that works on Windows). For linking with a static curl library you need the export declarations on Windows (either using a DEF file or using the static lib itself) and --export-dynamic on Posix. |
8162aef
to
30ec62a
Compare
Updated with detailed changelog entry and manual on how to statically link against curl. |
Pretty cool solution to allow linking libcurl statically on Windows, wouldn't have thought of that. 👍 |
- avoids issues with versioned symbols on different platforms - lazy loading/initialization of curl library - fix Issue 13324 - try to load curl from the exe itself before loading a shared library thus allowing to link against a different or a static curl library
30ec62a
to
592ce24
Compare
Well some very security focused people might not like it, but the weak references weren't an option with DMC. |
The nice side effect is that you can link with any shared library on Posix, e.g. a hypothetical libcurl-botan.so and it will work, even without --export-dynamic. |
Auto-merge toggled on |
Finally |
Is it intended that this now still requires to manually pass a library flag ( |
Already fixed dlang/dmd#5084. |
Okay, thanks, then the other guy probably just tested with a slightly older version of GIT master. |
Issue 13324 – dynamically load libcurl at runtime