Search Unity

  1. Unity Asset Manager is now available in public beta. Try it out now and join the conversation here in the forums.
    Dismiss Notice

Fails to compile on WebGL platform contain native plugin files (.bc)

Discussion in '2021.2 Beta' started by EnoxSoftware, Jul 7, 2021.

  1. EnoxSoftware

    EnoxSoftware

    Joined:
    Oct 29, 2014
    Posts:
    1,566
    I created a simple native plugin for Unity 2021.2.0b2 with emscripten 2.0.19.

    emcc Main.cpp -emit-llvm -c -o NativePluginExample.bc

    Main.cpp
    Code (CSharp):
    1. #if defined(__CYGWIN32__)
    2.     #define UNITY_INTERFACE_API __stdcall
    3.     #define UNITY_INTERFACE_EXPORT __declspec(dllexport)
    4. #elif defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(_WIN64) || defined(WINAPI_FAMILY)
    5.     #define UNITY_INTERFACE_API __stdcall
    6.     #define UNITY_INTERFACE_EXPORT __declspec(dllexport)
    7. #elif defined(__MACH__) || defined(__ANDROID__) || defined(__linux__)
    8.     #define UNITY_INTERFACE_API
    9.     #define UNITY_INTERFACE_EXPORT
    10. #else
    11.     #define UNITY_INTERFACE_API
    12.     #define UNITY_INTERFACE_EXPORT
    13. #endif
    14.  
    15. extern "C"
    16. {
    17.  
    18. UNITY_INTERFACE_EXPORT int UNITY_INTERFACE_API GetNumber()
    19. {
    20.     return 123;
    21. }
    22.  
    23. }
    24.  
    NativePluginExample.cs
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Runtime.InteropServices;
    3.  
    4. namespace NativePluginExample
    5. {
    6.  
    7.     public class NativePluginExample : MonoBehaviour
    8.     {
    9.  
    10. #if (UNITY_IOS || UNITY_TVOS || UNITY_WEBGL) && !UNITY_EDITOR
    11.         [DllImport ("__Internal")]
    12. #else
    13.         [DllImport("NativePluginExample")]
    14. #endif
    15.         public static extern int GetNumber();
    16.  
    17.         void Start()
    18.         {
    19.             Debug.Log(GetNumber());
    20.         }
    21.     }
    22.  
    23. }
    I imported the generated NativePluginExample.bc into new project ( Unity2021.2.0b2 ) and tried to build it. However, the following error occurs during compilation. ( NativePluginExample.unitypackage )
    Code (CSharp):
    1. Building Library/Bee/artifacts/WebGL/build/debug_WebGL_wasm/build.js failed with output:
    2. emcc:WARNING: --llvm-lto ignored when using llvm backend
    3. emcc2: warning: EXTRA_EXPORTED_RUNTIME_METHODS is deprecated, please use EXPORTED_RUNTIME_METHODS instead [-Wdeprecated]
    4. emcc2: error: /var/folders/l3/v_86snz551j_cjtcwbd_chb80000gn/T/tmpzxgicvcyNativePluginExample.a: File has a suffix of a static library ('.a',), but instead is an LLVM bitcode file! When linking LLVM bitcode files use .bc or .o.
    5. UnityEngine.GUIUtility:ProcessEvent (int,intptr,bool&) (at /Users/bokken/buildslave/unity/build/Modules/IMGUI/GUIUtility.cs:189)
    6.  
    スクリーンショット 2021-07-08 0.54.43.png


    When using emscripten1.38.11 and Unity2019.4.18f1, it works fine.
    スクリーンショット 2021-07-08 0.51.23.png
     

    Attached Files:

    Bersaelor likes this.
  2. Bersaelor

    Bersaelor

    Joined:
    Oct 8, 2016
    Posts:
    111
    This seemed to work up until 2021.2.0a19 but stopped working in 2.0b1 , 2.0b2 and 2.0b3 .
    Unfortunately the prior version 2021.1 didn't have the recent changes to the WebGL video-permissions so in order for our apps to work on Safari, we needed to use 2021.2 (as discussed with @jukka_j ).

    Now, with 2.0b1 no longer building the native plugin files above, we're kind of stuck. Please help @Unity-Team!
     
  3. Bersaelor

    Bersaelor

    Joined:
    Oct 8, 2016
    Posts:
    111
    Tried again with 2021.2.0b4, still not working.

    Did you file a bug report yet @EnoxSoftware ? (via the built-in editor tool in Unity? I usually got good feedback on those)
     
  4. EnoxSoftware

    EnoxSoftware

    Joined:
    Oct 29, 2014
    Posts:
    1,566
    Yesterday, I submitted a bug report via UnityEditor.
     
    Bersaelor likes this.
  5. LeonhardP

    LeonhardP

    Unity Technologies

    Joined:
    Jul 4, 2016
    Posts:
    3,136
    EnoxSoftware likes this.
  6. LeonhardP

    LeonhardP

    Unity Technologies

    Joined:
    Jul 4, 2016
    Posts:
    3,136
    EnoxSoftware likes this.
  7. anthony_b_

    anthony_b_

    Unity Technologies

    Joined:
    Oct 27, 2020
    Posts:
    24
    Thank you for reporting the bug. The issue here is caused by the manner in which we are integrating the Emscripten compiler into the Unity Build process. We're still investigating solutions.

    However, as a workaround, you can convert the NativePluginExample.bc into an archive file, and then rename with a .bc extension. Then the Unity build will correctly include and link your plugin.

    For example, use the emar tool to build the archive
    emar r NativePluginExample.a NativePluginExample.bc

    And then rename the NativePluginExample.a to have a .bc extension and include that in your Unity project.