name: CI
on:
  workflow_dispatch:
  push:
    branches: [master, main, fix]
  pull_request:
    branches: [master, main, fix]

jobs:
  ubuntu-focal-make:
    timeout-minutes: 20
    runs-on: llamafile-runner

    steps:
      - name: Clone
        id: checkout
        uses: actions/checkout@9c091bb21b7c1c1d1991bb908d89e4e9dddfe3e0 # v7.0.0

      - name: Dependencies
        id: depends
        run: |
          sudo apt-get update
          sudo apt-get install make patch

      - name: Setup dependencies and patch dependencies
        run: |
          make setup

      - name: Cache cosmocc toolchain
        id: cache-cosmocc-toolchain
        uses: actions/cache@27d5ce7f107fe9357f9df03efb73ab90386fccae # v5.0.5
        env:
          cache-name: cache-cosmocc-toolchain
        with:
          path: |
            .cosmocc
            o/depend
            o/depend.test
          key: ${{ runner.os }}-build-${{ env.cache-name }}-${{ hashFiles('**/config.mk') }}
          restore-keys: |
            ${{ runner.os }}-build-${{ env.cache-name }}

      - name: Setup cosmocc and ape loader
        run: |
          sudo make cosmocc-ci PREFIX=/usr

      - name: Build
        run: |
          sudo make -j $(nproc)

      - name: Make Llamafile
        run: |
          cp ./models/TinyLLama-v0.1-5M-F16.gguf tinyllama.gguf
          cat << EoF > .args
          -m
          tinyllama.gguf
          ...
          EoF
          cp o//llamafile/llamafile \
            tinyllama.llamafile
          o//third_party/zipalign/zipalign -j0 \
            tinyllama.llamafile \
            tinyllama.gguf \
            .args

      - name: Execute LLM CLI CPU
        run: |
          ./tinyllama.llamafile --cli -p '## Famous Speech\n\nFour score and seven'
