name: build

on: [push, pull_request]

env:
  MMCV_WITH_OPS: 1

jobs:
  lint:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      - name: Set up Python 3.7
        uses: actions/setup-python@v2
        with:
          python-version: 3.7
      - name: Install pre-commit hook
        run: |
          pip install pre-commit
          pre-commit install
      - name: Linting
        run: pre-commit run --all-files
      - name: Format c/cuda codes with clang-format
        uses: DoozyX/clang-format-lint-action@v0.11
        with:
          source: mmcv/ops/csrc
          extensions: h,c,cpp,hpp,cu,cuh
          style: google

  build_without_torch:
    runs-on: ubuntu-latest
    strategy:
      matrix:
        python-version: [3.7]

    steps:
      - uses: actions/checkout@v2
      - name: Set up Python ${{ matrix.python-version }}
        uses: actions/setup-python@v2
        with:
          python-version: ${{ matrix.python-version }}
      - name: Install system dependencies
        run: sudo apt-get update && sudo apt-get install -y ffmpeg libturbojpeg
      - name: Install unittest dependencies
        run: pip install -r requirements/test.txt
      - name: Build and install
        run: rm -rf .eggs && pip install -e .
      - name: Run unittests and generate coverage report
        run: |
          pytest tests/ --ignore=tests/test_runner --ignore=tests/test_optimizer.py --ignore=tests/test_cnn --ignore=tests/test_parallel.py --ignore=tests/test_ops --ignore=tests/test_load_model_zoo.py --ignore=tests/test_utils/test_logging.py --ignore=tests/test_image/test_io.py --ignore=tests/test_utils/test_registry.py

  build_without_ops:
    runs-on: ubuntu-latest
    env:
      MMCV_WITH_OPS: 0
    strategy:
      matrix:
        python-version: [3.7]
        torch: [1.7.0]
        include:
          - torch: 1.7.0
            torchvision: 0.8.1
    steps:
      - uses: actions/checkout@v2
      - name: Set up Python ${{ matrix.python-version }}
        uses: actions/setup-python@v2
        with:
          python-version: ${{ matrix.python-version }}
      - name: Install system dependencies
        run: sudo apt-get update && sudo apt-get install -y ffmpeg libturbojpeg
      - name: Install PyTorch
        run: pip install torch==${{matrix.torch}}+cpu torchvision==${{matrix.torchvision}}+cpu -f https://download.pytorch.org/whl/torch_stable.html
      - name: Install unittest dependencies
        run: pip install -r requirements/test.txt
      - name: Build and install
        run: rm -rf .eggs && pip install -e .
      - name: Run unittests
        run: pytest tests/ --ignore=tests/test_ops

  build_cpu:
    runs-on: ubuntu-latest
    strategy:
      matrix:
        python-version: [3.7]
        torch: [1.3.1, 1.4.0, 1.5.1, 1.6.0, 1.7.0]
        include:
          - torch: 1.3.1
            torchvision: 0.4.2
          - torch: 1.4.0
            torchvision: 0.5.0
          - torch: 1.5.1
            torchvision: 0.6.1
          - torch: 1.6.0
            torchvision: 0.7.0
          - torch: 1.7.0
            torchvision: 0.8.1
    steps:
      - uses: actions/checkout@v2
      - name: Set up Python ${{ matrix.python-version }}
        uses: actions/setup-python@v2
        with:
          python-version: ${{ matrix.python-version }}
      - name: Install system dependencies
        run: sudo apt-get update && sudo apt-get install -y ffmpeg libturbojpeg
      - name: Install Pillow
        run: pip install Pillow==6.2.2
        if: ${{matrix.torchvision == '0.4.2'}}
      - name: Install PyTorch
        run: pip install torch==${{matrix.torch}}+cpu torchvision==${{matrix.torchvision}}+cpu -f https://download.pytorch.org/whl/torch_stable.html
      - name: Install unittest dependencies
        run: pip install -r requirements/test.txt
      - name: Build and install
        run: rm -rf .eggs && pip install -e .
      - name: Run unittests and generate coverage report
        run: |
          coverage run --branch --source=mmcv -m pytest tests/
          coverage xml
          coverage report -m

  build_cuda:
    runs-on: ubuntu-latest
    env:
      CUDA: 10.1.105-1
      CUDA_SHORT: 10.1
      UBUNTU_VERSION: ubuntu1804
      FORCE_CUDA: 1
      MMCV_CUDA_ARGS: -gencode=arch=compute_61,code=sm_61
    strategy:
      matrix:
        python-version: [3.7]
        torch: [1.3.1, 1.5.1+cu101, 1.6.0+cu101, 1.7.0+cu101]
        include:
          - torch: 1.3.1
            torchvision: 0.4.2
          - torch: 1.5.1+cu101
            torchvision: 0.6.1+cu101
          - torch: 1.6.0+cu101
            torchvision: 0.7.0+cu101
          - torch: 1.7.0+cu101
            torchvision: 0.8.1+cu101
          - python-version: 3.6
            torch: 1.7.0+cu101
            torchvision: 0.8.1+cu101
          - python-version: 3.8
            torch: 1.7.0+cu101
            torchvision: 0.8.1+cu101

    steps:
      - uses: actions/checkout@v2
      - name: Set up Python ${{ matrix.python-version }}
        uses: actions/setup-python@v2
        with:
          python-version: ${{ matrix.python-version }}
      - name: Install CUDA
        run: |
          export INSTALLER=cuda-repo-${UBUNTU_VERSION}_${CUDA}_amd64.deb
          wget http://developer.download.nvidia.com/compute/cuda/repos/${UBUNTU_VERSION}/x86_64/${INSTALLER}
          sudo dpkg -i ${INSTALLER}
          wget https://developer.download.nvidia.com/compute/cuda/repos/${UBUNTU_VERSION}/x86_64/7fa2af80.pub
          sudo apt-key add 7fa2af80.pub
          sudo apt update -qq
          sudo apt install -y cuda-${CUDA_SHORT/./-} cuda-cufft-dev-${CUDA_SHORT/./-}
          sudo apt clean
          export CUDA_HOME=/usr/local/cuda-${CUDA_SHORT}
          sudo cp /usr/local/cuda-10.2/include/* /usr/local/cuda/include
          sudo cp -r /usr/local/cuda-10.2/lib64/* /usr/local/cuda/lib64/
          export LD_LIBRARY_PATH=${CUDA_HOME}/lib64:${CUDA_HOME}/include:${LD_LIBRARY_PATH}
          export PATH=${CUDA_HOME}/bin:${PATH}
          sudo apt-get install -y ninja-build
      - name: Install Pillow
        run: pip install Pillow==6.2.2
        if: ${{matrix.torchvision == '0.4.2'}}
      - name: Install PyTorch
        run: pip install torch==${{matrix.torch}} torchvision==${{matrix.torchvision}} -f https://download.pytorch.org/whl/torch_stable.html
      - name: Install system dependencies
        run: sudo apt-get update && sudo apt-get install -y ffmpeg libturbojpeg
      - name: Install unittest dependencies
        run: pip install -r requirements/test.txt
      - name: Build and install
        run: rm -rf .eggs && pip install -e .
      - name: Run unittests and generate coverage report
        run: |
          coverage run --branch --source=mmcv -m pytest tests/ --ignore=tests/test_ops/test_onnx.py
          coverage xml
          coverage report -m
      # Only upload coverage report for python3.7 && pytorch1.6
      - name: Upload coverage to Codecov
        if: ${{matrix.torch == '1.6.0+cu101' && matrix.python-version == '3.7'}}
        uses: codecov/codecov-action@v1.0.14
        with:
          file: ./coverage.xml
          flags: unittests
          env_vars: OS,PYTHON
          name: codecov-umbrella
          fail_ci_if_error: false

  build_macos:
    runs-on: macos-latest
    strategy:
      matrix:
        torch: [1.3.1, 1.5.1, 1.6.0, 1.7.0]
        include:
          - torch: 1.3.1
            torchvision: 0.4.2
          - torch: 1.5.1
            torchvision: 0.6.1
          - torch: 1.6.0
            torchvision: 0.7.0
          - torch: 1.7.0
            torchvision: 0.8.1
    steps:
      - uses: actions/checkout@v2
      - name: Set up Python 3.7
        uses: actions/setup-python@v2
        with:
          python-version: 3.7
      - name: Install system dependencies
        run: brew install ffmpeg jpeg-turbo
      - name: Install unittest dependencies
        run: pip install -r requirements/test.txt
      - name: Install Pillow
        run: pip install Pillow==6.2.2
        if: ${{matrix.torchvision == '0.4.2'}}
      - name: Install PyTorch
        run: pip install torch==${{matrix.torch}} torchvision==${{matrix.torchvision}} --no-cache-dir
      - name: Build and install
        run: |
          rm -rf .eggs
          CC=clang CXX=clang++ CFLAGS='-stdlib=libc++' pip install -e .
      - name: Run unittests
        run: |
          # The timing on macos VMs is not precise, so we skip the progressbar tests
          pytest tests/ --ignore tests/test_utils/test_progressbar.py --ignore tests/test_utils/test_timer.py
