version: 2.1

# How to test the Linux jobs:
#   - Install CircleCI local CLI: https://circleci.com/docs/2.0/local-cli/
#   - circleci config process .circleci/config.yml > gen.yml && circleci local execute -c gen.yml --job binary_linux_wheel_py3.8
#     - Replace binary_linux_wheel_py3.8 with the name of the job you want to test.
#       Job names are 'name:' key.

executors:
  windows-cpu:
    machine:
      resource_class: windows.xlarge
      image: windows-server-2019-vs2019:stable
      shell: bash.exe

  windows-gpu:
    machine:
      resource_class: windows.gpu.nvidia.medium
      image: windows-server-2019-nvidia:stable
      shell: bash.exe

commands:
  generate_cache_key:
    description: "Generates a cache key file that changes daily"
    steps:
      - run:
          name: Generate cache key
          command: echo "$(date +"%Y-%m-%d")" > .cachekey
  designate_upload_channel:
    description: "inserts the correct upload channel into ${BASH_ENV}"
    steps:
      - run:
          name: adding UPLOAD_CHANNEL to BASH_ENV
          command: |
            our_upload_channel=nightly
            # On tags upload to test instead
            if [[ -n "${CIRCLE_TAG}" ]] || [[ ${CIRCLE_BRANCH} =~ release/* ]]; then
              our_upload_channel=test
            fi
            echo "export UPLOAD_CHANNEL=${our_upload_channel}" >> ${BASH_ENV}
  load_conda_channel_flags:
    description: "Determines whether we need extra conda channels"
    steps:
      - run:
          name: Adding CONDA_CHANNEL_FLAGS to BASH_ENV
          command: |
              CONDA_CHANNEL_FLAGS=""
              # formerly used to add conda-forge flags for Python 3.9, reserving the mechanism for future python upgrades
  windows_install_cuda:
    description: "Install desired CUDA version on Windows runners"
    steps:
      - run:
          name: Install CUDA
          command: |
              packaging/windows/internal/cuda_install.bat

binary_common: &binary_common
  parameters:
    # Edit these defaults to do a release
    build_version:
      description: "version number of release binary; by default, build a nightly"
      type: string
      default: ""
    pytorch_version:
      description: "PyTorch version to build against; by default, use a nightly"
      type: string
      default: ""
    # Don't edit these
    python_version:
      description: "Python version to build against (e.g., 3.8)"
      type: string
    cuda_version:
      description: "CUDA version to build against (e.g., cpu, cu101)"
      type: string
      default: "cpu"
    wheel_docker_image:
      description: "Wheel only: what docker image to use"
      type: string
      default: "pytorch/manylinux-cuda116"
    conda_docker_image:
      description: "Conda only: what docker image to use"
      type: string
      default: "pytorch/conda-builder:cuda116"
  environment: &environment
    PYTHON_VERSION: << parameters.python_version >>
    BUILD_VERSION: << parameters.build_version >>
    PYTORCH_VERSION: << parameters.pytorch_version >>
    CU_VERSION: << parameters.cuda_version >>
    MACOSX_DEPLOYMENT_TARGET: 10.9

smoke_test_common: &smoke_test_common
  <<: *binary_common
  docker:
    - image: pytorch/torchaudio_unittest_base:smoke_test-20220425
  resource_class: large

jobs:
  circleci_consistency:
    docker:
      - image: cimg/python:3.8
    steps:
      - checkout
      - run:
          command: |
            pip install --user --progress-bar off jinja2 pyyaml
            python .circleci/regenerate.py
            git diff --exit-code || (echo ".circleci/config.yml not in sync with config.yml.in! Run .circleci/regenerate.py to update config"; exit 1)

  lint_python_and_config:
    docker:
      - image: circleci/python:3.8
    steps:
      - checkout
      - run:
          name: Install pre-commit
          command: pip install --user --progress-bar off pre-commit
      - run:
          name: Install pre-commit hooks
          command: pre-commit install-hooks
      - run:
          name: Lint Python code and config files
          command: pre-commit run --all-files
      - run:
          name: Required lint modifications
          when: always
          command: git --no-pager diff --color=always

  download_third_parties:
    docker:
      - image: "pytorch/torchaudio_unittest_base:manylinux"
    resource_class: small
    steps:
      - checkout
      - generate_cache_key
      - restore_cache:
          {% raw %}
          keys:
            - tp-nix-v2-{{ checksum ".cachekey" }}
          {% endraw %}
      - run:
          command: |
              mkdir -p third_party/archives/
              wget --no-clobber --directory-prefix=third_party/archives/ $(awk '/URL /{print $2}' third_party/*/CMakeLists.txt)
      - save_cache:
          {% raw %}
          key: tp-nix-v2-{{ checksum ".cachekey" }}
          {% endraw %}
          paths:
            - third_party/archives
      - persist_to_workspace:
          root: third_party
          paths:
            - archives

  build_ffmpeg_linux:
    <<: *binary_common
    docker:
      - image: << parameters.wheel_docker_image >>
    resource_class: 2xlarge+
    steps:
      - checkout
      - generate_cache_key
      - restore_cache:
          {% raw %}
          keys:
            - ffmpeg-linux-v0-{{ checksum ".cachekey" }}
          {% endraw %}
      - run:
          command: |
            export FFMPEG_ROOT=${PWD}/third_party/ffmpeg
            if [[ ! -d ${FFMPEG_ROOT} ]]; then
                packaging/ffmpeg/build.sh
            fi
      - save_cache:
          {% raw %}
          key: ffmpeg-linux-v0-{{ checksum ".cachekey" }}
          {% endraw %}
          paths:
            - third_party/ffmpeg
      - persist_to_workspace:
          root: third_party
          paths:
            - ffmpeg

  build_ffmpeg_macos:
    <<: *binary_common
    macos:
      xcode: "14.0"
    steps:
      - checkout
      - generate_cache_key
      - restore_cache:
          {% raw %}
          keys:
            - ffmpeg-macos-v0-{{ checksum ".cachekey" }}
          {% endraw %}
      - run:
          command: |
            export FFMPEG_ROOT=${PWD}/third_party/ffmpeg
            if [[ ! -d ${FFMPEG_ROOT} ]]; then
                packaging/ffmpeg/build.sh
            fi
      - save_cache:
          {% raw %}
          key: ffmpeg-macos-v0-{{ checksum ".cachekey" }}
          {% endraw %}
          paths:
            - third_party/ffmpeg
      - persist_to_workspace:
          root: third_party
          paths:
            - ffmpeg

  build_ffmpeg_windows:
    <<: *binary_common
    machine:
      resource_class: windows.xlarge
      image: windows-server-2019-vs2019:stable
      # Note:
      # Unlike other Windows job, this job uses cmd.exe as shell because
      # we need to invoke bash.exe from msys2 in ffmpeg build process, and doing so
      # from different installation of bash.exe (the one from the VM) cause issue
      shell: cmd.exe
    steps:
      - checkout
      - run: date /t > .cachekey
      - restore_cache:
          {% raw %}
          keys:
            - ffmpeg-windows-{{ checksum ".cachekey" }}
          {% endraw %}
      - run: packaging\ffmpeg\build.bat
      - save_cache:
          {% raw %}
          key: ffmpeg-windows-{{ checksum ".cachekey" }}
          {% endraw %}
          paths:
            - third_party/ffmpeg
      - persist_to_workspace:
          root: third_party
          paths:
            - ffmpeg

workflows:
  lint:
    jobs:
      - lint_python_and_config
  unittest:
    jobs:
      - circleci_consistency
