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, "nightly", or "version-file").
                      Leave off (or empty) to let Chisel manage the CIRCT version'
        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
  #      id: install-circt
  #      if: ${{ inputs.circt }}
  #      uses: circt/install-circt@v1.1.1
  #      with:
  #        version: ${{ inputs.circt }}
  #        github-token: ${{ github.token }}
  #    # TODO have install-circt do this
  #    - name: Set FIRTOOL_PATH
  #      if: steps.install-circt.outcome == 'success'
  #      run: |
  #        dir=$(dirname $(which firtool))
  #        echo "FIRTOOL_PATH=$dir" >> "$GITHUB_ENV"
  #    - name: Print firtool version
  #      if: steps.install-circt.outcome == 'success'
  #      run: |
  #        echo "The CIRCT version used is:" >> $GITHUB_STEP_SUMMARY
  #        echo \`\`\` >> $GITHUB_STEP_SUMMARY
  #        $FIRTOOL_PATH/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
  #    # Mill needs firtool to compile with bindings, cannot use firtool-resolver at runtime
  #    - name: Pick CIRCT version
  #      id: circt-version
  #      run: |
  #        if [[ -z "${{ inputs.circt }}" ]]; then
  #          echo "version=version-file" >> "$GITHUB_OUTPUT"
  #        else
  #          echo "version=${{ inputs.circt }}" >> "$GITHUB_OUTPUT"
  #        fi
  #    - name: Install CIRCT
  #      uses: circt/install-circt@v1.1.1
  #      with:
  #        version: ${{ steps.circt-version.outputs.version }}
  #        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: 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
  #      id: install-circt
  #      if: ${{ inputs.circt }}
  #      uses: circt/install-circt@v1.1.1
  #      with:
  #        version: ${{ inputs.circt }}
  #        github-token: ${{ github.token }}
  #    # TODO have install-circt do this
  #    - name: Set FIRTOOL_PATH
  #      if: steps.install-circt.outcome == 'success'
  #      run: |
  #        dir=$(dirname $(which firtool))
  #        echo "FIRTOOL_PATH=$dir" >> "$GITHUB_ENV"
  #    - 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 }}
          # Need full history to correctly determine SNAPSHOT version
          fetch-depth: 0
      - name: Cache Scala
        uses: coursier/cache-action@v6
      - name: Setup Scala
        uses: VirtusLab/scala-cli-setup@v1
        with:
          jvm: adopt:8
          apps: sbt
      - name: Setup Node
        uses: actions/setup-node@v4
        with:
          node-version: 18
      - name: Install CIRCT
        id: install-circt
        if: ${{ inputs.circt }}
        uses: circt/install-circt@v1.1.1
        with:
          version: ${{ inputs.circt }}
          github-token: ${{ github.token }}
      # TODO have install-circt do this
      - name: Set FIRTOOL_PATH
        if: steps.install-circt.outcome == 'success'
        run: |
          dir=$(dirname $(which firtool))
          echo "FIRTOOL_PATH=$dir" >> "$GITHUB_ENV"
        # TODO: make the website building include building ScalaDoc
      - name: Build the website
        run: make -C website build
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
      - name: Tar built website
        run: tar zcf website.tar.gz website/build
      - 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 }}
