Description
Right now, coreclr has no good way to handle the differences between platforms when it comes to p/invoking native libraries.
E.g. imagine I want to call an openssl function, on Unix the library is called libssl.so/dylib
but on Windows it is libeay32.dll
(there are countless other examples, e.g. libz.so
vs zlib1.dll
).
corefx "solves" this by conditionally compiling the p/invoke code for each platform with the correct library name. This seems like a major organizational overhead though and is brittle when new platforms are added.
Mono does two things to make this much easier:
- It automatically probes for variations of the string passed to
DllImport
, e.g. if I specifyDllImport("myFoo")
it triesmyFoo.dll
,myFoo.so
,libmyFoo.so
and so on. In the happy case this is enough to find the library on all the platforms. - DllMap: this is for cases where the library name is just too different, or if only a certain function should be redirected, or only on some specific platforms.
In my opinion, coreclr should implement something along those lines. I know that using DllMap 1:1 is not really possible since there's no System.Configuration stack in .NET Core, but it should at least guide an alternate implementation.
What do you think?
edit I proposed adding DllMap attributes in https://github.com/dotnet/coreclr/issues/930#issuecomment-100675743 :
[assembly: DllMap("foo", "foo.dll", OSName.Windows)]
[assembly: DllMap("foo", "libfoo.dylib", OSName.OSX)]
[assembly: DllMap(Dll="foo", Target="libfoo.so", OS=OSName.Linux)]
[assembly: DllMap(Dll="dl", Name="dlclose", Target="libc.so", OS="FreeBSD")]