name: CI
on: [push, pull_request]

jobs:
  lint-python-scripts:
    runs-on: ubuntu-latest

    strategy:
      fail-fast: true

    steps:
      - uses: actions/checkout@v3

      - name: Install flake8 & mypy
        run: |
          sudo apt update
          sudo apt install python3 python3-pip
          export PIP_BREAK_SYSTEM_PACKAGES=1
          pip install flake8 mypy

      - name: Run flake8 on the project
        run: flake8 --ignore=E743 tests/*.py tests/utilities/*.py tests/generated_test_cases/*.py

      - name: Run mypy on the project
        run: mypy --disallow-incomplete-defs --no-implicit-optional tests/*.py tests/utilities/*.py tests/generated_test_cases/*.py
  build-and-run-tests:
    runs-on: ${{ matrix.os }}

    strategy:
      fail-fast: true
      matrix:
        os: [ubuntu-latest, macos-latest, windows-latest]
    steps:
      - uses: actions/checkout@v3
        with:
          submodules: true
      - if: ${{ matrix.os != 'macos-latest' }}
        name: Set up OpenWatcom
        uses: open-watcom/setup-watcom@v0
        with:
          version: '2.0'
      - if: ${{ matrix.os == 'ubuntu-latest' }}
        name: Install tools & libraries (Ubuntu)
        run: |
          sudo apt update
          sudo apt install python3 python3-pytest acpica-tools cmake gcc-multilib g++-multilib
          # https://github.com/actions/runner-images/issues/9491#issuecomment-1989718917
          sudo sysctl vm.mmap_rnd_bits=28
      - if: ${{ matrix.os == 'macos-latest' }}
        name: Install tools & libraries (MacOS)
        run: |
          export PIP_BREAK_SYSTEM_PACKAGES=1
          brew install python3 acpica cmake
          python3 -m pip install pytest
      - if: ${{ matrix.os == 'windows-latest' }}
        name: Install tools & libraries (Windows)
        run: |
          choco install python3 iasl cmake llvm
          python3 -m pip install pytest

      - name: Ensure reduced-hardware/unsized-frees/fmt-logging/no-kernel-init/builtin-string build compiles
        run: |
          cd ${{ github.workspace}}/tests/runner
          mkdir reduced-hw-build && cd reduced-hw-build
          cmake .. -DREDUCED_HARDWARE_BUILD=1 -DSIZED_FREES_BUILD=0 -DFORMATTED_LOGGING_BUILD=1 -DNATIVE_ALLOC_ZEROED=1 -DKERNEL_INITIALIZATION=0 -DBUILTIN_STRING=1
          cmake --build .

      - name: Run tests (64-bit)
        run: python3 ${{ github.workspace }}/tests/run_tests.py --bitness=64 --large --barebones

      # MacOS doesn't want to compile i386 (at least easily) so just ignore it:
      #     ld: warning: The i386 architecture is deprecated for macOS
      #     ld: dynamic executables or dylibs must link with libSystem.dylib for architecture i386
      #     clang: error: linker command failed with exit code 1 (use -v to see invocation)
      - if: ${{ matrix.os != 'macos-latest' }}
        name: Run tests (32-bit)
        run: python3 ${{ github.workspace }}/tests/run_tests.py --bitness=32 --large --barebones

      - if: ${{ matrix.os != 'macos-latest' }}
        name: Run tests (OpenWatcom)
        run: python3 ${{ github.workspace }}/tests/run_tests.py --large --barebones --watcom
