cjdroid-build.sh 5.0 KB

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