blob: a3b2cad96bdc95db3040e4fce26a20267e8be850 (
plain) (
blame)
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
|
#!/bin/sh
# Build the rsh SSH transport for each Android ABI as a Bionic PIE executable
# named lib*.so (packaged inside the APK and exec'd from nativeLibraryDir),
# using the Android NDK. Also build a plain host binary for tests and manual
# debugging.
#
# Android needs PIE and the Bionic dynamic linker (/system/bin/linker64), so
# the on-device build is GOOS=android with cgo and the NDK clang. A GOOS=linux
# build targets glibc's linker and will not exec on a phone.
set -eu
root=$(CDPATH= cd -- "$(dirname -- "$0")/.." && pwd)
. "$root/versions"
# Locate the NDK toolchain.
ndk=${ANDROID_NDK_HOME:-${ANDROID_NDK_ROOT:-}}
if [ -z "$ndk" ] && [ -n "${ANDROID_HOME:-}" ]; then
ndk="$ANDROID_HOME/ndk/$ANDROID_NDK"
fi
if [ -z "$ndk" ] || [ ! -d "$ndk" ]; then
echo "rsh: Android NDK not found; set ANDROID_NDK_HOME" >&2
exit 1
fi
# NDK prebuilt toolchains are named by build host, not target. Pick the tag
# matching this machine (macOS ships an x86_64 clang that runs under Rosetta on
# Apple Silicon); the target ABI is selected separately below.
case "$(uname -s)" in
Linux) host_tag=linux-x86_64 ;;
Darwin) host_tag=darwin-x86_64 ;;
*) echo "rsh: unsupported build host $(uname -s)" >&2; exit 1 ;;
esac
tc="$ndk/toolchains/llvm/prebuilt/$host_tag/bin"
if [ ! -d "$tc" ]; then
echo "rsh: NDK toolchain not found at $tc" >&2
exit 1
fi
ldflags="-s -w -buildid="
cd "$root/rsh"
for abi in $ABIS; do
case "$abi" in
arm64-v8a) goarch=arm64 goarm= cc="aarch64-linux-android${ANDROID_MIN_SDK}-clang" ;;
armeabi-v7a) goarch=arm goarm=7 cc="armv7a-linux-androideabi${ANDROID_MIN_SDK}-clang" ;;
x86_64) goarch=amd64 goarm= cc="x86_64-linux-android${ANDROID_MIN_SDK}-clang" ;;
x86) goarch=386 goarm= cc="i686-linux-android${ANDROID_MIN_SDK}-clang" ;;
*) echo "rsh: unknown ABI $abi" >&2; exit 1 ;;
esac
dest="$root/app/src/main/jniLibs/$abi"
mkdir -p "$dest"
echo "rsh: building $abi (android/$goarch)"
CGO_ENABLED=1 GOOS=android GOARCH="$goarch" GOARM="$goarm" \
CC="$tc/$cc" GOFLAGS=-trimpath CGO_CFLAGS="-ffile-prefix-map=$root=." \
go build -buildmode=pie -ldflags "$ldflags" -o "$dest/libxrsh.so" .
done
# Host binary (no NDK, cgo off) for tests and manual debugging.
mkdir -p "$root/out"
echo "rsh: building host binary -> out/rsh"
CGO_ENABLED=0 GOFLAGS=-trimpath go build -ldflags "$ldflags" -o "$root/out/rsh" .
|