version: 2.1

executors:
  default:
    machine:
      image: ubuntu-1604-cuda-10.1:201909-23
    working_directory: ~/gpuci
    resource_class: gpu.nvidia.medium

restore-workspace: &restore-workspace
  attach_workspace:
    at: ~/

restore-cache: &restore-cache
  restore_cache:
    keys:
      - cargo-v1-{{ checksum "rust-toolchain" }}-{{ checksum "Cargo.toml" }}-{{ checksum "Cargo.lock" }}-{{ arch }}
      - repo-source-{{ .Branch }}-{{ .Revision }}

commands:
  test_target:
    parameters:
      target:
        type: string
    steps:
      - *restore-workspace
      - *restore-cache
      - run:
          name: Test (pairing) (<< parameters.target >>)
          command: TARGET=<< parameters.target >> cargo test --release
          no_output_timeout: 5m
      - run:
          name: Test (blst) (<< parameters.target >>)
          command: TARGET=<< parameters.target >> cargo test --no-default-features --features blst --release  -- --test-threads=1
          no_output_timeout: 5m
      - run:
          name: Test (pairing, GPU) (<< parameters.target >>)
          command: TARGET=<< parameters.target >> cargo test --release --no-default-features --features pairing,gpu -- --test-threads=1
          no_output_timeout: 30m

      - run:
          name: Test (blst, GPU) (<< parameters.target >>)
          command: TARGET=<< parameters.target >> cargo test --release --no-default-features --features blst,gpu -- --test-threads=1
          no_output_timeout: 30m
      - run:
          name: Test (pairing, opencl) (<< parameters.target >>)
          command: TARGET=<< parameters.target >> cargo test --release --no-default-features --features pairing,opencl -- --test-threads=1
          no_output_timeout: 30m

      - run:
          name: Test (blst, opencl) (<< parameters.target >>)
          command: TARGET=<< parameters.target >> cargo test --release --no-default-features --features blst,opencl -- --test-threads=1
          no_output_timeout: 30m

jobs:
  cargo_fetch:
    executor: default
    steps:
      - checkout
      - run: curl https://sh.rustup.rs -sSf | sh -s -- -y
      - run: echo 'export PATH="$HOME:~/.cargo/bin:$PATH"' >> $BASH_ENV
      - run: echo $BASH_ENV
      - run: echo $HOME
      - run: source $BASH_ENV
      - run: cargo --version
      - run: rustc --version
      - run:
          name: Update submodules
          command: git submodule update --init --recursive
      - run:
          name: Calculate dependencies
          command: cargo generate-lockfile
      - restore_cache:
          keys:
            - cargo-v1-{{ checksum "rust-toolchain" }}-{{ checksum "Cargo.toml" }}-{{ checksum "Cargo.lock" }}-{{ arch }}
      - run: cargo update
      - run: cargo fetch
      - run: rustup install $(cat rust-toolchain)
      - run: rustup default $(cat rust-toolchain)
      - run: rustup install nightly-2020-11-18
      - run: rustup component add rustfmt-preview
      - run: rustup component add clippy-preview
      - run: rustc --version
      - run: rm -rf .git
      - persist_to_workspace:
          root: ~/
          paths:
            - gpuci
      - save_cache:
          key: cargo-v1-{{ checksum "rust-toolchain" }}-{{ checksum "Cargo.toml" }}-{{ checksum "Cargo.lock" }}-{{ arch }}
          paths:
            - "~/.cargo"
            - "~/.rustup"

  test_x86_64-unknown-linux-gnu:
    executor: default
    steps:
      - run: echo 'export PATH="$HOME:~/.cargo/bin:$PATH"' >> $BASH_ENV
      - run: source $BASH_ENV
      - run: sudo apt-get update -y
      - run: apt-cache search opencl
      - run: sudo apt install -y ocl-icd-opencl-dev
      - test_target:
          target: "x86_64-unknown-linux-gnu"

  test_darwin:
    macos:
      xcode: "10.0.0"
    working_directory: ~/crate
    resource_class: large
    steps:
      - checkout
      - run:
          name: Install Rust
          command: |
            curl https://sh.rustup.rs -sSf | sh -s -- -y
      - run: rustup install $(cat rust-toolchain)
      - run: rustup default $(cat rust-toolchain)
      - run: cargo update
      - run: cargo fetch
      - test_target:
          target: "darwin"

  rustfmt:
    executor: default
    steps:
      - *restore-workspace
      - *restore-cache
      - run: echo 'export PATH="$HOME:~/.cargo/bin:$PATH"' >> $BASH_ENV
      - run: source $BASH_ENV
      - run:
          name: Run cargo fmt
          command: cargo fmt --all -- --check

  clippy:
    executor: default
    steps:
      - *restore-workspace
      - *restore-cache
      - run: echo 'export PATH="$HOME:~/.cargo/bin:$PATH"' >> $BASH_ENV
      - run: source $BASH_ENV
      - run: sudo apt-get update -y
      - run: apt-cache search opencl
      - run: sudo apt install -y ocl-icd-opencl-dev
      - run:
          name: Run cargo clippy (pairing)
          command: cargo clippy --no-default-features --features pairing,gpu
      - run:
          name: Run cargo clippy (blst)
          command: cargo clippy --no-default-features --features blst,gpu

  build:
    executor: default
    steps:
      - *restore-workspace
      - *restore-cache
      - run: echo 'export PATH="$HOME:~/.cargo/bin:$PATH"' >> $BASH_ENV
      - run: source $BASH_ENV
      - run: sudo apt-get update -y
      - run: apt-cache search opencl
      - run: sudo apt install -y ocl-icd-opencl-dev
      - run:
          name: Run cargo release build (pairing)
          command: cargo build --release --no-default-features --features pairing
      - run:
          name: Run cargo release build (blst)
          command: cargo build --release --no-default-features --features blst
      - run:
          name: Run cargo release build (pairing, gpu)
          command: cargo build --release --no-default-features --features pairing,gpu
      - run:
          name: Run cargo release build (blst, gpu)
          command: cargo build --release --no-default-features --features blst,gpu

  build_gbench:
    executor: default
    steps:
      - *restore-workspace
      - *restore-cache
      - run: echo 'export PATH="$HOME:~/.cargo/bin:$PATH"' >> $BASH_ENV
      - run: source $BASH_ENV
      - run: sudo apt-get update -y
      - run: apt-cache search opencl
      - run: sudo apt install -y ocl-icd-opencl-dev
      - run:
          name: Run cargo release build (pairing, gpu)
          command: cargo +nightly-2020-11-18 build -Zpackage-features --release -p gbench --no-default-features --features pairing,gpu
      - run:
          name: Run cargo release build (blst, gpu)
          command: cargo +nightly-2020-11-18 build -Zpackage-features --release -p gbench --no-default-features --features blst,gpu

  benches:
    executor: default
    steps:
      - *restore-workspace
      - *restore-cache
      - run: echo 'export PATH="$HOME:~/.cargo/bin:$PATH"' >> $BASH_ENV
      - run: source $BASH_ENV
      - run: sudo apt-get update -y
      - run: apt-cache search opencl
      - run: sudo apt install -y ocl-icd-opencl-dev
      - run:
          name: Run cargo build --benches (pairing)
          command: cargo build --benches --no-default-features --features pairing --release
      - run:
          name: Run cargo build --benches (blst)
          command: cargo build --benches --no-default-features --features blst --release

workflows:
  version: 2.1

  test:
    jobs:
      - cargo_fetch
      - rustfmt:
          requires:
            - cargo_fetch
      - clippy:
          requires:
            - cargo_fetch
      - test_x86_64-unknown-linux-gnu:
          requires:
            - cargo_fetch
      - test_darwin:
          requires:
            - cargo_fetch
      - build:
          requires:
            - cargo_fetch
      - build_gbench:
          requires:
            - cargo_fetch
      - benches:
          requires:
            - cargo_fetch
