android_do 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. #!/bin/sh
  2. # Fedora
  3. # dnf install wget git make which patch findutils unzip
  4. # Ubuntu
  5. # apt install wget git build-essential unzip time python
  6. # ./android_do
  7. die() { echo "Error: $1"; exit 1; }
  8. need() { which "$1" >/dev/null 2>/dev/null || die "Missing $1"; }
  9. need which
  10. need wget
  11. need git
  12. need make
  13. need patch
  14. need find
  15. need unzip
  16. need ls
  17. need uname
  18. need pwd
  19. need mkdir
  20. need rm
  21. need time
  22. need rustc
  23. need cargo
  24. BUILD_PATH=$(pwd)/build_android
  25. NDK_VERSION="android-ndk-r25b"
  26. case $(uname -s) in
  27. Darwin)
  28. TYPE=darwin
  29. ;;
  30. Linux)
  31. TYPE=linux
  32. ;;
  33. *)
  34. TYPE=
  35. ;;
  36. esac
  37. if test "x$CJDNS_NDK_PATH" != "x"; then
  38. false # we already have an ndk path...
  39. elif test "x$TYPE" = xdarwin; then
  40. CJDNS_NDK_PATH_HINT=$(echo "$HOME"/Library/Android/sdk/ndk/* | tail -1)
  41. elif test "x$TYPE" = xlinux; then
  42. CJDNS_NDK_PATH_HINT=$(echo "$HOME"/Android/Sdk/ndk/* | tail -1)
  43. fi
  44. if test "x$CJDNS_NDK_PATH" = "x"; then
  45. if test "x$CJDNS_NDK_PATH_HINT" != "x" && [ -d "$CJDNS_NDK_PATH_HINT" ]; then
  46. echo "Using NDK at path $CJDNS_NDK_PATH_HINT"
  47. CJDNS_NDK_PATH=$CJDNS_NDK_PATH_HINT
  48. elif [ -d "$BUILD_PATH/$NDK_VERSION/" ]; then
  49. CJDNS_NDK_PATH="$BUILD_PATH/$NDK_VERSION"
  50. echo "Using previously downloaded NDK $CJDNS_NDK_PATH"
  51. else
  52. echo "No NDK path known, NDK will be downloaded"
  53. fi
  54. elif [ -d "$CJDNS_NDK_PATH" ]; then
  55. echo "Using NDK at specified path $CJDNS_NDK_PATH"
  56. else
  57. die "NDK path $CJDNS_NDK_PATH is not a directory"
  58. fi
  59. cpu_arch="$(uname -m)"
  60. [ -z "$cpu_arch" ] && die "NO CPU ARCHITECTURE DETECTED"
  61. [ "$cpu_arch" = "i686" ] && cpu_arch="x86"
  62. if test "x$CJDNS_NDK_PATH" = "x"; then
  63. mkdir "$BUILD_PATH" 2>/dev/null
  64. cd "$BUILD_PATH"
  65. filename="$NDK_VERSION-${TYPE}.zip"
  66. echo "$filename"
  67. if ! [ -f "$filename" ]; then
  68. url="https://dl.google.com/android/repository/$NDK_VERSION-${TYPE}.zip"
  69. wget "$url" || die "wget $url failed"
  70. fi
  71. if ! [ -d "$NDK_VERSION" ]; then
  72. unzip -q "$filename" || die "failed to unzip "
  73. fi
  74. CJDNS_NDK_PATH="$BUILD_PATH/$NDK_VERSION"
  75. [ -d "$CJDNS_NDK_PATH" ] || die "NDK zip file does not contain expected content"
  76. cd ..
  77. fi
  78. BUILD_0='
  79. APP_ABI=armv7a
  80. RUST_TARGET=armv7-linux-androideabi
  81. EABI=androideabi
  82. VERSION=26
  83. '
  84. BUILD_1='
  85. APP_ABI=aarch64
  86. VERSION=26
  87. '
  88. BUILD_2='
  89. APP_ABI=i686
  90. VERSION=26
  91. '
  92. BUILD_3='
  93. APP_ABI=x86_64
  94. VERSION=26
  95. '
  96. # https://github.com/rust-lang/rust/pull/85806
  97. find "${BUILD_PATH}" -name 'libunwind.a' | \
  98. sed 's@libunwind.a$@libgcc.a@' | \
  99. while read x; do
  100. echo "INPUT(-lunwind)" > $x
  101. done
  102. for i in $(seq 0 100); do
  103. BUILD=$(eval echo "\$BUILD_$i")
  104. if test "x$BUILD" = "x"; then
  105. continue
  106. fi
  107. RUST_TARGET=
  108. APP_ABI=
  109. VERSION=
  110. EABI=android
  111. export $BUILD
  112. if test "x$RUST_TARGET" = "x"; then
  113. RUST_TARGET="$APP_ABI-linux-$EABI"
  114. fi
  115. if ! [ -d "$(rustc --target "$RUST_TARGET" --print target-libdir)" ]; then
  116. echo
  117. echo "Skipping $RUST_TARGET because we dont have that toolchain"
  118. echo "Use: \`rustup target add $RUST_TARGET\` to install it"
  119. echo
  120. echo
  121. continue
  122. fi
  123. CROSS_PATH=$CJDNS_NDK_PATH/toolchains/llvm/prebuilt/${TYPE}-${cpu_arch}/bin
  124. CROSS_PREFIX=${CROSS_PATH}/llvm
  125. export CC=${CROSS_PATH}/${APP_ABI}-linux-${EABI}${VERSION}-clang
  126. # Used by cjdns nodejs build
  127. export SYSTEM=linux
  128. export CROSS=1
  129. # Used by cjdns libuv build AND sodiumoxide
  130. export AR=${CROSS_PREFIX}-ar
  131. export RANLIB=${CROSS_PREFIX}-ranlib
  132. # Whatever target we happen to be using, use CC as the linker
  133. export CARGO_TARGET_AARCH64_LINUX_ANDROID_LINKER=$CC
  134. export CARGO_TARGET_I686_LINUX_ANDROID_LINKER=$CC
  135. export CARGO_TARGET_X86_64_LINUX_ANDROID_LINKER=$CC
  136. export CARGO_TARGET_ARMV7_LINUX_ANDROIDEABI_LINKER=$CC
  137. echo "Compiler CC: $CC - $(${CC} --version)"
  138. cargo build --release -v --target "$RUST_TARGET"
  139. done