You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

android_whl_env_prepare.sh 6.2 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. #!/bin/bash -e
  2. # Installing package by pkg in termux is unwise because pkg will upgrade
  3. # package to the latest version which is undesired sometimes. We will
  4. # use apt as default package tool. If your env is already broken,e.g.
  5. # clang does not work, you can execute following commands to fix it:
  6. # pkg update
  7. # pkg upgrade
  8. SRC_DIR=$(readlink -f "`dirname $0`/../../../")
  9. cd ${SRC_DIR}
  10. source scripts/whl/android/utils.sh
  11. function install_apt_package() {
  12. APT_PACKAGE="proot git wget clang cmake libandroid-spawn binutils build-essential ninja texinfo patchelf python"
  13. echo "try install: ${APT_PACKAGE}"
  14. apt install ${APT_PACKAGE}
  15. echo "check termux status by running: clang --version"
  16. log=`clang --version || true`
  17. if [[ "${log}" =~ "clang version" ]]; then
  18. echo "valid env after installing apt package"
  19. else
  20. echo "Failed to run clang command, please check termux env!!! You can run: pkg update && pkg upgrade to try to solve it"
  21. echo "raw log: ${log}"
  22. exit -1
  23. fi
  24. }
  25. function patch_termux_env() {
  26. # do not try to modify other project build files to adapt to only-llvm environment
  27. echo "many projects can not build without gcc libs, so we create a fake gcc libs linked to librt"
  28. RT_LIB_TARGET="${PREFIX}/lib/librt.so"
  29. if [ -e ${RT_LIB_TARGET} ];then
  30. echo "find librt.so and link it to libgcc.so"
  31. GCC_LIB_TARGET="${PREFIX}/lib/libgcc.so"
  32. if [ -e ${GCC_LIB_TARGET} ];then
  33. echo "already find: ${GCC_LIB_TARGET} skip it"
  34. else
  35. create_libgcc_cmd="ln -s ${RT_LIB_TARGET} ${GCC_LIB_TARGET}"
  36. echo "run cmd: $create_libgcc_cmd"
  37. ${create_libgcc_cmd}
  38. fi
  39. else
  40. echo "broken termux env, can not find librt.so"
  41. exit -1
  42. fi
  43. }
  44. function build_python() {
  45. # Up to now many tools (build multi python3) are not supported in termux so that we have to build them from source.
  46. # This function will be changed when some tools are supported in termux, e.g. pyenv.
  47. mge_python_env_root="${HOME}/mge_python_env"
  48. mkdir -p ${mge_python_env_root}
  49. cd ${mge_python_env_root}
  50. if [ -e ${PREFIX}/local/lib/libffi.a ];then
  51. echo "always find libffi, skip build it"
  52. else
  53. echo "build libffi for python module"
  54. rm -rf libffi
  55. git clone https://github.com/libffi/libffi.git
  56. cd libffi
  57. termux-chroot "./autogen.sh && ./configure && make -j$(nproc) && make install"
  58. # remove dynamic lib to force python to use static lib
  59. rm ${PREFIX}/local/lib/libffi.so*
  60. fi
  61. if [ -e ${PREFIX}/local/lib/libz.a ];then
  62. echo "always find libzlib, skip build it"
  63. else
  64. echo "build zlib for python module"
  65. rm -rf zlib
  66. git clone https://github.com/madler/zlib.git
  67. cd zlib
  68. termux-chroot "CFLAGS=\"-O3 -fPIC\" ./configure && make -j$(nproc) && make install"
  69. # remove dynamic lib to force python to use static lib
  70. rm ${PREFIX}/local/lib/libz.so*
  71. fi
  72. cpython_repo_dir=${mge_python_env_root}/cpython
  73. if [ -d ${cpython_repo_dir}/.git ];then
  74. echo "already find cpython repo"
  75. cd ${cpython_repo_dir}
  76. git reset --hard
  77. git clean -xdf
  78. git fetch
  79. else
  80. cd ${mge_python_env_root}
  81. rm -rf ${cpython_repo_dir}
  82. git clone https://github.com/python/cpython.git
  83. fi
  84. for ver in ${ALL_PYTHON}
  85. do
  86. install_dir=${mge_python_env_root}/${ver}/install
  87. if [ -e ${install_dir}/bin/python3 ];then
  88. echo "always find python3, skip build it"
  89. else
  90. mkdir -p ${install_dir}
  91. echo "try build python: ${ver} to ${install_dir}"
  92. cd ${cpython_repo_dir}
  93. git reset --hard
  94. git clean -xdf
  95. git checkout v${ver}
  96. index=`awk -v a="${ver}" -v b="." 'BEGIN{print index(a,b)}'`
  97. sub_str=${ver:${index}}
  98. index_s=`awk -v a="${sub_str}" -v b="." 'BEGIN{print index(a,b)}'`
  99. ((finally_index = ${index} + ${index_s}))
  100. finally_verson=${ver:0:${finally_index}-1}
  101. MINOR_VER=${ver:${index}:${finally_index}-${index}-1}
  102. finally_verson="python${finally_verson}"
  103. echo "finally_verson is: ${finally_verson}"
  104. # apply patchs
  105. git apply ${SRC_DIR}/scripts/whl/android/patchs/*.patch
  106. if [[ ${MINOR_VER} -gt 8 ]]
  107. then
  108. echo "apply more patchs"
  109. git apply ${SRC_DIR}/scripts/whl/android/up_3_9_patch/*.patch
  110. fi
  111. termux-chroot "FLAGS=\"-D__ANDROID_API__=24 -Wno-unused-value -Wno-empty-body -Qunused-arguments -Wno-error\" \
  112. ./configure CFLAGS=\"${FLAGS}\" CPPFLAGS=\"${FLAGS}\" CC=clang CXX=clang++ --enable-shared --prefix=${install_dir} \
  113. && sed -i 's/-Werror=implicit-function-declaration//g' Makefile && \
  114. sed -i 's/\$(LN) -f \$(INSTSONAME)/cp \$(INSTSONAME)/g' Makefile && make -j$(nproc) && make install"
  115. # after building successfully, patchelf to make python work out of termux-chroot env
  116. cd ${install_dir}/bin
  117. # Some python versions won't link to python3 automatically, so we create link manually here'
  118. rm -rf python3
  119. if [[ ${MINOR_VER} -gt 7 ]]
  120. then
  121. echo "python3 remove suffix m after 3.8"
  122. patchelf --add-rpath ${install_dir}/lib ${finally_verson}
  123. cp ${finally_verson} python3
  124. else
  125. echo "python3 with suffix m before 3.8, add it"
  126. patchelf --add-rpath ${install_dir}/lib "${finally_verson}m"
  127. cp "${finally_verson}m" ${finally_verson}
  128. cp ${finally_verson} python3
  129. fi
  130. echo "finally try run python3"
  131. ./python3 --version
  132. ./python3 -m pip install --upgrade pip
  133. ./python3 -m pip install numpy wheel
  134. fi
  135. done
  136. }
  137. ############install env now###########
  138. echo "run at root dir: ${SRC_DIR}"
  139. check_termux_env
  140. install_apt_package
  141. patch_termux_env
  142. build_python