name: ChiselStage


on:
  # Triggers the workflow on push or pull request events but only for the master branch
  push:
    branches: [ master, develop ]
  # pull_request:
  #   branches: [ master ]

  # Allows you to run this workflow manually from the Actions tab
  workflow_dispatch:

# A workflow run is made up of one or more jobs that can run sequentially or in parallel
jobs:

  chiselStage:
    # The type of runner that the job will run on
    runs-on: [self-hosted, Linux, X64]
    name: chiselStage
    container:
      image: whutddk/rift2env:chisel5
      # options: >-
      #   --memory 60g
      #   --oom-kill-disable
      #   --memory-swap -1

    # needs: clean
    # Steps represent a sequence of tasks that will be executed as part of the job
    steps:
      - name: set up apt
        run: |
          apt-get update
          apt-get install -y wget git make curl


      - uses: actions/checkout@v3.3.0
        with:
          submodules: 'recursive'
      # - name: Set up JDK 17
      #   uses: actions/setup-java@v3.10.0
      #   with:
      #     java-version: '17'
      #     distribution: 'microsoft'

      - name: Compile
        run: |
          echo ${GITHUB_WORKSPACE}

          rm -f dependencies/rocket-chip/src/main/resources/META-INF/services/firrtl.options.RegisteredLibrary
          mill -i rift2Core[chisel].test.runMain test.testAll
          mill --no-server show rift2Core[chisel].docJar
          unzip -d ScalaDoc/ out/rift2Core/chisel/docJar.dest/out.jar

          echo $GITHUB_REF_NAME
          
          mkdir -p ${GITHUB_WORKSPACE}/../ScalaDoc/
          mkdir -p ${GITHUB_WORKSPACE}/../ScalaDoc/$GITHUB_REF_NAME/

          cd ${GITHUB_WORKSPACE}

          cp ${GITHUB_WORKSPACE}/LICENSE.Apache ${GITHUB_WORKSPACE}/../
          cp ${GITHUB_WORKSPACE}/LICENSE.NPL ${GITHUB_WORKSPACE}/../
          cp -R ScalaDoc/* ${GITHUB_WORKSPACE}/../ScalaDoc/$GITHUB_REF_NAME

          # cd ${GITHUB_WORKSPACE}/generated/Release/
          # tar -cvf Rift2300-Release.tar Rift2300/*
          # tar -cvf Rift2310-Release.tar Rift2310/*
          # tar -cvf Rift2320-Release.tar Rift2320/*
          # tar -cvf Rift2330-Release.tar Rift2330/*
          # tar -cvf Rift2340-Release.tar Rift2340/*
          # tar -cvf Rift2350-Release.tar Rift2350/*
          # tar -cvf Rift2360-Release.tar Rift2360/*
          # tar -cvf Rift2370-Release.tar Rift2370/*
          # tar -cvf Rift2380-Release.tar Rift2380/*
          # tar -cvf Rift2390-Release.tar Rift2390/*

          cd ${GITHUB_WORKSPACE}/generated/Debug/
          tar -cvf Rift2300-Debug.tar  Rift2300/*
          tar -cvf Rift2310-Debug.tar  Rift2310/*
          tar -cvf Rift2320-Debug.tar  Rift2320/*
          tar -cvf Rift2330-Debug.tar  Rift2330/*
          tar -cvf Rift2340-Debug.tar  Rift2340/*
          tar -cvf Rift2350-Debug.tar  Rift2350/*
          tar -cvf Rift2360-Debug.tar  Rift2360/*
          tar -cvf Rift2370-Debug.tar  Rift2370/*
          tar -cvf Rift2380-Debug.tar  Rift2380/*
          tar -cvf Rift2390-Debug.tar  Rift2390/*


      - name: Commit
        run: |
          cd ${GITHUB_WORKSPACE}
          git config --global --add safe.directory ${GITHUB_WORKSPACE}
          git checkout -- .
          git fetch origin gh_pages
          git checkout gh_pages

          rm -rf ${GITHUB_WORKSPACE}/ScalaDoc/$GITHUB_REF_NAME
          cp -R ${GITHUB_WORKSPACE}/../ScalaDoc/* ${GITHUB_WORKSPACE}/ScalaDoc/
          cp ${GITHUB_WORKSPACE}/../LICENSE.Apache ${GITHUB_WORKSPACE}/
          cp ${GITHUB_WORKSPACE}/../LICENSE.NPL ${GITHUB_WORKSPACE}/

          git config --local user.email "action@github.com"
          git config --local user.name "GitHub Action"

          git add ./ScalaDoc
          git commit --no-gpg-sign --allow-empty -m "update ci result"
 
      - name: push
        uses: ad-m/github-push-action@master
        with:
          github_token: ${{ secrets.GITHUB_TOKEN }}
          branch: gh_pages
          force: false

      - name: Get current time
        id: gettime
        run: |
          echo "::set-output name=time::$(date +'%Y-%m-%d-%H-%M-%S')"

        #echo "NOW::$(date +'%Y-%m-%dT%H:%M:%S')" >> $GITHUB_ENV

      - name: Create Pre-Release and Upload Release Asset
        if: github.ref_name != 'master'
        uses: softprops/action-gh-release@v1
        with:
          tag_name: PreRelease-${{ steps.gettime.outputs.time }} 
          name: PreRelease-${{ steps.gettime.outputs.time }}
          body: CI compile on ${{ steps.gettime.outputs.time }}.
          draft: false
          prerelease: true
          target_commitish: ${{github.ref_name}}
          files: |
            # ./generated/Release/Rift2300-Release.tar
            ./generated/Debug/Rift2300-Debug.tar
            # ./generated/Release/Rift2310-Release.tar
            ./generated/Debug/Rift2310-Debug.tar
            # ./generated/Release/Rift2320-Release.tar
            ./generated/Debug/Rift2320-Debug.tar
            # ./generated/Release/Rift2330-Release.tar
            ./generated/Debug/Rift2330-Debug.tar
            # ./generated/Release/Rift2340-Release.tar
            ./generated/Debug/Rift2340-Debug.tar
            # ./generated/Release/Rift2350-Release.tar
            ./generated/Debug/Rift2350-Debug.tar
            # ./generated/Release/Rift2360-Release.tar
            ./generated/Debug/Rift2360-Debug.tar
            # ./generated/Release/Rift2370-Release.tar
            ./generated/Debug/Rift2370-Debug.tar
            # ./generated/Release/Rift2380-Release.tar
            ./generated/Debug/Rift2380-Debug.tar
            # ./generated/Release/Rift2390-Release.tar
            ./generated/Debug/Rift2390-Debug.tar
            ./LICENSE.Apache
            ./LICENSE.NPL



      - name: Create Release and Upload Release Asset
        if: github.ref_name == 'master'
        uses: softprops/action-gh-release@v1
        with:
          tag_name: ${{ steps.gettime.outputs.time }} 
          name: Release-${{ steps.gettime.outputs.time }} 
          body: CI compile on ${{ steps.gettime.outputs.time }}.
          draft: false
          prerelease: false
          target_commitish: ${{github.ref_name}}
          files: |
            # ./generated/Release/Rift2300-Release.tar
            ./generated/Debug/Rift2300-Debug.tar
            # ./generated/Release/Rift2310-Release.tar
            ./generated/Debug/Rift2310-Debug.tar
            # ./generated/Release/Rift2320-Release.tar
            ./generated/Debug/Rift2320-Debug.tar
            # ./generated/Release/Rift2330-Release.tar
            ./generated/Debug/Rift2330-Debug.tar
            # ./generated/Release/Rift2340-Release.tar
            ./generated/Debug/Rift2340-Debug.tar
            # ./generated/Release/Rift2350-Release.tar
            ./generated/Debug/Rift2350-Debug.tar
            # ./generated/Release/Rift2360-Release.tar
            ./generated/Debug/Rift2360-Debug.tar
            # ./generated/Release/Rift2370-Release.tar
            ./generated/Debug/Rift2370-Debug.tar
            # ./generated/Release/Rift2380-Release.tar
            ./generated/Debug/Rift2380-Debug.tar
            # ./generated/Release/Rift2390-Release.tar
            ./generated/Debug/Rift2390-Debug.tar
            ./LICENSE.Apache
            ./LICENSE.NPL


      # Runs a single command using the runners shell
      # - name: Run a one-line script
      #   run: echo Hello, world!

      # Runs a set of commands using the runners shell
      # - name: Run a multi-line script
      #   run: |
      #     echo Add other actions to build,
      #     echo test, and deploy your project.


  rift23x0CI:
    name: rift23x0 CI
    needs: chiselStage
      
    strategy:
      fail-fast: false
      matrix:
        version: [Rift2330, Rift2370]
    runs-on: [self-hosted, Linux, X64]
    container:
      image: whutddk/rift2env:chisel5
      # options: >-
      #   --memory 60g
      #   --oom-kill-disable
      #   --memory-swap -1


    # services:
    #   testDocker:
    #     image: whutddk/rift2env:riscvtest


    steps:


      - name: set up apt
        run: |
          apt-get update
          apt-get install -y wget git make g++ ccache zlib1g zlib1g-dev

          
      - name: Checkout Github Page
        run: |
          cd /
          git clone https://github.com/whutddk/Rift2Core.git
          cd Rift2Core
          pwd
          git fetch --all
          git checkout ${{ github.ref_name }}
          git checkout gh_pages
          git pull
          
     
      - name: Get Release
        id: getrelease
        uses: cardinalby/git-get-release-action@1.2.2
        with:
          latest: true
          prerelease: true
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}


      - name: Check Release
        run: |
          echo ${{ steps.getrelease.outputs.tag_name }}

      - name: Download Latest Pkg
        run: |
          cd /Rift2Core

          wget https://github.com/whutddk/Rift2Core/releases/download/${{ steps.getrelease.outputs.tag_name }}/${{matrix.version}}-Debug.tar
          # wget https://github.com/whutddk/Rift2Core/releases/download/${{ steps.getrelease.outputs.tag_name }}/${{matrix.version}}-Release.tar



      - name: Unzip files
        run: |
          cd /Rift2Core
          
          mkdir -p ./generated/Debug
          # mkdir -p ./generated/Release
          
          tar -xvf ./${{matrix.version}}-Debug.tar -C ./generated/Debug
          # tar -xvf ./${{matrix.version}}-Release.tar -C ./generated/Release



      - name: Checkout Env
        run: |
          cd /Rift2Core
          mkdir -p ./tb/ci
          cp /test/* ./tb/ci
          git restore -s ${{ github.ref_name }} -- ./tb
          git restore -s ${{ github.ref_name }} -- ./Makefile
          # git restore -s ${{ github.ref_name }} -- ./src/yosys/area.ys

        
      - name: isa, dhrystone, coremark, yosys
        run: |
          cd /Rift2Core
          export R2=/Rift2Core

          rm -f /Rift2Core/generated/Debug/${{matrix.version}}/isa.json
          echo "{\n  \"schemaVersion\": 1, \n  \"label\": \"\", \n  \"message\": \"Failed\", \n  \"color\": \"red\" \n}" >> /Rift2Core/generated/Debug/${{matrix.version}}/isa.json

          make test CONFIG=/Debug/${{matrix.version}}/




      # make area CONFIG=/Release/${{matrix.version}}/

      - name: commit result
        if: success() || failure()
        run: |
          cd /Rift2Core
          git pull
          git config --local user.email "action@github.com"
          git config --local user.name "GitHub Action"
          git add ./generated/Debug/${{matrix.version}}/*.json
          git commit --no-gpg-sign --allow-empty -m "ci update ${{matrix.version}}"

          # git add ./generated/Release/${{matrix.version}}/area.json

      - name: push
        if: success() || failure()
        uses: ad-m/github-push-action@master
        with:
          github_token: ${{ secrets.GITHUB_TOKEN }}
          branch: gh_pages
          directory: /Rift2Core
          force: false

