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.

macos_build_whl.sh 7.5 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. #!/bin/bash -e
  2. READLINK=readlink
  3. OS=$(uname -s)
  4. if [ $OS = "Darwin" ];then
  5. READLINK=greadlink
  6. else
  7. echo "ERR: only run at macos env"
  8. exit -1
  9. fi
  10. function err_env() {
  11. echo "check_env failed: pls refs ${SRC_DIR}/scripts/whl/BUILD_PYTHON_WHL_README.md"
  12. echo "try call ./scripts/whl/macos/macos_whl_env_prepare.sh to init env"
  13. exit -1
  14. }
  15. function append_path_env_and_check() {
  16. export PATH=/usr/local/opt/findutils/libexec/gnubin:$PATH
  17. export PATH=/usr/local/opt/binutils/bin:$PATH
  18. export PATH=/usr/local/opt/llvm/bin:$PATH
  19. export PATH=/Users/${USER}/megengine_use_cmake/install/bin:$PATH
  20. if [ ! -f "/usr/local/opt/llvm/bin/llvm-strip" ]; then
  21. err_env
  22. fi
  23. which cmake
  24. if [ ! -f "/Users/${USER}/megengine_use_cmake/install/bin/cmake" ]; then
  25. err_env
  26. fi
  27. }
  28. append_path_env_and_check
  29. SRC_DIR=$($READLINK -f "`dirname $0`/../../../")
  30. ALL_PYTHON=${ALL_PYTHON}
  31. FULL_PYTHON_VER="3.5.9 3.6.10 3.7.7 3.8.3"
  32. if [[ -z ${ALL_PYTHON} ]]
  33. then
  34. ALL_PYTHON=${FULL_PYTHON_VER}
  35. fi
  36. PYTHON_DIR=
  37. PYTHON_LIBRARY=
  38. PYTHON_INCLUDE_DIR=
  39. MACOS_WHL_HOME=${SRC_DIR}/scripts/whl/macos/macos_whl_home
  40. if [ -e "${MACOS_WHL_HOME}" ]; then
  41. echo "remove old macos whl file"
  42. rm -rf ${MACOS_WHL_HOME}
  43. fi
  44. mkdir -p ${MACOS_WHL_HOME}
  45. function config_python_env() {
  46. PYTHON_DIR=/Users/${USER}/.pyenv/versions/$1/
  47. PYTHON_BIN=/Users/${USER}/.pyenv/versions/$1/bin
  48. if [ ! -f "$PYTHON_BIN/python3" ]; then
  49. echo "ERR: can not find $PYTHON_BIN , Invalid python package"
  50. echo "now support list: ${FULL_PYTHON_VER}"
  51. err_env
  52. else
  53. echo "put python3 to env..."
  54. export PATH=${PYTHON_BIN}:$PATH
  55. which python3
  56. fi
  57. echo ${ver}
  58. if [ "$1" = "3.5.9" ]; then
  59. PYTHON_INCLUDE_DIR=${PYTHON_DIR}include/python3.5m
  60. PYTHON_LIBRARY=${PYTHON_DIR}/lib/libpython3.5m.dylib
  61. elif [ "$1" = "3.6.10" ]; then
  62. PYTHON_INCLUDE_DIR=${PYTHON_DIR}include/python3.6m
  63. PYTHON_LIBRARY=${PYTHON_DIR}/lib/libpython3.6m.dylib
  64. elif [ "$1" = "3.7.7" ]; then
  65. PYTHON_INCLUDE_DIR=${PYTHON_DIR}include/python3.7m
  66. PYTHON_LIBRARY=${PYTHON_DIR}/lib/libpython3.7m.dylib
  67. elif [ "$1" = "3.8.3" ]; then
  68. PYTHON_INCLUDE_DIR=${PYTHON_DIR}include/python3.8
  69. PYTHON_LIBRARY=${PYTHON_DIR}/lib/libpython3.8.dylib
  70. else
  71. echo "ERR: DO NOT SUPPORT PYTHON VERSION"
  72. echo "now support list: ${FULL_PYTHON_VER}"
  73. exit -1
  74. fi
  75. }
  76. if [[ -z ${BUILD_IMPERATIVE} ]]
  77. then
  78. BUILD_IMPERATIVE="OFF"
  79. fi
  80. function do_build() {
  81. for ver in ${ALL_PYTHON}
  82. do
  83. #config
  84. config_python_env ${ver}
  85. #check env
  86. if [ ! -f "$PYTHON_LIBRARY" ]; then
  87. echo "ERR: can not find $PYTHON_LIBRARY , Invalid python package"
  88. err_env
  89. fi
  90. if [ ! -d "$PYTHON_INCLUDE_DIR" ]; then
  91. echo "ERR: can not find $PYTHON_INCLUDE_DIR , Invalid python package"
  92. err_env
  93. fi
  94. echo "PYTHON_LIBRARY: ${PYTHON_LIBRARY}"
  95. echo "PYTHON_INCLUDE_DIR: ${PYTHON_INCLUDE_DIR}"
  96. #append cmake args for config python
  97. export EXTRA_CMAKE_ARGS="-DCMAKE_PREFIX_PATH=${PYTHON_DIR} -DPYTHON_LIBRARY=${PYTHON_LIBRARY} -DPYTHON_INCLUDE_DIR=${PYTHON_INCLUDE_DIR} "
  98. #config build type to RelWithDebInfo to enable MGB_ENABLE_DEBUG_UTIL etc
  99. export EXTRA_CMAKE_ARGS="${EXTRA_CMAKE_ARGS} -DCMAKE_BUILD_TYPE=RelWithDebInfo "
  100. #call build and install
  101. #FIXME: cmake do not triger update python config, after
  102. #change PYTHON_LIBRARY and PYTHON_INCLUDE_DIR, so add
  103. #-r to remove build cache after a new ver build, which
  104. #will be more slow build than without -r
  105. if [ ${BUILD_IMPERATIVE} = "ON" ]; then
  106. echo "build whl with IMPERATIVE python rt"
  107. ${SRC_DIR}/scripts/cmake-build/host_build.sh -t -n -r
  108. else
  109. echo "build whl with legacy python rt"
  110. ${SRC_DIR}/scripts/cmake-build/host_build.sh -t -r
  111. fi
  112. #call setup.py
  113. BUILD_DIR=${SRC_DIR}/build_dir/host/MGE_WITH_CUDA_OFF/MGE_INFERENCE_ONLY_OFF/Release/build/
  114. cd ${BUILD_DIR}
  115. if [ -d "staging" ]; then
  116. echo "remove old build cache file"
  117. rm -rf staging
  118. fi
  119. mkdir -p staging
  120. if [ ${BUILD_IMPERATIVE} = "ON" ]; then
  121. echo "build whl with IMPERATIVE python rt"
  122. cp -a imperative/python/{megengine,setup.py,requires.txt,requires-style.txt,requires-test.txt} staging/
  123. cd ${BUILD_DIR}/staging/megengine/core
  124. rt_file=`ls _imperative_rt.*.so`
  125. echo "rt file is: ${rt_file}"
  126. if [[ -z ${rt_file} ]]
  127. then
  128. echo "ERR: can not find valid rt file"
  129. exit -1
  130. fi
  131. llvm-strip -s ${rt_file}
  132. mv ${rt_file} _imperative_rt.so
  133. echo "check so valid or not..."
  134. otool_out=`otool -L _imperative_rt.so`
  135. if [[ "${otool_out}" =~ "ython" ]]; then
  136. echo "ERR: invalid _imperative_rt.so which depend on python lib, detail: log"
  137. echo ${otool_out}
  138. exit -1
  139. else
  140. echo "valid..."
  141. fi
  142. else
  143. echo "build whl with legacy python rt"
  144. cp -a python_module/{megengine,setup.py,requires.txt,requires-style.txt,requires-test.txt} staging/
  145. cd ${BUILD_DIR}/staging/megengine/_internal
  146. #FIXME: set lib suffix to dylib may be better, BUT we find after distutils.file_util.copy_file
  147. #will change to .so at macos even we set suffix to dylib, at the same time, macos also support .so
  148. echo "check so valid or not..."
  149. llvm-strip -s _mgb.so
  150. otool_out=`otool -L _mgb.so`
  151. if [[ "${otool_out}" =~ "ython" ]]; then
  152. echo "ERR: invalid _mgb.so which depend on python lib, detail: log"
  153. echo ${otool_out}
  154. exit -1
  155. else
  156. echo "valid..."
  157. fi
  158. fi
  159. cd ${BUILD_DIR}/staging
  160. ${PYTHON_DIR}/bin/python3 setup.py bdist_wheel
  161. cd ${BUILD_DIR}/staging/dist/
  162. org_whl_name=`ls Meg*.whl`
  163. index=`awk -v a="${org_whl_name}" -v b="-macosx" 'BEGIN{print index(a,b)}'`
  164. #compat for osx version from 10.5(Leopard)
  165. #FIXME: same no need at -macosx-version-min=10.5 for build so
  166. compat_whl_name=`echo ${org_whl_name} |cut -b -$index`macosx_10_5_x86_64.whl
  167. echo "org whl name: ${org_whl_name}"
  168. echo "comapt whl name: ${compat_whl_name}"
  169. cp ${BUILD_DIR}/staging/dist/Meg*.whl ${MACOS_WHL_HOME}/${compat_whl_name}
  170. cd ${SRC_DIR}
  171. echo ""
  172. echo "##############################################################################################"
  173. echo "macos whl package location: ${MACOS_WHL_HOME}"
  174. ls ${MACOS_WHL_HOME}
  175. echo "##############################################################################################"
  176. done
  177. }
  178. function third_party_prepare() {
  179. echo "init third_party..."
  180. ${SRC_DIR}/third_party/prepare.sh
  181. if [[ -z ${ALREADY_INSTALL_MKL} ]]
  182. then
  183. echo "init third_party..."
  184. ${SRC_DIR}/third_party/install-mkl.sh
  185. else
  186. echo "skip init mkl internal"
  187. fi
  188. }
  189. ######################
  190. third_party_prepare
  191. do_build

MegEngine 安装包中集成了使用 GPU 运行代码所需的 CUDA 环境,不用区分 CPU 和 GPU 版。 如果想要运行 GPU 程序,请确保机器本身配有 GPU 硬件设备并安装好驱动。 如果你想体验在云端 GPU 算力平台进行深度学习开发的感觉,欢迎访问 MegStudio 平台