cc-rs icon indicating copy to clipboard operation
cc-rs copied to clipboard

Library creation fails with Android NDK r23b

Open b-spencer opened this issue 2 years ago • 8 comments

Problem

Using fresh installations of Rust installed on x86_64-unknown-linux-gnu (Debian 10) via the rustup method, compiling packages for Android with NDK r23b (the current LTS release) does not work.

Steps

  1. curl -sf -L https://static.rust-lang.org/rustup.sh | sh (fresh install for the local user)

  2. rustup target add aarch64-linux-android

  3. Create an empty library crate that depends on ring:

cargo new --lib hello
echo 'ring = "0.16"' >>hello/Cargo.toml
  1. Compile for the host and all is well:
$ cargo build
    Updating crates.io index
   Compiling libc v0.2.107
   Compiling cc v1.0.72
   Compiling once_cell v1.8.0
   Compiling spin v0.5.2
   Compiling untrusted v0.7.1
   Compiling ring v0.16.20
   Compiling hello v0.1.0 (/home/.../hello)
    Finished dev [unoptimized + debuginfo] target(s) in 7.48s
  1. Compile for aarch64-linux-android the simple, documented way and it fails, but that's because of all the reasons in #459:
$ cargo build --target aarch64-linux-android
   Compiling untrusted v0.7.1
   Compiling spin v0.5.2
   Compiling once_cell v1.8.0
   Compiling libc v0.2.107
   Compiling ring v0.16.20
error: failed to run custom build command for `ring v0.16.20`

Caused by:
  process didn't exit successfully: `/home/.../hello/target/debug/build/ring-b1c7634ecf998ee6/build-script-build` (exit status: 101)
...
  1. Add all the workarounds from #459 and it still doesn't work:
$ PATH=$ANDROID_NDK_HOME/toolchains/llvm/prebuilt/linux-x86_64/bin/:$PATH \
  CC_aarch64_linux_android=$ANDROID_NDK_HOME/toolchains/llvm/prebuilt/linux-x86_64/bin/aarch64-linux-android24-clang \
  CARGO_TARGET_AARCH64_LINUX_ANDROID_LINKER=$ANDROID_NDK_HOME/toolchains/llvm/prebuilt/linux-x86_64/bin/aarch64-linux-android24-clang \
  cargo build --lib --release --target aarch64-linux-android
   Compiling ring v0.16.20
error: failed to run custom build command for `ring v0.16.20`

Caused by:
  process didn't exit successfully: `/home/.../hello/target/release/build/ring-14a8ac965b8995d8/build-script-build` (exit status: 1)
...
  --- stdout
  running: "aarch64-linux-android-ar" "cq" ...
  ...

  --- stderr

  error occurred: Failed to find tool. Is `aarch64-linux-android-ar` installed?

The newest versions of the Android NDK simply do not ship GNU binutils and thus there is no aarch64-linux-android-ar to use. No amount of changing paths or environment variables is likely to make this work. I suspect that the rust toolchain will need to use llvm-ar and the other LLVM tools instead if it is to work with the current LTS versions of the Android NDK.

b-spencer avatar Nov 18 '21 00:11 b-spencer

Same issue here. Would love to see a fix to allow me to use the current version of the NDK

TroyNeubauer avatar Jan 11 '22 14:01 TroyNeubauer

Same problem here.

outdated profile for cc::build: default compiler = 'aarch64-linux-android21-clang' default archiver = 'aarch64-linux-android-ar'

sshxmz avatar Feb 14 '22 02:02 sshxmz

Same issue here. Would love to see a fix to allow me to use the current version of the NDK

Does version 22/21 have the files. Was hesitant after downloading via Android studio and manually downloading the NDK archive from the website.

[Edit] Found what I was looking for. Hopefully this issue gets attention, will just be using version 22 for now.

rileyhawk1417 avatar Feb 18 '22 09:02 rileyhawk1417

Would you mind testing a variant of #657? "A variant" means that I would have to prepare a variant that would extend the suggested approach to android targets.

dot-asm avatar Mar 11 '22 16:03 dot-asm

In the context I also wonder how do you specify the compiler? The background for the question is following. My understanding is that you're supposed to specify linker through .cargo/config.toml. In which case it would be only natural to use it even as default C compiler and use it to derive C++ compiler name...

dot-asm avatar Mar 14 '22 16:03 dot-asm

To fix copy and rename llvm-ar to aarch64-linux-android-ar in ${ANDROID_NDK_HOME}/toolchains/llvm/prebuilt/{SYSTEM}/bin

Do this for all your target arch.

irediaes avatar Mar 22 '22 14:03 irediaes

@irediaes Thanks for the information!

Armed with this tip, I guessed that I can also set an AR_* variable just like CC_* in my current list of workarounds.

Taking the command from my item 6 above (in the first comment) and adding

AR_aarch64_linux_android=$ANDROID_NDK_HOME/toolchains/llvm/prebuilt/linux-x86_64/bin/llvm-ar

to it seems to make the build succeed.

PATH=$ANDROID_NDK_HOME/toolchains/llvm/prebuilt/linux-x86_64/bin/:$PATH \
  CC_aarch64_linux_android=$ANDROID_NDK_HOME/toolchains/llvm/prebuilt/linux-x86_64/bin/aarch64-linux-android24-clang \
  AR_aarch64_linux_android=$ANDROID_NDK_HOME/toolchains/llvm/prebuilt/linux-x86_64/bin/llvm-ar \
  CARGO_TARGET_AARCH64_LINUX_ANDROID_LINKER=$ANDROID_NDK_HOME/toolchains/llvm/prebuilt/linux-x86_64/bin/aarch64-linux-android24-clang \
  cargo build --lib --release --target aarch64-linux-android

I don't understand why rust's tools' expectations don't align with how the Android NDK ships, but it seems that at least I can configure it to work without modifying the Android NDK itself.

I'll leave this issue open in the hopes that either the rust toolchain will adapt to the NDK such that no workarounds at all are required, or at least that how to do this becomes documented somewhere.

Thanks again for the key information.

b-spencer avatar Mar 22 '22 16:03 b-spencer

Isn't answer to all these questions cargo install cargo-apk?

I don't understand why rust's tools' expectations don't align with how the Android NDK ships

It has nothing to do with Android. If you want to cross-compile for any platform, it is your responsibility to specify appropriate cross-linker. You can do it yourself or use helpers like cargo-apk. (Just in case, no, one can't ~do~ choose linker in a build script, it's too late.)

dot-asm avatar Mar 25 '22 09:03 dot-asm