name: Linux build

on:
  push:
    branches:
      - '*'
  pull_request:
    branches:
      - '*'
  workflow_dispatch:

env:
  BUILD_TYPE: Debug

jobs:
  linux-build:
    name: ${{ matrix.os.image }} ${{ matrix.os.arch }} (Build)
    runs-on: ${{ matrix.os.label }}
    strategy:
      matrix:
        os:
          # x86_64 (https://github.com/actions/runner-images/tree/main/images/ubuntu)
          - { arch: x86_64,  label: ubuntu-24.04,     image: archlinux:base-devel }
          - { arch: x86_64,  label: ubuntu-24.04,     image: debian:11 }
          - { arch: x86_64,  label: ubuntu-24.04,     image: debian:12 }
          - { arch: x86_64,  label: ubuntu-24.04,     image: fedora:40 }
          - { arch: x86_64,  label: ubuntu-24.04,     image: fedora:41 }
          - { arch: x86_64,  label: ubuntu-24.04,     image: opensuse/leap:15.6 }
          - { arch: x86_64,  label: ubuntu-24.04,     image: ubuntu:22.04 }
          - { arch: x86_64,  label: ubuntu-24.04,     image: ubuntu:24.04 }
          - { arch: x86_64,  label: ubuntu-24.04,     image: ubuntu:25.04 }
          # ARM64 (https://github.com/actions/partner-runner-images)
          - { arch: aarch64, label: ubuntu-24.04-arm, image: debian:11 }
          - { arch: aarch64, label: ubuntu-24.04-arm, image: debian:12 }
          - { arch: aarch64, label: ubuntu-24.04-arm, image: fedora:40 }
          - { arch: aarch64, label: ubuntu-24.04-arm, image: fedora:41 }
          - { arch: aarch64, label: ubuntu-24.04-arm, image: opensuse/leap:15.6 }
          - { arch: aarch64, label: ubuntu-24.04-arm, image: ubuntu:22.04 }
          - { arch: aarch64, label: ubuntu-24.04-arm, image: ubuntu:24.04 }
          - { arch: aarch64, label: ubuntu-24.04-arm, image: ubuntu:25.04 }

    container: ${{ matrix.os.image }}

    steps:
    - name: Install prerequisites
      run: |
        if command -v pacman; then
          pacman -Syu --noconfirm sudo
        elif command -v dnf; then
          dnf update -y
          dnf install -y sudo
        elif command -v zypper; then
          zypper update -y
          zypper install -y sudo tar gzip
        elif command -v apt-get; then
          apt-get update -y -qq
          apt-get install -y -qq sudo
        fi

    - uses: actions/checkout@v5

    - name: Build libcpuid
      run: ./scripts/build_libcpuid.sh -t "$BUILD_TYPE"

    - name: Build CPU-X
      run: ./scripts/build_cpu_x.sh -s "$GITHUB_WORKSPACE" -t "$BUILD_TYPE"

    - name: Run tests
      run: ninja -C build test

    - name: Run CPU-X
      run: |
        error=0
        sudo CPUX_BCLK=100 cpu-x --issue-fmt || error=1
        cat /tmp/cpu-x.log
        cat /tmp/cpu-x-daemon.log
        exit $error
