name: CMake

on:
  push:
    branches: [ '*' ]
  pull_request:
    branches: [ "main" ]

env:
  # Customize the CMake build type here (Release, Debug, RelWithDebInfo, etc.)
  BUILD_TYPE: Release

jobs:
  build:
    # The CMake configure and build commands are platform agnostic and should work equally well on Windows or Mac.
    # You can convert this to a matrix build if you need cross-platform coverage.
    # See: https://docs.github.com/en/free-pro-team@latest/actions/learn-github-actions/managing-complex-workflows#using-a-build-matrix
    runs-on: ubuntu-latest

    strategy:
      matrix:
        sqisign_build_type: [opt]

    steps:
    - uses: actions/checkout@v3
    - name: Set up Python 3.10
      uses: actions/setup-python@v3
      with:
        python-version: "3.10"
    
    - name: Install dependencies Valgrind, GMP, Doxygen, TeX
      run: |
          sudo apt --fix-missing install valgrind libgmp-dev doxygen texlive-xetex
          echo "Valgrind installed"
          
    - name: Install Valgrind dependencies
      run: |
        python -m pip install --upgrade pip
        pip install ValgrindCI

    - name: Configure CMake
      # Configure CMake in a 'build' subdirectory. `CMAKE_BUILD_TYPE` is only required if you are using a single-configuration generator such as make.
      # See https://cmake.org/cmake/help/latest/variable/CMAKE_BUILD_TYPE.html?highlight=cmake_build_type
      run: cmake -B ${{github.workspace}}/build -DCMAKE_BUILD_TYPE=${{env.BUILD_TYPE}} -DSQISIGN_BUILD_TYPE=${{ matrix.sqisign_build_type }}

    - name: Build
      # Build your program with the given configuration
      run: cmake --build ${{github.workspace}}/build --config ${{env.BUILD_TYPE}}

    - name: Build documentation
      # Create html and latex documentation, TODO: do we need different docs for ref and opt?
      run: doxygen Doxyfile && cd latex && xelatex refman

    - name: Upload latex documentation
      uses: actions/upload-artifact@v3
      with:
        name: docs
        path: latex/refman.pdf

    - name: Test
      working-directory: ${{github.workspace}}/build
      # Execute tests defined by the CMake configuration.
      # See https://cmake.org/cmake/help/latest/manual/ctest.1.html for more detail
      run: ctest -C ${{env.BUILD_TYPE}}
    
    - name: Examples
      if: false
      working-directory: ${{github.workspace}}/build/apps
      run: |
          ./PQCgenKAT_sign_lvl1
          ./PQCgenKAT_sign_lvl1_varp6983
          ./example_nistapi_lvl1
          ./example_nistapi_lvl1_varp6983

    - name: CT-Tests
      # TODO: re-enable for those tests that should be ct
      if: false
      run: |
          rm -rf build
          cmake -Bbuild -DENABLE_CT_TESTING=ON -DCMAKE_BUILD_TYPE=Debug -DSQISIGN_BUILD_TYPE=${{ matrix.sqisign_build_type }}
          cmake --build build
          # valgrind --track-origins=yes build/
          # valgrind --track-origins=yes build/
          # valgrind --track-origins=yes build/
          # valgrind --track-origins=yes build/

    - name: Memcheck
      run: |
          rm -rf build
          cmake -Bbuild -DSQISIGN_BUILD_TYPE=${{ matrix.sqisign_build_type }} -DSQISIGN_TEST_REPS=10
          cmake --build build
          ctest -T memcheck --test-dir build
      if: false

    - name: Address Sanitizer ASAN
      run: |
          rm -rf build
          cmake -Bbuild -DCMAKE_BUILD_TYPE=ASAN -DSQISIGN_BUILD_TYPE=${{ matrix.sqisign_build_type }} -DCMAKE_C_COMPILER=clang
          cmake --build build
          ctest -v --test-dir build

    - name: Memory Sanitizer MSAN
      run: |
          rm -rf build
          cmake -Bbuild -DCMAKE_BUILD_TYPE=MSAN -DSQISIGN_BUILD_TYPE=${{ matrix.sqisign_build_type }} -DCMAKE_C_COMPILER=clang
          cmake --build build
          ctest -v --test-dir build

    - name: Leak Sanitizer LSAN
      run: |
          rm -rf build
          cmake -Bbuild -DCMAKE_BUILD_TYPE=LSAN -DSQISIGN_BUILD_TYPE=${{ matrix.sqisign_build_type }} -DCMAKE_C_COMPILER=clang
          cmake --build build
          ctest -v --test-dir build

    - name: Undefined Behavior Sanitizer UBSAN
      run: |
          rm -rf build
          cmake -Bbuild -DCMAKE_BUILD_TYPE=UBSAN -DSQISIGN_BUILD_TYPE=${{ matrix.sqisign_build_type }} -DCMAKE_C_COMPILER=clang
          cmake --build build
          ctest -v --test-dir build