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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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. #config a dir to trick cmake find a null pythonlib
  59. PYTHON_LIBRARY=${PYTHON_DIR}lib/
  60. if [ "$1" = "3.5.9" ]; then
  61. PYTHON_INCLUDE_DIR=${PYTHON_DIR}include/python3.5m
  62. elif [ "$1" = "3.6.10" ]; then
  63. PYTHON_INCLUDE_DIR=${PYTHON_DIR}include/python3.6m
  64. elif [ "$1" = "3.7.7" ]; then
  65. PYTHON_INCLUDE_DIR=${PYTHON_DIR}include/python3.7m
  66. elif [ "$1" = "3.8.3" ]; then
  67. PYTHON_INCLUDE_DIR=${PYTHON_DIR}include/python3.8
  68. else
  69. echo "ERR: DO NOT SUPPORT PYTHON VERSION"
  70. echo "now support list: ${FULL_PYTHON_VER}"
  71. exit -1
  72. fi
  73. }
  74. function do_build() {
  75. for ver in ${ALL_PYTHON}
  76. do
  77. #config
  78. config_python_env ${ver}
  79. #check env
  80. if [ ! -d "$PYTHON_LIBRARY" ]; then
  81. echo "ERR: can not find $PYTHON_LIBRARY , Invalid python package"
  82. err_env
  83. fi
  84. if [ ! -d "$PYTHON_INCLUDE_DIR" ]; then
  85. echo "ERR: can not find $PYTHON_INCLUDE_DIR , Invalid python package"
  86. err_env
  87. fi
  88. echo "PYTHON_LIBRARY: ${PYTHON_LIBRARY}"
  89. echo "PYTHON_INCLUDE_DIR: ${PYTHON_INCLUDE_DIR}"
  90. #append cmake args for config python
  91. export EXTRA_CMAKE_ARGS="-DCMAKE_PREFIX_PATH=${PYTHON_DIR} -DPYTHON_LIBRARY=${PYTHON_LIBRARY} -DPYTHON_INCLUDE_DIR=${PYTHON_INCLUDE_DIR} "
  92. #config build type to RelWithDebInfo to enable MGB_ENABLE_DEBUG_UTIL etc
  93. export EXTRA_CMAKE_ARGS=${EXTRA_CMAKE_ARGS}" -DCMAKE_BUILD_TYPE=RelWithDebInfo "
  94. #call build and install
  95. #FIXME: cmake do not triger update python config, after
  96. #change PYTHON_LIBRARY and PYTHON_INCLUDE_DIR, so add
  97. #-r to remove build cache after a new ver build, which
  98. #will be more slow build than without -r
  99. ${SRC_DIR}/scripts/cmake-build/host_build.sh -t -r
  100. #call setup.py
  101. BUILD_DIR=${SRC_DIR}/build_dir/host/MGE_WITH_CUDA_OFF/MGE_INFERENCE_ONLY_OFF/Release/build/
  102. cd ${BUILD_DIR}
  103. if [ -d "staging" ]; then
  104. echo "remove old build cache file"
  105. rm -rf staging
  106. fi
  107. mkdir -p staging
  108. cp -a python_module/{megengine,setup.py,requires.txt,requires-style.txt,requires-test.txt} staging/
  109. cd ${BUILD_DIR}/staging/megengine/_internal
  110. #FIXME: set lib suffix to dylib may be better, BUT we find after distutils.file_util.copy_file
  111. #will change to .so at macos even we set suffix to dylib, at the same time, macos also support .so
  112. llvm-strip -s _mgb.so
  113. cd ${BUILD_DIR}/staging
  114. ${PYTHON_DIR}/bin/python3 setup.py bdist_wheel
  115. cd ${BUILD_DIR}/staging/dist/
  116. org_whl_name=`ls Meg*.whl`
  117. index=`awk -v a="${org_whl_name}" -v b="-macosx" 'BEGIN{print index(a,b)}'`
  118. #compat for osx version from 10.5(Leopard)
  119. #FIXME: same no need at -macosx-version-min=10.5 for build so
  120. compat_whl_name=`echo ${org_whl_name} |cut -b -$index`macosx_10_5_x86_64.whl
  121. echo "org whl name: ${org_whl_name}"
  122. echo "comapt whl name: ${compat_whl_name}"
  123. cp ${BUILD_DIR}/staging/dist/Meg*.whl ${MACOS_WHL_HOME}/${compat_whl_name}
  124. cd ${SRC_DIR}
  125. echo ""
  126. echo "##############################################################################################"
  127. echo "macos whl package location: ${MACOS_WHL_HOME}"
  128. ls ${MACOS_WHL_HOME}
  129. echo "##############################################################################################"
  130. done
  131. }
  132. function third_party_prepare() {
  133. echo "init third_party..."
  134. ${SRC_DIR}/third_party/prepare.sh
  135. if [[ -z ${ALREADY_INSTALL_MKL} ]]
  136. then
  137. echo "init third_party..."
  138. ${SRC_DIR}/third_party/install-mkl.sh
  139. else
  140. echo "skip init mkl internal"
  141. fi
  142. }
  143. ######################
  144. third_party_prepare
  145. do_build

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