zw3rk / hs-android-helloworld

Hello World Android application with Haskell lib
3 stars 0 forks source link

[Q] Android ghc prebuild cross compiler build fail. #1

Open Guang1234567 opened 5 years ago

Guang1234567 commented 5 years ago

Hello dev:

I download a prebuild cross compiler that base on ghc 8.4.0 for android on OSX. (dowload from http://hackage.mobilehaskell.org/).

Then build the libhs.a by typing in termianl:

armv7-linux-androideabi-ghc \
  -staticlib \
  -L/Users/XXXX/dev_kit/haskell_sdk/ghc-build-scripts/dist/libiconv-prefix/arm-linux-androideabi/lib -liconv -lcharset \
  -o /Users/XXXX/dev_kit/src_code/haskell_code/hs-android-helloworld/app/hs-libs/armeabi-v7a/libhs.a \
  ./src/main/hs/Lib.hs

Finally, run this demo by gradle assembleDebug, i get a ndk compile error :

Execution failed for task ':app:externalNativeBuildDebug'.
> Build command failed.
  Error while executing process /Users/XXXX/dev_kit/android_sdk/cmake/3.6.4111459/bin/cmake with arguments {--build /Users/XXXX/dev_kit/src_code/haskell_code/hs-android-helloworld/app/.externalNativeBuild/cmake/debug/armeabi-v7a --target native-lib}
  [1/1] Linking CXX shared library ../../../../build/intermediates/cmake/debug/obj/armeabi-v7a/libnative-lib.so
  FAILED: : && /Users/XXXX/dev_kit/android_sdk/ndk-bundle/toolchains/llvm/prebuilt/darwin-x86_64/bin/clang++  -target armv7-none-linux-androideabi -gcc-toolchain /Users/XXXX/dev_kit/android_sdk/ndk-bundle/toolchains/arm-linux-androideabi-4.9/prebuilt/darwin-x86_64 --sysroot=/Users/XXXX/dev_kit/android_sdk/ndk-bundle/platforms/android-21/arch-arm -fPIC -g -DANDROID -ffunction-sections -funwind-tables -fstack-protector-strong -no-canonical-prefixes -march=armv7-a -mfloat-abi=softfp -mfpu=vfpv3-d16 -fno-integrated-as -mthumb -Wa,--noexecstack -Wformat -Werror=format-security -fno-exceptions -fno-rtti -g -DANDROID -ffunction-sections -funwind-tables -fstack-protector-strong -no-canonical-prefixes -march=armv7-a -mfloat-abi=softfp -mfpu=vfpv3-d16 -fno-integrated-as -mthumb -Wa,--noexecstack -Wformat -Werror=format-security -fno-exceptions -fno-rtti  -O0 -fno-limit-debug-info -O0 -fno-limit-debug-info  -Wl,--build-id -Wl,--warn-shared-textrel -Wl,--fatal-warnings -Wl,--fix-cortex-a8 -Wl,--no-undefined -Wl,-z,noexecstack -Qunused-arguments -Wl,-z,relro -Wl,-z,now -Wl,--build-id -Wl,--warn-shared-textrel -Wl,--fatal-warnings -Wl,--fix-cortex-a8 -Wl,--no-undefined -Wl,-z,noexecstack -Qunused-arguments -Wl,-z,relro -Wl,-z,now -shared -Wl,-soname,libnative-lib.so -o ../../../../build/intermediates/cmake/debug/obj/armeabi-v7a/libnative-lib.so CMakeFiles/native-lib.dir/src/main/cpp/native-lib.cpp.o  -llog -lc ../../../../hs-libs/armeabi-v7a/libhs.a -lm "/Users/XXXX/dev_kit/android_sdk/ndk-bundle/sources/cxx-stl/gnu-libstdc++/4.9/libs/armeabi-v7a/libgnustl_static.a" && :
  /Users/XXXX/dev_kit/android_sdk/ndk-bundle/toolchains/arm-linux-androideabi-4.9/prebuilt/darwin-x86_64/lib/gcc/arm-linux-androideabi/4.9.x/../../../../arm-linux-androideabi/bin/ld: error: ../../../../hs-libs/armeabi-v7a/libhs.a(Lib.o): requires unsupported dynamic reloc R_ARM_MOVW_ABS_NC; recompile with -fPIC
  /Users/XXXX/dev_kit/android_sdk/ndk-bundle/toolchains/arm-linux-androideabi-4.9/prebuilt/darwin-x86_64/lib/gcc/arm-linux-androideabi/4.9.x/../../../../arm-linux-androideabi/bin/ld: warning: shared library text segment is not shareable
  ../../../../hs-libs/armeabi-v7a/libhs.a(iconv.o):libraries/base/cbits/iconv.c:function hs_iconv_open: error: undefined reference to 'libiconv_open'
  ../../../../hs-libs/armeabi-v7a/libhs.a(iconv.o):libraries/base/cbits/iconv.c:function hs_iconv: error: undefined reference to 'libiconv'
  ../../../../hs-libs/armeabi-v7a/libhs.a(iconv.o):libraries/base/cbits/iconv.c:function hs_iconv_close: error: undefined reference to 'libiconv_close'
  ../../../../hs-libs/armeabi-v7a/libhs.a(PrelIOUtils.o):libraries/base/cbits/PrelIOUtils.c:function localeEncoding: error: undefined reference to 'locale_charset'
  clang++: error: linker command failed with exit code 1 (use -v to see invocation)
  ninja: build stopped: subcommand failed.

Could you give me some suggestion? Thanks.

angerman commented 5 years ago

you need to instruct gradle to also link in libiconv together with libhs. And you may need to pass -fPIC to armv7-linux-androideabi-ghc as well.

Guang1234567 commented 5 years ago

@angerman

I build success by instruct gradle to link libiconv, thanks your help. The completed demo is here, hope help other one

But I want to build a share lib by typing :

aarch64-linux-android-ghc -fPIC \
  -shared -liconv -lcharset \
  -L/Users/XXX/dev_kit/src_code/hs-android-helloworld/app/pre-build-libs/libiconv_prefix/arm64-v8a/lib \
  ./src/main/hs/Lib.hs \
  -o /Users/XXXX/dev_kit/src_code/hs-android-helloworld/app/hs-libs/arm64-v8a/libhs.so

but get compile error :

Linking /Users/lihanguang/dev_kit/src_code/hs-android-helloworld/app/hs-libs/arm64-v8a/libhs.so ...
WRAPPED: clang --target=aarch64-linux-android --sysroot /Users/lihanguang/dev_kit/Android/sdk/ndk-bundle/platforms/android-24/arch-arm64 -isystem /Users/lihanguang/dev_kit/Android/sdk/ndk-bundle/sysroot/usr/include/aarch64-linux-android -D__ANDROID_API__=24 @/var/folders/x4/9b7hp58x4t54jdslq2mk7_wc0000gn/T/ghc54058_0/ghc_1.rsp
WRAPPED: ld --sysroot /Users/XXX/dev_kit/Android/sdk/ndk-bundle/platforms/android-24/arch-arm64 --sysroot=/Users/XXX/dev_kit/Android/sdk/ndk-bundle/platforms/android-24/arch-arm64 --fix-cortex-a53-843419 --eh-frame-hdr -m aarch64linux -shared -o /Users/XXX/dev_kit/src_code/hs-android-helloworld/app/hs-libs/arm64-v8a/libhs.so /Users/XXX/dev_kit/Android/sdk/ndk-bundle/platforms/android-24/arch-arm64/usr/lib/crtbegin_so.o -L/Users/XXX/dev_kit/src_code/hs-android-helloworld/app/pre-build-libs/libiconv_prefix/arm64-v8a/lib -L/Users/XXX/dev_kit/haskell/ghc-8.4.0.20180109-aarch64-linux-android/lib/../lib/aarch64-linux-android-ghc-8.4.0.20180109/base-4.11.0.0 -L/Users/angerman/Projects/zw3rk/prebuilt/libiconv/aarch64-linux-android/lib -L/Users/XXX/dev_kit/haskell/ghc-8.4.0.20180109-aarch64-linux-android/lib/../lib/aarch64-linux-android-ghc-8.4.0.20180109/integer-simple-0.1.1.1 -L/Users/XXX/dev_kit/haskell/ghc-8.4.0.20180109-aarch64-linux-android/lib/../lib/aarch64-linux-android-ghc-8.4.0.20180109/ghc-prim-0.5.2.0 -L/Users/XXX/dev_kit/haskell/ghc-8.4.0.20180109-aarch64-linux-android/lib/../lib/aarch64-linux-android-ghc-8.4.0.20180109/rts-1.0 -L/Users/XXX/dev_kit/Android/sdk/ndk-bundle/toolchains/llvm/prebuilt/darwin-x86_64/lib64/clang/6.0.2/lib/linux/aarch64 -L/Users/XXX/dev_kit/Android/sdk/ndk-bundle/platforms/android-24/arch-arm64/usr/lib -z noexecstack --hash-size=31 --reduce-memory-overheads --no-as-needed -lm src/main/hs/Lib.o -Bsymbolic -h libhs.so -lHSbase-4.11.0.0 -lHSinteger-simple-0.1.1.1 -lHSghc-prim-0.5.2.0 -liconv -lgcc -ldl -lc -lgcc -ldl /Users/XXX/dev_kit/Android/sdk/ndk-bundle/platforms/android-24/arch-arm64/usr/lib/crtend_so.o
ld: cannot find -lgcc
ld: cannot find -lgcc
clang: error: linker command failed with exit code 1 (use -v to see invocation)
`aarch64-linux-android-clang' failed in phase `Linker'. (Exit code: 1)

I donot know why has redundant -lgcc -ldl -lc -lgcc -ldl option argument?

Is sth i missing or the cross-compiler not support build share lib?

angerman commented 5 years ago

The redundant flags should be no issue to the linker.

ld: cannot find -lgcc

means it's looking for libgcc.a or libgcc.so or something similar. You'll need to add a Library Search Path for that lib as well -L/path/to/folder/containing/libgcc/.

Guang1234567 commented 5 years ago

@angerman

Thanks your reply.

The share lib version demo is here, hope help other people.