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_build_whl.sh 6.6 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. #!/bin/bash -e
  2. SRC_DIR=$(readlink -f "`dirname $0`/../../../")
  3. cd ${SRC_DIR}
  4. source scripts/whl/android/utils.sh
  5. ANDROID_WHL_HOME=${SRC_DIR}/scripts/whl/android/ANDROID_WHL_HOME
  6. if [ -e "${ANDROID_WHL_HOME}" ]; then
  7. echo "remove old android whl file"
  8. rm -rf ${ANDROID_WHL_HOME}
  9. fi
  10. mkdir -p ${ANDROID_WHL_HOME}
  11. BUILD_DIR=${SRC_DIR}/build_dir/host/MGE_WITH_CUDA_OFF/MGE_INFERENCE_ONLY_OFF/Release/build/
  12. # We only handle the case where dnn/src/common/conv_bias.cpp is not in the list of incremental build files.
  13. INCREMENT_KEY_WORDS="conv_bias.cpp.o is dirty"
  14. IS_IN_FIRST_LOOP=TRUE
  15. ORG_EXTRA_CMAKE_FLAG=${EXTRA_CMAKE_FLAG}
  16. function handle_strip() {
  17. echo "now handle strip $1"
  18. objcopy --only-keep-debug $1 $1.dbg
  19. strip -s $1
  20. objcopy --add-gnu-debuglink=$1.dbg $1
  21. rm $1.dbg
  22. }
  23. function patch_elf_depend_lib_mgb_mge() {
  24. echo "handle common depend lib for mgb or mge"
  25. LIBS_DIR=${BUILD_DIR}/staging/megengine/core/lib
  26. mkdir -p ${LIBS_DIR}
  27. patchelf --remove-rpath ${BUILD_DIR}/staging/megengine/core/_imperative_rt.so
  28. patchelf --set-rpath '$ORIGIN/lib' ${BUILD_DIR}/staging/megengine/core/_imperative_rt.so
  29. handle_strip ${BUILD_DIR}/staging/megengine/core/_imperative_rt.so
  30. cp ${BUILD_DIR}/src/libmegengine_shared.so ${LIBS_DIR}
  31. patchelf --remove-rpath ${LIBS_DIR}/libmegengine_shared.so
  32. patchelf --set-rpath '$ORIGIN/.' ${LIBS_DIR}/libmegengine_shared.so
  33. # FXIME: third_party LLVM need c++_static > 5.1
  34. # but now clang(13) at termux env do not satisfy it
  35. # may use -static-libstdc++ at CMakeLists.txt after
  36. # upgrade third_party LLVM
  37. cp /data/data/com.termux/files/usr/lib/libc++_shared.so ${LIBS_DIR}
  38. handle_strip ${LIBS_DIR}/libmegengine_shared.so
  39. }
  40. function patch_elf_depend_lib_megenginelite() {
  41. echo "handle common depend lib for megenginelite"
  42. LIBS_DIR=${BUILD_DIR}/staging/megenginelite/libs
  43. mkdir -p ${LIBS_DIR}
  44. cp ${BUILD_DIR}/lite/liblite_shared_whl.so ${LIBS_DIR}/
  45. patchelf --remove-rpath ${LIBS_DIR}/liblite_shared_whl.so
  46. patchelf --set-rpath '$ORIGIN/../../megengine/core/lib' ${LIBS_DIR}/liblite_shared_whl.so
  47. handle_strip ${LIBS_DIR}/liblite_shared_whl.so
  48. }
  49. function do_build() {
  50. mge_python_env_root="${HOME}/mge_python_env"
  51. for ver in ${ALL_PYTHON}
  52. do
  53. python_install_dir=${mge_python_env_root}/${ver}/install
  54. # we want to run a full clean build in the first loop
  55. if [ ${IS_IN_FIRST_LOOP} = "TRUE" ]; then
  56. # TODO: can all cmake issues be resolved after removing CMakeCache?
  57. # if YES, remove this logic to use old cache and speed up CI
  58. echo "warning: remove old build_dir for the first loop"
  59. rm -rf ${BUILD_DIR}
  60. fi
  61. # insert python3_install_dir into head of PATH to enable CMake find it
  62. if [ -e ${python_install_dir}/bin/python3 ];then
  63. echo "will use ${python_install_dir}/bin/python3 to build mge wheel"
  64. export PATH=${python_install_dir}/bin:$PATH
  65. else
  66. echo "ERROR: can not find python3 in: ${python_install_dir}/bin"
  67. echo "please run: %{SRC_DIR}/scripts/whl/android/android_whl_env_prepare.sh to prepare env"
  68. exit -1
  69. fi
  70. export EXTRA_CMAKE_ARGS="${ORG_EXTRA_CMAKE_FLAG} -DCMAKE_BUILD_TYPE=RelWithDebInfo"
  71. export EXTRA_CMAKE_ARGS="${EXTRA_CMAKE_ARGS} -DMGE_WITH_CUSTOM_OP=ON"
  72. if [ -d "${BUILD_DIR}" ]; then
  73. # insure rm have args
  74. touch ${BUILD_DIR}/empty.so
  75. touch ${BUILD_DIR}/CMakeCache.txt
  76. find ${BUILD_DIR} -name "*.so" | xargs rm
  77. # Force remove CMakeCache.txt to avoid error owing to unknown issue in CMakeLists.txt
  78. # which comes from using increment build mode when switching python version
  79. find ${BUILD_DIR} -name CMakeCache.txt | xargs rm
  80. fi
  81. HOST_BUILD_ARGS="-t -s"
  82. # call ninja dry run and check increment is invalid or not
  83. if [ ${IS_IN_FIRST_LOOP} = "FALSE" ]; then
  84. ninja_dry_run_and_check_increment "${SRC_DIR}/scripts/cmake-build/host_build.sh" "${HOST_BUILD_ARGS}" "${INCREMENT_KEY_WORDS}"
  85. fi
  86. # call real build
  87. echo "host_build.sh HOST_BUILD_ARGS: ${HOST_BUILD_ARGS}"
  88. bash ${SRC_DIR}/scripts/cmake-build/host_build.sh ${HOST_BUILD_ARGS}
  89. # check python api call setup.py
  90. cd ${BUILD_DIR}
  91. check_build_ninja_python_api ${ver}
  92. rm -rf staging
  93. mkdir -p staging
  94. cp -a imperative/python/{megengine,setup.py,requires.txt,requires-style.txt,requires-test.txt} staging/
  95. cp -a ${SRC_DIR}/src/custom/include staging/megengine/core/include/
  96. patch_elf_depend_lib_mgb_mge
  97. # handle megenginelite
  98. cd ${BUILD_DIR}
  99. mkdir -p staging/megenginelite
  100. cp ${SRC_DIR}/lite/pylite/megenginelite/* staging/megenginelite/
  101. patch_elf_depend_lib_megenginelite
  102. cd ${BUILD_DIR}/staging
  103. python3 setup.py bdist_wheel
  104. cd ${BUILD_DIR}/staging/dist/
  105. cp ${BUILD_DIR}/staging/dist/Meg*.whl ${ANDROID_WHL_HOME}
  106. cd ${SRC_DIR}
  107. echo ""
  108. echo "##############################################################################################"
  109. echo "android whl package location: ${ANDROID_WHL_HOME}"
  110. ls ${ANDROID_WHL_HOME}
  111. echo "##############################################################################################"
  112. IS_IN_FIRST_LOOP=FALSE
  113. done
  114. }
  115. function third_party_prepare() {
  116. echo "init third_party..."
  117. bash ${SRC_DIR}/third_party/prepare.sh
  118. # fix flatbuffers build at pure LLVM env(not cross link gcc)
  119. # TODO: pr to flatbuffers to fix this issue
  120. sed -i 's/lc++abi/lc/g' ${SRC_DIR}/third_party/flatbuffers/CMakeLists.txt
  121. }
  122. function remove_requires() {
  123. # do not worry about this, we will provide 'scripts/whl/android/android_opencv_python.sh'
  124. # to build opencv-python from opencv src!! This function may be removed after termux fixes
  125. # this issue
  126. cd ${SRC_DIR}
  127. git checkout imperative/python/requires.txt
  128. sed -i '/opencv-python/d' imperative/python/requires.txt
  129. # FIXME: termux install pyarrow will build error now
  130. # remove this logic after pyarrow fix this issue
  131. # see imperative/python/megengine/data/dataloader.py
  132. # for detail, now will use _SerialStreamDataLoaderIter
  133. sed -i '/pyarrow/d' imperative/python/requires.txt
  134. cd -
  135. }
  136. ######################
  137. check_termux_env
  138. third_party_prepare
  139. remove_requires
  140. do_build