-
[Rust] ARMv7 Android 빌드Programming 2021. 1. 22. 17:12
Hello, world! 샘플을 ARMv7 Android용으로 빌드하는 방법에 대하여 설명합니다.
ARMv7 Androd는 Tier2 이기 때문에 빌드는 문제 없지만 사용하는 패키지에 따라서 동작상에 문제가 있을 수 있을 수 있습니다.
Rust의 Platform 지원 현황: doc.rust-lang.org/beta/rustc/platform-support.html
Host용으로 먼저 만들어 봅니다.
$ cargo new hello $ cd hello $ cargo run Compiling hello v0.1.0 (/home/zeper/tmp/rust/hello) Finished dev [unoptimized + debuginfo] target(s) in 0.52s Running `target/debug/hello` Hello, world!
잘 되네요
Target(ARMv7 Android) 설정을 합니다.
먼저 아래 명령어로 지원하는 Target List를 확인해 봅니다.
$ rustup target list aarch64-apple-darwin aarch64-apple-ios aarch64-fuchsia aarch64-linux-android aarch64-pc-windows-msvc aarch64-unknown-linux-gnu aarch64-unknown-linux-musl aarch64-unknown-none aarch64-unknown-none-softfloat arm-linux-androideabi arm-unknown-linux-gnueabi (installed) arm-unknown-linux-gnueabihf arm-unknown-linux-musleabi arm-unknown-linux-musleabihf armebv7r-none-eabi armebv7r-none-eabihf armv5te-unknown-linux-gnueabi armv5te-unknown-linux-musleabi armv7-linux-androideabi (installed) armv7-unknown-linux-gnueabi (installed) armv7-unknown-linux-gnueabihf (installed) armv7-unknown-linux-musleabi armv7-unknown-linux-musleabihf armv7a-none-eabi (installed) armv7r-none-eabi armv7r-none-eabihf asmjs-unknown-emscripten i586-pc-windows-msvc i586-unknown-linux-gnu i586-unknown-linux-musl i686-linux-android i686-pc-windows-gnu i686-pc-windows-msvc i686-unknown-freebsd i686-unknown-linux-gnu i686-unknown-linux-musl mips-unknown-linux-gnu mips-unknown-linux-musl mips64-unknown-linux-gnuabi64 mips64-unknown-linux-muslabi64 mips64el-unknown-linux-gnuabi64 mips64el-unknown-linux-muslabi64 mipsel-unknown-linux-gnu mipsel-unknown-linux-musl nvptx64-nvidia-cuda powerpc-unknown-linux-gnu powerpc64-unknown-linux-gnu powerpc64le-unknown-linux-gnu riscv32i-unknown-none-elf riscv32imac-unknown-none-elf riscv32imc-unknown-none-elf riscv64gc-unknown-linux-gnu riscv64gc-unknown-none-elf riscv64imac-unknown-none-elf s390x-unknown-linux-gnu sparc64-unknown-linux-gnu sparcv9-sun-solaris thumbv6m-none-eabi thumbv7em-none-eabi thumbv7em-none-eabihf thumbv7m-none-eabi thumbv7neon-linux-androideabi thumbv7neon-unknown-linux-gnueabihf thumbv8m.base-none-eabi thumbv8m.main-none-eabi thumbv8m.main-none-eabihf wasm32-unknown-emscripten wasm32-unknown-unknown wasm32-wasi x86_64-apple-darwin x86_64-apple-ios x86_64-fortanix-unknown-sgx x86_64-fuchsia x86_64-linux-android x86_64-pc-windows-gnu x86_64-pc-windows-msvc x86_64-rumprun-netbsd x86_64-sun-solaris x86_64-unknown-freebsd x86_64-unknown-illumos x86_64-unknown-linux-gnu (installed) x86_64-unknown-linux-gnux32 x86_64-unknown-linux-musl x86_64-unknown-netbsd x86_64-unknown-redox
Target을 추가합니다.
$ rustup target add armv7-linux-androideabi info: downloading component 'rust-std' for 'armv7-linux-androideabi' info: installing component 'rust-std' for 'armv7-linux-androideabi' info: using up to 500.0 MiB of RAM to unpack components 18.5 MiB / 18.5 MiB (100 %) 15.7 MiB/s in 1s ETA: 0s
Target으로 빌드해 봅시다
$ cargo build --target=armv7-linux-androideabi Compiling hello v0.1.0 (/home/zeper/tmp/rust/hello) error: linking with `cc` failed: exit code: 1 | = note: "cc" "-Wl,--as-needed" "-Wl,-z,noexecstack" "-Wl,--allow-multiple-definition" "-march=armv7-a" "-Wl,--eh-frame-hdr" "-L" "/home/zeper/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/armv7-linux-androideabi/lib" "/home/zeper/tmp/rust/hello/target/armv7-linux-androideabi/debug/deps/hello-e649096974718c1d.13i238i1nw4f582w.rcgu.o" "/home/zeper/tmp/rust/hello/target/armv7-linux-androideabi/debug/deps/hello-e649096974718c1d.1puxji1j4al5q2th.rcgu.o" "/home/zeper/tmp/rust/hello/target/armv7-linux-androideabi/debug/deps/hello-e649096974718c1d.1venxgktxoit4xfy.rcgu.o" "/home/zeper/tmp/rust/hello/target/armv7-linux-androideabi/debug/deps/hello-e649096974718c1d.29bn060081e4f81e.rcgu.o" "/home/zeper/tmp/rust/hello/target/armv7-linux-androideabi/debug/deps/hello-e649096974718c1d.2w3q3wmuhih636y3.rcgu.o" "/home/zeper/tmp/rust/hello/target/armv7-linux-androideabi/debug/deps/hello-e649096974718c1d.3cr4znzzex5qwdd2.rcgu.o" "/home/zeper/tmp/rust/hello/target/armv7-linux-androideabi/debug/deps/hello-e649096974718c1d.4sz5506n9ooxqo40.rcgu.o" "/home/zeper/tmp/rust/hello/target/armv7-linux-androideabi/debug/deps/hello-e649096974718c1d.569h0m7xwblhj3xh.rcgu.o" "-o" "/home/zeper/tmp/rust/hello/target/armv7-linux-androideabi/debug/deps/hello-e649096974718c1d" "/home/zeper/tmp/rust/hello/target/armv7-linux-androideabi/debug/deps/hello-e649096974718c1d.1wp905j662mlrn4i.rcgu.o" "-Wl,--gc-sections" "-pie" "-Wl,-zrelro" "-Wl,-znow" "-nodefaultlibs" "-L" "/home/zeper/tmp/rust/hello/target/armv7-linux-androideabi/debug/deps" "-L" "/home/zeper/tmp/rust/hello/target/debug/deps" "-L" "/home/zeper/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/armv7-linux-androideabi/lib" "-Wl,--start-group" "-Wl,-Bstatic" "/home/zeper/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/armv7-linux-androideabi/lib/libstd-cb0beac250436a0d.rlib" "/home/zeper/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/armv7-linux-androideabi/lib/libpanic_unwind-e434dcd9317d78c2.rlib" "/home/zeper/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/armv7-linux-androideabi/lib/libminiz_oxide-afa459219c4d92d6.rlib" "/home/zeper/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/armv7-linux-androideabi/lib/libadler-f57f538a958bc55d.rlib" "/home/zeper/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/armv7-linux-androideabi/lib/libobject-c8797db28b71e53c.rlib" "/home/zeper/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/armv7-linux-androideabi/lib/libaddr2line-bb211f245cec9142.rlib" "/home/zeper/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/armv7-linux-androideabi/lib/libgimli-a6ab61380c35ee99.rlib" "/home/zeper/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/armv7-linux-androideabi/lib/librustc_demangle-4e4d81621f8f8e44.rlib" "/home/zeper/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/armv7-linux-androideabi/lib/libhashbrown-f6130bf85123b5dd.rlib" "/home/zeper/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/armv7-linux-androideabi/lib/librustc_std_workspace_alloc-f0b4a50ac6fb811f.rlib" "/home/zeper/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/armv7-linux-androideabi/lib/libunwind-78a31eba47812823.rlib" "/home/zeper/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/armv7-linux-androideabi/lib/libcfg_if-8c49356d4184b030.rlib" "/home/zeper/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/armv7-linux-androideabi/lib/liblibc-bc3ec46ad7e712af.rlib" "/home/zeper/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/armv7-linux-androideabi/lib/liballoc-cb332653b205c56d.rlib" "/home/zeper/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/armv7-linux-androideabi/lib/librustc_std_workspace_core-0ad625bb743eec22.rlib" "/home/zeper/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/armv7-linux-androideabi/lib/libcore-0dbd748a486ccd14.rlib" "-Wl,--end-group" "/home/zeper/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/armv7-linux-androideabi/lib/libcompiler_builtins-9259c2a9436ab113.rlib" "-Wl,-Bdynamic" "-ldl" "-llog" "-lgcc" "-lc" "-lm" = note: /usr/bin/ld: /home/zeper/tmp/rust/hello/target/armv7-linux-androideabi/debug/deps/hello-e649096974718c1d.13i238i1nw4f582w.rcgu.o: Relocations in generic ELF (EM: 40) /usr/bin/ld: /home/zeper/tmp/rust/hello/target/armv7-linux-androideabi/debug/deps/hello-e649096974718c1d.13i238i1nw4f582w.rcgu.o: Relocations in generic ELF (EM: 40) /usr/bin/ld: /home/zeper/tmp/rust/hello/target/armv7-linux-androideabi/debug/deps/hello-e649096974718c1d.13i238i1nw4f582w.rcgu.o: Relocations in generic ELF (EM: 40) /usr/bin/ld: /home/zeper/tmp/rust/hello/target/armv7-linux-androideabi/debug/deps/hello-e649096974718c1d.13i238i1nw4f582w.rcgu.o: Relocations in generic ELF (EM: 40) /usr/bin/ld: /home/zeper/tmp/rust/hello/target/armv7-linux-androideabi/debug/deps/hello-e649096974718c1d.13i238i1nw4f582w.rcgu.o: Relocations in generic ELF (EM: 40) /usr/bin/ld: /home/zeper/tmp/rust/hello/target/armv7-linux-androideabi/debug/deps/hello-e649096974718c1d.13i238i1nw4f582w.rcgu.o: Relocations in generic ELF (EM: 40) /usr/bin/ld: /home/zeper/tmp/rust/hello/target/armv7-linux-androideabi/debug/deps/hello-e649096974718c1d.13i238i1nw4f582w.rcgu.o: Relocations in generic ELF (EM: 40) /home/zeper/tmp/rust/hello/target/armv7-linux-androideabi/debug/deps/hello-e649096974718c1d.13i238i1nw4f582w.rcgu.o: error adding symbols: File in wrong format collect2: error: ld returned 1 exit status error: aborting due to previous error error: could not compile `hello` To learn more, run the command again with --verbose.
host 용 cc로 링크를 하려고 해서 링크 에러가 발생합니다.
Android용 Toolchain 설정 및 sysroot 설정해줍니다.
$ mkdir .cargo $ vi .cargo/config [target.armv7-linux-androideabi] ar = "arm-linux-androideabi-ar" linker = "arm-linux-androideabi-gcc" [build] rustflags = ["-C", "link-args=--sysroot=/home/zeper/target/build_sysroot/"]
sysroot는 CrossCompiler가 링크에 필요한 라이브러리 및 헤더파일들이 있는 위치가 됩니다.
위 config 파일을 생성하고 다시 빌드해봅니다.
$ cargo build --target=armv7-linux-androideabi Compiling hello v0.1.0 (/home/mjkim/tmp/rust/hello) Finished dev [unoptimized + debuginfo] target(s) in 0.61s
에러 없이 빌드가 됩니다.
arm-linux-androideabi-ar 을 찾을수 없다고 에러가 뜨면 PATH 환경변수에 적절히 추가합니다.
실행파일이 잘 생성됐는지 확인해 보면 ARM 용으로 잘 빌드 됐음을 확인 할 수 있습니다.
$ file target/armv7-linux-androideabi/debug/hello target/armv7-linux-androideabi/debug/hello: ELF 32-bit LSB shared object, ARM, EABI5 version 1 (SYSV), dynamically linked, interpreter /system/bin/linker, with debug_info, not stripped
$ readelf -A target/armv7-linux-androideabi/debug/hello Attribute Section: aeabi File Attributes Tag_CPU_name: "ARM v7" Tag_CPU_arch: v7 Tag_CPU_arch_profile: Application Tag_ARM_ISA_use: Yes Tag_THUMB_ISA_use: Thumb-2 Tag_FP_arch: VFPv3 Tag_Advanced_SIMD_arch: NEONv1 Tag_ABI_PCS_GOT_use: GOT-indirect Tag_ABI_PCS_wchar_t: 4 Tag_ABI_FP_denormal: Needed Tag_ABI_FP_exceptions: Needed Tag_ABI_FP_number_model: IEEE 754 Tag_ABI_align_needed: 8-byte Tag_ABI_enum_size: int Tag_ABI_optimization_goals: Aggressive Speed Tag_CPU_unaligned_access: v6 Tag_ABI_FP_16bit_format: IEEE 754 Tag_Virtualization_use: TrustZone
728x90반응형