1#!/usr/bin/env bash
2
3# http://cjdns.ca/cjdns-droid.sh
4
5# Does most things required to build cjdns for android.
6# See bottom of file for tips on installing/usage.
7# ADB, Android, plus basic command line skills required
8# ircerr 20140507
9
10# Parts stolen from:
11#  https://github.com/cjdelisle/cjdns/pull/476
12#  https://gist.github.com/lgierth/01ce4bda638f8c863349
13#  larsg@HypeIRC
14# + mods by prurigro
15
16# Update files from:
17#  https://developer.android.com/tools/sdk/ndk/index.html
18
19# Possible Deps (phone):
20#  1. rooted:
21#   The method required to root a phone differs from model to model.
22#   If your phone isn't rooted yet and you're not sure where to
23#   start, look for the subforum for your device on XDA forums
24#   (linked below), and hopefully you'll find something that works.
25#
26#   http://forum.xda-developers.com/index.php?tab=all
27#
28#  2. tun device:
29#   Most (if not all) 4.0+ phones include tun support. If yours
30#   uses 2.x, CyanogenMod and some stock ROMs include support, but
31#   many don't. If your phone doesn't have a TUN device at /dev/tun,
32#   download the link below (or find 'com.aed.tun.installer' to
33#   download it yourself), then install and run it to have it set
34#   one up for you.
35#
36#   http://cjdns.ca/com.aed.tun.installer.apk
37
38# Report success/failure including phone type, android version, kernel version,
39# and as much information as possible to #cjdns @ HypeIRC
40
41# NOTES:
42#  Use a custom NDK directory:
43#   Before running this script, configure $NDK: export NDK="/path/to/ndk"
44#
45#  Use a different repo:
46#   Remove 'cjdns-android/cjdns' and below change: cjdns_repo="https://newaddr"
47#
48#  Use a different branch:
49#   Run: cjdroid-bulid.sh branchname
50
51##CONFIGURABLE VARIABLES
52cjdns_repo="https://github.com/cjdelisle/cjdns/"
53[[ -n "$1" ]] \
54    && cjdns_repo_branch="-$1"
55
56build_dir="$PWD/build_android"
57src_dir="$build_dir/source"
58ndk_dir="$src_dir/ndk"
59work_dir="$build_dir/workspace"
60
61ndkver="android-ndk-r10e"
62cpu_arch="$(uname -m)"
63[[ -z "$cpu_arch" ]] && {
64    echo "ERROR: NO CPU ARCHITECTURE DETECTED"
65    exit 1
66}
67[[ "$cpu_arch" = "i686" ]] \
68    && cpu_arch="x86"
69
70##CREATE REQUIRED DIRECTORIES
71install -d "$src_dir"
72install -d "$work_dir"
73
74##SETUP NDK
75cd "$src_dir"
76[[ -z "$NDK" ]] && {
77    if [ -z "$ANDROID_NDK" ]; then
78        echo "$ndkver-linux-${cpu_arch}.bin"
79        [[ -f "$ndkver-linux-${cpu_arch}.bin" ]] \
80            || wget "http://dl.google.com/android/ndk/$ndkver-linux-${cpu_arch}.bin" \
81            || (echo "Can't find download for your system" && exit 1)
82        [[ -d "$ndkver" ]] || (chmod a+x $ndkver-linux-${cpu_arch}.bin && ./$ndkver-linux-${cpu_arch}.bin || exit 1)
83        NDK="$ndkver"
84    else
85        NDK="$ANDROID_NDK"
86    fi
87}
88[[ ! -d "$NDK" ]] && {
89    echo "The NDK variable is not pointing to a valid directory"
90    exit 1
91}
92[[ -h "$ndk_dir" ]] \
93    && rm "$ndk_dir"
94[[ ! -e "$ndk_dir" ]] \
95    && ln -sf "$NDK" "$ndk_dir"
96
97GCC=$work_dir/android-arm-toolchain/bin/arm-linux-androideabi-gcc
98TOOLCHAIN=arm-linux-androideabi-4.9
99COMPILER=arm-linux-androideabi-
100[[ "x$TARGET_ARCH" == "xarm64" ]] \
101    && GCC=$work_dir/android-arm-toolchain/bin/aarch64-linux-android-gcc \
102    && TOOLCHAIN=aarch64-linux-android-4.9 \
103    && COMPILER=aarch64-linux-android-
104
105
106##BUILD TOOLCHAIN: build gcc toolchain
107[[ ! -x "$GCC" ]] && {
108    cd "$src_dir"
109    "$ndk_dir/build/tools/make-standalone-toolchain.sh" \
110        --platform=android-21 \
111        --toolchain=$TOOLCHAIN \
112        --install-dir="$work_dir/android-arm-toolchain/" \
113        --system=linux-$cpu_arch \
114            || exit 1
115}
116
117##CLONE or PULL: the repo and change branch if requested
118cd "$build_dir"
119[[ -d cjdns ]] && {
120    cd cjdns
121    git pull --ff-only
122} || {
123    git clone $cjdns_repo cjdns
124    [[ ! -d cjdns ]] && {
125        echo "ERROR: Couldn't clone $cjdns_repo"
126        exit 1
127    }
128    cd cjdns
129}
130[[ -n "$1" ]] \
131    && git checkout "$1"
132./clean
133
134##SETUP TOOLCHAIN VARS
135export PATH="$work_dir/android-arm-toolchain/bin:$PATH"
136
137##BUILD cjdns (without tests)
138CROSS_COMPILE=$COMPILER ./cross-do 2>&1 \
139    | tee cjdns-build.log
140[[ ! -f 'cjdroute' ]] && {
141    echo -e "\nBUILD FAILED :("
142    exit 1
143}
144echo -e "\nBUILD COMPLETE! @ $build_dir/cjdns/cjdroute"
145
146##PACKAGE CJDROUTE AND ASSOCIATED SCRIPTS FOR DEPLOYMENT
147cd "$build_dir"
148cjdns_version=$(git -C cjdns describe --always | sed 's|-|.|g;s|[^\.]*\.||;s|\.[^\.]*$||')
149[[ -f ../cjdroid-$cjdns_version${cjdns_repo_branch}.tar.gz ]] && {
150    echo "Error: Package not built because $(readlink -f ../cjdroid-$cjdns_version${cjdns_repo_branch}.tar.gz) already exists"
151    exit 1
152}
153[[ ! -f cjdns/cjdroute ]] && {
154    echo "Error: Package not built because $PWD/cjdns/cjdroute does not exist"
155    exit 1
156}
157[[ ! -d cjdns/contrib/android/cjdroid ]] && {
158    echo "Error: Package not built because $PWD/cjdns/contrib/android/cjdroid does not exist"
159    exit 1
160}
161cp -R cjdns/contrib/android/cjdroid .
162install -Dm755 cjdns/cjdroute cjdroid/files/cjdroute
163tar cfz ../cjdroid-$cjdns_version${cjdns_repo_branch}.tar.gz cjdroid
164echo -e "\nSuccess: A deployable package has been created @ $(readlink -f ../cjdroid-$cjdns_version${cjdns_repo_branch}.tar.gz)"
165