cjdroid-build.sh 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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-r9d"
  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}.tar.bz2"
  67. [[ -f "$ndkver-linux-${cpu_arch}.tar.bz2" ]] \
  68. || wget "http://dl.google.com/android/ndk/$ndkver-linux-${cpu_arch}.tar.bz2" \
  69. || (echo "Can't find download for your system" && exit 1)
  70. [[ -d "$ndkver" ]] || (tar jxf "$ndkver-linux-${cpu_arch}.tar.bz2" || 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. ##BUILD TOOLCHAIN: build gcc toolchain
  85. [[ ! -x "$work_dir/android-arm-toolchain/bin/arm-linux-androideabi-gcc" ]] && {
  86. cd "$src_dir"
  87. "$ndk_dir/build/tools/make-standalone-toolchain.sh" \
  88. --platform=android-9 \
  89. --toolchain=arm-linux-androideabi-4.8 \
  90. --install-dir="$work_dir/android-arm-toolchain/" \
  91. --system=linux-$cpu_arch \
  92. || exit 1
  93. }
  94. ##CLONE or PULL: the repo and change branch if requested
  95. cd "$build_dir"
  96. [[ -d cjdns ]] && {
  97. cd cjdns
  98. git pull --ff-only
  99. } || {
  100. git clone $cjdns_repo cjdns
  101. [[ ! -d cjdns ]] && {
  102. echo "ERROR: Couldn't clone $cjdns_repo"
  103. exit 1
  104. }
  105. cd cjdns
  106. }
  107. [[ -n "$1" ]] \
  108. && git checkout "$1"
  109. ./clean
  110. ##ADD TWEAKS TO GET THINGS BUILDING
  111. patch -p1 < ../../remove-ifaddrs.patch
  112. sed -i 's/#ifndef linux/#if !defined(linux) || defined(android)/' util/ArchInfo.c
  113. ##SETUP TOOLCHAIN VARS
  114. export PATH="$work_dir/android-arm-toolchain/bin:$PATH"
  115. ##BUILD cjdns (without tests)
  116. CROSS_COMPILE=arm-linux-androideabi- ./cross-do 2>&1 \
  117. | tee cjdns-build.log
  118. [[ ! -f 'cjdroute' ]] && {
  119. echo -e "\nBUILD FAILED :("
  120. exit 1
  121. }
  122. echo -e "\nBUILD COMPLETE! @ $build_dir/cjdns/cjdroute"
  123. ##PACKAGE CJDROUTE AND ASSOCIATED SCRIPTS FOR DEPLOYMENT
  124. cd "$build_dir"
  125. cjdns_version=$(git -C cjdns describe --always | sed 's|-|.|g;s|[^\.]*\.||;s|\.[^\.]*$||')
  126. [[ -f ../cjdroid-$cjdns_version${cjdns_repo_branch}.tar.gz ]] && {
  127. echo "Error: Package not built because $(readlink -f ../cjdroid-$cjdns_version${cjdns_repo_branch}.tar.gz) already exists"
  128. exit 1
  129. }
  130. [[ ! -f cjdns/cjdroute ]] && {
  131. echo "Error: Package not built because $PWD/cjdns/cjdroute does not exist"
  132. exit 1
  133. }
  134. [[ ! -d cjdns/contrib/android/cjdroid ]] && {
  135. echo "Error: Package not built because $PWD/cjdns/contrib/android/cjdroid does not exist"
  136. exit 1
  137. }
  138. cp -R cjdns/contrib/android/cjdroid .
  139. install -Dm755 cjdns/cjdroute cjdroid/files/cjdroute
  140. tar cfz ../cjdroid-$cjdns_version${cjdns_repo_branch}.tar.gz cjdroid
  141. echo -e "\nSuccess: A deployable package has been created @ $(readlink -f ../cjdroid-$cjdns_version${cjdns_repo_branch}.tar.gz)"