name: Test Chisel

on:
  workflow_call:
    inputs:
      system:
        description: 'The GitHub runner to use'
        default: 'ubuntu-20.04'
        required: true
        type: string
      jvm:
        description: 'The Java version to use'
        default: 8
        required: true
        type: number
      scala:
        description: 'The Scala version to use'
        default: '2.13.12'
        required: true
        type: string
      espresso:
        description: 'The espresso version to use'
        default: '2.4'
        required: true
        type: string
      circt:
        description: 'The CIRCT version to use (must be a valid release tag or "nightly")'
        required: true
        type: string
      ref:
        description: 'The branch, tag, or git hash to checkout'
        default: ''
        required: false
        type: string

jobs:
  ci:
    name: ci
    runs-on: ${{ inputs.system }}

    steps:
      - name: Checkout
        uses: actions/checkout@v3
        with:
          ref: ${{ inputs.ref }}
      - name: Install Tabby OSS Cad Suite
        uses: ./.github/workflows/setup-oss-cad-suite
      - name: Install Espresso
        uses: ./.github/workflows/install-espresso
        with:
          version: ${{ inputs.espresso }}
      - name: Setup Scala
        uses: actions/setup-java@v3
        with:
          distribution: 'adopt'
          java-version: ${{ inputs.jvm }}
          cache: 'sbt'
      - name: Install CIRCT
        uses: circt/install-circt@v1.1.1
        with:
          version: ${{ inputs.circt }}
          github-token: ${{ github.token }}
      - name: Print firtool version
        run: |
          echo "The CIRCT version used is:" >> $GITHUB_STEP_SUMMARY
          echo \`\`\` >> $GITHUB_STEP_SUMMARY
          firtool -version >> $GITHUB_STEP_SUMMARY
          echo \`\`\` >> $GITHUB_STEP_SUMMARY
      - name: Test
        run: sbt ++${{ inputs.scala }} test
      - name: Binary compatibility
        # TODO either make this also check the plugin or decide that we don't
        #      support binary compatibility for the plugin
        run: sbt ++${{ inputs.scala }} unipublish/mimaReportBinaryIssues

  mill:
    name: compile project with mill
    runs-on: ubuntu-20.04
    steps:
      - name: Checkout
        uses: actions/checkout@v3
        with:
          ref: ${{ inputs.ref }}
      # FIXME: installling jextract before installing mill to workaround `os.proc` can't find jextract in the outdated PATH env from mill server
      - name: Install Jextract
        uses: ./.github/workflows/install-jextract
      - name: Setup Scala
        uses: actions/setup-java@v3
        with:
          distribution: 'adopt'
          java-version: '21'
          cache: 'sbt'
      - name: Install Mill
        uses: jodersky/setup-mill@v0.3.0
        with:
          mill-version: 0.11.5
      - name: Install CIRCT
        uses: circt/install-circt@v1.1.1
        with:
          version: ${{ inputs.circt }}
          github-token: ${{ github.token }}
      - name: Compile Mill
        run: |
          export CIRCT_INSTALL_PATH="$(pwd)/circt"
          mill __.compile

  doc:
    name: Formatting
    runs-on: ubuntu-20.04
    steps:
      - name: Checkout
        uses: actions/checkout@v3
        with:
          ref: ${{ inputs.ref }}
      - name: Setup Scala
        uses: actions/setup-java@v3
        with:
          distribution: 'adopt'
          java-version: '11'
          cache: 'sbt'
      - name: Install CIRCT
        uses: circt/install-circt@v1.1.1
        with:
          version: ${{ inputs.circt }}
          github-token: ${{ github.token }}
      - name: Check Formatting
        run: sbt fmtCheck

  integration:
    name: Integration Tests
    runs-on: ubuntu-20.04
    steps:
      - name: Checkout
        uses: actions/checkout@v3
        with:
          ref: ${{ inputs.ref }}
      - name: Install Tabby OSS Cad Suite
        uses: ./.github/workflows/setup-oss-cad-suite
      - name: Install Espresso
        uses: ./.github/workflows/install-espresso
      - name: Setup Scala
        uses: actions/setup-java@v3
        with:
          distribution: 'adopt'
          java-version: '11'
          cache: 'sbt'
      - name: Install CIRCT
        uses: circt/install-circt@v1.1.1
        with:
          version: ${{ inputs.circt }}
          github-token: ${{ github.token }}
      - name: Integration Tests
        run: sbt integrationTests/test

  std:
    name: Standard Library Tests
    runs-on: ubuntu-20.04
    strategy:
      matrix:
        scala: ["2.13.12"]
    steps:
      - name: Checkout
        uses: actions/checkout@v3
        with:
          ref: ${{ inputs.ref }}
      - name: Install Tabby OSS Cad Suite
        uses: ./.github/workflows/setup-oss-cad-suite
      - name: Setup Scala
        uses: actions/setup-java@v3
        with:
          distribution: 'adopt'
          java-version: '11'
          cache: 'sbt'
      - name: Check Formatting
        run: sbt ++${{ inputs.scala }} standardLibrary/scalafmtCheckAll
      - name: Unit Tests
        run: sbt ++${{ inputs.scala }} standardLibrary/test

  website:
    name: Build Mdoc & Website
    runs-on: ubuntu-latest

    steps:
      - name: Checkout
        uses: actions/checkout@v3
        with:
          ref: ${{ inputs.ref }}
      - name: Setup Scala
        uses: actions/setup-java@v3
        with:
          distribution: 'adopt'
          java-version: '11'
          cache: 'sbt'
      - name: Setup Ruby
        uses: ruby/setup-ruby@v1
        with:
          ruby-version: '2.7.8'
      - name: Setup Jekyll
        run: |
          gem install jekyll -v 4.2.0
          gem install jekyll-redirect-from
      - name: Install CIRCT
        uses: circt/install-circt@v1.1.1
        with:
          version: ${{ inputs.circt }}
          github-token: ${{ github.token }}
        #TODO: make the microsite building include building ScalaDoc
      - name: Build the docs
        run: sbt doc
      - name: Build the microsite
        run: make -C website
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
      - name: Tar built website
        run: tar zcf website.tar.gz website/docs/target/site
      - name: Share Built Website
        uses: actions/upload-artifact@v3
        with:
          name: website
          path: website.tar.gz

  scala-cli-template:
    name: Test Scala-CLI Template
    uses: ./.github/workflows/build-scala-cli-template.yml
    with:
      circt: ${{ inputs.circt }}
