name: Auto-Sync
on:
  push:
    paths:
      - "suite/auto-sync/**"
      - ".github/workflows/auto-sync.yml"
  pull_request:

# Stop previous runs on the same branch on new push
concurrency:
  group: ${{ github.workflow }}-${{ github.ref }}
  cancel-in-progress: true

jobs:
  check:
    runs-on: ubuntu-24.04
    defaults:
      run:
        working-directory: suite/auto-sync/
    steps:
      - name: Set up Python
        uses: actions/setup-python@v4
        with:
          python-version: '3.11'

      - name: Check out repository
        uses: actions/checkout@v4
        with:
          submodules: true

      - name: Install auto-sync package
        run: |
          pip install .

      - name: Check formatting
        run: |
          python3.11 -m black --check src/autosync

      - name: Install llvm-mc
        run: |
          sudo apt install llvm-18
          llvm-mc-18 --version
          FileCheck-18 --version
          sudo ln -s $(whereis -b llvm-mc-18 | grep -Eo "/.*") /usr/local/bin/llvm-mc
          sudo ln -s $(whereis -b FileCheck-18 | grep -Eo "/.*") /usr/local/bin/FileCheck
          llvm-mc --version
          FileCheck --version

      - name: Clone llvm-capstone
        run: |
          git clone https://github.com/capstone-engine/llvm-capstone.git vendor/llvm_root

      - name: Build llvm-tblgen
        run: |
          cd vendor/llvm_root
          mkdir build
          cd build
          cmake -G Ninja -DCMAKE_BUILD_TYPE=Debug ../llvm
          cmake --build . --target llvm-tblgen --config Debug
          cd ../../../

      - name: Test Header patcher
        run: |
          python -m unittest src/autosync/Tests/test_header_patcher.py
          python -m unittest src/autosync/Tests/test_mcupdater.py

      - name: Remove llvm-mc
        run: |
          sudo apt remove llvm-18
          sudo rm /usr/local/bin/llvm-mc
          sudo rm /usr/local/bin/FileCheck

      - name: Test generation of inc files
        run: |
          ./src/autosync/ASUpdater.py -d -a AArch64 -s IncGen
          ./src/autosync/ASUpdater.py -d -a Alpha -s IncGen
          ./src/autosync/ASUpdater.py -d -a ARM -s IncGen
          ./src/autosync/ASUpdater.py -d -a PPC -s IncGen
          ./src/autosync/ASUpdater.py -d -a LoongArch -s IncGen

      - name: CppTranslator - Patch tests
        run: |
          python -m unittest src/autosync/cpptranslator/Tests/test_patches.py

      - name: CppTranslator - Differ tests
        run: |
          python -m unittest src/autosync/cpptranslator/Tests/test_differ.py

      - name: CppTranslator - Test translation
        run: |
          ./src/autosync/ASUpdater.py --ci -d -a AArch64 -s Translate
          ./src/autosync/ASUpdater.py --ci -d -a ARM -s Translate
          ./src/autosync/ASUpdater.py --ci -d -a PPC -s Translate
          ./src/autosync/ASUpdater.py --ci -d -a LoongArch -s Translate

      - name: Differ - Test save file is up-to-date
        run: |
          ./src/autosync/cpptranslator/Differ.py -a AArch64 --check_saved
          ./src/autosync/cpptranslator/Differ.py -a ARM --check_saved
          ./src/autosync/cpptranslator/Differ.py -a PPC --check_saved
          ./src/autosync/cpptranslator/Differ.py -a LoongArch --check_saved
