ImGuiNET / ImGui.NET-nativebuild

Windows/macOS/Linux/iOS/Android build automation for cimgui. Used to package up ImGui.NET.
https://github.com/mellinoe/imgui.net
26 stars 57 forks source link

Help building for Android ( arm64-v8a ) #32

Open nukadelic opened 7 months ago

nukadelic commented 7 months ago

Wasn't sure how to use the shell scripts so i tried using cmake-gui -downloaded and added ninja to path , selected the following toolchain : D:\UnityHubEditors\2021.3.27f1\Editor\Data\PlaybackEngines\AndroidPlayer\NDK\build\cmake\android.toolchain.cmake used the following config :

CMAKE_ANDROID_ARCH_ABI=arm64-v8a
ANDROID_PLATFORM=30
CMAKE_ANDROID_API=30
CMAKE_ANDROID_ARM_MODE=ON

source code location : D:/GitClone/ImGui.NET-nativebuild/cimgui - generated and build .so file with ninja, renamed it to add a lib prefix and dropped into Assets\Plugins\Android\libcimgui.so with platform cpu target set to arm64 , and in project player settings android 11 ( api level 30 ) , IL2CPP backend and ARM64 target architecture , build the apk and made sure the .so file was included by opening it as an archive with 7zip ( it was located alongside all the other .so in \lib\arm64-v8a\libcimgui.so ). But it looks like it fails to load the library

AndroidPlayer "Oculus_Quest" DllNotFoundException: Unable to load DLL 'cimgui'. Tried the load the following dynamic libraries: Unable to load dynamic library 'cimgui' because of 'Failed to open the requested dynamic library (0x06000000) dlerror() = dlopen failed: library "cimgui" not found
  at ImGuiNET.ImGuiNative.igCreateContext (ImGuiNET.ImFontAtlas* shared_font_atlas) [0x00000] in <00000000000000000000000000000000>:0 
  at UImGui.UImGuiUtility.CreateContext () [0x00000] in <00000000000000000000000000000000>:0 
  at UImGui.UImGui.Awake () [0x00000] in <00000000000000000000000000000000>:0 
nukadelic commented 3 months ago

So i recently tried again and I think the found the problem , build and ran logcat and it says the binary is in the wrong arch ( detected 32-bit instead of 64-bit ) :

Failed to load native plugin: Unable to load library '/data/app/~~o9jQvhOgPc5UJHN2ln-qyQ==/com.nukadelic.imgui-Ly_fwrnvjyfFsQ2tyWJzkg==/lib/arm64/libcimgui.so', error 'java.lang.UnsatisfiedLinkError: dlopen failed: "/data/app/~~o9jQvhOgPc5UJHN2ln-qyQ==/com.nukadelic.imgui-Ly_fwrnvjyfFsQ2tyWJzkg==/lib/arm64/libcimgui.so" is 32-bit instead of 64-bit'

was trying to build the .so - in cmake gui i specified the repository path and the output path image then when configuring in a new folder , this will popup , i chose ninja and toolchain for cross compile image then it will ask for the tool chain path in the next screen so i chose this :

according to unity ( https://docs.unity3d.com/2023.2/Documentation/Manual/android-sdksetup.html ) : Note: Only Unity version 2023.2 supports CMake version 3.22.1. Unity versions 2022.3 and below do not support CMake. not sure how if its relevant to what im doing ( the cmake build is separate from unity apk build ) but i figured to use a newer toolchain just in case. And those are the arguments i manually added according to cmake/android docs :

ANDROID_PLATFORM=30
CMAKE_ANDROID_API=30
CMAKE_ANDROID_ARM_MODE=ON
BUILD_ARCH=ARM64
BUILD_CMAKE_GENERATOR_PLATFORM=ARM64
CMAKE_ANDROID_ARCH=arm64
CMAKE_ANDROID_ARCH_ABI=arm64-v8a

Configure -> Generate

Open console in build path ( .../cimgui/build-new ) and ran ninja to build , no errors. Dropped the so file into unity image with the following configuration image Build and run and launched logcat, but it still shows me that the binray is 32bit and not in 64bit as i wanted it to be.

So i tried swapping the -match=armv7 to -match=arm64 , build and compiled - this time the 32bit error seem to went away , but the same DLL error is there ...

CMAKE_CXX_FLAGS:STRING=-DANDROID -fdata-sections -ffunction-sections -funwind-tables -fstack-protector-strong -no-canonical-prefixes -D_FORTIFY_SOURCE=2 -march=arm64 -mthumb -Wformat -Werror=format-security -fexceptions -frtti -stdlib=libc++
CMAKE_C_FLAGS:STRING=-DANDROID -fdata-sections -ffunction-sections -funwind-tables -fstack-protector-strong -no-canonical-prefixes -D_FORTIFY_SOURCE=2 -march=arm64 -mthumb -Wformat -Werror=format-security -fexceptions

That's the change ☝️ , and that's the same error in the console when starting the apk :

DllNotFoundException: Unable to load DLL 'cimgui'. Tried the load the following dynamic libraries: Unable to load dynamic library 'cimgui' because of 'Failed to open the requested dynamic library (0x06000000) dlerror() = dlopen failed: library "cimgui" not found

Next i Have tried to switch match to arm64-v8a , which looks something like this :

CMAKE_CXX_FLAGS:STRING=-DANDROID -fdata-sections -ffunction-sections -funwind-tables -fstack-protector-strong -no-canonical-prefixes -D_FORTIFY_SOURCE=2 -march=arm64-v8a -mthumb -Wformat -Werror=format-security -fexceptions -frtti -stdlib=libc++
CMAKE_C_FLAGS:STRING=-DANDROID -fdata-sections -ffunction-sections -funwind-tables -fstack-protector-strong -no-canonical-prefixes -D_FORTIFY_SOURCE=2 -march=arm64-v8a -mthumb -Wformat -Werror=format-security -fexceptions

But looks like clang doesn't support it ? ....

> ninja
[1/7] Building CXX object CMakeFiles/cimgui.dir/cimgui.cpp.o
FAILED: CMakeFiles/cimgui.dir/cimgui.cpp.o
D:\UnityHubEditors\2023.2.0b10\Editor\Data\PlaybackEngines\AndroidPlayer\NDK\toolchains\llvm\prebuilt\windows-x86_64\bin\clang++.exe --target=armv7-none-linux-androideabi16 --sysroot=D:/UnityHubEditors/2023.2.0b10/Editor/Data/PlaybackEngines/AndroidPlaye
r/NDK/toolchains/llvm/prebuilt/windows-x86_64/sysroot -DIMGUI_DISABLE_OBSOLETE_FUNCTIONS=1 -DIMGUI_IMPL_API="extern     \"C\"   " -Dcimgui_EXPORTS -ID:/GitClone/ImGui.NET-nativebuild/cimgui -ID:/GitClone/ImGui.NET-nativebuild/cimgui/imgui -DANDROID -fdat
a-sections -ffunction-sections -funwind-tables -fstack-protector-strong -no-canonical-prefixes -D_FORTIFY_SOURCE=2 -march=arm64-v8a -mthumb -Wformat -Werror=format-security -fexceptions -frtti -stdlib=libc++ -O2 -g -DNDEBUG -std=gnu++11 -fPIC -MD -MT CMa
keFiles/cimgui.dir/cimgui.cpp.o -MF CMakeFiles\cimgui.dir\cimgui.cpp.o.d -o CMakeFiles/cimgui.dir/cimgui.cpp.o -c D:/GitClone/ImGui.NET-nativebuild/cimgui/cimgui.cpp

clang++: error: the clang compiler does not support '-march=arm64-v8a'

[2/7] Building CXX object CMakeFiles/cimgui.dir/imgui/imgui.cpp.o
...

Do i need a v8a llvm toolchain or something like this ? Or am I doing doing something fundamentally wrong ?