name: CI

on:
  push:
    branches:
      - master
      - develop
      - /^release\/.*$/
  pull_request:
    branches:
      - master
      - develop
      - /^release\/.*$/

jobs:
  Docs:
    runs-on: ubuntu-20.04
    container: texlive/texlive:latest-medium
    steps:
      - uses: actions/checkout@v2
      - name: generate PDF
        run: |
          make docs
          cp doc/crypt.pdf crypt-${{ github.run_id }}.pdf
      - name: upload PDF
        uses: actions/upload-artifact@v3
        with:
          name: crypt-${{ github.run_id }}.pdf
          path: crypt-${{ github.run_id }}.pdf
  Build:
    runs-on: ${{ matrix.os }}
    strategy:
      matrix:
        cc: [ gcc, clang ]
        os: [ ubuntu-20.04, ubuntu-22.04 ]
        config:
          - { BUILDNAME: 'META_BUILDS',             BUILDOPTIONS: '-DGMP_DESC',                                                           BUILDSCRIPT: '.ci/meta_builds.sh' }
          - { BUILDNAME: 'VALGRIND',                BUILDOPTIONS: '',                                                                     BUILDSCRIPT: '.ci/valgrind.sh' }
          - { BUILDNAME: 'STOCK',                   BUILDOPTIONS: '',                                                                     BUILDSCRIPT: '.ci/run.sh' }
          - { BUILDNAME: 'STOCK-MPI',               BUILDOPTIONS: '-ULTM_DESC -UTFM_DESC -UUSE_LTM -UUSE_TFM',                            BUILDSCRIPT: '.ci/run.sh' }
          - { BUILDNAME: 'STOCK+AESNI',             BUILDOPTIONS: '-msse4.1 -maes',                                                       BUILDSCRIPT: '.ci/run.sh' }
          - { BUILDNAME: 'EASY',                    BUILDOPTIONS: '-DLTC_EASY',                                                           BUILDSCRIPT: '.ci/run.sh' }
          - { BUILDNAME: 'SMALL',                   BUILDOPTIONS: '-DLTC_SMALL_CODE',                                                     BUILDSCRIPT: '.ci/run.sh' }
          - { BUILDNAME: 'NO_TABLES',               BUILDOPTIONS: '-DLTC_NO_TABLES',                                                      BUILDSCRIPT: '.ci/run.sh' }
          - { BUILDNAME: 'NO_FAST',                 BUILDOPTIONS: '-DLTC_NO_FAST',                                                        BUILDSCRIPT: '.ci/run.sh' }
          - { BUILDNAME: 'NO_FAST+SMALL+NO_TABLES', BUILDOPTIONS: '-DLTC_NO_FAST -DLTC_SMALL_CODE -DLTC_NO_TABLES',                       BUILDSCRIPT: '.ci/run.sh' }
          - { BUILDNAME: 'NO_ASM',                  BUILDOPTIONS: '-DLTC_NO_ASM',                                                         BUILDSCRIPT: '.ci/run.sh' }
          - { BUILDNAME: 'NO_TIMING_RESISTANCE',    BUILDOPTIONS: '-DLTC_NO_ECC_TIMING_RESISTANT -DLTC_NO_RSA_BLINDING',                  BUILDSCRIPT: '.ci/run.sh' }
          - { BUILDNAME: 'FORTUNA_CUSTOM_OPTIONS',  BUILDOPTIONS: '-DLTC_FORTUNA_USE_ENCRYPT_ONLY -DLTC_FORTUNA_RESEED_RATELIMIT_STATIC', BUILDSCRIPT: '.ci/run.sh' }
          - { BUILDNAME: 'PTHREAD',                 BUILDOPTIONS: '-DLTC_PTHREAD',                                                        BUILDSCRIPT: '.ci/run.sh' }
          - { BUILDNAME: 'STOCK+ARGTYPE=1',         BUILDOPTIONS: '-DARGTYPE=1',                                                          BUILDSCRIPT: '.ci/run.sh' }
          - { BUILDNAME: 'STOCK+ARGTYPE=2',         BUILDOPTIONS: '-DARGTYPE=2',                                                          BUILDSCRIPT: '.ci/run.sh' }
          - { BUILDNAME: 'STOCK+ARGTYPE=3',         BUILDOPTIONS: '-DARGTYPE=3',                                                          BUILDSCRIPT: '.ci/run.sh' }
          - { BUILDNAME: 'STOCK+ARGTYPE=4',         BUILDOPTIONS: '-DARGTYPE=4',                                                          BUILDSCRIPT: '.ci/run.sh' }
    steps:
      - uses: actions/checkout@v2
      - name: install dependencies
        run: |
          sudo apt-get update -qq
          sudo apt-get install -y libgmp-dev valgrind libtool-bin clang-tools lcov ruby clang
          sudo apt-get remove -y libtommath1
          sudo gem install coveralls-lcov
          curl -s https://packagecloud.io/install/repositories/libtom/packages/script.deb.sh | sudo bash
          sudo apt-get install libtfm-git-dev libtommath-git-dev
      - name: run tests
        env:
          CC: "${{ matrix.cc }}"
          PR_NUMBER: ${{ github.event.number }}
          REPO_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        run: |
          bash "${{ matrix.config.BUILDSCRIPT }}" "${{ matrix.config.BUILDNAME }}" "-DUSE_LTM -DLTM_DESC" "makefile V=1"        "${{ matrix.config.BUILDOPTIONS }}" "-ltommath"
          bash "${{ matrix.config.BUILDSCRIPT }}" "${{ matrix.config.BUILDNAME }}" "-DUSE_TFM -DTFM_DESC" "makefile.shared V=1" "${{ matrix.config.BUILDOPTIONS }}" "-ltfm"
      - name: regular logs
        if: ${{ !failure() }}
        run: |
          cat gcc_1.txt || true
          cat gcc_2.txt || true
      - name: error logs
        if: ${{ failure() }}
        run: |
          cat gcc_1.txt || true
          cat gcc_2.txt || true
          cat test_std.txt || true
          cat test_err.txt || true
          cat tv.txt || true
      - name: pack build directory
        if: ${{ failure() }}
        run: |
          tar cJf build-${{ github.run_id }}.tar.xz --exclude ./build-${{ github.run_id }}.tar.xz .
      - name: upload Artifact
        if: ${{ failure() }}
        uses: actions/upload-artifact@v3
        with:
          name: build-${{ github.run_id }}.tar.xz
          path: build-${{ github.run_id }}.tar.xz
          retention-days: 1