android_do 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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-r21"
  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}-${cpu_arch}.zip"
  66. echo "$filename"
  67. if ! [ -f "$filename" ]; then
  68. url="https://dl.google.com/android/repository/$NDK_VERSION-${TYPE}-${cpu_arch}.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. TOOLCHAIN_PREFIX=arm
  81. EABI=androideabi
  82. VERSION=16
  83. '
  84. BUILD_1='
  85. APP_ABI=aarch64
  86. VERSION=21
  87. '
  88. BUILD_2='
  89. APP_ABI=i686
  90. VERSION=16
  91. '
  92. BUILD_3='
  93. APP_ABI=x86_64
  94. VERSION=21
  95. '
  96. for i in $(seq 1 100); do
  97. BUILD=$(eval echo "\$BUILD_$i")
  98. if test "x$BUILD" = "x"; then
  99. continue
  100. fi
  101. TOOLCHAIN_PREFIX=
  102. APP_ABI=
  103. VERSION=
  104. EABI=android
  105. export $BUILD
  106. if test "x$TOOLCHAIN_PREFIX" = "x"; then
  107. TOOLCHAIN_PREFIX=$APP_ABI
  108. fi
  109. RUST_TARGET="$APP_ABI-linux-$EABI"
  110. if ! [ -d "$(rustc --target "$RUST_TARGET" --print target-libdir)" ]; then
  111. echo
  112. echo 'Skipping i686-linux-android because we dont have that toolchain'
  113. echo "Use: \`rustup target add $RUST_TARGET\` to install it"
  114. echo
  115. echo
  116. continue
  117. fi
  118. CROSS_PATH=$CJDNS_NDK_PATH/toolchains/llvm/prebuilt/${TYPE}-${cpu_arch}/bin
  119. CROSS_PREFIX=${CROSS_PATH}/${TOOLCHAIN_PREFIX}-linux-${EABI}
  120. export CC=${CROSS_PATH}/${APP_ABI}-linux-${EABI}${VERSION}-clang
  121. # Used by cjdns nodejs build
  122. export SYSTEM=linux
  123. export CROSS=1
  124. # Used by cjdns libuv build AND sodiumoxide
  125. export AR=${CROSS_PREFIX}-ar
  126. export RANLIB=${CROSS_PREFIX}-ranlib
  127. # Whatever target we happen to be using, use CC as the linker
  128. export CARGO_TARGET_AARCH64_LINUX_ANDROID_LINKER=$CC
  129. export CARGO_TARGET_I686_LINUX_ANDROID_LINKER=$CC
  130. export CARGO_TARGET_X86_64_LINUX_ANDROID_LINKER=$CC
  131. export CARGO_TARGET_ARMV7A_LINUX_ANDROIDEABI_LINKER=$CC
  132. echo "Compiler CC: $CC - $(${CC} --version)"
  133. cargo build --release -vv --target "$RUST_TARGET"
  134. done