Cross compile issue on zstd

In my project I am using crate zip and I find it uses zstd. Here is a compilation error when targeting android.The message is

➜  untitled git:(master) ✗ cargo build --release --target aarch64-linux-android --config "target.aarch64-linux-android.linker = '/Users/leojangh/Library/Android/sdk/ndk/25.1.8937393/toolchains/llvm/prebuilt/darwin-x86_64/bin/aarch64-linux-android26-clang++'"
   Compiling zstd-sys v2.0.9+zstd.1.5.5
error: failed to run custom build command for `zstd-sys v2.0.9+zstd.1.5.5`

Caused by:
  process didn't exit successfully: `/Users/leojangh/RustroverProjects/untitled/target/release/build/zstd-sys-b9bc29653f37cb0e/build-script-build` (exit status: 1)
  --- stdout
  cargo:rerun-if-env-changed=ZSTD_SYS_USE_PKG_CONFIG
  TARGET = Some("aarch64-linux-android")
  OPT_LEVEL = Some("3")
  HOST = Some("aarch64-apple-darwin")
  cargo:rerun-if-env-changed=CC_aarch64-linux-android
  CC_aarch64-linux-android = None
  cargo:rerun-if-env-changed=CC_aarch64_linux_android
  CC_aarch64_linux_android = None
  cargo:rerun-if-env-changed=TARGET_CC
  TARGET_CC = None
  cargo:rerun-if-env-changed=CC
  CC = None
  cargo:rerun-if-env-changed=CRATE_CC_NO_DEFAULTS
  CRATE_CC_NO_DEFAULTS = None
  DEBUG = Some("false")
  cargo:rerun-if-env-changed=CFLAGS_aarch64-linux-android
  CFLAGS_aarch64-linux-android = None
  cargo:rerun-if-env-changed=CFLAGS_aarch64_linux_android
  CFLAGS_aarch64_linux_android = None
  cargo:rerun-if-env-changed=TARGET_CFLAGS
  TARGET_CFLAGS = None
  cargo:rerun-if-env-changed=CFLAGS
  CFLAGS = None
  cargo:rerun-if-env-changed=CC_aarch64-linux-android
  CC_aarch64-linux-android = None
  cargo:rerun-if-env-changed=CC_aarch64_linux_android
  CC_aarch64_linux_android = None
  cargo:rerun-if-env-changed=TARGET_CC
  TARGET_CC = None
  cargo:rerun-if-env-changed=CC
  CC = None
  cargo:rerun-if-env-changed=CRATE_CC_NO_DEFAULTS
  CRATE_CC_NO_DEFAULTS = None
  cargo:rerun-if-env-changed=CFLAGS_aarch64-linux-android
  CFLAGS_aarch64-linux-android = None
  cargo:rerun-if-env-changed=CFLAGS_aarch64_linux_android
  CFLAGS_aarch64_linux_android = None
  cargo:rerun-if-env-changed=TARGET_CFLAGS
  TARGET_CFLAGS = None
  cargo:rerun-if-env-changed=CFLAGS
  CFLAGS = None
  cargo:rerun-if-env-changed=CC_aarch64-linux-android
  CC_aarch64-linux-android = None
  cargo:rerun-if-env-changed=CC_aarch64_linux_android
  CC_aarch64_linux_android = None
  cargo:rerun-if-env-changed=TARGET_CC
  TARGET_CC = None
  cargo:rerun-if-env-changed=CC
  CC = None
  cargo:rerun-if-env-changed=CRATE_CC_NO_DEFAULTS
  CRATE_CC_NO_DEFAULTS = None
  cargo:rerun-if-env-changed=CFLAGS_aarch64-linux-android
  CFLAGS_aarch64-linux-android = None
  cargo:rerun-if-env-changed=CFLAGS_aarch64_linux_android
  CFLAGS_aarch64_linux_android = None
  cargo:rerun-if-env-changed=TARGET_CFLAGS
  TARGET_CFLAGS = None
  cargo:rerun-if-env-changed=CFLAGS
  CFLAGS = None
  cargo:rerun-if-env-changed=CC_aarch64-linux-android
  CC_aarch64-linux-android = None
  cargo:rerun-if-env-changed=CC_aarch64_linux_android
  CC_aarch64_linux_android = None
  cargo:rerun-if-env-changed=TARGET_CC
  TARGET_CC = None
  cargo:rerun-if-env-changed=CC
  CC = None
  cargo:rerun-if-env-changed=CRATE_CC_NO_DEFAULTS
  CRATE_CC_NO_DEFAULTS = None
  cargo:rerun-if-env-changed=CFLAGS_aarch64-linux-android
  CFLAGS_aarch64-linux-android = None
  cargo:rerun-if-env-changed=CFLAGS_aarch64_linux_android
  CFLAGS_aarch64_linux_android = None
  cargo:rerun-if-env-changed=TARGET_CFLAGS
  TARGET_CFLAGS = None
  cargo:rerun-if-env-changed=CFLAGS
  CFLAGS = None
  running: "aarch64-linux-android-clang" "-O3" "-DANDROID" "-ffunction-sections" "-fdata-sections" "-fPIC" "-I" "zstd/lib/" "-I" "zstd/lib/common" "-I" "zstd/lib/legacy" "-fvisibility=hidden" "-DZSTD_LIB_DEPRECATED=0" "-DXXH_PRIVATE_API=" "-DZSTDLIB_VISIBILITY=" "-DZDICTLIB_VISIBILITY=" "-DZSTDERRORLIB_VISIBILITY=" "-DZSTD_LEGACY_SUPPORT=1" "-o" "/Users/leojangh/RustroverProjects/untitled/target/aarch64-linux-android/release/build/zstd-sys-2bdb415299fc2ff1/out/zstd/lib/common/debug.o" "-c" "zstd/lib/common/debug.c"

  --- stderr


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

I can confirm this issue comes from zstd because it happens after zstd added to Cargo.toml and disappears after removed.

Test on Windows and MacOS host. How to fix that? Thanks!

And I noticed there is an alpha version of zip and it doesn't depend zstd,it says 'no_std', what no_std means here?

no_std means "without the standard library".This is normally used on environments such as embedded devices where you want the absolute minimal binary size. For crates, this means that they will not depend on the standard library, so that you can use them when you have such requirements.

As for your problem with the zip package, I would expect it to only depend on zstd if you explicitly opted-in to use this compression algorithm.

And, effectively you can opt-out from using zstd in the zip crate:

The features available are:

    aes-crypto: Enables decryption of files which were encrypted with AES. Supports AE-1 and AE-2 methods.
    deflate: Enables the deflate compression algorithm, which is the default for zip files.
    bzip2: Enables the BZip2 compression algorithm.
    time: Enables features using the time crate.
    zstd: Enables the Zstandard compression algorithm.

All of these are enabled by default.

You should learn about Features - The Cargo Book.

1 Like

Thanks for you guiding. It fixed by opt-out from using zstd!
But why zstd is incompatible with android platform? who control this?

Don't be fooled by the word "Android" in the compilation target. Probably the linker that you used has something to do with the compilation error. A quick search shows that not many people have any problems when cross-compiling the library to Arm64, but those that did have problems were caused by an older version of the clang compiler: [package] zstd/1.5.2: Fails to cross compile to armv8 · Issue #12129 · conan-io/conan-center-index · GitHub

You could also open an issue for the zstd crate repository if you suspect that the Rust bindings caused the compilation error.

1 Like

Thank you, I understand. Let me ask another question about binary size.

As you mentioned before, no_std can shrink binary size,After I refer to some docs,perhaps it is infeasible in my project. But it doesn't matter.I want to know what methods can be used to reduce the binary size?

I known strip, it should be a universal tool because Android packaging tool wil strip all ELFs so is there other special way for rust?

Here's a list of different things you can do to reduce the binary size: GitHub - johnthagen/min-sized-rust: 🦀 How to minimize Rust binary size 📦

1 Like

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.