name: AArch64

on: [ push, pull_request ]

env:
  BUILD_TYPE: Release

jobs:
  build:
    strategy:
      matrix:
        os: [ ubuntu-latest ]
      fail-fast: false

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

    steps:

      - name: Install build dependencies
        if: ${{matrix.os == 'ubuntu-latest'}}
        run: >
          sudo apt-get update &&
          sudo apt-get install
          gcc-10-aarch64-linux-gnu
          g++-10-aarch64-linux-gnu
          ninja-build
          qemu-user

      - name: Checkout dynarmic repo
        uses: actions/checkout@v2

      - name: Ccache
        uses: hendrikmuhs/ccache-action@v1.2

      - name: Checkout ext-boost repo
        uses: actions/checkout@v2
        with:
          repository: MerryMage/ext-boost
          path: externals/ext-boost

      - name: Configure CMake for AArch64
        env:
          CC: aarch64-linux-gnu-gcc-10
          CXX: aarch64-linux-gnu-g++-10
        run: >
          cmake
          -B ${{github.workspace}}/build-arm64
          -DBOOST_INCLUDEDIR=${{github.workspace}}/externals/ext-boost
          -DCMAKE_BUILD_TYPE=${{env.BUILD_TYPE}}
          -DDYNARMIC_TESTS_USE_UNICORN=0
          -DDYNARMIC_USE_LLVM=0
          -G Ninja

      - name: Build AArch64
        working-directory: ${{github.workspace}}/build-arm64
        run: cmake --build . --config Release

      - name: Configure CMake for x86_64
        env:
          CC: gcc-10
          CXX: g++-10
        run: >
          cmake
          -B ${{github.workspace}}/build-x64
          -DBOOST_INCLUDEDIR=${{github.workspace}}/externals/ext-boost
          -DCMAKE_BUILD_TYPE=${{env.BUILD_TYPE}}
          -DCMAKE_C_COMPILER_LAUNCHER=ccache
          -DCMAKE_CXX_COMPILER_LAUNCHER=ccache
          -DDYNARMIC_TESTS_USE_UNICORN=0
          -DDYNARMIC_USE_LLVM=0
          -G Ninja

      - name: Build x86_64
        working-directory: ${{github.workspace}}/build-x64
        run: cmake --build . --config Release

      - name: Basic tests
        working-directory: ${{github.workspace}}
        run: qemu-aarch64 -L /usr/aarch64-linux-gnu ./build-arm64/tests/dynarmic_tests -d yes

      - name: Test against x86_64 implementation (A32, thumb)
        working-directory: ${{github.workspace}}
        run: |
          diff <(qemu-aarch64 -L /usr/aarch64-linux-gnu ./build-arm64/tests/dynarmic_test_generator thumb 42 1 100000) <(./build-x64/tests/dynarmic_test_generator thumb 42 1 100000)
          diff <(qemu-aarch64 -L /usr/aarch64-linux-gnu ./build-arm64/tests/dynarmic_test_generator thumb 42 10 10000) <(./build-x64/tests/dynarmic_test_generator thumb 42 10 10000)
          diff <(qemu-aarch64 -L /usr/aarch64-linux-gnu ./build-arm64/tests/dynarmic_test_generator thumb 42 100 1000) <(./build-x64/tests/dynarmic_test_generator thumb 42 100 1000)

      - name: Test against x86_64 implementation (A32, arm)
        working-directory: ${{github.workspace}}
        run: |
          diff <(qemu-aarch64 -L /usr/aarch64-linux-gnu ./build-arm64/tests/dynarmic_test_generator arm 42 1 100000) <(./build-x64/tests/dynarmic_test_generator arm 42 1 100000)
          diff <(qemu-aarch64 -L /usr/aarch64-linux-gnu ./build-arm64/tests/dynarmic_test_generator arm 42 10 10000) <(./build-x64/tests/dynarmic_test_generator arm 42 10 10000)
          diff <(qemu-aarch64 -L /usr/aarch64-linux-gnu ./build-arm64/tests/dynarmic_test_generator arm 42 100 1000) <(./build-x64/tests/dynarmic_test_generator arm 42 100 1000)

      - name: Test against x86_64 implementation (A64)
        working-directory: ${{github.workspace}}
        run: |
          diff <(qemu-aarch64 -L /usr/aarch64-linux-gnu ./build-arm64/tests/dynarmic_test_generator a64 42 1 100000) <(./build-x64/tests/dynarmic_test_generator a64 42 1 100000)
          diff <(qemu-aarch64 -L /usr/aarch64-linux-gnu ./build-arm64/tests/dynarmic_test_generator a64 42 10 10000) <(./build-x64/tests/dynarmic_test_generator a64 42 10 10000)
          diff <(qemu-aarch64 -L /usr/aarch64-linux-gnu ./build-arm64/tests/dynarmic_test_generator a64 42 100 1000) <(./build-x64/tests/dynarmic_test_generator a64 42 100 1000)
