name: CI

on: [push, pull_request]

jobs:

  build:
    strategy:
      fail-fast: false
      matrix:
        lua_ver: ['5.1', '5.3']
        numbers: ['default', 'alternate']
        target: ['esp32', 'esp32s2', 'esp32s3', 'esp32c3']

    runs-on: ubuntu-20.04

    steps:
      - uses: actions/checkout@v2
        with:
          submodules: recursive
      - name: Prepare cache key
        run: git rev-parse HEAD:sdk/esp32-esp-idf > idf.rev
        shell: bash
      - name: Cache Espressif tools
        uses: actions/cache@v2
        with:
          path: ~/.espressif
          key: ${{ runner.os }}-espressif-tools-${{ hashFiles('idf.rev') }}
      - name: Install dependencies
        run: ./install.sh
        shell: bash
      - name: Prepare default sdkconfig
        run: |
          cp sdkconfig.defaults sdkconfig
        shell: bash
      - name: Update config for Lua 5.1, integer-only
        if: ${{ matrix.lua_ver == '5.1' && matrix.numbers == 'alternate' }}
        run: |
          echo CONFIG_LUA_NUMBER_INTEGRAL=y >> sdkconfig
        shell: bash
      - name: Update config for Lua 5.3
        if: ${{ matrix.lua_ver == '5.3' }}
        run: |
          echo CONFIG_LUA_VERSION_53=y >> sdkconfig
        shell: bash
      - name: Update config for Lua 5.3, 64bit numbers
        if: ${{ matrix.lua_ver == '5.3' && matrix.numbers == 'alternate' }}
        run: |
          echo CONFIG_LUA_NUMBER_INT64=y >> sdkconfig
          echo CONFIG_LUA_NUMBER_DOUBLE=y >> sdkconfig
        shell: bash
      - name: Build firmware
        run: |
          make IDF_TARGET=${{ matrix.target }}
        shell: bash
      - name: Get Lua build options
        run: |
          echo lua_build_opts="$(expr "$(./build/luac_cross/luac.cross -v)" : '.*\[\(.*\)\]')" >> $GITHUB_ENV
        shell: bash
      - name: Upload luac.cross
        uses: actions/upload-artifact@v2
        if: ${{ success() }}
        with:
          name: luac.cross-${{ env.lua_build_opts }}-${{ matrix.target }}
          path: build/luac_cross/luac.cross
