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.

windows_build_whl.sh 8.8 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. #!/bin/bash -e
  2. NT=$(echo `uname` | grep "NT")
  3. echo $NT
  4. if [ -z "$NT" ];then
  5. echo "only run at windows bash env"
  6. echo "pls consider install bash-like tools, eg MSYS or git-cmd, etc"
  7. exit -1
  8. fi
  9. function err_env() {
  10. echo "check_env failed: pls refs ${SRC_DIR}/scripts/whl/BUILD_PYTHON_WHL_README.md to init env"
  11. exit -1
  12. }
  13. function append_path_env_and_check() {
  14. echo "export vs2019 install path"
  15. export VS_PATH=/c/Program\ Files\ \(x86\)/Microsoft\ Visual\ Studio/2019/Enterprise
  16. echo "export LLVM install path"
  17. export LLVM_PATH=/c/Program\ Files/LLVM_12_0_1
  18. }
  19. append_path_env_and_check
  20. SRC_DIR=$(READLINK -f "`dirname $0`/../../../")
  21. source ${SRC_DIR}/scripts/whl/utils/utils.sh
  22. ALL_PYTHON=${ALL_PYTHON}
  23. FULL_PYTHON_VER="3.6.8 3.7.7 3.8.3 3.9.4 3.10.1"
  24. if [[ -z ${ALL_PYTHON} ]]
  25. then
  26. ALL_PYTHON=${FULL_PYTHON_VER}
  27. else
  28. check_python_version_is_valid "${ALL_PYTHON}" "${FULL_PYTHON_VER}"
  29. fi
  30. PYTHON_DIR=
  31. PYTHON_LIBRARY=
  32. PYTHON_INCLUDE_DIR=
  33. WINDOWS_WHL_HOME=${SRC_DIR}/scripts/whl/windows/windows_whl_home
  34. if [ -e "${WINDOWS_WHL_HOME}" ]; then
  35. echo "remove old windows whl file"
  36. rm -rf ${WINDOWS_WHL_HOME}
  37. fi
  38. mkdir -p ${WINDOWS_WHL_HOME}
  39. function config_python_env() {
  40. PYTHON_DIR=/c/Users/${USER}/mge_whl_python_env/$1
  41. PYTHON_BIN=${PYTHON_DIR}
  42. if [ ! -f "${PYTHON_BIN}/python3.exe" ]; then
  43. echo "ERR: can not find $PYTHON_BIN , Invalid python package"
  44. echo "now support list: ${FULL_PYTHON_VER}"
  45. err_env
  46. else
  47. echo "put ${PYTHON_BIN}/python3.exe to env..."
  48. export PATH=${PYTHON_BIN}:$PATH
  49. which python3
  50. fi
  51. echo ${ver}
  52. PYTHON_LIBRARY=${PYTHON_DIR}/libs/python3.lib
  53. PYTHON_INCLUDE_DIR=${PYTHON_DIR}/include
  54. }
  55. BUILD_WHL_CPU_ONLY=${BUILD_WHL_CPU_ONLY}
  56. if [[ -z ${BUILD_WHL_CPU_ONLY} ]]
  57. then
  58. BUILD_WHL_CPU_ONLY="OFF"
  59. fi
  60. # config NVIDIA libs
  61. TRT_LIB="/c/Program Files/NVIDIA GPU Computing Toolkit/TensorRT-6.0.1.5/lib/nvinfer.dll"
  62. TRT_PLUGIN_LIB="/c/Program Files/NVIDIA GPU Computing Toolkit/TensorRT-6.0.1.5/lib/nvinfer_plugin.dll"
  63. CUDNN_LIB="/c/Program Files/NVIDIA GPU Computing Toolkit/cudnn-10.1-windows10-x64-v7.6.5.32/cuda/bin/cudnn64_7.dll"
  64. CUSOLVER_LIB="/c/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v10.1/bin/cusolver64_10.dll"
  65. CUBLAS_LIB="/c/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v10.1/bin/cublas64_10.dll"
  66. CURAND_LIB="/c/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v10.1/bin/curand64_10.dll"
  67. CUBLASLT_LIB="/c/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v10.1/bin/cublasLt64_10.dll"
  68. CUDART_LIB="/c/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v10.1/bin/cudart64_101.dll"
  69. MGE_EXPORT_DLL="${SRC_DIR}/build_dir/host/build/src/megengine_shared.dll"
  70. MGE_EXPORT_LIB="${SRC_DIR}/build_dir/host/build/src/megengine_shared.lib"
  71. function depend_real_copy() {
  72. REAL_DST=$1
  73. echo "real copy lib to $1"
  74. cp "${MGE_EXPORT_DLL}" ${REAL_DST}
  75. cp "${MGE_EXPORT_LIB}" ${REAL_DST}
  76. if [ ${BUILD_WHL_CPU_ONLY} = "OFF" ]; then
  77. echo "copy nvidia lib...."
  78. cp "${TRT_LIB}" ${REAL_DST}
  79. cp "${TRT_PLUGIN_LIB}" ${REAL_DST}
  80. cp "${CUDNN_LIB}" ${REAL_DST}
  81. cp "${CUSOLVER_LIB}" ${REAL_DST}
  82. cp "${CUBLAS_LIB}" ${REAL_DST}
  83. cp "${CURAND_LIB}" ${REAL_DST}
  84. cp "${CUBLASLT_LIB}" ${REAL_DST}
  85. cp "${CUDART_LIB}" ${REAL_DST}
  86. fi
  87. }
  88. function copy_more_dll() {
  89. # for python whl real use
  90. echo "config BUILD_IMPERATIVE core lib dir"
  91. CP_WHL_DST_IMP=${BUILD_DIR}/staging/megengine/core/lib
  92. rm -rf ${CP_WHL_DST_IMP}
  93. mkdir ${CP_WHL_DST_IMP}
  94. depend_real_copy ${CP_WHL_DST_IMP}
  95. }
  96. function lite_copy_more_dll() {
  97. if [ ${IN_CI} = "true" ]; then
  98. echo "copy lib for lite for ci test"
  99. IMP_TEST_DST=${SRC_DIR}/build_dir/host/build/lite/test/
  100. depend_real_copy ${IMP_TEST_DST}
  101. rm "${IMP_TEST_DST}/megengine_shared.dll"
  102. fi
  103. }
  104. BUILD_DIR=${SRC_DIR}/build_dir/host/build/
  105. # here we just treat cu file should not in the increment build file list
  106. INCREMENT_KEY_WORDS=".cu.obj is dirty"
  107. IS_IN_FIRST_LOOP=TRUE
  108. ORG_EXTRA_CMAKE_FLAG=${EXTRA_CMAKE_FLAG}
  109. function do_build() {
  110. for ver in ${ALL_PYTHON}
  111. do
  112. # we want run a full clean build at the first loop
  113. if [ ${IS_IN_FIRST_LOOP} = "TRUE" ]; then
  114. # TODO: may all cmake issue can be resolved after rm CMakeCache?
  115. # if YES, remove this to use old cache and speed up CI
  116. echo "warning: remove old build_dir for the first loop"
  117. rm -rf ${BUILD_DIR}
  118. fi
  119. #config python3
  120. config_python_env ${ver}
  121. #check env
  122. if [ ! -f "$PYTHON_LIBRARY" ]; then
  123. echo "ERR: can not find $PYTHON_LIBRARY , Invalid python package"
  124. err_env
  125. fi
  126. if [ ! -d "$PYTHON_INCLUDE_DIR" ]; then
  127. echo "ERR: can not find $PYTHON_INCLUDE_DIR , Invalid python package"
  128. err_env
  129. fi
  130. echo "PYTHON_LIBRARY: ${PYTHON_LIBRARY}"
  131. echo "PYTHON_INCLUDE_DIR: ${PYTHON_INCLUDE_DIR}"
  132. #config build type to RelWithDebInfo to enable MGB_ENABLE_DEBUG_UTIL etc
  133. export EXTRA_CMAKE_ARGS="${ORG_EXTRA_CMAKE_FLAG} -DCMAKE_BUILD_TYPE=RelWithDebInfo "
  134. export EXTRA_CMAKE_ARGS="${EXTRA_CMAKE_ARGS} -DMGE_WITH_CUSTOM_OP=ON"
  135. #call build and install
  136. HOST_BUILD_ARGS=" -t -s"
  137. if [ ${BUILD_WHL_CPU_ONLY} = "OFF" ]; then
  138. echo "build windows whl with cuda"
  139. HOST_BUILD_ARGS="${HOST_BUILD_ARGS} -c "
  140. else
  141. echo "build windows whl with cpu only"
  142. fi
  143. if [ -d "${BUILD_DIR}" ]; then
  144. # insure rm have args
  145. touch ${BUILD_DIR}/empty.pyd
  146. touch ${BUILD_DIR}/CMakeCache.txt
  147. /usr/bin/find ${BUILD_DIR} -name "*.pyd" | xargs rm
  148. # ninja/cmake on windows will handle error if just export
  149. # PYTHON_LIBRARY/PYTHON_INCLUDE_DIR/PYTHON_EXECUTABLE
  150. # But after put python3.exe to HEAD of PATH by config_python_env
  151. # and force remove CMakeCache.txt, ninja/cmake will auto update
  152. # PYTHON_LIBRARY/PYTHON_INCLUDE_DIR/PYTHON_EXECUTABLE
  153. /usr/bin/find ${BUILD_DIR} -name CMakeCache.txt | xargs rm
  154. fi
  155. echo "host_build.sh HOST_BUILD_ARGS: ${HOST_BUILD_ARGS}"
  156. # call ninja dry run and check increment is invalid or not
  157. if [ ${IS_IN_FIRST_LOOP} = "FALSE" ]; then
  158. ninja_dry_run_and_check_increment "${SRC_DIR}/scripts/cmake-build/host_build.sh" "${HOST_BUILD_ARGS}" "${INCREMENT_KEY_WORDS}"
  159. fi
  160. #call real build
  161. ${SRC_DIR}/scripts/cmake-build/host_build.sh ${HOST_BUILD_ARGS}
  162. # check python api call setup.py
  163. cd ${BUILD_DIR}
  164. check_build_ninja_python_api ${ver}
  165. rm -rf staging
  166. mkdir -p staging
  167. cp -a imperative/python/{megengine,setup.py,requires.txt,requires-style.txt,requires-test.txt} staging/
  168. cp -a ${SRC_DIR}/src/custom/include/megbrain staging/megengine/core/include/
  169. cd ${BUILD_DIR}/staging/megengine/core
  170. rt_file=`ls _imperative_rt.*.pyd`
  171. echo "rt file is: ${rt_file}"
  172. if [[ -z ${rt_file} ]]
  173. then
  174. echo "ERR: can not find valid rt file"
  175. exit -1
  176. fi
  177. mv ${rt_file} _imperative_rt.pyd
  178. copy_more_dll
  179. # handle megenginelite
  180. cd ${BUILD_DIR}
  181. mkdir -p staging/megenginelite
  182. cp ${SRC_DIR}/lite/pylite/megenginelite/* staging/megenginelite/
  183. LITE_CORE_LIB_DIR=${BUILD_DIR}/staging/megenginelite/libs/
  184. mkdir -p ${LITE_CORE_LIB_DIR}
  185. cd ${LITE_CORE_LIB_DIR}
  186. cp ${BUILD_DIR}/lite/lite_shared_whl.dll liblite_shared_whl.pyd
  187. lite_copy_more_dll
  188. cd ${BUILD_DIR}/staging
  189. echo "call setup.py now"
  190. ${PYTHON_DIR}/python3 setup.py bdist_wheel
  191. cp ${BUILD_DIR}/staging/dist/Meg*.whl ${WINDOWS_WHL_HOME}/
  192. echo ""
  193. echo "##############################################################################################"
  194. echo "windows whl package location: ${WINDOWS_WHL_HOME}"
  195. ls ${WINDOWS_WHL_HOME}
  196. echo "##############################################################################################"
  197. IS_IN_FIRST_LOOP=FALSE
  198. done
  199. }
  200. function third_party_prepare() {
  201. echo "init third_party..."
  202. ${SRC_DIR}/third_party/prepare.sh
  203. if [[ -z ${ALREADY_INSTALL_MKL} ]]
  204. then
  205. echo "init third_party..."
  206. ${SRC_DIR}/third_party/install-mkl.sh
  207. else
  208. echo "skip init mkl internal"
  209. fi
  210. }
  211. ######################
  212. export ALREADY_CONFIG_PYTHON_VER="yes"
  213. if [ ${BUILD_WHL_CPU_ONLY} = "OFF" ]; then
  214. export SDK_NAME="cu101"
  215. else
  216. export SDK_NAME="cpu"
  217. fi
  218. third_party_prepare
  219. do_build