permissions: read-all
name: Meson build / test
on: [push, pull_request]
concurrency:
  group: ${{ github.workflow }}-${{ github.ref }}
  cancel-in-progress: true
env:
  meson_setup_args: -Dwarning_level=2 -Dbuildtype=debugoptimized -Dgtest:cpp_eh=default

jobs:
  ubuntu_jammy_x86_64:
    name: Meson build and test ${{ matrix.compiler }} (C++${{ matrix.cxx_standard }}) on Jammy x86_64
    runs-on: ubuntu-22.04
    strategy:
      matrix:
        compiler: [Clang-15, GCC-12]
        cxx_standard: [11]
        include:
          - compiler: Clang-15
            extra_deps: clang-15
            c_compiler: clang-15
            cxx_compiler: clang++-15
            cxx_flags: -DHWY_DISABLED_TARGETS=0x59d8  # disable every x86 target except AVX2

          - compiler: GCC-12
            extra_deps: g++-12
            c_compiler: gcc-12
            cxx_compiler: g++-12
            cxx_flags: -ftrapv -DHWY_DISABLED_TARGETS=0x59d8 

    steps:
      - name: Harden Runner
        uses: step-security/harden-runner@df199fb7be9f65074067a9eb93f12bb4c5547cf2 # v2.13.3
        with:
          egress-policy: audit  # cannot be block - runner does git checkout

      - uses: actions/checkout@1af3b93b6815bc44a9784bd300feb67ff0d1eeb3 # v4.0.0

        # meson on jammy is at version 0.61.2, so install through pipx
      - name: Install build requirements
        run: pipx install meson

      - name: Install deps
        run: sudo apt-get install libgtest-dev ${{ matrix.extra_deps }}

      - name: Configure Build
        run: meson setup out -Dwerror=true -Dcpp_std=c++${{ matrix.cxx_standard }} ${{env.meson_setup_args}} ${{ matrix.extra_setup_args }}
        env:
          CXX: ${{matrix.cxx_compiler}}
          CC: ${{matrix.c_compiler}}
          CXXFLAGS: ${{ matrix.cxx_flags }}

      - name: Report Targets
        run: |
          meson compile -C out hwy_list_targets
          out/hwy_list_targets

      - name: Build
        run: meson compile -C out -j 2 -v

      - name: Test
        run: meson test -C out -j 2 --print-errorlogs

  ubuntu_noble_x86_64:
    name: Meson build and test ${{ matrix.compiler }} (C++${{ matrix.cxx_standard }}) on Noble x86_64
    runs-on: ubuntu-24.04
    strategy:
      matrix:
        compiler: [Clang-18, GCC-14]
        cxx_standard: [17]
        include:
            - compiler: Clang-18
              extra_deps: clang-18
              c_compiler: clang-18
              cxx_compiler: clang++-18
              cxx_flags: -DHWY_DISABLED_TARGETS=0x59d8
            
            - compiler: GCC-14
              c_compiler: gcc-14
              cxx_compiler: g++-14
              cxx_flags: -ftrapv -DHWY_DISABLED_TARGETS=0x59d8

    steps:
      - name: Harden Runner
        uses: step-security/harden-runner@df199fb7be9f65074067a9eb93f12bb4c5547cf2 # v2.13.3
        with:
          egress-policy: audit  # cannot be block - runner does git checkout

      - uses: actions/checkout@1af3b93b6815bc44a9784bd300feb67ff0d1eeb3 # v4.0.0

        # meson on noble is at 1.3.2
      - name: Install deps
        run: sudo apt-get install ninja-build meson libgtest-dev ${{ matrix.extra_deps }}

      - name: Configure Build
        run: meson setup out -Dcpp_std=c++${{ matrix.cxx_standard }} ${{env.meson_setup_args}} ${{ matrix.extra_setup_args }}
        env:
          CXX: ${{matrix.cxx_compiler}}
          CC: ${{matrix.c_compiler}}
          CXXFLAGS: ${{ matrix.cxx_flags }}
      
      # builds libhwy and compiles the target list executable
      - name: Report Targets
        run: |
          meson compile -C out -v hwy_list_targets
          out/hwy_list_targets

      - name: Build
        run: meson compile -C out -j 2 -v

      - name: Test
        run: meson test -C out -j 2 --print-errorlogs

  ubuntu_noble_arm:
    name: Meson build and test ${{ matrix.compiler }} (C++${{ matrix.cxx_standard}}) on Noble AArch64
    runs-on: ubuntu-24.04-arm
    strategy:
      matrix:
        compiler: [GCC-14]
        cxx_standard: [17]
        include:

          - compiler: GCC-14
            extra_deps: g++-14
            c_compiler: gcc-14
            cxx_compiler: g++-14
            cxx_flags: -ftrapv -DHWY_DISABLED_TARGETS=0x318c0000  # Only target HWY_NEON_BF16

    steps:
      - name: Harden Runner
        uses: step-security/harden-runner@df199fb7be9f65074067a9eb93f12bb4c5547cf2 # v2.13.3
        with:
          egress-policy: audit  # cannot be block - runner does git checkout

      - uses: actions/checkout@1af3b93b6815bc44a9784bd300feb67ff0d1eeb3 # v4.0.0

      - name: Install deps
        run: sudo apt-get install ninja-build meson libgtest-dev ${{ matrix.extra_deps }}

      - name: Configure Build
        run: meson setup out -Dcpp_std=c++${{ matrix.cxx_standard }} ${{env.meson_setup_args}} ${{ matrix.extra_setup_args }}
        env:
          CXX: ${{matrix.cxx_compiler}}
          CC: ${{matrix.c_compiler}}
          CXXFLAGS: ${{ matrix.cxx_flags }}
      
      # builds libhwy and compiles the target list executable
      - name: Report Targets
        run: |
          meson compile -C out -v hwy_list_targets
          out/hwy_list_targets

      - name: Build
        run: meson compile -C out -j 2 -v

      - name: Test
        run: meson test -C out -j 2 --print-errorlogs ${{ matrix.extra_test_args }}

  win:
    name: Meson build and test ${{ matrix.compiler }} (C++${{matrix.cxx_standard}}) on ${{ matrix.os }}
    runs-on: ${{ matrix.os }}
    continue-on-error: true
    strategy:
      matrix:
        include:
          # these compilers currently default to what is installed on the runner images
          - os: windows-2022
            compiler: LLVM  # @ 20.1.8
            c_compiler: clang-cl
            cxx_compiler: clang-cl
            extra_setup_args: -Dvsenv=true
            extra_test_args: --timeout-multiplier=5
            cxx_flags: /DHWY_DISABLED_TARGETS=0x59d8
            cxx_standard: 20

    steps:
      - name: Harden Runner
        uses: step-security/harden-runner@df199fb7be9f65074067a9eb93f12bb4c5547cf2 # v2.13.3
        with:
          egress-policy: audit  # cannot be block - runner does git checkout

      - uses: actions/checkout@1af3b93b6815bc44a9784bd300feb67ff0d1eeb3 # v4.0.0

        # update the meson gtest wrap because we're going to use a c++17 standard
      - name: Install deps
        run: |
            pipx install meson

      - name: Configure Build
        run: meson setup out -Dcpp_std=c++${{ matrix.cxx_standard }} ${{env.meson_setup_args}} ${{ matrix.extra_setup_args }}
        env:
          CXX: ${{matrix.cxx_compiler}}
          CC: ${{matrix.c_compiler}}
          CXXFLAGS: ${{ matrix.cxx_flags }}
      
      # builds libhwy and compiles the target list executable
      - name: Report Targets
        run: |
          meson compile -C out hwy_list_targets
          out\hwy_list_targets

      - name: Build
        run: meson compile -C out -j 2 -v

      - name: Test
        run: meson test -C out -j 2 --print-errorlogs ${{ matrix.extra_test_args }}

  run_on_arch:
    runs-on: ubuntu-22.04
    strategy:
      fail-fast: true
      matrix:
        include:
          - arch: ppc64le
            distro: ubuntu_latest
    steps:
    - name: Checkout code
      uses: actions/checkout@v6.0.0
    - name: Build and test
      uses: uraimo/run-on-arch-action@v3.0.1
      id: build
      with:
        arch: ${{ matrix.arch }}
        distro: ${{ matrix.distro }}
        # Not required, but speeds up builds
        githubToken: ${{ github.token }}
        install: |
          apt-get update -q -y
          apt-get install -q -y --no-install-recommends \
                build-essential \
                libgtest-dev \
                ninja-build \
                meson \
                ;
        run: |
          CXXFLAGS=${{ matrix.cxx_flags }} meson setup out ${{env.meson_setup_args}} ${{ matrix.meson_flags }}
          
          # compile just the libhwy and list targets
          meson compile -C out -v hwy_list_targets
          out/hwy_list_targets

          # dry run the rest of the compile commands
          meson compile -C out -j 2 -v --ninja-args=-n,-d,explain
          # meson test -C out -j 2 --print-errorlogs