name: LLVM CI

on:
  push:
    paths:
      - 'src/compiler/crystal/codegen/**'
      - 'src/llvm/**'
      - 'spec/std/llvm/**'
      - 'spec/llvm-ir/**'
      - 'spec/compiler/codegen/**'
      - '.github/workflows/llvm.yml'
  pull_request:
    paths:
      - 'src/compiler/crystal/codegen/**'
      - 'src/llvm/**'
      - 'spec/std/llvm/**'
      - 'spec/llvm-ir/**'
      - 'spec/compiler/codegen/**'
      - '.github/workflows/llvm.yml'
  schedule:
    - cron: '0 3 * * *'
  workflow_dispatch:

permissions: {}

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

env:
  SPEC_SPLIT_DOTS: 160

jobs:
  llvm_test:
    runs-on: ${{ matrix.runs-on }}
    strategy:
      fail-fast: false
      matrix:
        include:
          - {llvm_version: 13, runs-on: ubuntu-22.04, codename: jammy}
          - {llvm_version: 14, runs-on: ubuntu-22.04, codename: jammy}
          - {llvm_version: 15, runs-on: ubuntu-22.04, codename: jammy}
          - {llvm_version: 16, runs-on: ubuntu-22.04, codename: jammy}
          - {llvm_version: 17, runs-on: ubuntu-24.04, codename: noble}
          - {llvm_version: 18, runs-on: ubuntu-24.04, codename: noble}
          - {llvm_version: 19, runs-on: ubuntu-24.04, codename: noble}
          - {llvm_version: 20, runs-on: ubuntu-24.04, codename: noble}
          - {llvm_version: 21, runs-on: ubuntu-24.04, codename: noble}
          - {llvm_version: 22, runs-on: ubuntu-24.04, codename: noble}
          - {runs-on: ubuntu-24.04, codename: noble}
    name: "LLVM ${{ matrix.llvm_version || 'Nightly' }}"
    steps:
      - name: Checkout Crystal source
        uses: actions/checkout@v6
        with:
          persist-credentials: false

      - name: Install LLVM ${{ matrix.llvm_version || 'Nightly' }}
        run: |
          sudo apt remove 'llvm-*' 'libllvm*'
          wget -qO- https://apt.llvm.org/llvm-snapshot.gpg.key | sudo tee /etc/apt/trusted.gpg.d/apt.llvm.org.asc
          sudo apt-add-repository -y \
            deb \
            http://apt.llvm.org/${{ matrix.codename }}/ \
            llvm-toolchain-${{ matrix.codename }}${{ matrix.llvm_version && format('-{0}', matrix.llvm_version) || '' }} \
            main
          sudo apt install -y llvm${{ matrix.llvm_version && format('-{0}', matrix.llvm_version) || '' }}-dev lld

      - name: Install Crystal
        uses: crystal-lang/install-crystal@v1
        with:
          crystal: "1.19.1"

      - name: Build libllvm_ext
        run: make -B deps

      - name: Test compiler_spec
        run: make compiler_spec junit_output=.junit/compiler_spec.xml

      - name: Integration test
        run: make crystal std_spec threads=1 junit_output=.junit/std_spec.xml
