Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Library creation fails with Android NDK r23b #636

Open
b-spencer opened this issue Nov 18, 2021 · 8 comments
Open

Library creation fails with Android NDK r23b #636

b-spencer opened this issue Nov 18, 2021 · 8 comments

Comments

@b-spencer
Copy link

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 Build and link issues on Linux hosts with newer Android NDKs #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 Build and link issues on Linux hosts with newer Android NDKs #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.

@TroyNeubauer
Copy link

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

@sshxmz
Copy link

sshxmz commented Feb 14, 2022

Same problem here.

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

@rileyhawk1417
Copy link

rileyhawk1417 commented Feb 18, 2022

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.

@dot-asm
Copy link
Contributor

dot-asm commented Mar 11, 2022

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
Copy link
Contributor

dot-asm commented Mar 14, 2022

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...

@irediaes
Copy link

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.

@b-spencer
Copy link
Author

@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.

@dot-asm
Copy link
Contributor

dot-asm commented Mar 25, 2022

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.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants