Ejemplo n.º 1
0
        static IntPtr LoadWindowsLibrary(string libName, out SymbolLookupDelegate symbolLookup)
        {
            string libFile           = libName + ".dll";
            string rootDirectory     = AppDomain.CurrentDomain.BaseDirectory;
            string assemblyDirectory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);

            var paths = new[]
            {
                Path.Combine(assemblyDirectory, "bin", Environment.Is64BitProcess ? "x64" : "x86", libFile),
                Path.Combine(assemblyDirectory, Environment.Is64BitProcess ? "x64" : "x86", libFile),
                Path.Combine(assemblyDirectory, libFile),

                Path.Combine(rootDirectory, "bin", Environment.Is64BitProcess ? "x64" : "x86", libFile),
                Path.Combine(rootDirectory, Environment.Is64BitProcess ? "x64" : "x86", libFile),
                Path.Combine(rootDirectory, libFile)
            };

            foreach (var path in paths)
            {
                if (File.Exists(path))
                {
                    var addr = LoadLibrary(path);
                    if (addr == IntPtr.Zero)
                    {
                        // Not using NanomsgException because it depends on nn_errno.
                        throw new Exception("LoadLibrary failed: " + path);
                    }
                    symbolLookup      = GetProcAddress;
                    NativeLibraryPath = path;
                    return(addr);
                }
            }

            throw new Exception("LoadLibrary failed: unable to locate library " + libFile + ". Searched: " + paths.Aggregate((a, b) => a + "; " + b));
        }
Ejemplo n.º 2
0
        public NativeLibrary(string filePath, IntPtr handle, SymbolLookupDelegate symbolLookup)
        {
            FilePath = filePath;
            Handle   = handle;

            SymbolLookup = symbolLookup;
        }
Ejemplo n.º 3
0
        static IntPtr LoadPosixLibrary(string libName, out SymbolLookupDelegate symbolLookup)
        {
            const int RTLD_NOW      = 2;
            string    libFile       = "lib" + libName.ToLower() + ".so";
            string    rootDirectory = AppDomain.CurrentDomain.BaseDirectory;

            var paths = new[]
            {
                Path.Combine(rootDirectory, "bin", Environment.Is64BitProcess ? "x64" : "x86", libFile),
                Path.Combine(rootDirectory, Environment.Is64BitProcess ? "x64" : "x86", libFile),
                Path.Combine(rootDirectory, libFile),
                Path.Combine("/usr/local/lib", libFile),
                Path.Combine("/usr/lib", libFile)
            };

            foreach (var path in paths)
            {
                if (File.Exists(path))
                {
                    var addr = dlopen(path, RTLD_NOW);
                    if (addr == IntPtr.Zero)
                    {
                        // Not using NanosmgException because it depends on nn_errno.
                        throw new Exception("dlopen failed: " + path + " : " + Marshal.PtrToStringAnsi(dlerror()));
                    }
                    symbolLookup      = dlsym;
                    NativeLibraryPath = path;
                    return(addr);
                }
            }

            throw new Exception("dlopen failed: unable to locate library " + libFile + ". Searched: " + paths.Aggregate((a, b) => a + "; " + b));
        }
Ejemplo n.º 4
0
        private static IntPtr LoadWindowsUniversalLibrary(out SymbolLookupDelegate symbolLookup)
        {
            var addr = LoadLibrary("freetype.dll");

            if (addr == IntPtr.Zero)
            {
                throw new Exception("LoadPackagedLibrary failed: freetype.dll");
            }

            symbolLookup      = GetProcAddress;
            NativeLibraryPath = ".";
            return(addr);
        }
Ejemplo n.º 5
0
        private static IntPtr LoadAndroidLibrary(out SymbolLookupDelegate symbolLookup)
        {
            var libName = "libfreetype.so";
            var addr    = dlopen(libName, RTLD_NOW);

            if (addr == IntPtr.Zero)
            {
                // Not using NanosmgException because it depends on nn_errno.
                var error = Marshal.PtrToStringAnsi(dlerror());
                throw new Exception("Android - dlopen failed: " + libName + " : " + error);
            }
            symbolLookup      = dlsym;
            NativeLibraryPath = libName;
            return(addr);
        }
Ejemplo n.º 6
0
        static IntPtr LoadPosixLibrary(string libName, out SymbolLookupDelegate symbolLookup)
        {
            var  libExt = ".so";
            bool isOSX  = System.Runtime.InteropServices.RuntimeInformation.IsOSPlatform(OSPlatform.OSX);

            if (isOSX)
            {
                libExt = ".dylib";
            }

            const int RTLD_NOW          = 2;
            string    libFile           = $"lib{libName.ToLower()}{libExt}";
            string    rootDirectory     = AppDomain.CurrentDomain.BaseDirectory;
            string    assemblyDirectory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);

            var paths = new[]
            {
                calculatexdir(assemblyDirectory, "net40", libFile),
                Path.Combine(rootDirectory, "bin", Environment.Is64BitProcess ? "x64" : "x86", libFile),
                Path.Combine(rootDirectory, Environment.Is64BitProcess ? "x64" : "x86", libFile),
                Path.Combine(rootDirectory, libFile),
                Path.Combine("/usr/local/lib", libFile),
                Path.Combine("/usr/lib", libFile)
            };

            foreach (var path in paths)
            {
                if (path == null)
                {
                    continue;
                }

                if (File.Exists(path))
                {
                    var addr = dlopen(path, RTLD_NOW);
                    if (addr == IntPtr.Zero)
                    {
                        // Not using NanosmgException because it depends on nn_errno.
                        throw new Exception("dlopen failed: " + path + " : " + Marshal.PtrToStringAnsi(dlerror()));
                    }
                    symbolLookup      = dlsym;
                    NativeLibraryPath = path;
                    return(addr);
                }
            }

            throw new Exception("dlopen failed: unable to locate library " + libFile + ". Searched: " + paths.Aggregate((a, b) => a + "; " + b));
        }
Ejemplo n.º 7
0
        private static IntPtr LoadPosixLibrary(out SymbolLookupDelegate symbolLookup)
        {
            string rootDirectory = AppDomain.CurrentDomain.BaseDirectory;

            // Environment.OSVersion.Platform returns "Unix" for Unix or OSX, so use RuntimeInformation here
            var    isOsx   = RuntimeInformation.IsOSPlatform(OSPlatform.OSX);
            string libFile = isOsx ? "libfreetype.dylib" : "libfreetype.so";
            string arch    = isOsx ? "osx" : "linux-" + (Environment.Is64BitProcess ? "x64" : "x86");

            // Search a few different locations for our native assembly
            var paths = new[]
            {
                // This is where native libraries in our nupkg should end up
                Path.Combine(rootDirectory, "runtimes", arch, "native", libFile),
                // The build output folder
                Path.Combine(rootDirectory, libFile),
                Path.Combine("/usr/local/lib", libFile),
                Path.Combine("/usr/lib", libFile)
            };

            foreach (var path in paths)
            {
                if (path == null)
                {
                    continue;
                }
                if (!File.Exists(path))
                {
                    continue;
                }

                var addr = dlopen(path, RTLD_NOW);
                if (addr == IntPtr.Zero)
                {
                    // Not using NanosmgException because it depends on nn_errno.
                    var error = Marshal.PtrToStringAnsi(dlerror());
                    throw new Exception("dlopen failed: " + path + " : " + error);
                }

                symbolLookup      = dlsym;
                NativeLibraryPath = path;
                return(addr);
            }

            throw new Exception("dlopen failed: unable to locate library " + libFile + ". Searched: " + paths.Aggregate((a, b) => a + "; " + b));
        }
Ejemplo n.º 8
0
        static IntPtr LoadPosixLibrary(string libName, out SymbolLookupDelegate symbolLookup)
        {
            const int RTLD_NOW          = 2;
            string    libFile           = "lib" + libName.ToLower() + ".so";
            string    assemblyDirectory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);

            var paths = new[]
            {
                CombinePaths(Is64BitProcess ? "x64" : "x86", libFile),
                CombinePaths(assemblyDirectory, Is64BitProcess ? "x64" : "x86", libFile),
                CombinePaths(assemblyDirectory, libFile),
                CombinePaths("/usr/local/lib", libFile),
                CombinePaths("/usr/lib", libFile)
            };

            foreach (var path in paths)
            {
                if (path == null)
                {
                    continue;
                }

                if (File.Exists(path))
                {
                    var addr = dlopen(path, RTLD_NOW);
                    if (addr == IntPtr.Zero)
                    {
                        // Not using NanosmgException because it depends on nn_errno.
                        throw new Exception("dlopen failed: " + path + " : " + Marshal.PtrToStringAnsi(dlerror()));
                    }
                    symbolLookup      = dlsym;
                    NativeLibraryPath = path;
                    return(addr);
                }
            }

            throw new Exception("dlopen failed: unable to locate library " + libFile + ". Searched: " + paths.Aggregate((a, b) => a + "; " + b));
        }
Ejemplo n.º 9
0
        private static IntPtr LoadWindowsLibrary(out SymbolLookupDelegate symbolLookup)
        {
            string libFile       = "freetype.dll";
            string arch          = Environment.Is64BitProcess ? "win-x64" : "win-x86";
            string rootDirectory = AppDomain.CurrentDomain.BaseDirectory;

            var paths = new[]
            {
                // This is where native libraries in our nupkg should end up
                Path.Combine(rootDirectory, "runtimes", arch, "native", libFile),
                Path.Combine(rootDirectory, Environment.Is64BitProcess ? "x64" : "x86", libFile),
                Path.Combine(rootDirectory, libFile)
            };

            foreach (var path in paths)
            {
                if (path == null)
                {
                    continue;
                }
                if (!File.Exists(path))
                {
                    continue;
                }

                var addr = LoadLibrary(path);
                if (addr == IntPtr.Zero)
                {
                    throw new Exception("LoadLibrary failed: " + path);
                }

                symbolLookup      = GetProcAddress;
                NativeLibraryPath = path;
                return(addr);
            }

            throw new Exception("LoadLibrary failed: unable to locate library " + libFile + ". Searched: " + paths.Aggregate((a, b) => a + "; " + b));
        }
Ejemplo n.º 10
0
        private static IntPtr LoadPosixLibrary(string libName, out SymbolLookupDelegate symbolLookup)
        {
            const int RTLD_NOW          = 2;
            const int RTLD_GLOBAL       = 256;
            string    libFile           = "lib" + libName.ToLower() + ".so";
            string    rootDirectory     = AppDomain.CurrentDomain.BaseDirectory;
            string    assemblyDirectory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);

            var libs = new[]
            {
                libFile + ".5",
                libFile + ".5.1.0",
                libFile + ".5.0.0",
                libFile + ".4.0.0",
                libFile + ".3.0.0",
                libFile + ".0",
                libFile
            };

            var paths = new[]
            {
                "/usr/local/lib" + (Environment.Is64BitProcess ? "64" : "32"),
                "/usr/local/lib",
                "/usr/lib" + (Environment.Is64BitProcess ? "64" : "32"),
                "/usr/lib",
                Path.Combine(assemblyDirectory, "net40"),
                Path.Combine(rootDirectory, "bin", Environment.Is64BitProcess ? "x64" : "x86"),
                Path.Combine(rootDirectory, Environment.Is64BitProcess ? "x64" : "x86"),
                rootDirectory,
            };

            foreach (var path in paths)
            {
                if (path == null)
                {
                    continue;
                }

                foreach (var lib in libs)
                {
                    if (lib == null)
                    {
                        continue;
                    }

                    string libpath = Path.Combine(path, lib);

                    if (File.Exists(libpath))
                    {
                        var addr = dlopen(libpath, RTLD_NOW + RTLD_GLOBAL);
                        if (addr == IntPtr.Zero)
                        {
                            // Not using NanosmgException because it depends on nn_errno.
                            throw new Exception("dlopen failed: " + libpath + " : " + Marshal.PtrToStringAnsi(dlerror()));
                        }
                        symbolLookup      = dlsym;
                        NativeLibraryPath = libpath;
                        return(addr);
                    }
                }
            }

            throw new Exception("dlopen failed: unable to locate library " + libFile + ". Searched: " + paths.Aggregate((a, b) => a + "; " + b));
        }
Ejemplo n.º 11
0
        private static IntPtr LoadPosixLibrary(string libName, out SymbolLookupDelegate symbolLookup)
        {
            const int RTLD_NOW = 2;
            // RTLD_NOLOAD is different on OSX and Linux
            // https://opensource.apple.com/source/dyld/dyld-195.5/include/dlfcn.h.auto.html
            int    RTLD_NOLOAD       = (Environment.OSVersion.Platform == PlatformID.MacOSX ? 16 : 4);
            string libsuffix         = (Environment.OSVersion.Platform == PlatformID.MacOSX ? ".dylib" : ".so");
            string libFileBase       = "lib" + libName.ToLower();
            string rootDirectory     = AppDomain.CurrentDomain.BaseDirectory;
            string assemblyDirectory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
            string searched          = "";

            // Support libnanomsg installed into the system.
            // Try to open nanomsg library versions from the newest to the oldest.
            // Versions:
            // 5     - nanomsg 1.1.3+
            // 5.1.0 - nanomsg 1.1.0 ... 1.1.2
            // 5.0.0 - nanomsg 0.9-beta ... 1.0.x
            // 4.0.0 - nanomsg 0.8-beta
            // 3.0.0 - nanomsg 0.7-beta
            // 0     - nanomsg 0.6-beta or older
            // ""    - fallback to the SDK symlink or the shipped binary.
            var versions = new []
            {
                ".5", ".5.1.0", ".5.0.0", ".4.0.0", ".3.0.0", ".0", ""
            };

            // Support the embedded Mono case when the native binary is
            // also linked with nanomsg.
            foreach (var ver in versions)
            {
                string libFile = (Environment.OSVersion.Platform == PlatformID.MacOSX ? libFileBase + ver + libsuffix : libFileBase + libsuffix + ver);
                var    addr    = dlopen(libFile, RTLD_NOW + RTLD_NOLOAD);

                // It's not an error when dlopen() returns NULL in this case.
                // We'll retry later without RTLD_NOLOAD.
                if (addr == IntPtr.Zero)
                {
                    continue;
                }

                symbolLookup      = dlsym;
                NativeLibraryPath = libFile;
                return(addr);
            }
            ;

            foreach (var ver in versions)
            {
                string libFile = (Environment.OSVersion.Platform == PlatformID.MacOSX ? libFileBase + ver + libsuffix : libFileBase + libsuffix + ver);

                // Prefer libFile as is, i.e. dlopen() searches it for us.
                // This solves multilib and path finding problems for us.
                var paths = new[]
                {
                    libFile,
                    calculatexdir(assemblyDirectory, "net40", libFile),
                    Path.Combine(rootDirectory, "bin", Environment.Is64BitProcess ? "x64" : "x86", libFile),
                    Path.Combine(rootDirectory, Environment.Is64BitProcess ? "x64" : "x86", libFile),
                    Path.Combine(rootDirectory, libFile),
                };

                foreach (var path in paths)
                {
                    if (path == null)
                    {
                        continue;
                    }

                    var addr = dlopen(path, RTLD_NOW);
                    if (addr == IntPtr.Zero)
                    {
                        // It's not an error if dlopen() returns NULL.
                        // We'll retry with a different path.
                        continue;
                    }
                    symbolLookup      = dlsym;
                    NativeLibraryPath = path;
                    return(addr);
                }

                searched = searched + paths.Aggregate((a, b) => a + "; " + b);
            }
            ;

            throw new Exception("dlopen failed: unable to locate nanomsg library. Searched: " + searched);
        }
Ejemplo n.º 12
0
 private static IntPtr LoadiOSLibrary(out SymbolLookupDelegate symbolLookup)
 {
     symbolLookup      = null;
     NativeLibraryPath = "__Internal";
     return(IntPtr.Zero);
 }
Ejemplo n.º 13
0
        static IntPtr LoadWindowsLibrary(string libName, out SymbolLookupDelegate symbolLookup)
        {
            string libFile = libName + ".dll";
            string rootDirectory = AppDomain.CurrentDomain.BaseDirectory;
            string assemblyDirectory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);

            var paths = new[]
                {
                    calculatexdir(assemblyDirectory, "net40", libFile),
                    Path.Combine(rootDirectory, "bin", Environment.Is64BitProcess ? "x64" : "x86", libFile),
                    Path.Combine(rootDirectory, Environment.Is64BitProcess ? "x64" : "x86", libFile),
                    Path.Combine(rootDirectory, libFile)
                };

            foreach (var path in paths)
            {
                if (path == null)
                {
                    continue;
                }

                if (File.Exists(path))
                {
                    var addr = LoadLibrary(path);
                    if (addr == IntPtr.Zero)
                    {
                        // Not using NanomsgException because it depends on nn_errno.
                        throw new Exception("LoadLibrary failed: " + path);
                    }
                    symbolLookup = GetProcAddress;
                    return addr;
                }
            }

            throw new Exception("LoadLibrary failed: unable to locate library " + libFile + ". Searched: " + paths.Aggregate((a, b) => a + "; " + b));
        }
Ejemplo n.º 14
0
        static IntPtr LoadPosixLibrary(string libName, out SymbolLookupDelegate symbolLookup)
        {
            const int RTLD_NOW = 2;
            string libFile = "lib" + libName.ToLower() + ".so";
            string rootDirectory = AppDomain.CurrentDomain.BaseDirectory;
            string assemblyDirectory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);

            var paths = new[]
                {
                    calculatexdir(assemblyDirectory, "net40", libFile),
                    Path.Combine(rootDirectory, "bin", Environment.Is64BitProcess ? "x64" : "x86", libFile),
                    Path.Combine(rootDirectory, Environment.Is64BitProcess ? "x64" : "x86", libFile),
                    Path.Combine(rootDirectory, libFile),
                    Path.Combine("/usr/local/lib", libFile),
                    Path.Combine("/usr/lib", libFile)
                };

            foreach (var path in paths)
            {
                if (path == null)
                {
                    continue;
                }

                if (File.Exists(path))
                {
                    var addr = dlopen(path, RTLD_NOW);
                    if (addr == IntPtr.Zero)
                    {
                        // Not using NanosmgException because it depends on nn_errno.
                        throw new Exception("dlopen failed: " + path + " : " + Marshal.PtrToStringAnsi(dlerror()));
                    }
                    symbolLookup = dlsym;
                    return addr;
                }
            }

            throw new Exception("dlopen failed: unable to locate library " + libFile + ". Searched: " + paths.Aggregate((a, b) => a + "; " + b));
        }