name: prebuild

on:
  push:
    branches:
      - main
    paths:
      - "mmdeploy/version.py"

  workflow_dispatch:
    inputs:
      run:
        description: 'Manually start pre-build workflow'
        required: false
        type: boolean
        default: false

permissions: write-all

jobs:
  linux_build:
    if: inputs.run == true || ${{ github.event_name == 'push' }}
    runs-on: [self-hosted, linux-3090]
    container:
      image: openmmlab/mmdeploy:manylinux2014_x86_64-cuda11.3
      options: "--gpus=all --ipc=host"
      volumes:
        - /data2/actions-runner/prebuild:/__w/mmdeploy/prebuild
    steps:
      - name: Checkout repository
        uses: actions/checkout@v3
        with:
          submodules: recursive
      - name: Get mmdeploy version
        run: |
          export MMDEPLOY_VERSION=$(python3 -c "import sys; sys.path.append('mmdeploy');from version import __version__;print(__version__)")
          echo $MMDEPLOY_VERSION
          echo "MMDEPLOY_VERSION=$MMDEPLOY_VERSION"  >> $GITHUB_ENV
          echo "OUTPUT_DIR=/__w/mmdeploy/prebuild/$MMDEPLOY_VERSION" >> $GITHUB_ENV
      - name: Build MMDeploy
        run: |
          source activate mmdeploy-3.6
          pip install pyyaml packaging setuptools wheel
          mkdir pack; cd pack
          python ../tools/package_tools/generate_build_config.py --backend 'trt;ort' \
            --system linux --output config.yml --build-mmdeploy
          python ../tools/package_tools/mmdeploy_builder.py --config config.yml
      - name: Build sdk cpu backend
        run: |
          source activate mmdeploy-3.6
          cd pack
          python ../tools/package_tools/generate_build_config.py --backend 'ort' \
            --system linux --output config.yml --device cpu --build-sdk --build-sdk-monolithic \
            --build-sdk-python --sdk-dynamic-net
          python ../tools/package_tools/mmdeploy_builder.py --config config.yml
      - name: Build sdk cuda backend
        run: |
          source activate mmdeploy-3.6
          cd pack
          python ../tools/package_tools/generate_build_config.py --backend 'ort;trt' \
            --system linux --output config.yml --device cuda --build-sdk --build-sdk-monolithic \
            --build-sdk-python --sdk-dynamic-net --onnxruntime-dir=$ONNXRUNTIME_GPU_DIR
          python ../tools/package_tools/mmdeploy_builder.py --config config.yml
      - name: Zip mmdeploy sdk
        run: |
          cd pack/sdk
          for folder in *
          do
            tar czf $folder.tar.gz $folder
          done
      - name: Move artifact
        run: |
          mkdir -p $OUTPUT_DIR/mmdeploy  $OUTPUT_DIR/mmdeploy_runtime $OUTPUT_DIR/sdk
          chmod -R 777 $OUTPUT_DIR
          mv -vf pack/mmdeploy/* $OUTPUT_DIR/mmdeploy/
          mv -vf pack/mmdeploy_runtime/* $OUTPUT_DIR/mmdeploy_runtime/
          mv -vf pack/sdk/* $OUTPUT_DIR/sdk/

  linux_build_cxx11abi:
    if: inputs.run == true || ${{ github.event_name == 'push' }}
    runs-on: [self-hosted, linux-3090]
    container:
      image: openmmlab/mmdeploy:build-ubuntu16.04-cuda11.3
      options: "--gpus=all --ipc=host"
      volumes:
        - /data2/actions-runner/prebuild:/__w/mmdeploy/prebuild
    steps:
      - name: Checkout repository
        uses: actions/checkout@v3
        with:
          submodules: recursive
      - name: Get mmdeploy version
        run: |
          export MMDEPLOY_VERSION=$(python3 -c "import sys; sys.path.append('mmdeploy');from version import __version__;print(__version__)")
          echo $MMDEPLOY_VERSION
          echo "MMDEPLOY_VERSION=$MMDEPLOY_VERSION"  >> $GITHUB_ENV
          echo "OUTPUT_DIR=/__w/mmdeploy/prebuild/$MMDEPLOY_VERSION" >> $GITHUB_ENV
      - name: Build sdk cpu backend
        run: |
          mkdir pack; cd pack
          python ../tools/package_tools/generate_build_config.py --backend 'ort' \
            --system linux --output config.yml --device cpu --build-sdk --build-sdk-monolithic \
            --sdk-dynamic-net --cxx11abi
          python ../tools/package_tools/mmdeploy_builder.py --config config.yml
      - name: Build sdk cuda backend
        run: |
          cd pack
          python ../tools/package_tools/generate_build_config.py --backend 'ort;trt' \
            --system linux --output config.yml --device cuda --build-sdk --build-sdk-monolithic \
            --sdk-dynamic-net --cxx11abi --onnxruntime-dir=$ONNXRUNTIME_GPU_DIR --cudnn-dir /usr
          python ../tools/package_tools/mmdeploy_builder.py --config config.yml
      - name: Zip mmdeploy sdk
        run: |
          cd pack/sdk
          for folder in *
          do
            tar czf $folder.tar.gz $folder
          done
      - name: Move artifact
        run: |
          mkdir -p $OUTPUT_DIR/sdk
          chmod -R 777 $OUTPUT_DIR
          mv -vf pack/sdk/* $OUTPUT_DIR/sdk/

  linux_test:
    if: inputs.run == true || ${{ github.event_name == 'push' }}
    runs-on: [self-hosted, linux-3090]
    needs:
      - linux_build
      - linux_build_cxx11abi
    container:
      image: openmmlab/mmdeploy:ubuntu20.04-cuda11.3
      options: "--gpus=all --ipc=host"
      volumes:
        - /data2/actions-runner/prebuild:/__w/mmdeploy/prebuild
        - /data2/actions-runner/testmodel:/__w/mmdeploy/testmodel
    steps:
      - name: Checkout repository
        uses: actions/checkout@v3
      - name: Get mmdeploy version
        run: |
          export MMDEPLOY_VERSION=$(python3 -c "import sys; sys.path.append('mmdeploy');from version import __version__;print(__version__)")
          echo $MMDEPLOY_VERSION
          echo "MMDEPLOY_VERSION=$MMDEPLOY_VERSION"  >> $GITHUB_ENV
          echo "OUTPUT_DIR=/__w/mmdeploy/prebuild/$MMDEPLOY_VERSION" >> $GITHUB_ENV
      - name: Test python
        run: |
          cd $OUTPUT_DIR
          bash $GITHUB_WORKSPACE/tools/package_tools/test/test_sdk_python.sh
      - name: Test c/cpp
        run: |
          cd $OUTPUT_DIR
          bash $GITHUB_WORKSPACE/tools/package_tools/test/test_sdk.sh

  windows_build:
    if: inputs.run == true || ${{ github.event_name == 'push' }}
    runs-on: [self-hosted, win10-3080]
    env:
      PREBUILD_DIR: D:\Runners\prebuild
    steps:
      - name: Checkout repository
        uses: actions/checkout@v3
        with:
          submodules: recursive
      - name: Get mmdeploy version
        run: |
          conda activate mmdeploy-3.8
          $env:MMDEPLOY_VERSION=(python -c "import sys; sys.path.append('mmdeploy');from version import __version__;print(__version__)")
          echo $env:MMDEPLOY_VERSION
          echo "MMDEPLOY_VERSION=$env:MMDEPLOY_VERSION"  >> $env:GITHUB_ENV
          echo "OUTPUT_DIR=$env:PREBUILD_DIR\$env:MMDEPLOY_VERSION" >> $env:GITHUB_ENV
      - name: Build MMDeploy
        run: |
          . D:\DEPS\cienv\prebuild_gpu_env.ps1
          conda activate mmdeploy-3.6
          New-Item -Path pack -ItemType Directory -Force
          cd pack
          python ../tools/package_tools/generate_build_config.py --backend 'trt;ort' `
            --system windows --output config.yml --build-mmdeploy
          python ../tools/package_tools/mmdeploy_builder.py --config config.yml
      - name: Build sdk cpu backend
        run: |
          . D:\DEPS\cienv\prebuild_cpu_env.ps1
          conda activate mmdeploy-3.6
          cd pack
          python ../tools/package_tools/generate_build_config.py --backend 'ort' `
            --system windows --output config.yml --device cpu --build-sdk --build-sdk-monolithic `
            --build-sdk-python --sdk-dynamic-net
          python ../tools/package_tools/mmdeploy_builder.py --config config.yml
      - name: Build sdk cuda backend
        run: |
          . D:\DEPS\cienv\prebuild_gpu_env.ps1
          conda activate mmdeploy-3.6
          cd pack
          python ../tools/package_tools/generate_build_config.py --backend 'ort;trt' `
            --system windows --output config.yml --device cuda --build-sdk --build-sdk-monolithic `
            --build-sdk-python --sdk-dynamic-net
          python ../tools/package_tools/mmdeploy_builder.py --config config.yml
      - name: Zip mmdeploy sdk
        run: |
          cd pack/sdk
          $folders = $(ls).Name
          foreach ($folder in $folders) {
            Compress-Archive -Path $folder -DestinationPath "$folder.zip"
          }
      - name: Move artifact
        run: |
          New-Item "$env:OUTPUT_DIR\mmdeploy" -ItemType Directory -Force
          New-Item "$env:OUTPUT_DIR\mmdeploy_runtime" -ItemType Directory -Force
          New-Item "$env:OUTPUT_DIR\sdk" -ItemType Directory -Force
          Move-Item pack/mmdeploy/* -Force "$env:OUTPUT_DIR\mmdeploy\"
          Move-Item pack/mmdeploy_runtime/* -Force "$env:OUTPUT_DIR\mmdeploy_runtime\"
          Move-Item pack/sdk/* -Force "$env:OUTPUT_DIR\sdk\"

  windows_test:
    if: inputs.run == true || ${{ github.event_name == 'push' }}
    runs-on: [self-hosted, win10-3080]
    needs: windows_build
    env:
      PREBUILD_DIR: D:\Runners\prebuild
    steps:
      - name: Checkout repository
        uses: actions/checkout@v3
      - name: Get mmdeploy version
        run: |
          conda activate mmdeploy-3.8
          $env:MMDEPLOY_VERSION=(python -c "import sys; sys.path.append('mmdeploy');from version import __version__;print(__version__)")
          echo $env:MMDEPLOY_VERSION
          echo "MMDEPLOY_VERSION=$env:MMDEPLOY_VERSION"  >> $env:GITHUB_ENV
          echo "OUTPUT_DIR=$env:PREBUILD_DIR\$env:MMDEPLOY_VERSION" >> $env:GITHUB_ENV
      - name: Test python
        run: |
          cd "$env:OUTPUT_DIR"
          . D:\DEPS\cienv\prebuild_cpu_env.ps1
          conda activate ci-test
          & "$env:GITHUB_WORKSPACE/tools/package_tools/test/test_sdk_python.ps1"
      - name: Test c/cpp
        run: |
          cd "$env:OUTPUT_DIR"
          . D:\DEPS\cienv\prebuild_cpu_env.ps1
          & "$env:GITHUB_WORKSPACE/tools/package_tools/test/test_sdk.ps1"
