#
# Copyright 2020 - 2022 Free Software Foundation, Inc.
#
# This file is part of VOLK
#
# SPDX-License-Identifier: LGPL-3.0-or-later
#

name: Run VOLK tests

on: [push, pull_request]

jobs:
  build-ubuntu:
    name: Build on ${{ matrix.compiler.distro }} ${{ matrix.compiler.name }}

    strategy:
      fail-fast: false
      matrix:
        compiler:
          - { name: g++-11, cc: gcc-11, cxx: g++-11, distro: ubuntu-22.04 }
          - { name: g++-11, cc: gcc-11, cxx: g++-11, distro: ubuntu-22.04-arm }
          - { name: g++-12, cc: gcc-12, cxx: g++-12, distro: ubuntu-22.04 }
          - { name: g++-12, cc: gcc-12, cxx: g++-12, distro: ubuntu-22.04-arm }
          - { name: g++-13, cc: gcc-13, cxx: g++-13, distro: ubuntu-24.04 }
          - { name: g++-13, cc: gcc-13, cxx: g++-13, distro: ubuntu-24.04-arm }
          - { name: g++-14, cc: gcc-14, cxx: g++-14, distro: ubuntu-24.04 }
          - { name: g++-14, cc: gcc-14, cxx: g++-14, distro: ubuntu-24.04-arm }
          - { name: clang-14, cc: clang-14, cxx: clang++-14, distro: ubuntu-22.04 }
          # - { name: clang-14, cc: clang-14, cxx: clang++-14, distro: ubuntu-22.04-arm } # possibly broken runner: https://github.com/actions/runner-images/issues/8659
          - { name: clang-15, cc: clang-15, cxx: clang++-15, distro: ubuntu-22.04 }
          - { name: clang-15, cc: clang-15, cxx: clang++-15, distro: ubuntu-22.04-arm }
          - { name: clang-16, cc: clang-16, cxx: clang++-16, distro: ubuntu-24.04 }
          - { name: clang-16, cc: clang-16, cxx: clang++-16, distro: ubuntu-24.04-arm }
          - { name: clang-17, cc: clang-17, cxx: clang++-17, distro: ubuntu-24.04 }
          - { name: clang-17, cc: clang-17, cxx: clang++-17, distro: ubuntu-24.04-arm }
          - { name: clang-18, cc: clang-18, cxx: clang++-18, distro: ubuntu-24.04 }
          - { name: clang-18, cc: clang-18, cxx: clang++-18, distro: ubuntu-24.04-arm }
          - { name: clang-19, cc: clang-19, cxx: clang++-19, distro: ubuntu-24.04 }
          - { name: clang-19, cc: clang-19, cxx: clang++-19, distro: ubuntu-24.04-arm }

    runs-on: ${{ matrix.compiler.distro }}

    steps:
      - uses: actions/checkout@v4
        with:
          submodules: "recursive"
      - name: Install dependencies
        run: sudo apt install python3-mako liborc-dev libgtest-dev libfmt-dev ${{ matrix.compiler.name }}
      - name: Configure
        env:
          CC: ${{ matrix.compiler.cc }}
          CXX: ${{ matrix.compiler.cxx }}
        run: mkdir build && cd build && cmake -DCMAKE_C_FLAGS="-Werror" -DCMAKE_CXX_FLAGS="-Werror" -DBUILD_EXECUTABLE=ON ..
      - name: Build
        run: |
          echo "Build with $(nproc) thread(s)"
          cmake --build build -j$(nproc)
      - name: Print info
        run: |
          if [ -f ./build/cpu_features/list_cpu_features ]; then
            ./build/cpu_features/list_cpu_features
          fi
          ./build/apps/volk-config-info --alignment
          ./build/apps/volk-config-info --avail-machines
          ./build/apps/volk-config-info --all-machines
          ./build/apps/volk-config-info --malloc
          ./build/apps/volk-config-info --cc
      - name: Test
        run: |
          cd build
          ctest -V

  build-ubuntu-arm:
    # The host should always be linux
    # see: https://github.com/uraimo/run-on-arch-action
    runs-on: ubuntu-22.04
    name: Build on ${{ matrix.distro }} ${{ matrix.arch }} ${{ matrix.compiler.name }}

    # Run steps on a matrix of compilers and possibly archs.
    strategy:
      fail-fast: false
      matrix:
        include:
          - arch: armv7
            distro: ubuntu22.04
            compiler: { name: g++, cc: gcc, cxx: g++ }
            cmakeargs: "-DCMAKE_TOOLCHAIN_FILE=../cmake/Toolchains/arm_cortex_a72_hardfp_native.cmake"
          - arch: ppc64le
            distro: ubuntu22.04
            compiler: { name: g++-12, cc: gcc-12, cxx: g++-12 }
          - arch: s390x
            distro: ubuntu22.04
            compiler: { name: g++-12, cc: gcc-12, cxx: g++-12 }
          - arch: riscv64
            distro: ubuntu22.04
            compiler: { name: g++-12, cc: gcc-12, cxx: g++-12 }

    steps:
      - uses: actions/checkout@v4
        with:
          submodules: "recursive"
      - uses: uraimo/run-on-arch-action@v3
        name: Build in non-x86 container
        id: build
        with:
          arch: ${{ matrix.arch }}
          distro: ${{ matrix.distro }}

          # Not required, but speeds up builds
          githubToken: ${{ github.token }}

          setup: |
            mkdir -p "${PWD}/build"

          dockerRunArgs: |
            --volume "${PWD}:/volk"

          env: | # YAML, but pipe character is necessary
            CC: ${{ matrix.compiler.cc }}
            CXX: ${{ matrix.compiler.cxx }}

          shell: /bin/sh

          install: |
            case "${{ matrix.distro }}" in
              ubuntu*|jessie|stretch|buster)
                apt-get update -q -y
                apt-get install -q -y git cmake python3-mako liborc-dev libgtest-dev libfmt-dev ${{ matrix.compiler.name }}
                ;;
              fedora*)
                dnf -y update
                dnf -y install git which
                ;;
            esac

          run: |
            cd /volk
            cd build
            cmake -DCMAKE_C_FLAGS="-Werror" -DCMAKE_CXX_FLAGS="-Werror" -DBUILD_EXECUTABLE=ON ${{ matrix.cmakeargs }} ..
            echo "Build with $(nproc) thread(s)"
            make -j$(nproc)
            if [ -f ./cpu_features/list_cpu_features ]; then
              ./cpu_features/list_cpu_features
            fi
            ./apps/volk-config-info --alignment
            ./apps/volk-config-info --avail-machines
            ./apps/volk-config-info --all-machines
            ./apps/volk-config-info --malloc
            ./apps/volk-config-info --cc
            ctest -V

  build-ubuntu-static:
    name: Build static on ubuntu-22.04
    runs-on: ubuntu-22.04

    steps:
      - uses: actions/checkout@v4
        with:
          submodules: "recursive"
      - name: dependencies
        run: sudo apt install python3-mako liborc-dev libgtest-dev libfmt-dev
      - name: configure
        run: mkdir build && cd build && cmake -DENABLE_STATIC_LIBS=True -DCMAKE_C_FLAGS="-Werror" -DCMAKE_CXX_FLAGS="-Werror" -DBUILD_EXECUTABLE=ON ..
      - name: build
        run: cmake --build build -j$(nproc)
      - name: Print info
        run: |
          ./build/cpu_features/list_cpu_features
          ./build/apps/volk-config-info --alignment
          ./build/apps/volk-config-info --avail-machines
          ./build/apps/volk-config-info --all-machines
          ./build/apps/volk-config-info --malloc
          ./build/apps/volk-config-info --cc
      - name: test
        run: cd build && ctest -V

  build-windows:
    runs-on: windows-latest

    steps:
      - uses: actions/checkout@v4
        with:
          submodules: "recursive"
      - name: dependencies
        run: pip install mako
      - name: configure
        run: mkdir build && cd build && cmake ..
      - name: build
        run: cmake --build build --config Release --target INSTALL -j4
      - name: test
        run: cd build && ctest -V -C Release

  # build-windows-msys2:
  #   name: Build on windows-latest using MinGW and MSYS2

  #   runs-on: windows-latest
  #   steps:
  #     - uses: msys2/setup-msys2@v2
  #       with:
  #         update: true
  #         install: >-
  #           base-devel
  #           git
  #           mingw-w64-x86_64-gcc-libs
  #           mingw-w64-x86_64-orc
  #           python
  #           python-mako
  #           python-six
  #           mingw-w64-x86_64-gcc
  #           mingw-w64-x86_64-cmake
  #     - uses: actions/checkout@v4
  #     - name: Checkout submodules
  #       run: git submodule update --init --recursive
  #     - name: Configure
  #       shell: msys2 {0}
  #       run: mkdir build && cd build && cmake -G "MSYS Makefiles" -DCMAKE_BUILD_TYPE=Release -DPYTHON_EXECUTABLE=/usr/bin/python3 -DCMAKE_CXX_FLAGS="-Werror" ..
  #     - name: Build
  #       shell: msys2 {0}
  #       run: cd build && make -j$(nproc)
  #     - name: Test
  #       shell: msys2 {0}
  #       run: |
  #         cd build
  #         ctest -V

  build-macos:
    strategy:
      matrix:
        os: [macos-15-intel, macos-latest]

    runs-on: ${{ matrix.os }}

    steps:
      - uses: actions/checkout@v4
        with:
          submodules: "recursive"
      - name: dependencies
        run: pip3 install --break-system-packages mako && brew install orc
      - name: configure
        run: mkdir build && cd build && cmake -DCMAKE_C_FLAGS="-Werror" -DCMAKE_CXX_FLAGS="-Werror" -DBUILD_EXECUTABLE=ON ..
      - name: build
        run: cmake --build build --config Debug -j4
      - name: Print info
        run: |
          ./build/cpu_features/list_cpu_features
          # ./build/apps/volk-config-info --alignment
          # ./build/apps/volk-config-info --avail-machines
          # ./build/apps/volk-config-info --all-machines
          # ./build/apps/volk-config-info --malloc
          # ./build/apps/volk-config-info --cc
      - name: test
        run: cd build && ctest -V
