forked from terl/lazysodium-android
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build_libsodium.sh
executable file
·78 lines (71 loc) · 2.14 KB
/
build_libsodium.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#!/usr/bin/env bash
# Taken from https://github.com/MuntashirAkon/AppManager/blob/master/toybox-all.sh
NDK=$1
API=$2
cd libsodium
# Check if NDK is set
if [[ "${NDK}" == "" ]]; then
echo "Environment variable NDK isn't set."
exit 1
fi
# Check if API is set
if [[ "${API}" == "" ]]; then
API=21
fi
# Check current architecture
hw_class=`uname -m`
if [[ "${hw_class}" != "x86_64" ]]; then
echo "Unsupported architecture."
exit 1
fi
# Check current OS, only macOS and Linux are supported
os=`uname -s`
ncpu=
if [[ "${os}" == "Darwin" ]]; then
BUILD_TAG=darwin-x86_64
ncpu=`sysctl -n hw.ncpu`
elif [[ "${os}" == "Linux" ]]; then
BUILD_TAG=linux-x86_64
ncpu=`nproc`
else
echo "Unsupported OS."
exit 1
fi
# Set targets, see https://developer.android.com/ndk/guides/other_build_systems
declare -a TARGETS
TARGETS=(armv7a-linux-androideabi aarch64-linux-android i686-linux-android x86_64-linux-android)
declare -a BIN_UTILS
BIN_UTILS=(arm-linux-androideabi aarch64-linux-android i686-linux-android x86_64-linux-android)
# Set JNI directories
declare -a JNI_DIRS
JNI_DIRS=(armeabi-v7a arm64-v8a x86 x86_64)
# Toolchain
export TOOLCHAIN=${NDK}/toolchains/llvm/prebuilt/${BUILD_TAG}
# Flags
export LDFLAGS='--specs=nosys.specs'
export CFLAGS='-Os'
# Copy to JNI dir
jni_dir=../app/src/main/jniLibs
target_name=libsodium.so
target_dir=
# Build and copy libsodium to their respective directories
for (( i = 0; i < 4; ++i )); do
export AR="${TOOLCHAIN}/bin/${BIN_UTILS[i]}-ar"
export AS="${TOOLCHAIN}/bin/${BIN_UTILS[i]}-as"
export CC="${TOOLCHAIN}/bin/${TARGETS[i]}${API}-clang"
export CXX="${TOOLCHAIN}/bin/${TARGETS[i]}${API}-clang++"
export LD="${TOOLCHAIN}/bin/${BIN_UTILS[i]}-ld"
export RANLIB="${TOOLCHAIN}/bin/${BIN_UTILS[i]}-ranlib"
export STRIP="${TOOLCHAIN}/bin/${BIN_UTILS[i]}-strip"
# Clean
make clean
# Run configure
./configure --host="${TARGETS[i]}" --prefix="$PWD"
# Install lib
make -j $ncpu install
if [[ $? -ne 0 ]]; then exit 1; fi
# move to jni dir
target_dir=${jni_dir}/${JNI_DIRS[i]}
mkdir -p ${target_dir}
mv lib/libsodium.so ${target_dir}/${target_name}
done