name: Build and Test

on:
  pull_request:
  push:
    branches:
      - main
      # For testing CI without starting a pull request:
      - test/*

permissions:
  contents: read

concurrency:
  group: ${{ github.workflow }}-${{ github.ref }}
  cancel-in-progress: ${{ github.ref != 'refs/heads/main' }}

jobs:
  check_lint:
    name: Check Lint
    runs-on: ubuntu-22.04
    steps:
      - uses: actions/checkout@v6
      - uses: pre-commit/action@v3.0.1

  linux_build_and_test:
    name: Linux (cpu, ${{ matrix.arch }})
    needs: check_lint
    strategy:
      fail-fast: false
      matrix:
        arch: ['x86_64', 'aarch64']
    runs-on: ${{ matrix.arch == 'x86_64' && 'ubuntu-22.04' || 'ubuntu-22.04-arm' }}
    steps:
      - uses: actions/checkout@v6
      - uses: ./.github/actions/setup-linux
      - uses: ./.github/actions/build-linux
      - uses: ./.github/actions/test-linux
      - run: df -h

  cuda_build_and_test:
    name: Linux (${{ matrix.toolkit }}, ${{ matrix.arch }})
    if: github.repository == 'ml-explore/mlx'
    needs: check_lint
    strategy:
      fail-fast: false
      matrix:
        arch: ['x86_64', 'aarch64']
        toolkit: ['cuda-12.6', 'cuda-12.9']
    runs-on: ${{ matrix.arch == 'x86_64' && 'gpu-t4-4-core' || 'ubuntu-22.04-arm' }}
    steps:
      - uses: actions/checkout@v6
      - uses: ./.github/actions/setup-linux
        with:
          toolkit: ${{ matrix.toolkit }}
      - uses: ./.github/actions/build-linux
        with:
          toolkit: ${{ matrix.toolkit }}
      - uses: ./.github/actions/test-linux
        if: matrix.arch == 'x86_64'
        with:
          has-gpu: true

  mac_build_and_test:
    name: macOS (${{ matrix.macos-target }})
    if: github.repository == 'ml-explore/mlx'
    strategy:
      matrix:
        macos-target: ["14.0", "15.0", "26.0"]
    runs-on: [self-hosted, macos]
    env:
      MACOSX_DEPLOYMENT_TARGET: ${{ matrix.macos-target }}
    needs: check_lint
    steps:
      - uses: actions/checkout@v6
      - uses: ./.github/actions/setup-macos
      - uses: ./.github/actions/build-macos

  windows_build_and_test:
    name: Windows (cpu, x86_64)
    needs: check_lint
    runs-on: windows-2025
    steps:
      - uses: actions/checkout@v6
      - uses: ./.github/actions/setup-windows
      - uses: ./.github/actions/build-windows
      - uses: ./.github/actions/test-windows

  build_documentation:
    name: Build Documentation
    if: github.repository == 'ml-explore/mlx'
    runs-on: ubuntu-22.04
    needs: check_lint
    steps:
      - uses: actions/checkout@v6
      - uses: ./.github/actions/build-docs

  linux_sanitizer_build_and_test:
    name: Linux Sanitizer Tests (${{ matrix.sanitizer }})
    needs: check_lint
    strategy:
      fail-fast: false
      matrix:
        sanitizer: [ASAN, UBSAN]
        # todo 12/16/2025: enable TSAN later + consider enabling ASAN for GPU backend tests.
        # sanitizer: [ASAN, UBSAN, TSAN]
    runs-on: ubuntu-22.04-arm
    steps:
      - name: Checkout code
        uses: actions/checkout@v6

      - name: Install Dependencies
        run: |
          export DEBIAN_FRONTEND=noninteractive
          sudo apt-get update -y
          sudo apt-get install -y \
            build-essential \
            libblas-dev \
            liblapacke-dev \
            libopenblas-dev \
            cmake \
            clang \
            git
          sudo apt-get clean
          sudo rm -rf /var/lib/apt/lists/*

      - name: Linux Build and Test with ${{ matrix.sanitizer }}
        run: |
          bash .github/scripts/build-sanitizer-tests.sh ${{ matrix.sanitizer }}

  linux_fedora_build_cpp:
    name: Linux Fedora (${{ matrix.arch }})
    needs: check_lint
    strategy:
      fail-fast: false
      matrix:
        include:
          - host: ubuntu-22.04
            arch: x86_64
          - host: ubuntu-22.04-arm
            arch: aarch64

    runs-on: ${{ matrix.host }}
    container:
      image: fedora:42
    steps:
      - name: Checkout code
        uses: actions/checkout@v6

      - name: CPP Build Test - No Release
        run: |
          bash ./.github/scripts/setup+build-cpp-linux-fedora-container.sh
