eclipsesource / J2V8

Java Bindings for V8
2.54k stars 355 forks source link

Building an armv8a.so myself #240

Closed mrmaffen closed 7 years ago

mrmaffen commented 7 years ago

Hi there,

this project is pure awesomeness. I'd love to use it in my Android app, however I don't want to drop support for my armv8 and x86_64 builds. That's why I have tried to build it myself and after a few hours of trying and mostly thanks to this how-to https://gist.github.com/magmastonealex/a9158173828932def9ffb81672d6c578 I was actually able to compile the build successfully. The resulting armv8a works just fine when I run my app on an armv8a emulated avd device. But when I start it on my Galaxy S7 the app crashes with a SIGILL:

03-25 19:10:50.812  30573-30628/? A/libc﹕ Fatal signal 4 (SIGILL), code 1, fault addr 0x7b48330e40 in tid 30628 (ScriptAccount_f)
03-25 19:10:50.812  30573-30627/? A/libc﹕ Fatal signal 4 (SIGILL), code 1, fault addr 0x7b8c32e560 in tid 30627 (ScriptAccount_f)
03-25 19:10:50.940  30656-30656/? A/DEBUG﹕ *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
03-25 19:10:50.940  30656-30656/? A/DEBUG﹕ Build fingerprint: 'samsung/heroltexx/herolte:7.0/NRD90M/G930FXXU1DQB3:user/release-keys'
03-25 19:10:50.940  30656-30656/? A/DEBUG﹕ Revision: '8'
03-25 19:10:50.940  30656-30656/? A/DEBUG﹕ ABI: 'arm64'
03-25 19:10:50.941  30656-30656/? A/DEBUG﹕ pid: 30573, tid: 30628, name: ScriptAccount_f  >>> my.app.up <<<
03-25 19:10:50.941  30656-30656/? A/DEBUG﹕ signal 4 (SIGILL), code 1 (ILL_ILLOPC), fault addr 0x7b48330e40
03-25 19:10:50.941  30656-30656/? A/DEBUG﹕ x0   0000000000000000  x1   0000007bd784e699  x2   0000000000000000  x3   0000007bd7804139
03-25 19:10:50.941  30656-30656/? A/DEBUG﹕ x4   0000007b48330e20  x5   00000000000000c8  x6   0000007bdc8958b0  x7   0000007bee4ee0d0
03-25 19:10:50.941  30656-30656/? A/DEBUG﹕ x8   0000007bdbbf26a8  x9   0000007bec4f5f68  x10  0000007bd7804139  x11  0000007b483204e1
03-25 19:10:50.941  30656-30656/? A/DEBUG﹕ x12  0000007b48320540  x13  ffffffffffffffff  x14  00341d0693000000  x15  003b9aca00000000
03-25 19:10:50.941  30656-30656/? A/DEBUG﹕ x16  0000007bd7804139  x17  0000007b483204e1  x18  0000007bd7fafc8f  x19  0000007bd7804139
03-25 19:10:50.941  30656-30656/? A/DEBUG﹕ x20  0000007bd7804139  x21  0000007bd7804139  x22  0000007bd7804139  x23  0000007bd7804139
03-25 19:10:50.941  30656-30656/? A/DEBUG﹕ x24  0000007bd7804139  x25  0000007bd7804139  x26  0000007bee4edcf8  x27  0000007bd783f051
03-25 19:10:50.941  30656-30656/? A/DEBUG﹕ x28  0000007bdbbf23a0  x29  0000007bdbbf23b8  x30  0000007b48320600
03-25 19:10:50.941  30656-30656/? A/DEBUG﹕ sp   0000007bdbbf23a0  pc   0000007b48330e40  pstate 0000000060000000
03-25 19:10:50.943  30656-30656/? A/DEBUG﹕ backtrace:
03-25 19:10:50.943  30656-30656/? A/DEBUG﹕ #00 pc 000000000002ae40  <anonymous:0000007b48306000>
03-25 19:10:50.943  30656-30656/? A/DEBUG﹕ #01 pc 000000000001a5fc  <anonymous:0000007b48306000>

Do you know what's going on here? I'm not that knowledgeable when it comes to native coding in c... mainly a Java/JS/Python guy. I'd love to get this to work.

Thanks for your help!

tnytown commented 7 years ago

According to this stackoverflow answer, it might be a bug in the NDK itself. To paraphrase, the answer proposed adding -m32 to LOCAL_CFLAGS in Android.mk (I'd imagine in J2V8 in this case, not V8 itself). Have you tried that?

mrmaffen commented 7 years ago

@KnownUnown Looks like the ndk bug only applies to x86 builds. The build that crashes with a SIGILL for me is armv8a though :/. But using a different NDK version seems like a good idea. I'll try that now ...

mrmaffen commented 7 years ago

@KnownUnown I was using the NDK r10e before. I've tried every other available release and wasn't able to successfully build the library. Seems like the NDK has changed quite a bit after r10e, therefore a lot of errors are popping up with later releases.

  1. __page_size has been removed and replaced by PAGE_SIZE. That's easily fixable by adding a short dummy cpp and including it in LOCAL_SRC_FILES in Android.mk:
    #include <unistd.h>
    extern unsigned int __page_size = getpagesize();
  2. After fixing that first trivial issue I've ran into more serious issue with "undefined references":
    [armeabi] SharedLibrary  : libj2v8_android_armeabi.so
    /home/mrmaffen/v8-builder/v8/out/android_armeabi.release/obj.target/tools/gyp/libv8_base.a(/home/mrmaffen/v8-builder/v8/out/android_arm.release/obj.target/v8_base/src/assembler.o):assembler.cc:function std::__1::basic_ostream<char, std::__1::char_traits<char> >::sentry::~sentry(): error: undefined reference to 'std::__1::ios_base::clear(unsigned int)'
    /home/mrmaffen/v8-builder/v8/out/android_armeabi.release/obj.target/tools/gyp/libv8_base.a(/home/mrmaffen/v8-builder/v8/out/android_arm.release/obj.target/v8_base/src/assembler.o):assembler.cc:function std::__1::basic_ostream<char, std::__1::char_traits<char> >::flush(): error: undefined reference to 'std::__1::ios_base::clear(unsigned int)'
    /home/mrmaffen/v8-builder/v8/out/android_armeabi.release/obj.target/tools/gyp/libv8_base.a(/home/mrmaffen/v8-builder/v8/out/android_arm.release/obj.target/v8_base/src/assembler.o):assembler.cc:function std::__1::basic_ostream<char, std::__1::char_traits<char> >::operator<<(void const*): error: undefined reference to 'std::__1::ios_base::getloc() const'
    /home/mrmaffen/v8-builder/v8/out/android_armeabi.release/obj.target/tools/gyp/libv8_base.a(/home/mrmaffen/v8-builder/v8/out/android_arm.release/obj.target/v8_base/src/assembler.o):assembler.cc:function std::__1::basic_ostream<char, std::__1::char_traits<char> >::operator<<(void const*): error: undefined reference to 'std::__1::locale::use_facet(std::__1::locale::id&) const'
    /home/mrmaffen/v8-builder/v8/out/android_armeabi.release/obj.target/tools/gyp/libv8_base.a(/home/mrmaffen/v8-builder/v8/out/android_arm.release/obj.target/v8_base/src/assembler.o):assembler.cc:function std::__1::basic_ostream<char, std::__1::char_traits<char> >::operator<<(void const*): error: undefined reference to 'std::__1::locale::~locale()'
    /home/mrmaffen/v8-builder/v8/out/android_armeabi.release/obj.target/tools/gyp/libv8_base.a(/home/mrmaffen/v8-builder/v8/out/android_arm.release/obj.target/v8_base/src/assembler.o):assembler.cc:function std::__1::basic_ostream<char, std::__1::char_traits<char> >::operator<<(void const*): error: undefined reference to 'std::__1::ios_base::getloc() const'
    /home/mrmaffen/v8-builder/v8/out/android_armeabi.release/obj.target/tools/gyp/libv8_base.a(/home/mrmaffen/v8-builder/v8/out/android_arm.release/obj.target/v8_base/src/assembler.o):assembler.cc:function std::__1::basic_ostream<char, std::__1::char_traits<char> >::operator<<(void const*): error: undefined reference to 'std::__1::locale::use_facet(std::__1::locale::id&) const'
    /home/mrmaffen/v8-builder/v8/out/android_armeabi.release/obj.target/tools/gyp/libv8_base.a(/home/mrmaffen/v8-builder/v8/out/android_arm.release/obj.target/v8_base/src/assembler.o):assembler.cc:function std::__1::basic_ostream<char, std::__1::char_traits<char> >::operator<<(void const*): error: undefined reference to 'std::__1::locale::~locale()'
    /home/mrmaffen/v8-builder/v8/out/android_armeabi.release/obj.target/tools/gyp/libv8_base.a(/home/mrmaffen/v8-builder/v8/out/android_arm.release/obj.target/v8_base/src/assembler.o):assembler.cc:function std::__1::basic_ostream<char, std::__1::char_traits<char> >::operator<<(void const*): error: undefined reference to 'std::__1::ios_base::clear(unsigned int)'
    /home/mrmaffen/v8-builder/v8/out/android_armeabi.release/obj.target/tools/gyp/libv8_base.a(/home/mrmaffen/v8-builder/v8/out/android_arm.release/obj.target/v8_base/src/assembler.o):assembler.cc:function std::__1::basic_ostream<char, std::__1::char_traits<char> >::operator<<(void const*): error: undefined reference to 'std::__1::num_put<char, std::__1::ostreambuf_iterator<char, std::__1::char_traits<char> > >::id'
    /home/mrmaffen/v8-builder/v8/out/android_armeabi.release/obj.target/tools/gyp/libv8_base.a(/home/mrmaffen/v8-builder/v8/out/android_arm.release/obj.target/v8_base/src/assembler.o):assembler.cc:function std::__1::basic_ostream<char, std::__1::char_traits<char> >::operator<<(void const*): error: undefined reference to 'std::__1::ctype<char>::id'
    /home/mrmaffen/v8-builder/v8/out/android_armeabi.release/obj.target/tools/gyp/libv8_base.a(/home/mrmaffen/v8-builder/v8/out/android_arm.release/obj.target/v8_base/src/assembler.o):assembler.cc:function std::__1::basic_ostream<char, std::__1::char_traits<char> >& std::__1::__put_character_sequence<char, std::__1::char_traits<char> >(std::__1::basic_ostream<char, std::__1::char_traits<char> >&, char const*, unsigned int): error: undefined reference to 'std::__1::ios_base::getloc() const'
    /home/mrmaffen/v8-builder/v8/out/android_armeabi.release/obj.target/tools/gyp/libv8_base.a(/home/mrmaffen/v8-builder/v8/out/android_arm.release/obj.target/v8_base/src/assembler.o):assembler.cc:function std::__1::basic_ostream<char, std::__1::char_traits<char> >& std::__1::__put_character_sequence<char, std::__1::char_traits<char> >(std::__1::basic_ostream<char, std::__1::char_traits<char> >&, char const*, unsigned int): error: undefined reference to 'std::__1::locale::use_facet(std::__1::locale::id&) const'
    etc ...

Lots of people on stackoverflow simply seem to suggest that this can best be solved by reverting to NDK r10e. Which doesn't help in my case obviously :/

mrmaffen commented 7 years ago

@irbull Do you intend to add support for armv8a and x86_64 or do you have suggestions on what I should try next? I don't know if all the time I'm spending on getting this to work is really worth it or if I should just wait until you can find the time to dig into it. I'd assume you're much more knowledgeable about all this than me :)

irbull commented 7 years ago

I'm currently trying to get arm and x86 builds working (actually I published a 4.7.0-SNAPSHOT to maven central last night based on the build work that @KnownUnown did). The aar is larger than it was in the past, and I'm trying to get the filesize down. Once I've solved that, I can look at other platforms.

As for your specific references, are these related to I18L and ICU? Did you get these errors when you were building the J2V8 .so files, or when you were building node / v8?

mrmaffen commented 7 years ago

@irbull The "undefined reference" errors occurred when I ran ndk-build (with NDK r14b) for the j2v8 project itself. It builds just fine with NDK r10e but every version after that produces the above mentioned "undefined reference" errors.

irbull commented 7 years ago

This works is finally complete and in master. Thanks again to @KnownUnown and others who helped. You can now build this form Android by

git clone https://github.com/eclipsesource/J2V8
cd j2v8
curl -O http://download.eclipsesource.com/j2v8/v8/node.out-7_4_0.tar.gz
./buildAll.sh

The curl statement downloads the prebuild node.js. You can build that yourself if you'd like. It comes from (github.com/eclipsesource/static-node-build). buildAll.sh will use docker containers to build Android.arm Android.x86 and Linux.x64. The Tests are run in the linux container. The Android arm and x86 so files are copied out, and placed in the proper resource directory.

SKrPl commented 7 years ago

@irbull Where should -D NODE_COMPATIBLE=1 be put so that NodeJS can be added to Android build? And after ./buildAll.sh how do I generate .aar file. I am not that good in C/C++ programming, would appreciate help on this :smile: .