#!/bin/bash

echo "### make sure 3rdparty already there."
# git submodule update --init
# git pull --recurse-submodules

echo "### remove old files..."
rm -rf aiter/jit/aiter_.so
rm -rf aiter/jit/build
rm -rf aiter/jit/*.so
rm -rf aiter/jit/jit/build
rm -rf build
rm -rf aiter.egg-info
rm -rf aiter_meta

echo "### uninstall aiter..."
pip uninstall aiter -y


echo "### start rebuild aiter..."
# 诊断卡住时建议：
# 1) AITER_PREBUILD_LOG_PROGRESS=1: 打印每个模块 START/DONE
# 2) AITER_PREBUILD_VERBOSE=1: 打开每个模块的详细编译输出（hipcc/ninja -v）
# 3）AITER_LOG_MORE=2: Python/JIT 流程、模块触发、参数和更高层日志
# 4) MAX_JOBS=1: 串行编译
# 5）AITER_REBUILD=1: 强制重新编译所有模块（默认只编译缺失的模块）
# 6) AITER_USE_SCM_VERSION=1: 版本号基于 'git describe --tags --long' 输出（默认为0）
#    AITER_USE_SCM_VERSION=0: 版本号使用 setup.py 中的 AITER_FIXED_VERSION，编译时可指定的环境变量；

# install mode:
PYTHONUNBUFFERED=1 GPU_ARCHS="gfx936;gfx938" PREBUILD_KERNELS=1 AITER_PREBUILD_LOG_PROGRESS=1 python setup.py bdist_wheel

# develop mode:
# GPU_ARCHS="gfx936;gfx938" python setup.py develop


echo "## How to run test cases"
echo "### 1. If you are in develop mode, just run: python op_tests/test_***.py"
echo "### 2. If you are in install mode:"
echo "       1) you can run: export PYTHONPATH=. to add current dir to PYTHONPATH, then run: python op_tests/test_***.py"
echo "       2) or you can run: python -m op_tests.test_***   # note that there is no '.py' suffix"
