name: arch-emu
on:
  workflow_dispatch:
  push:
  pull_request:

concurrency: ci-arch-emu-${{ github.ref }}

jobs:

  alpine:
    runs-on: ubuntu-latest

    defaults:
      run:
        # Use emulated shell as default
        shell: alpine.sh {0}

    strategy:
      # Allow other runners in the matrix to continue if some fail
      fail-fast: false

      matrix:
        # For available CPU architectures, see:
        # https://github.com/marketplace/actions/setup-alpine-linux-environment
        arch: [x86, aarch64, armv7, ppc64le, riscv64, s390x]

    name: alpine (${{ matrix.arch }})

    steps:
      - name: checkout repository
        uses: actions/checkout@v4

      - name: install dependencies
        uses: jirutka/setup-alpine@v1
        with:
          arch: ${{ matrix.arch }}
          packages: >
            bash
            build-base
            ccache
            cmake
            gfortran
            eigen-dev
            lapack-dev

      - name: prepare ccache
        # create key with human readable timestamp
        # used in action/cache/restore and action/cache/save steps
        id: ccache-prepare
        run: |
          echo "key=ccache:alpine:${{ matrix.arch }}:${{ github.ref }}:$(date +"%Y-%m-%d_%H-%M-%S"):${{ github.sha }}" >> $GITHUB_OUTPUT

      - name: restore ccache
        # setup the GitHub cache used to maintain the ccache from one job to the next
        uses: actions/cache/restore@v4
        with:
          # location of the ccache of the chroot in the root file system
          path: /home/runner/rootfs/alpine-latest-${{ matrix.arch }}/home/runner/.ccache
          key: ${{ steps.ccache-prepare.outputs.key }}
          # Prefer caches from the same branch. Fall back to caches from the default branch.
          restore-keys: |
            ccache:alpine:${{ matrix.arch }}:${{ github.ref }}
            ccache:alpine:${{ matrix.arch }}

      - name: configure ccache
        env:
          CCACHE_MAX: ${{ matrix.ccache-max }}
        run: |
          test -d ~/.ccache || mkdir ~/.ccache
          echo "max_size = 20M" >> ~/.ccache/ccache.conf
          echo "compression = true" >> ~/.ccache/ccache.conf
          ccache -s
          which ccache

      - name: configure
        run: |
          echo "gcc --version"
          gcc --version
          echo "gcc -dumpmachine"
          gcc -dumpmachine
          echo " "
          mkdir -p ${GITHUB_WORKSPACE}/build && cd ${GITHUB_WORKSPACE}/build
          cmake \
            -DEXAMPLES=ON \
            -DMPI=OFF \
            -DICB=ON \
            -DEIGEN=ON \
            -DCMAKE_C_COMPILER_LAUNCHER="ccache" \
            -DCMAKE_CXX_COMPILER_LAUNCHER="ccache" \
            ..

      - name: build
        run: |
          cd ${GITHUB_WORKSPACE}/build
          cmake --build .

      - name: ccache status
        continue-on-error: true
        run: ccache -s

      - name: save ccache
        # Save the cache after we are done (successfully) building
        # This helps to retain the ccache even if the subsequent steps are failing.
        uses: actions/cache/save@v4
        with:
          path: /home/runner/rootfs/alpine-latest-${{ matrix.arch }}/home/runner/.ccache
          key: ${{ steps.ccache-prepare.outputs.key }}

      - name: test
        run: |
          cd ${GITHUB_WORKSPACE}/build
          ctest . || ctest . --rerun-failed --output-on-failure
