# https://github.com/marketplace/actions/run-on-architecture
name: Foreign architectures

on: [push, pull_request]
concurrency:
  group: ${{ github.workflow }}-${{ github.ref }}
  cancel-in-progress: true

permissions:
  contents: read

jobs:
  multiarch:
    runs-on: ubuntu-22.04
    strategy:
      fail-fast: true
      matrix:
        include:
          - arch: armv7
            distro: ubuntu_latest
            cxx_flags: -Wno-psabi
            cmake_flags: -DHWY_CMAKE_ARM7:BOOL=ON
          - arch: ppc64le
            distro: ubuntu_latest
    steps:
    - name: Checkout code
      uses: actions/checkout@v4
    - 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 \
                cmake \
                libgtest-dev \
                ninja-build \
                ;
        run: |
          export CMAKE_BUILD_PARALLEL_LEVEL=2
          export CTEST_PARALLEL_LEVEL=2
          CXXFLAGS=${{ matrix.cxx_flags }} cmake -GNinja ${{ matrix.cmake_flags }} -DHWY_SYSTEM_GTEST=ON -DHWY_WARNINGS_ARE_ERRORS=ON -B out .
          cmake --build out
          ctest --test-dir out
  aarch64_cmake:
    name: Build and test ${{ matrix.name }} on AArch64
    runs-on: ubuntu-24.04-arm
    strategy:
      matrix:
        include:
          - name: Clang-18
            extra_deps: clang-18
            c_compiler: clang-18
            cxx_compiler: clang++-18
            cxx_standard: 17

          - name: GCC-14
            extra_deps: g++-14
            c_compiler: gcc-14
            cxx_compiler: g++-14
            cxx_flags: -ftrapv
            cxx_standard: 17

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

      - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.0.0

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

      - name: Build and test
        run: |
          export CMAKE_BUILD_PARALLEL_LEVEL=2
          export CTEST_PARALLEL_LEVEL=2
          CXXFLAGS="${{ matrix.cxx_flags }}" CC=${{ matrix.c_compiler }} CXX=${{ matrix.cxx_compiler }} cmake -DHWY_WARNINGS_ARE_ERRORS=ON -DCMAKE_CXX_STANDARD=${{ matrix.cxx_standard }} ${{ matrix.extra_cmake_flags }} -B out .
          cmake --build out
          ctest --test-dir out

  loongarch64_cmake:
    name: Build and test ${{ matrix.name }} on LoongArch64
    runs-on: ubuntu-24.04
    strategy:
      matrix:
        include:
          - name: GCC-14
            extra_deps: qemu-loongarch64
            c_compiler: loongarch64-unknown-linux-gnu-gcc
            cxx_compiler: loongarch64-unknown-linux-gnu-g++
            cxx_standard: 17
    steps:
      - name: get cross-tools evn
        run: |
          wget https://github.com/loongson/build-tools/releases/download/2025.02.21/x86_64-cross-tools-loongarch64-binutils_2.44-gcc_14.2.0-glibc_2.41.tar.xz
          sudo tar -xvf x86_64-cross-tools-loongarch64-binutils_2.44-gcc_14.2.0-glibc_2.41.tar.xz -C /opt
      - name: Harden Runner
        uses: step-security/harden-runner@6c439dc8bdf85cadbbce9ed30d1c7b959517bc49 # v2.12.2
        with:
          egress-policy: audit  # cannot be block - runner does git checkout

      - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.0.0

      - name: Install deps
        run: |
          wget https://github.com/loongson/build-tools/releases/download/2025.02.21/${{ matrix.extra_deps }}
          chmod +x ${{ matrix.extra_deps }}
          sudo mv ${{ matrix.extra_deps }} /opt/cross-tools/bin
      - name: Build and test
        run: |
          export CMAKE_BUILD_PARALLEL_LEVEL=2
          export CTEST_PARALLEL_LEVEL=2
          export PATH="/opt/cross-tools/bin:$PATH"
          export LD_LIBRARY_PATH="/opt/cross-tools/loongarch64-unknown-linux-gnu/lib:$LD_LIBRARY_PATH"
          export CC=${{ matrix.c_compiler }} CXX=${{ matrix.cxx_compiler }}
          cmake -DCMAKE_C_COMPILER_TARGET="loongarch64-unknown-linux-gnu" -DCMAKE_CXX_COMPILER_TARGET="loongarch64-unknown-linux-gnu" -DCMAKE_CROSSCOMPILING=true -DCMAKE_CROSSCOMPILING_EMULATOR="qemu-loongarch64;-cpu;max;-L;/opt/cross-tools/target" -DCMAKE_SYSTEM_NAME=Linux -DCMAKE_SYSTEM_PROCESSOR=loongarch64 -B out .
          cmake --build out
          ctest --test-dir out
