SupinePandora43 / UltralightNet

.NET bindings for Ultralight next-gen HTML renderer
https://github.com/SupinePandora43/UltralightNet
MIT License
63 stars 10 forks source link

DLL not found on macOS #22

Closed nesk closed 3 years ago

nesk commented 3 years ago

When I try to run UltralightNet.AppCore.TestApp on macOS, I use the following commands:

git clone git@github.com:SupinePandora43/UltralightNet.git
cd UltralightNet
dotnet restore UltralightNet.AppCore.TestApp
dotnet run --project  UltralightNet.AppCore.TestApp

And I get the following error:

Unhandled exception. System.TypeInitializationException: The type initializer for 'UltralightNet.AppCore.AppCoreMethods' threw an exception.
 ---> System.TypeInitializationException: The type initializer for 'UltralightNet.Methods' threw an exception.
 ---> System.DllNotFoundException: Unable to load shared library 'libUltralightCore.dylib' or one of its dependencies. In order to help diagnose loading problems, consider setting the DYLD_PRINT_LIBRARIES environment variable: dlopen(libUltralightCore.dylib, 1): image not found
   at System.Runtime.InteropServices.NativeLibrary.LoadFromPath(String libraryName, Boolean throwOnError)
   at System.Runtime.InteropServices.NativeLibrary.Load(String libraryPath)
   at UltralightNet.Methods.Preload() in /Users/johann/dev/UltralightNet/UltralightNet/Methods.cs:line 45
   at UltralightNet.Methods..cctor() in /Users/johann/dev/UltralightNet/UltralightNet/Methods.cs:line 10
   --- End of inner exception stack trace ---
   at UltralightNet.Methods.Preload() in /Users/johann/dev/UltralightNet/UltralightNet/Methods.cs:line 30
   at UltralightNet.AppCore.AppCoreMethods..cctor() in /Users/johann/dev/UltralightNet/UltralightNet.AppCore/AppCoreMethods.cs:line 9
   --- End of inner exception stack trace ---
   at UltralightNet.AppCore.AppCoreMethods.ulEnableDefaultLogger(String log_path) in /Users/johann/dev/UltralightNet/UltralightNet.AppCore/DllImportGenerator/Microsoft.Interop.DllImportGenerator/DllImportGenerator.g.cs:line 47
   at UltralightNet.AppCore.TestApp.Program.Main() in /Users/johann/dev/UltralightNet/UltralightNet.AppCore.TestApp/Program.cs:line 10

FYI, the same commands work perfectly on Windows, I didn't try on Linux tho but I think it could fail the same (and it will be easier to reproduce if you don't have an Apple device).

Maybe you know what happens here? πŸ˜…

SupinePandora43 commented 3 years ago

I think linux version should work. Because manual loading of libs is only on macos. (see Methods.cs) I will try to fix that (load without lib, .dylib etc like a minigun catching a fly)

Thanks for feedback!

nesk commented 3 years ago

I can try to run some patches if you need help πŸ™‚

nesk commented 3 years ago

I tried variants like UltralightCore.dylib, libUltralightCore, UltralightCore, it didn't work.

I will try other things, now I see where it fails.

nesk commented 3 years ago

Ooooh, it's a simple issue with relative/absolute paths. If I change like that, it works:

#if NET5_0_OR_GREATER
            // i don't have iphone/mac, and probably never
            // so it will not work for ios, i'm sure 100%%%
            if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
            {
                string[] libs = new[] { "libUltralightCore.dylib", "libWebCore.dylib", "libUltralight.dylib" };
                foreach(string lib in libs)
                {
-                   string path = $"runtimes/osx-x64/native/{lib}";
+                   string path = $"/Users/johann/dev/UltralightNet/UltralightNet.AppCore.TestApp/bin/Debug/net5.0/runtimes/osx-x64/native/{lib}";
                    if (File.Exists(path))
                    {
                        NativeLibrary.Load(path);
                        continue;
                    }
                    NativeLibrary.Load(lib);
                }
            }
#endif

I can't work on this right now, but I will create a PR to solve that. πŸ˜‰

SupinePandora43 commented 3 years ago

Ooooh, it's a simple issue with relative/absolute paths. If I change like that, it works:

#if NET5_0_OR_GREATER
          // i don't have iphone/mac, and probably never
          // so it will not work for ios, i'm sure 100%%%
          if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
          {
              string[] libs = new[] { "libUltralightCore.dylib", "libWebCore.dylib", "libUltralight.dylib" };
              foreach(string lib in libs)
              {
-                 string path = $"runtimes/osx-x64/native/{lib}";
+                 string path = $"/Users/johann/dev/UltralightNet/UltralightNet.AppCore.TestApp/bin/Debug/net5.0/runtimes/osx-x64/native/{lib}";
                  if (File.Exists(path))
                  {
                      NativeLibrary.Load(path);
                      continue;
                  }
                  NativeLibrary.Load(lib);
              }
          }
#endif

I can't work on this right now, but I will create a PR to solve that. πŸ˜‰

Can you try using Path.Combine(typeof(Methods).Assembly.Location, "runtimes", "osx-x64", "native", lib)? If it will work, you can submit a pr then.