bkaradzic / bgfx

Cross-platform, graphics API agnostic, "Bring Your Own Engine/Framework" style rendering library.
https://bkaradzic.github.io/bgfx/overview.html
BSD 2-Clause "Simplified" License
14.75k stars 1.92k forks source link

Android compilation issues (multiple OS, multiple NDKs) #1340

Open rgipd opened 6 years ago

rgipd commented 6 years ago

Hello,

I'm trying to compile the android version with: make android-arm or make android-x86 with the atest repo's git files.

I alreay tested with: macOS High Sierra (all updated XCode and files) Ubuntu 16.04 64-bit (the 32-bit genie does not works :)) Ubuntu 17.04 64-bit as well

With the NDKs versions: android-ndk-r10e android-ndk-r16b (latest)

All with the same error. First, I set the 3 main env:

ANDROID_NDK_ROOT=/dev/android/android-ndk-r16b ANDROID_NDK_ARM=/dev/android/android-ndk-r16b/toolchains/arm-linux-androideabi-4.9/prebuilt/linux-x86_64 ANDROID_NDK_CLANG=/dev/android/android-ndk-r16b/toolchains/llvm/prebuilt/linux-x86_64

The first error is the:

In file included from ../../../../bx/src/bx_p.h:42: ../../../../bx/include/bx/bx.h:9:10: fatal error: 'alloca.h' file not found

include // alloca

^~~~~~ 1 error generated. bx.make:395: recipe for target '../../android-arm/obj/Debug/bx/bx/src/allocator.o' failed

It seems that android has not a compat/android-arm or compat/android to put alloca.h, so I removed the #include . Then goes to next error:

==== Building bx (debug) ==== allocator.cpp In file included from ../../../../bx/src/allocator.cpp:6: In file included from ../../../../bx/src/bx_p.h:42: In file included from ../../../../bx/include/bx/bx.h:11: In file included from /dev/android/android-ndk-r16b/sources/cxx-stl/llvm-libc++/include/stdint.h:102: /dev/android/android-ndk-r16b/sources/cxx-stl/llvm-libc++/include/__config:169:10: fatal error: 'features.h' file not found

include

^~~~ 1 error generated. bx.make:395: recipe for target '../../android-arm/obj/Debug/bx/bx/src/allocator.o' failed

Everytime I try to fix some header (deleting it to check), a new error appears.

Which NDK version should I try?

Thanks.

TumblerSGQ commented 6 years ago

how to solve it? have you solved it?

rgipd commented 6 years ago

No, I couldn't solve it for days. The android-ndk-r13b is better, but still couldn't do it.

TumblerSGQ commented 6 years ago

ndk-r14b can wok! hope it can help you

bkaradzic commented 6 years ago

I'm building with r13b and everything works...

jimon commented 6 years ago

This is my script that I use to add android ndk to the path:

set ANDROID_SDK_ROOT=C:/Users/%enter_your_user_here%/AppData/Local/Android/sdk
set ANDROID_NDK_ROOT=%ANDROID_SDK_ROOT%/ndk-bundle
set ANDROID_NDK_ARM=%ANDROID_NDK_ROOT%/toolchains/arm-linux-androideabi-4.9/prebuilt/windows-x86_64
set ANDROID_NDK_MIPS=%ANDROID_NDK_ROOT%/toolchains/mipsel-linux-android-4.9/prebuilt/windows-x86_64
set ANDROID_NDK_X86=%ANDROID_NDK_ROOT%/toolchains/x86-4.9/prebuilt/windows-x86_64
set ANDROID_NDK_CLANG=%ANDROID_NDK_ROOT%/toolchains/llvm/prebuilt/windows-x86_64

set PATH=%PATH%;%ANDROID_SDK_ROOT%/platform-tools

And this is what I had to modify to build with latest NDK 16:

For Android I had to modify bx/scripts/toolchain.lua as following:

Under configuration { "android-*" }:

Also for Android I had to remove references to stdin, stdout, stderr in file.cpp, by replacing them with NoopWriter/NoopReader (later I just created by myself).

jimon commented 6 years ago

@rgipd any luck?

rgipd commented 6 years ago

@jimon I will try today or tomorrow and I will let you know. Right now I can't make this test :) Thank you !

rgipd commented 6 years ago

@jimon I tried, using macOS High Sierra. No luck. Lot of warnings. No example compiled for use with android-glue too.

Sorry for the delay in the answer...

thenewvu commented 6 years ago

@rgipd Did you make projgen to apply changes in the toolchain.lua before recompiling?


I tried the fix that @jimon mentioned and it solved missing include directories issues, but I modified some on my own, which almost works with android-ndk-r16b:

diff --git a/scripts/toolchain.lua b/scripts/toolchain.lua
index 9163118..ccf0bfe 100644
--- a/scripts/toolchain.lua
+++ b/scripts/toolchain.lua
@@ -822,6 +822,7 @@ function toolchain(_buildDir, _libDir)
            "NoImportLib",
        }
        includedirs {
+      "${ANDROID_NDK_ROOT}/sysroot/usr/include",
            "$(ANDROID_NDK_ROOT)/sources/cxx-stl/llvm-libc++/include",
            "$(ANDROID_NDK_ROOT)/sources/android/native_app_glue",
        }
@@ -885,12 +886,10 @@ function toolchain(_buildDir, _libDir)
        targetdir (path.join(_buildDir, "android-arm/bin"))
        objdir (path.join(_buildDir, "android-arm/obj"))
        libdirs {
-           path.join(_libDir, "lib/android-arm"),
            "$(ANDROID_NDK_ROOT)/sources/cxx-stl/llvm-libc++/libs/armeabi-v7a",
        }
        includedirs {
-           "$(ANDROID_NDK_ROOT)/sources/cxx-stl/llvm-libc++/libs/armeabi-v7a/include",
-           "$(ANDROID_NDK_ROOT)/sources/cxx-stl/llvm-libc++/include",
+           "$(ANDROID_NDK_ROOT)/sysroot/usr/include/arm-linux-androideabi",
        }
        buildoptions {
            "-gcc-toolchain $(ANDROID_NDK_ARM)",
@@ -917,11 +916,10 @@ function toolchain(_buildDir, _libDir)
        targetdir (path.join(_buildDir, "android-mips/bin"))
        objdir (path.join(_buildDir, "android-mips/obj"))
        libdirs {
-           path.join(_libDir, "lib/android-mips"),
            "$(ANDROID_NDK_ROOT)/sources/cxx-stl/llvm-libc++/libs/mips",
        }
        includedirs {
-           "$(ANDROID_NDK_ROOT)/sources/cxx-stl/llvm-libc++/libs/mips/include",
+           "$(ANDROID_NDK_ROOT)/sysroot/usr/include/mipsel-linux-androideabi",
        }
        buildoptions {
            "-gcc-toolchain $(ANDROID_NDK_MIPS)",
@@ -944,11 +942,10 @@ function toolchain(_buildDir, _libDir)
        targetdir (path.join(_buildDir, "android-x86/bin"))
        objdir (path.join(_buildDir, "android-x86/obj"))
        libdirs {
-           path.join(_libDir, "lib/android-x86"),
            "$(ANDROID_NDK_ROOT)/sources/cxx-stl/llvm-libc++/libs/x86",
        }
        includedirs {
-           "$(ANDROID_NDK_ROOT)/sources/cxx-stl/llvm-libc++/libs/x86/include",
+           "$(ANDROID_NDK_ROOT)/sysroot/usr/include/x86_64-linux-android",
        }
        buildoptions {
            "-gcc-toolchain $(ANDROID_NDK_X86)",
@@ -963,6 +960,7 @@ function toolchain(_buildDir, _libDir)
            "-Wundef",
        }
        linkoptions {
+      "-v",
            "-gcc-toolchain $(ANDROID_NDK_X86)",
            "--sysroot=" .. path.join("$(ANDROID_NDK_ROOT)/platforms", androidPlatform, "arch-x86"),
            path.join("$(ANDROID_NDK_ROOT)/platforms", androidPlatform, "arch-x86/usr/lib/crtbegin_so.o"),

But with android-ndk-r16b, I got another issue, it says unknown option -eh-frame-hdr in linking. Currently, I have no fix for it, it's an ndk issue I believe.

thenewvu commented 6 years ago

Finally I got success in compiling for android with android-ndk-r17-beta1. Below are what I modified:

bgfx

diff --git a/examples/20-nanovg/nanovg.cpp b/examples/20-nanovg/nanovg.cpp
index 418614a..fe4668b 100644
--- a/examples/20-nanovg/nanovg.cpp
+++ b/examples/20-nanovg/nanovg.cpp
@@ -69,7 +69,7 @@ static char* cpToUTF8(int cp, char* str)
    else if (cp < 0x10000) n = 3;
    else if (cp < 0x200000) n = 4;
    else if (cp < 0x4000000) n = 5;
-   else if (cp <= 0x7fffffff) n = 6;
+   else n = 6;
    str[n] = '\0';
    switch (n)
    {

bx

diff --git a/scripts/toolchain.lua b/scripts/toolchain.lua
index 9163118..cd69ccb 100644
--- a/scripts/toolchain.lua
+++ b/scripts/toolchain.lua
@@ -237,11 +237,13 @@ function toolchain(_buildDir, _libDir)
            if not os.getenv("ANDROID_NDK_X86")
            or not os.getenv("ANDROID_NDK_CLANG")
            or not os.getenv("ANDROID_NDK_ROOT") then
-               print("Set ANDROID_NDK_CLANG, ANDROID_NDK_ARM, and ANDROID_NDK_ROOT environment variables.")
+               print("Set ANDROID_NDK_CLANG, ANDROID_NDK_X86, and ANDROID_NDK_ROOT environment variables.")
            end

            premake.gcc.cc   = "$(ANDROID_NDK_CLANG)/bin/clang"
            premake.gcc.cxx  = "$(ANDROID_NDK_CLANG)/bin/clang++"
+           premake.gcc.ar   = "$(ANDROID_NDK_X86)/bin/i686-linux-android-ar"
+
            premake.gcc.llvm = true
            location (path.join(_buildDir, "projects", _ACTION .. "-android-x86"))

@@ -822,6 +824,7 @@ function toolchain(_buildDir, _libDir)
            "NoImportLib",
        }
        includedirs {
+      "${ANDROID_NDK_ROOT}/sysroot/usr/include",
            "$(ANDROID_NDK_ROOT)/sources/cxx-stl/llvm-libc++/include",
            "$(ANDROID_NDK_ROOT)/sources/android/native_app_glue",
        }
@@ -885,12 +888,10 @@ function toolchain(_buildDir, _libDir)
        targetdir (path.join(_buildDir, "android-arm/bin"))
        objdir (path.join(_buildDir, "android-arm/obj"))
        libdirs {
-           path.join(_libDir, "lib/android-arm"),
            "$(ANDROID_NDK_ROOT)/sources/cxx-stl/llvm-libc++/libs/armeabi-v7a",
        }
        includedirs {
-           "$(ANDROID_NDK_ROOT)/sources/cxx-stl/llvm-libc++/libs/armeabi-v7a/include",
-           "$(ANDROID_NDK_ROOT)/sources/cxx-stl/llvm-libc++/include",
+      "$(ANDROID_NDK_ROOT)/sysroot/usr/include/arm-linux-androideabi",
        }
        buildoptions {
            "-gcc-toolchain $(ANDROID_NDK_ARM)",
@@ -917,11 +918,9 @@ function toolchain(_buildDir, _libDir)
        targetdir (path.join(_buildDir, "android-mips/bin"))
        objdir (path.join(_buildDir, "android-mips/obj"))
        libdirs {
-           path.join(_libDir, "lib/android-mips"),
            "$(ANDROID_NDK_ROOT)/sources/cxx-stl/llvm-libc++/libs/mips",
        }
        includedirs {
-           "$(ANDROID_NDK_ROOT)/sources/cxx-stl/llvm-libc++/libs/mips/include",
        }
        buildoptions {
            "-gcc-toolchain $(ANDROID_NDK_MIPS)",
@@ -944,11 +943,10 @@ function toolchain(_buildDir, _libDir)
        targetdir (path.join(_buildDir, "android-x86/bin"))
        objdir (path.join(_buildDir, "android-x86/obj"))
        libdirs {
-           path.join(_libDir, "lib/android-x86"),
            "$(ANDROID_NDK_ROOT)/sources/cxx-stl/llvm-libc++/libs/x86",
        }
        includedirs {
-           "$(ANDROID_NDK_ROOT)/sources/cxx-stl/llvm-libc++/libs/x86/include",
+      "$(ANDROID_NDK_ROOT)/sysroot/usr/include/i686-linux-android"
        }
        buildoptions {
            "-gcc-toolchain $(ANDROID_NDK_X86)",
@@ -963,6 +961,7 @@ function toolchain(_buildDir, _libDir)
            "-Wundef",
        }
        linkoptions {
+      "-v",
            "-gcc-toolchain $(ANDROID_NDK_X86)",
            "--sysroot=" .. path.join("$(ANDROID_NDK_ROOT)/platforms", androidPlatform, "arch-x86"),
            path.join("$(ANDROID_NDK_ROOT)/platforms", androidPlatform, "arch-x86/usr/lib/crtbegin_so.o"),

.bashrc

+export ANDROID_NDK_ROOT="$HOME/Library/Android/ndk/r17-beta1"
+export ANDROID_NDK_CLANG="$ANDROID_NDK_ROOT/toolchains/llvm/prebuilt/darwin-x86_64"
+export ANDROID_NDK_ARM="$ANDROID_NDK_ROOT/toolchains/arm-linux-androideabi-4.9/prebuilt/darwin-x86_64"
+export ANDROID_NDK_X86="$ANDROID_NDK_ROOT/toolchains/x86-4.9/prebuilt/darwin-x86_64"

BUT... I didn't test the so file yet ... eh ...

Anyone has a working template project for android that just loads the libexamplesDebug.so and runs its entry?

rgipd commented 6 years ago

@thenewvu Thank you, I will try it and let you know. If the nsdk-17-beta1 worked, now the chances for nsdk-16 works is better!

ekicam2 commented 5 years ago

I'm building with r13b and everything works...

Tested with r13b as well as the newest one, both failed for me.