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 6.8 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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. MEGENGINE_LIB="${SRC_DIR}/build_dir/host/MGE_WITH_CUDA_OFF/MGE_INFERENCE_ONLY_OFF/Release/build/src/libmegengine_export.dylib"
  77. function depend_real_copy() {
  78. REAL_DST=$1
  79. echo "real copy lib to $1"
  80. cp "${MEGENGINE_LIB}" ${REAL_DST}
  81. }
  82. function do_build() {
  83. for ver in ${ALL_PYTHON}
  84. do
  85. #config
  86. config_python_env ${ver}
  87. #check env
  88. if [ ! -f "$PYTHON_LIBRARY" ]; then
  89. echo "ERR: can not find $PYTHON_LIBRARY , Invalid python package"
  90. err_env
  91. fi
  92. if [ ! -d "$PYTHON_INCLUDE_DIR" ]; then
  93. echo "ERR: can not find $PYTHON_INCLUDE_DIR , Invalid python package"
  94. err_env
  95. fi
  96. echo "PYTHON_LIBRARY: ${PYTHON_LIBRARY}"
  97. echo "PYTHON_INCLUDE_DIR: ${PYTHON_INCLUDE_DIR}"
  98. #append cmake args for config python
  99. export EXTRA_CMAKE_ARGS="-DCMAKE_PREFIX_PATH=${PYTHON_DIR} -DPYTHON_LIBRARY=${PYTHON_LIBRARY} -DPYTHON_INCLUDE_DIR=${PYTHON_INCLUDE_DIR} "
  100. #config build type to RelWithDebInfo to enable MGB_ENABLE_DEBUG_UTIL etc
  101. export EXTRA_CMAKE_ARGS="${EXTRA_CMAKE_ARGS} -DCMAKE_BUILD_TYPE=RelWithDebInfo "
  102. #we use std::visit in src, so set osx version minimum to 10.14, but 10.14 have objdump
  103. #issue, so we now config to 10.15, whl name to 10.14
  104. #TODO: can set to 10.12 after remove use std::visit
  105. export EXTRA_CMAKE_ARGS="${EXTRA_CMAKE_ARGS} -DCMAKE_OSX_DEPLOYMENT_TARGET=10.15 "
  106. #call build and install
  107. #FIXME: cmake do not triger update python config, after
  108. #change PYTHON_LIBRARY and PYTHON_INCLUDE_DIR, so add
  109. #-r to remove build cache after a new ver build, which
  110. #will be more slow build than without -r
  111. echo "build whl with legacy python rt"
  112. ${SRC_DIR}/scripts/cmake-build/host_build.sh -t -r
  113. #call setup.py
  114. BUILD_DIR=${SRC_DIR}/build_dir/host/MGE_WITH_CUDA_OFF/MGE_INFERENCE_ONLY_OFF/Release/build/
  115. cd ${BUILD_DIR}
  116. rm -rf staging
  117. mkdir -p staging
  118. cp -a imperative/python/{megengine,setup.py,requires.txt,requires-style.txt,requires-test.txt} staging/
  119. cd ${BUILD_DIR}/staging/megengine/core
  120. rt_file=`ls _imperative_rt.*.so`
  121. echo "rt file is: ${rt_file}"
  122. if [[ -z ${rt_file} ]]
  123. then
  124. echo "ERR: can not find valid rt file"
  125. exit -1
  126. fi
  127. llvm-strip -s ${rt_file}
  128. mv ${rt_file} _imperative_rt.so
  129. echo "check so valid or not..."
  130. otool_out=`otool -L _imperative_rt.so`
  131. if [[ "${otool_out}" =~ "ython" ]]; then
  132. echo "ERR: invalid _imperative_rt.so which depend on python lib, detail: log"
  133. echo ${otool_out}
  134. exit -1
  135. else
  136. echo "valid..."
  137. fi
  138. #handle dlopen path
  139. install_name_tool -change @rpath/libmegengine_export.dylib @loader_path/lib/libmegengine_export.dylib _imperative_rt.so
  140. #copy megbrain_export lib
  141. DEPEND_LIB=${BUILD_DIR}/staging/megengine/core/lib/
  142. rm -rf ${DEPEND_LIB}
  143. mkdir ${DEPEND_LIB}
  144. depend_real_copy ${DEPEND_LIB}
  145. cd ${BUILD_DIR}/staging
  146. ${PYTHON_DIR}/bin/python3 setup.py bdist_wheel
  147. cd ${BUILD_DIR}/staging/dist/
  148. org_whl_name=`ls Meg*.whl`
  149. index=`awk -v a="${org_whl_name}" -v b="-macosx" 'BEGIN{print index(a,b)}'`
  150. compat_whl_name=`echo ${org_whl_name} |cut -b -$index`macosx_10_14_x86_64.whl
  151. echo "org whl name: ${org_whl_name}"
  152. echo "comapt whl name: ${compat_whl_name}"
  153. cp ${BUILD_DIR}/staging/dist/Meg*.whl ${MACOS_WHL_HOME}/${compat_whl_name}
  154. cd ${SRC_DIR}
  155. echo ""
  156. echo "##############################################################################################"
  157. echo "macos whl package location: ${MACOS_WHL_HOME}"
  158. ls ${MACOS_WHL_HOME}
  159. echo "##############################################################################################"
  160. done
  161. }
  162. function third_party_prepare() {
  163. echo "init third_party..."
  164. ${SRC_DIR}/third_party/prepare.sh
  165. if [[ -z ${ALREADY_INSTALL_MKL} ]]
  166. then
  167. echo "init third_party..."
  168. ${SRC_DIR}/third_party/install-mkl.sh
  169. else
  170. echo "skip init mkl internal"
  171. fi
  172. }
  173. ######################
  174. third_party_prepare
  175. do_build

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