LWJGL / lwjgl3

LWJGL is a Java library that enables cross-platform access to popular native APIs useful in the development of graphics (OpenGL, Vulkan, bgfx), audio (OpenAL, Opus), parallel computing (OpenCL, CUDA) and XR (OpenVR, LibOVR, OpenXR) applications.
https://www.lwjgl.org
BSD 3-Clause "New" or "Revised" License
4.67k stars 628 forks source link

libfmod.dylib is not automatically loaded on Mac #941

Closed soundasleep closed 7 months ago

soundasleep commented 7 months ago

Version

3.3.3

Platform

macOS x64

JDK

OpenJDK 64-Bit Server VM v17.0.4+8 by Eclipse Adoptium

Module

FMOD

Bug description

Hello! As far as I can tell the best way to package FMOD is to use both libfmod and libfmodstudio (two separate .dlls or .dylibs). This works fine on Windows but out-of-the-box I got an UnsatisfiedLinkError when trying to load libfmodstudio.dylib (see attached).

A simple workaround is to first load the library before your first FMOD library call:

        MacOSXLibraryDL library = new MacOSXLibraryDL("libfmod.dylib");
        System.out.println("library -> " + library);
        // continue as normal
        FMODStudio.FMOD_Studio_System_Create(buf /* out */, FMOD.FMOD_VERSION);

Stacktrace or crash log output

Could not start game: Failed to dynamically load library: /Users/jevon/workspace/xxx/desktop/./libfmodstudio.dylib(error = dlopen(/Users/jevon/workspace/xxx/desktop/./libfmodstudio.dylib, 5): Library not loaded: @rpath/libfmod.dylib
  Referenced from: /Users/jevon/workspace/xxx/desktop/libfmodstudio.dylib
  Reason: image not found)
    at org.jevon.gdx.desktop.DesktopLauncher.main(DesktopLauncher.java:136)
Caused by: java.lang.UnsatisfiedLinkError: Failed to dynamically load library: /Users/jevon/workspace/xxx/desktop/./libfmodstudio.dylib(error = dlopen(/Users/jevon/workspace/xxx/desktop/./libfmodstudio.dylib, 5): Library not loaded: @rpath/libfmod.dylib
  Referenced from: /Users/jevon/workspace/xxx/desktop/libfmodstudio.dylib
  Reason: image not found)
    at org.lwjgl.system.macosx.MacOSXLibraryDL.loadLibrary(MacOSXLibraryDL.java:33)
    at org.lwjgl.system.macosx.MacOSXLibraryDL.<init>(MacOSXLibraryDL.java:20)
    at org.lwjgl.system.macosx.MacOSXLibrary.create(MacOSXLibrary.java:27)
    at org.lwjgl.system.APIUtil.apiCreateLibrary(APIUtil.java:130)
    at org.lwjgl.system.Library.loadNative(Library.java:382)
    at org.lwjgl.system.Library.loadNative(Library.java:327)
    at org.lwjgl.system.Library.loadNative(Library.java:236)
    at org.lwjgl.system.Library.loadNative(Library.java:217)
    at org.lwjgl.system.Library.loadNative(Library.java:431)
    at org.lwjgl.system.Library.loadNative(Library.java:402)
    at org.lwjgl.fmod.FMODStudio.<clinit>(FMODStudio.java:26)
    at org.jevon.fmod.FMODStudioSystemImpl.fmodStudioSystemCreate(FMODStudioSystemImpl.java:135)
    at org.jevon.fmod.FMODStudioSystemImpl.<init>(FMODStudioSystemImpl.java:86)
    at org.jevon.gdx.desktop.DesktopLauncher.run(DesktopLauncher.java:99)
    at org.jevon.gdx.desktop.DesktopLauncher.main(DesktopLauncher.java:134)
Spasi commented 7 months ago

Hey @soundasleep,

A cleaner workaround is to simply call FMOD.getLibrary(). This will trigger FMOD's static init and libfmod.dylib will be loaded by LWJGL.

Anyway, I tried using the FMOD bindings on macOS and got a similar error. Loading the dylibs fails with "Apple cannot check it for malicious software", i.e. the libraries are getting quarantined. After running sudo xattr -r -d com.apple.quarantine . in FMOD's api folder, the bindings worked fine.

soundasleep commented 7 months ago

Thank you @Spasi ! FMOD.getLibrary() works great, and it looks like this has better smarts in finding the location of libraries packaged up through jpackage. (You may need to first codesign the .dylibs before passing them in as --input to jpackage.)

Perhaps in lwjgl, FMODStudio.getLibrary() should first call FMOD.getLibrary()?

Yeah, Apple gatekeeper/quarantine/etc is a nightmare. My understanding is that com.apple.quarantine is applied to anything that's extracted from a .zip.

Super helpful guides on dealing with quarantine and gatekeeper and notarization and signing issues --> https://developer.apple.com/forums/thread/706442

Spasi commented 7 months ago

@soundasleep FMODStudio and FSBank will load the fmod library automatically in 3.3.4-snapshot+2. Thanks!