notedeck icon indicating copy to clipboard operation
notedeck copied to clipboard

nix: prepare targets with rust-overlay

Open hellodword opened this issue 1 year ago • 7 comments

pre-install the system-images add doc of using avd without android-studio

hellodword avatar May 16 '24 03:05 hellodword

does cargo-apk work with this? we were relying on rustup for cross compiling to android

jb55 avatar May 16 '24 22:05 jb55

does cargo-apk work with this? we were relying on rustup for cross compiling to android

Yeah, works for me, tested on x86_64 Linux:

RUST_LOG=debug cargo apk build

RUST_LOG=debug cargo apk build --target aarch64-linux-android

# `--arg android_emulator true` required
RUST_LOG=debug cargo apk build --target x86_64-linux-android

hellodword avatar May 17 '24 02:05 hellodword

[nix-shell:~/Documents/GitRemotes/damus-android]$ rustup target add aarch64-linux-android
info: component 'rust-std' for target 'aarch64-linux-android' is up to date

[nix-shell:~/Documents/GitRemotes/damus-android]$ cargo apk run --release
Using package `notedeck` in `/Users/[redacted]/Documents/GitRemotes/damus-android/Cargo.toml`
Warning: Environment variable ANDROID_SDK_ROOT is deprecated (https://developer.android.com/studio/command-line/variables#envar). It will be used until it is unset and replaced by ANDROID_HOME.
   Compiling cfg-if v1.0.0
   Compiling log v0.4.20
   Compiling once_cell v1.19.0
   Compiling libc v0.2.153
   Compiling scopeguard v1.2.0
   Compiling memchr v2.7.1
   Compiling bytes v1.5.0
   Compiling ring v0.17.7
   Compiling serde v1.0.196
   Compiling generic-array v0.14.7
error[E0463]: can't find crate for `std`
  |
  = note: the `aarch64-linux-android` target may not be installed
  = help: consider downloading the target with `rustup target add aarch64-linux-android`
  = help: consider building the standard library from source with `cargo build -Zbuild-std`

error[E0463]: can't find crate for `core`
  |
  = note: the `aarch64-linux-android` target may not be installed
  = help: consider downloading the target with `rustup target add aarch64-linux-android`
  = help: consider building the standard library from source with `cargo build -Zbuild-std`

For more information about this error, try `rustc --explain E0463`.
error: could not compile `scopeguard` (lib) due to 1 previous error
warning: build failed, waiting for other jobs to finish...
error: could not compile `cfg-if` (lib) due to 1 previous error
error: could not compile `once_cell` (lib) due to 1 previous error
error: could not compile `bytes` (lib) due to 1 previous error
error: could not compile `log` (lib) due to 1 previous error
error: could not compile `memchr` (lib) due to 1 previous error
error: could not compile `libc` (lib) due to 1 previous error
error: could not compile `serde` (lib) due to 1 previous error
Error: Command `AR_aarch64-linux-android=/nix/store/hsckmq4g1y2252icajh4q2n4hbckwn88-android-sdk-env/share/android-sdk/ndk/24.0.8215888/toolchains/llvm/prebuilt/darwin-x86_64/bin/llvm-ar CARGO_ENCODED_RUSTFLAGS=-Clink-arg=--target=aarch64-linux-android23\u{1f}-L\u{1f}/Users/[redacted]/Documents/GitRemotes/damus-android/target/cargo-apk-temp-extra-link-libraries CARGO_TARGET_AARCH64_LINUX_ANDROID_AR=/nix/store/hsckmq4g1y2252icajh4q2n4hbckwn88-android-sdk-env/share/android-sdk/ndk/24.0.8215888/toolchains/llvm/prebuilt/darwin-x86_64/bin/llvm-ar CARGO_TARGET_AARCH64_LINUX_ANDROID_LINKER=/nix/store/hsckmq4g1y2252icajh4q2n4hbckwn88-android-sdk-env/share/android-sdk/ndk/24.0.8215888/toolchains/llvm/prebuilt/darwin-x86_64/bin/clang CC_aarch64-linux-android=/nix/store/hsckmq4g1y2252icajh4q2n4hbckwn88-android-sdk-env/share/android-sdk/ndk/24.0.8215888/toolchains/llvm/prebuilt/darwin-x86_64/bin/clang CFLAGS_aarch64-linux-android=--target=aarch64-linux-android23 CXXFLAGS_aarch64-linux-android=--target=aarch64-linux-android23 CXX_aarch64-linux-android=/nix/store/hsckmq4g1y2252icajh4q2n4hbckwn88-android-sdk-env/share/android-sdk/ndk/24.0.8215888/toolchains/llvm/prebuilt/darwin-x86_64/bin/clang++ cargo build --target aarch64-linux-android --release` had a non-zero exit code.

kernelkind avatar May 17 '24 16:05 kernelkind

@kernelkind I think the point of this is that we wouldn't use rustup at least for fetching the compiler? can you try with just a nix shell?

jb55 avatar May 17 '24 17:05 jb55

I confirmed it on an aarch64 linux cloud server, it's working:

root@nix-test-arm:~# git clone -b patch-2 https://github.com/hellodword/notedeck && cd notedeck
root@nix-test-arm:~/notedeck# nix-shell 

[nix-shell:~/notedeck]# cargo build
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 2m 11s

[nix-shell:~/notedeck]# echo $?
0

[nix-shell:~/notedeck]# file target/debug/notedeck
target/debug/notedeck: ELF 64-bit LSB pie executable, ARM aarch64, version 1 (SYSV), dynamically linked, interpreter /nix/store/j4pprbyxslahv9lc3mdr6y2pa1xyghdp-glibc-2.39-31/lib/ld-linux-aarch64.so.1, for GNU/Linux 3.10.0, with debug_info, not stripped

hellodword avatar May 18 '24 01:05 hellodword

But the cargo apk build fails, there's a related issue https://github.com/tadfisher/android-nixpkgs/issues/50 , and it's an existing issue for master branch:

(/nix/store/n0mlzi96rg8p35zkndgf8cvnr9xxmipi-android-sdk-env/share/android-sdk/ndk/24.0.8215888/toolchains/llvm/prebuilt/linux-x86_64/bin/clang)

Caused by:
  process didn't exit successfully: `/root/notedeck/target/debug/build/ring-c8f5710186c61e64/build-script-build` (exit status: 1)
  --- stdout
  cargo:rerun-if-env-changed=RING_PREGENERATE_ASM
  cargo:rustc-env=RING_CORE_PREFIX=ring_core_0_17_7_
  OPT_LEVEL = Some("0")
  TARGET = Some("aarch64-linux-android")
  HOST = Some("aarch64-unknown-linux-gnu")
  cargo:rerun-if-env-changed=CC_aarch64-linux-android
  CC_aarch64-linux-android = Some("/nix/store/n0mlzi96rg8p35zkndgf8cvnr9xxmipi-android-sdk-env/share/android-sdk/ndk/24.0.8215888/toolchains/llvm/prebuilt/linux-x86_64/bin/clang")
  cargo:rerun-if-env-changed=CRATE_CC_NO_DEFAULTS
  CRATE_CC_NO_DEFAULTS = None
  DEBUG = Some("true")
  cargo:rerun-if-env-changed=CFLAGS_aarch64-linux-android
  CFLAGS_aarch64-linux-android = Some("--target=aarch64-linux-android23")
  running: "/nix/store/n0mlzi96rg8p35zkndgf8cvnr9xxmipi-android-sdk-env/share/android-sdk/ndk/24.0.8215888/toolchains/llvm/prebuilt/linux-x86_64/bin/clang" "-O0" "-DANDROID" "-ffunction-sections" "-fdata-sections" "-fPIC" "-gdwarf-2" "-fno-omit-frame-pointer" "--target=aarch64-linux-android" "--target=aarch64-linux-android23" "-I" "include" "-I" "/root/notedeck/target/aarch64-linux-android/debug/build/ring-925c723764f6ea9b/out" "-fvisibility=hidden" "-std=c1x" "-pedantic" "-Wall" "-Wextra" "-Wbad-function-cast" "-Wcast-align" "-Wcast-qual" "-Wconversion" "-Wenum-compare" "-Wfloat-equal" "-Wformat=2" "-Winline" "-Winvalid-pch" "-Wmissing-field-initializers" "-Wmissing-include-dirs" "-Wnested-externs" "-Wredundant-decls" "-Wshadow" "-Wsign-compare" "-Wsign-conversion" "-Wstrict-prototypes" "-Wundef" "-Wuninitialized" "-Wwrite-strings" "-g3" "-DNDEBUG" "-o" "/root/notedeck/target/aarch64-linux-android/debug/build/ring-925c723764f6ea9b/out/crypto/curve25519/curve25519.o" "-c" "crypto/curve25519/curve25519.c"

  --- stderr


  error occurred: Command "/nix/store/n0mlzi96rg8p35zkndgf8cvnr9xxmipi-android-sdk-env/share/android-sdk/ndk/24.0.8215888/toolchains/llvm/prebuilt/linux-x86_64/bin/clang" "-O0" "-DANDROID" "-ffunction-sections" "-fdata-sections" "-fPIC" "-gdwarf-2" "-fno-omit-frame-pointer" "--target=aarch64-linux-android" "--target=aarch64-linux-android23" "-I" "include" "-I" "/root/notedeck/target/aarch64-linux-android/debug/build/ring-925c723764f6ea9b/out" "-fvisibility=hidden" "-std=c1x" "-pedantic" "-Wall" "-Wextra" "-Wbad-function-cast" "-Wcast-align" "-Wcast-qual" "-Wconversion" "-Wenum-compare" "-Wfloat-equal" "-Wformat=2" "-Winline" "-Winvalid-pch" "-Wmissing-field-initializers" "-Wmissing-include-dirs" "-Wnested-externs" "-Wredundant-decls" "-Wshadow" "-Wsign-compare" "-Wsign-conversion" "-Wstrict-prototypes" "-Wundef" "-Wuninitialized" "-Wwrite-strings" "-g3" "-DNDEBUG" "-o" "/root/notedeck/target/aarch64-linux-android/debug/build/ring-925c723764f6ea9b/out/crypto/curve25519/curve25519.o" "-c" "crypto/curve25519/curve25519.c" with args "clang" failed to start: Os { code: 8, kind: Uncategorized, message: "Exec format error" }


Error: Command `AR_aarch64-linux-android=/nix/store/n0mlzi96rg8p35zkndgf8cvnr9xxmipi-android-sdk-env/share/android-sdk/ndk/24.0.8215888/toolchains/llvm/prebuilt/linux-x86_64/bin/llvm-ar CARGO_ENCODED_RUSTFLAGS=-Clink-arg=--target=aarch64-linux-android23\u{1f}-L\u{1f}/root/notedeck/target/cargo-apk-temp-extra-link-libraries CARGO_TARGET_AARCH64_LINUX_ANDROID_AR=/nix/store/n0mlzi96rg8p35zkndgf8cvnr9xxmipi-android-sdk-env/share/android-sdk/ndk/24.0.8215888/toolchains/llvm/prebuilt/linux-x86_64/bin/llvm-ar CARGO_TARGET_AARCH64_LINUX_ANDROID_LINKER=/nix/store/n0mlzi96rg8p35zkndgf8cvnr9xxmipi-android-sdk-env/share/android-sdk/ndk/24.0.8215888/toolchains/llvm/prebuilt/linux-x86_64/bin/clang CC_aarch64-linux-android=/nix/store/n0mlzi96rg8p35zkndgf8cvnr9xxmipi-android-sdk-env/share/android-sdk/ndk/24.0.8215888/toolchains/llvm/prebuilt/linux-x86_64/bin/clang CFLAGS_aarch64-linux-android=--target=aarch64-linux-android23 CXXFLAGS_aarch64-linux-android=--target=aarch64-linux-android23 CXX_aarch64-linux-android=/nix/store/n0mlzi96rg8p35zkndgf8cvnr9xxmipi-android-sdk-env/share/android-sdk/ndk/24.0.8215888/toolchains/llvm/prebuilt/linux-x86_64/bin/clang++ cargo check --target aarch64-linux-android` had a non-zero exit code.

And for the master branch:

root@nix-test-arm:~# git clone https://github.com/damus-io/notedeck notedeck2 && cd notedeck2
root@nix-test-arm:~/notedeck2# nix-shell 

[nix-shell:~/notedeck2]# rustup target add aarch64-linux-android
[nix-shell:~/notedeck2]# cargo apk check

Caused by:
  process didn't exit successfully: `/root/notedeck2/target/debug/build/ring-c8f5710186c61e64/build-script-build` (exit status: 1)
  --- stdout
  cargo:rerun-if-env-changed=RING_PREGENERATE_ASM
  cargo:rustc-env=RING_CORE_PREFIX=ring_core_0_17_7_
  OPT_LEVEL = Some("0")
  TARGET = Some("aarch64-linux-android")
  HOST = Some("aarch64-unknown-linux-gnu")
  cargo:rerun-if-env-changed=CC_aarch64-linux-android
  CC_aarch64-linux-android = Some("/nix/store/n0mlzi96rg8p35zkndgf8cvnr9xxmipi-android-sdk-env/share/android-sdk/ndk/24.0.8215888/toolchains/llvm/prebuilt/linux-x86_64/bin/clang")
  cargo:rerun-if-env-changed=CRATE_CC_NO_DEFAULTS
  CRATE_CC_NO_DEFAULTS = None
  DEBUG = Some("true")
  cargo:rerun-if-env-changed=CFLAGS_aarch64-linux-android
  CFLAGS_aarch64-linux-android = Some("--target=aarch64-linux-android23")
  running: "/nix/store/n0mlzi96rg8p35zkndgf8cvnr9xxmipi-android-sdk-env/share/android-sdk/ndk/24.0.8215888/toolchains/llvm/prebuilt/linux-x86_64/bin/clang" "-O0" "-DANDROID" "-ffunction-sections" "-fdata-sections" "-fPIC" "-gdwarf-2" "-fno-omit-frame-pointer" "--target=aarch64-linux-android" "--target=aarch64-linux-android23" "-I" "include" "-I" "/root/notedeck2/target/aarch64-linux-android/debug/build/ring-925c723764f6ea9b/out" "-fvisibility=hidden" "-std=c1x" "-pedantic" "-Wall" "-Wextra" "-Wbad-function-cast" "-Wcast-align" "-Wcast-qual" "-Wconversion" "-Wenum-compare" "-Wfloat-equal" "-Wformat=2" "-Winline" "-Winvalid-pch" "-Wmissing-field-initializers" "-Wmissing-include-dirs" "-Wnested-externs" "-Wredundant-decls" "-Wshadow" "-Wsign-compare" "-Wsign-conversion" "-Wstrict-prototypes" "-Wundef" "-Wuninitialized" "-Wwrite-strings" "-g3" "-DNDEBUG" "-o" "/root/notedeck2/target/aarch64-linux-android/debug/build/ring-925c723764f6ea9b/out/crypto/curve25519/curve25519.o" "-c" "crypto/curve25519/curve25519.c"

  --- stderr


  error occurred: Command "/nix/store/n0mlzi96rg8p35zkndgf8cvnr9xxmipi-android-sdk-env/share/android-sdk/ndk/24.0.8215888/toolchains/llvm/prebuilt/linux-x86_64/bin/clang" "-O0" "-DANDROID" "-ffunction-sections" "-fdata-sections" "-fPIC" "-gdwarf-2" "-fno-omit-frame-pointer" "--target=aarch64-linux-android" "--target=aarch64-linux-android23" "-I" "include" "-I" "/root/notedeck2/target/aarch64-linux-android/debug/build/ring-925c723764f6ea9b/out" "-fvisibility=hidden" "-std=c1x" "-pedantic" "-Wall" "-Wextra" "-Wbad-function-cast" "-Wcast-align" "-Wcast-qual" "-Wconversion" "-Wenum-compare" "-Wfloat-equal" "-Wformat=2" "-Winline" "-Winvalid-pch" "-Wmissing-field-initializers" "-Wmissing-include-dirs" "-Wnested-externs" "-Wredundant-decls" "-Wshadow" "-Wsign-compare" "-Wsign-conversion" "-Wstrict-prototypes" "-Wundef" "-Wuninitialized" "-Wwrite-strings" "-g3" "-DNDEBUG" "-o" "/root/notedeck2/target/aarch64-linux-android/debug/build/ring-925c723764f6ea9b/out/crypto/curve25519/curve25519.o" "-c" "crypto/curve25519/curve25519.c" with args "clang" failed to start: Os { code: 8, kind: Uncategorized, message: "Exec format error" }


Error: Command `AR_aarch64-linux-android=/nix/store/n0mlzi96rg8p35zkndgf8cvnr9xxmipi-android-sdk-env/share/android-sdk/ndk/24.0.8215888/toolchains/llvm/prebuilt/linux-x86_64/bin/llvm-ar CARGO_ENCODED_RUSTFLAGS=-Clink-arg=--target=aarch64-linux-android23\u{1f}-L\u{1f}/root/notedeck2/target/cargo-apk-temp-extra-link-libraries CARGO_TARGET_AARCH64_LINUX_ANDROID_AR=/nix/store/n0mlzi96rg8p35zkndgf8cvnr9xxmipi-android-sdk-env/share/android-sdk/ndk/24.0.8215888/toolchains/llvm/prebuilt/linux-x86_64/bin/llvm-ar CARGO_TARGET_AARCH64_LINUX_ANDROID_LINKER=/nix/store/n0mlzi96rg8p35zkndgf8cvnr9xxmipi-android-sdk-env/share/android-sdk/ndk/24.0.8215888/toolchains/llvm/prebuilt/linux-x86_64/bin/clang CC_aarch64-linux-android=/nix/store/n0mlzi96rg8p35zkndgf8cvnr9xxmipi-android-sdk-env/share/android-sdk/ndk/24.0.8215888/toolchains/llvm/prebuilt/linux-x86_64/bin/clang CFLAGS_aarch64-linux-android=--target=aarch64-linux-android23 CXXFLAGS_aarch64-linux-android=--target=aarch64-linux-android23 CXX_aarch64-linux-android=/nix/store/n0mlzi96rg8p35zkndgf8cvnr9xxmipi-android-sdk-env/share/android-sdk/ndk/24.0.8215888/toolchains/llvm/prebuilt/linux-x86_64/bin/clang++ cargo check --target aarch64-linux-android` had a non-zero exit code.

hellodword avatar May 18 '24 01:05 hellodword

I'll draft this and try to achieve:

  • compatible with both Nix flakes and legacy Nix command
  • minimize dependencies for all platforms
  • make cargo-apk happy on aarch64 linux

hellodword avatar May 22 '24 01:05 hellodword