name: FPGA interchange CI tests

on: [push, pull_request]

jobs:
  Build-yosys:
    runs-on: ubuntu-latest
    steps:

    - uses: actions/checkout@v2
      with:
        submodules: recursive

    - uses: actions/setup-python@v2

    - name: Install
      run: |
        sudo apt-get update
        sudo apt-get install git make cmake libboost-all-dev python3-dev libeigen3-dev tcl-dev clang bison flex swig

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

    - name: Get yosys
      run: git clone https://github.com/YosysHQ/yosys.git

    - name: Cache yosys installation
      uses: actions/cache@v2
      id: cache-yosys
      with:
        path: .yosys
        key: cache-yosys-${{ hashFiles('**/yosys/.git/HEAD') }}

    - name: Build yosys
      run: |
        export PATH="/usr/lib/ccache:/usr/local/opt/ccache/libexec:$PATH"
        source ./.github/ci/build_interchange.sh
        build_yosys
      if: steps.cache-yosys.outputs.cache-hit != 'true'

  Build-nextpnr:
    runs-on: ubuntu-latest
    steps:

    - uses: actions/checkout@v2
      with:
        submodules: recursive

    - uses: actions/setup-python@v2

    - name: Install
      run: |
        sudo apt-get update
        sudo apt-get install git make cmake libboost-all-dev python3-dev libeigen3-dev tcl-dev clang bison flex swig

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

    - name: Execute build nextpnr
      run: |
        export PATH="/usr/lib/ccache:/usr/local/opt/ccache/libexec:$PATH"
        source ./.github/ci/build_interchange.sh
        build_nextpnr

  Run-Tests:
    runs-on: ubuntu-latest
    needs: [Build-yosys, Build-nextpnr]
    strategy:
      # Don't terminate jobs when one fails.  This is important when
      # debugging CI failures.
      fail-fast: false
      matrix:
        device: [xc7a35t, xc7a100t, xc7a200t, xc7z010, LIFCL-17]
    steps:

    - uses: actions/checkout@v2
      with:
        submodules: recursive

    - uses: actions/setup-python@v2

    - name: Install
      run: |
        sudo apt-get update
        sudo apt-get install git make cmake libboost-all-dev python3-dev libeigen3-dev tcl-dev clang bison flex swig

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

    - name: Get yosys
      run: git clone https://github.com/YosysHQ/yosys.git

    - name: Cache yosys installation
      uses: actions/cache@v2
      id: cache-yosys
      with:
        path: .yosys
        key: cache-yosys-${{ hashFiles('**/yosys/.git/HEAD') }}

    - name: Build yosys
      run: |
        export PATH="/usr/lib/ccache:/usr/local/opt/ccache/libexec:$PATH"
        source ./.github/ci/build_interchange.sh
        build_yosys
      if: steps.cache-yosys.outputs.cache-hit != 'true'

    - name: Execute build interchange script
      env:
        RAPIDWRIGHT_PATH: ${{ github.workspace }}/RapidWright
        PYTHON_INTERCHANGE_PATH: ${{ github.workspace }}/python-fpga-interchange
        PYTHON_INTERCHANGE_TAG: v0.0.10
        PRJOXIDE_REVISION: a85135648c3ef2f7b3fd53ae2187ef6460e34b16
        DEVICE: ${{ matrix.device }}
      run: |
        export PATH="/usr/lib/ccache:/usr/local/opt/ccache/libexec:$PATH"
        source ./.github/ci/build_interchange.sh
        build_nextpnr && get_dependencies

    - name: Run tests
      env:
        DEVICE: ${{ matrix.device }}
      run: |
        export PATH="$GITHUB_WORKSPACE/.yosys/usr/local/bin:$PATH"
        which yosys
        cd build
        make all-$DEVICE-tests -j`nproc`