name: ci

on: [push, pull_request]

jobs:
  build:
    runs-on: ubuntu-18.04
    steps:
      # Checkout Repository
      - name: Checkout
        uses: actions/checkout@v2

      - name: Setup CCache
        uses: hendrikmuhs/ccache-action@v1

      # Install Tools
      - name: Install Tools
        run: |
          sudo apt-get install wget build-essential python3 ninja-build
          sudo apt-get install libevent-dev libjson-c-dev flex bison
          sudo apt-get install libfl-dev libfl2 zlibc zlib1g-dev
          pip3 install setuptools
          pip3 install requests
          pip3 install pexpect
          pip3 install meson

      # Install (n)Migen / LiteX / Cores
      - name: Install LiteX
        run: |
          wget https://raw.githubusercontent.com/enjoy-digital/litex/master/litex_setup.py
          python3 litex_setup.py init install --user

      # Install RISC-V GCC
      - name: Install RISC-V GCC
        run: |
          wget https://raw.githubusercontent.com/enjoy-digital/litex/master/litex_setup.py
          python3 litex_setup.py gcc
          sudo mkdir /usr/local/riscv
          sudo cp -r $PWD/../riscv64-*/* /usr/local/riscv

      - name: Build Verilator
        run: |
          export PATH="/usr/lib/ccache:/usr/local/opt/ccache/libexec:$PATH"
          git clone https://github.com/verilator/verilator
          cd verilator
          autoconf
          ./configure
          make -j$(nproc)
          sudo make install

      # Install Project
      - name: Install Project
        run: python3 setup.py develop --user

      # Test
      - name: Run Tests
        run: |
          export PATH=/usr/local/riscv/bin:$PATH
          python3 setup.py test