# Copyright lowRISC contributors.
# Licensed under the Apache License, Version 2.0, see LICENSE for details.
# SPDX-License-Identifier: Apache-2.0

# Azure Pipelines CI build configuration
# Documentation at https://aka.ms/yaml

variables:
  VERILATOR_VERSION: 4.032
  RISCV_TOOLCHAIN_TAR_VERSION: 20200323-1
  RISCV_COMPLIANCE_GIT_VERSION: 844c6660ef3f0d9b96957991109dfd80cc4938e2
  VERIBLE_VERSION: v0.0-266-g9e55307

trigger:
  batch: true
  branches:
    include:
    - '*'
  tags:
    include:
    - '*'
pr:
  branches:
    include:
      - '*'

# Note: All tests run as part of one job to avoid copying intermediate build
# artifacts around (e.g. Verilator and toolchain builds). Once more builds/tests
# are added, we need to re-evaluate this decision to parallelize jobs and
# improve end-to-end CI times.

jobs:
- job: lint_dv
  displayName: Run quality checks (Lint and DV)
  pool:
    vmImage: "ubuntu-16.04"
  steps:
  # Installing six is a workaround for pip dependency resolution: six is already
  # installed as system package with a version below the required one.
  # Explicitly installing six through pip gets us a supported version.
  #
  # Updating pip and setuptools is required to have these tools properly parse
  # Python-version metadata, which some packages uses to specify that an older
  # version of a package must be used for a certain Python version. If that
  # information is not read, pip installs the latest version, which then fails
  # to run.
  - bash: |
      curl -L https://download.opensuse.org/repositories/home:phiwag:edatools/xUbuntu_16.04/Release.key | sudo apt-key add -
      sudo sh -c "echo 'deb http://download.opensuse.org/repositories/home:/phiwag:/edatools/xUbuntu_16.04/ /' > /etc/apt/sources.list.d/edatools.list"
      sudo apt-get update
      sudo apt-get install -y \
          python3 \
          python3-pip \
          python3-setuptools \
          srecord \
          zlib1g-dev \
          git \
          make \
          autoconf \
          g++ \
          flex \
          bison \
          curl \
          libelf-dev \
          clang-format \
          verilator-$(VERILATOR_VERSION) \
        && sudo pip3 install -U setuptools pip six \
        && sudo pip3 install -U -r python-requirements.txt
    displayName: Install dependencies

  - bash: |
      set -e
      mkdir -p build/verible
      cd build/verible
      curl -Ls -o verible.tar.gz https://github.com/google/verible/releases/download/$(VERIBLE_VERSION)/verible-$(VERIBLE_VERSION)-Ubuntu-16.04-xenial-x86_64.tar.gz
      sudo mkdir -p /tools/verible && sudo chmod 777 /tools/verible
      tar -C /tools/verible -xf verible.tar.gz --strip-components=1
      echo "##vso[task.setvariable variable=PATH]/tools/verible/bin:$PATH"
    displayName: Install Verible

  - bash: |
      export TOOLCHAIN_URL=https://github.com/lowRISC/lowrisc-toolchains/releases/download/${RISCV_TOOLCHAIN_TAR_VERSION}/lowrisc-toolchain-gcc-rv32imc-${RISCV_TOOLCHAIN_TAR_VERSION}.tar.xz
      mkdir -p build/toolchain
      curl -Ls -o build/toolchain/rv32-toolchain.tar.xz $TOOLCHAIN_URL
      sudo mkdir -p /tools/riscv && sudo chmod 777 /tools/riscv
      tar -C /tools/riscv -xf build/toolchain/rv32-toolchain.tar.xz --strip-components=1
      echo "##vso[task.setvariable variable=PATH]/tools/riscv/bin:$PATH"
    displayName: Get precompiled RISC-V toolchain

  - bash: |
      echo $PATH
      python3 --version
      echo -n "fusesoc "
      fusesoc --version
      verilator --version
      riscv32-unknown-elf-gcc --version
      verilog_lint --version
    displayName: Display environment

  # Verible lint/format is experimental so only run on default config for now,
  # will eventually become part of the per-config CI
  - bash: |
      fusesoc --cores-root . run --no-export --target=lint --tool=veriblelint lowrisc:ibex:ibex_core_tracing
      if [ $? != 0 ]; then
        echo -n "##vso[task.logissue type=error]"
        echo "Verilog lint with Verible failed. Run 'fusesoc --cores-root . run --target=lint --tool=veriblelint lowrisc:ibex:ibex_core_tracing' to check and fix all errors."
        echo "This flow is currently experimental and failures can be ignored."
        exit 1
      fi
    continueOnError: true
    displayName: Lint Verilog source files with Verible (experimental)

  - bash: |
      fusesoc --cores-root . run --no-export --target=format --tool=veribleformat lowrisc:ibex:ibex_core_tracing
      if [ $? != 0 ]; then
        echo -n "##vso[task.logissue type=error]"
        echo "Verilog format with Verible failed. Run 'fusesoc --cores-root . run --no-export --target=format --tool=veribleformat lowrisc:ibex:ibex_core_tracing' to check and fix all errors."
        echo "This flow is currently experimental and failures can be ignored."
      fi
      # Show diff of what verilog_format would have changed, and then revert.
      git diff
      git reset --hard HEAD
    continueOnError: true
    displayName: Format all source code with Verible format (experimental)

  - bash: |
      fork_origin=$(git merge-base --fork-point origin/master)
      changed_files=$(git diff --name-only $fork_origin | grep -v '^vendor' | grep -E '\.(cpp|cc|c|h)$')
      test -z "$changed_files" || git diff -U0 $fork_origin $changed_files | clang-format-diff -p1 | tee clang-format-output
      if [ -s clang-format-output ]; then
        echo -n "##vso[task.logissue type=error]"
        echo "C/C++ lint failed. Use 'git clang-format' with appropriate options to reformat the changed code."
        exit 1
      fi
    # This check is not idempotent, but checks changes to a base branch.
    # Run it only on pull requests.
    condition: eq(variables['Build.Reason'], 'PullRequest')
    displayName: 'Use clang-format to check C/C++ coding style'

  - bash: |
      # Build and run CSR testbench, chosen Ibex configuration does not effect
      # this so doesn't need to be part of per-config CI
      fusesoc --cores-root=. run --target=sim --tool=verilator lowrisc:ibex:tb_cs_registers
    displayName: Build and run CSR testbench with Verilator

  - bash: |
      cd build
      git clone https://github.com/riscv/riscv-compliance.git
      cd riscv-compliance
      git checkout "$(RISCV_COMPLIANCE_GIT_VERSION)"
    displayName: Get RISC-V Compliance test suite

  # Run Ibex RTL CI per supported configuration
  - template : ci/ibex-rtl-ci-steps.yml
    parameters:
      ibex_configs:
        - small-3cmult
        - experimental-maxperf-1cmult
        - experimental-maxperf-bm-1cmult
