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

Android rust build fix #57

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions rust/android/build.sh
Original file line number Diff line number Diff line change
@@ -1,17 +1,22 @@
#!/bin/bash

# build docker images
docker build --tag stunnar/rustndk:latest rustNDKDocker
docker build --tag zecwalletmobile/android:latest docker

# build rust library for android, used nightly build to rebuild core-librarys not there at compile time
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typo "libraries"

docker run --rm -v $(pwd)/..:/opt/zecwalletmobile -v $(pwd)/target/registry:/root/.cargo/registry zecwalletmobile/android:latest bash -c "
cd /opt/zecwalletmobile/android && \
CC=i686-linux-android24-clang OPENSSL_DIR=/opt/openssl-1.1.1e/x86 cargo build --target i686-linux-android --release && i686-linux-android-strip ../target/i686-linux-android/release/librust.so && \
CC=armv7a-linux-androideabi24-clang OPENSSL_DIR=/opt/openssl-1.1.1e/armv7 cargo build --target armv7-linux-androideabi --release && arm-linux-androideabi-strip ../target/armv7-linux-androideabi/release/librust.so && \
CC=aarch64-linux-android24-clang OPENSSL_DIR=/opt/openssl-1.1.1e/aarch64 cargo build --target aarch64-linux-android --release && aarch64-linux-android-strip ../target/aarch64-linux-android/release/librust.so"
AR=llvm-ar LD=ld RANLIB=llvm-ranlib CC=i686-linux-android29-clang OPENSSL_DIR=/opt/openssl-3.0.1/x86 cargo +nightly build -Z build-std --target i686-linux-android --release && llvm-strip ../target/i686-linux-android/release/librust.so && \
AR=llvm-ar LD=ld RANLIB=llvm-ranlib CC=armv7a-linux-androideabi29-clang OPENSSL_DIR=/opt/openssl-3.0.1/armv7 cargo +nightly build -Z build-std --target armv7-linux-androideabi --release && llvm-strip ../target/armv7-linux-androideabi/release/librust.so && \
AR=llvm-ar LD=ld RANLIB=llvm-ranlib CC=aarch64-linux-android29-clang OPENSSL_DIR=/opt/openssl-3.0.1/aarch64 cargo +nightly build -Z build-std --target aarch64-linux-android --release && llvm-strip ../target/aarch64-linux-android/release/librust.so"

# build library folder for android
mkdir -p ../../android/app/src/main/jniLibs/arm64-v8a
mkdir -p ../../android/app/src/main/jniLibs/armeabi-v7a
mkdir -p ../../android/app/src/main/jniLibs/x86

# copy over .so files
cp ../target/i686-linux-android/release/librust.so ../../android/app/src/main/jniLibs/x86/
cp ../target/armv7-linux-androideabi/release/librust.so ../../android/app/src/main/jniLibs/armeabi-v7a/
cp ../target/aarch64-linux-android/release/librust.so ../../android/app/src/main/jniLibs/arm64-v8a/
Expand Down
81 changes: 46 additions & 35 deletions rust/android/docker/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,52 +1,63 @@
FROM lakoo/android-ndk:latest

RUN curl https://sh.rustup.rs -sSf | \
sh -s -- --default-toolchain none -y
ENV PATH=/root/.cargo/bin:/opt/android-ndk-linux/toolchains/llvm/prebuilt/linux-x86_64/bin:$PATH

RUN rustup toolchain install 1.50.0

RUN rustup target add i686-linux-android
RUN rustup target add armv7-linux-androideabi
RUN rustup target add aarch64-linux-android

RUN echo "[target.aarch64-linux-android]" >> ~/.cargo/config
RUN echo "ar = \"aarch64-linux-android-ar\"" >> ~/.cargo/config
RUN echo "linker = \"aarch64-linux-android24-clang\"" >> ~/.cargo/config
RUN echo "" >> ~/.cargo/config

RUN echo "[target.armv7-linux-androideabi]" >> ~/.cargo/config
RUN echo "ar = \"arm-linux-androideabi-ar\"" >> ~/.cargo/config
RUN echo "linker = \"armv7a-linux-androideabi24-clang\"" >> ~/.cargo/config
RUN echo "" >> ~/.cargo/config

RUN echo "[target.i686-linux-android]" >> ~/.cargo/config
RUN echo "ar = \"i686-linux-android-ar\"" >> ~/.cargo/config
RUN echo "linker = \"i686-linux-android24-clang\"" >> ~/.cargo/config
RUN echo "" >> ~/.cargo/config
FROM stunnar/rustndk:latest

# install needed libaries
RUN apt-get update
RUN apt-get install -y gcc make automake build-essential
RUN apt-get install -y curl wget gcc make automake build-essential

RUN cd /opt && wget https://www.openssl.org/source/old/1.1.1/openssl-1.1.1e.tar.gz && \
tar xvf openssl-1.1.1e.tar.gz && cd openssl-1.1.1e && \
# make open ssl 3.0.1 for targets
RUN cd /opt && wget https://www.openssl.org/source/openssl-3.0.1.tar.gz && \
tar xvf openssl-3.0.1.tar.gz && cd openssl-3.0.1 && \
mkdir x86 && mkdir aarch64 && mkdir armv7

RUN cd /opt/openssl-1.1.1e && \
./Configure --prefix=/opt/openssl-1.1.1e/aarch64 android-arm64 -D__ANDROID_API__=24 && \
RUN cd /opt/openssl-3.0.1 && \
./Configure --prefix=/opt/openssl-3.0.1/aarch64 android-arm64 -D__ANDROID_API__=29 && \
make -j$(nproc) && make -j$(nproc) install && \
make clean && make distclean

RUN cd /opt/openssl-1.1.1e && \
./Configure --prefix=/opt/openssl-1.1.1e/armv7 android-arm -D__ANDROID_API__=24 && \
RUN cd /opt/openssl-3.0.1 && \
./Configure --prefix=/opt/openssl-3.0.1/armv7 android-arm -D__ANDROID_API__=29 && \
make -j$(nproc) && make -j$(nproc) install && \
make clean && make distclean

RUN cd /opt/openssl-1.1.1e && \
./Configure --prefix=/opt/openssl-1.1.1e/x86 android-x86 -D__ANDROID_API__=24 && \
RUN cd /opt/openssl-3.0.1 && \
./Configure --prefix=/opt/openssl-3.0.1/x86 android-x86 -D__ANDROID_API__=29 && \
make -j$(nproc) && make -j$(nproc) install && \
make clean && make distclean


# build zecwalletmobile
RUN rustup component add rustfmt

RUN apt-get install -y git
RUN cd /opt && git clone --depth=1 https://github.com/zecwalletco/zecwallet-mobile zecwalletmobile && cd zecwalletmobile/rust/android && cargo fetch && cargo build --release && rm -rf /opt/zecwalletmobile

# install various other needed libs
RUN apt-get install --assume-yes --no-install-recommends \
g++-aarch64-linux-gnu \
libc6-dev-arm64-cross

# configure target build settings
RUN rustup target install aarch64-linux-android
RUN echo "[target.aarch64-linux-android]" >> /usr/local/cargo/config
RUN echo "ar = \"llvm-ar\"" >> /usr/local/cargo/config
RUN echo "linker = \"aarch64-linux-android29-clang\"" >> /usr/local/cargo/config
RUN echo "" >> /usr/local/cargo/config

RUN rustup target install armv7-linux-androideabi
RUN echo "[target.armv7-linux-androideabi]" >> /usr/local/cargo/config
RUN echo "ar = \"llvm-ar\"" >> /usr/local/cargo/config
RUN echo "linker = \"armv7a-linux-androideabi29-clang\"" >> /usr/local/cargo/config
RUN echo "" >> /usr/local/cargo/config

RUN rustup target install i686-linux-android
RUN echo "[target.i686-linux-android]" >> /usr/local/cargo/config
RUN echo "ar = \"llvm-ar\"" >> /usr/local/cargo/config
RUN echo "linker = \"i686-linux-android29-clang\"" >> /usr/local/cargo/config
RUN echo "" >> /usr/local/cargo/config

ENV OPENSSL_STATIC=yes

# including nightly build to be able to compile -lgcc library
RUN rustup install nightly-x86_64-unknown-linux-gnu
RUN rustup component add rust-src --toolchain nightly-x86_64-unknown-linux-gnu
RUN rustup component add rustfmt --toolchain nightly-x86_64-unknown-linux-gnu
56 changes: 56 additions & 0 deletions rust/android/rustNDKDocker/dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
ARG rust_ver=1.56
FROM rust:${rust_ver}-slim-bullseye
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't remember what directed my decision, I believe it was over dependences and other containers I saw and got ideas from


ARG android_ndk_ver=r23b
ARG cargo_ndk_ver=2.5.0
ARG build_rev=0

# get latest stable toolchain
RUN rustup toolchain install stable

# Install Rust targets for Android platforms
RUN rustup target add aarch64-linux-android \
armv7-linux-androideabi \
i686-linux-android \
x86_64-linux-android


# Install Android NDK
RUN apt-get update \
&& apt-get upgrade -y \
&& apt-get install -y --no-install-recommends --no-install-suggests \
ca-certificates \
make \
&& update-ca-certificates \
\
# Install installation tools
&& toolDeps="curl unzip" \
&& apt-get install -y --no-install-recommends --no-install-suggests \
$toolDeps \
\
# Install NDK itself
&& curl -fL -o /tmp/android-ndk.zip \
https://dl.google.com/android/repository/android-ndk-${android_ndk_ver}-linux.zip \
&& unzip /tmp/android-ndk.zip -d /usr/local/ \
\
# Cleanup unnecessary stuff
&& apt-get purge -y --auto-remove \
-o APT::AutoRemove::RecommendsImportant=false \
$toolDeps \
&& rm -rf /var/lib/apt/lists/* \
/tmp/*

# Path to the Android NDK for cargo-ndk Cargo plugin
ENV ANDROID_NDK_HOME=/usr/local/android-ndk-${android_ndk_ver} \
NDK_HOME=/usr/local/android-ndk-${android_ndk_ver} \
ANDROID_NDK_ROOT=/usr/local/android-ndk-${android_ndk_ver}

ENV PATH=/root/.cargo/bin:/usr/local/android-ndk-${android_ndk_ver}/toolchains/llvm/prebuilt/linux-x86_64/bin:$PATH


# Install cargo-ndk Cargo plugin
RUN cargo install cargo-ndk \
\
# Cleanup unnecessary stuff
&& rm -rf /usr/local/cargo/registry/ \
/tmp/*