name: prepare-rtl
description: 'Builds RTL based on parameters, caches the entire chipyard root dir when done'

inputs:
  group-key:
    description: group key
    required: true
  build-script:
    description: rtl build script to use
    required: false
    default: "remote-do-rtl-build.sh"
  build-type:
    description: type of build
    required: false
    default: "sim"
  toolchain:
    description: toolchain to use
    required: false
    default: "riscv-tools"

runs:
  using: "composite"
  steps:
    - uses: actions/cache@v3
      id: rtl-build-id
      with:
        path: |
          sims/verilator
          sims/vcs
          sims/firesim/sim
          generators/gemmini/software/gemmini-rocc-tests
          generators/radiance/radpie
        key: ${{ inputs.group-key }}-${{ github.sha }}

    - name: Run RTL build if not cached
      run: |
        conda activate ${{ env.conda-env-name-no-time }}-$(date --date "${{ env.workflow-timestamp }}" +%Y%m%d)-${{ inputs.toolchain }}
        if [[ "${{ steps.rtl-build-id.outputs.cache-hit }}" != 'true' ]]; then
          echo "Cache miss on ${{ inputs.group-key }}-${{ github.sha }}"
          echo "Cache miss happened in: $PWD"
          ./.github/scripts/${{ inputs.build-script }} ${{ inputs.group-key }} ${{ inputs.build-type }}
        else
          echo "Cache hit do not rebuild RTL for ${{ inputs.group-key }}"
        fi
      shell: bash -leo pipefail {0}
