name: CI

on:
  push:
    branches:
      - main

  pull_request:
    branches:
      - main

  schedule:
    - cron: 00 4 * * *

env:
  CARGO_TERM_COLOR: always

concurrency:
  group: ${{ github.workflow }}-${{ github.ref }}
  cancel-in-progress: true

jobs:
  build:
    strategy:
      fail-fast: false
      matrix:
        runner:
          - ubuntu-latest # x86
        program:
          - kprobe
          - kretprobe
          - fentry
          - fexit
          - uprobe
          - uretprobe
          - sock_ops
          - socket_filter
          - sk_msg
          - xdp
          - cgroup_skb
          - cgroup_sockopt
          - cgroup_sysctl
          - classifier
          - lsm
          - perf_event
          - raw_tracepoint
          - tp_btf
          - tracepoint
        include:
          - runner: macos-13 # x86
            program: kprobe
          - runner: macos-14 # arm64
            program: kprobe

    runs-on: ${{ matrix.runner }}

    steps:
      - uses: actions/checkout@v4

      - uses: dtolnay/rust-toolchain@nightly
        with:
          components: clippy,rust-src,rustfmt

      - uses: dtolnay/rust-toolchain@stable
        if: runner.os == 'macOS' && runner.arch == 'X64'
        with:
          targets: x86_64-unknown-linux-musl

      - uses: dtolnay/rust-toolchain@stable
        if: runner.os == 'macOS' && runner.arch == 'ARM64'
        with:
          targets: aarch64-unknown-linux-musl

      - uses: dtolnay/rust-toolchain@stable
        if: runner.os == 'Linux'

      - uses: Swatinem/rust-cache@v2

      - uses: taiki-e/install-action@v2
        with:
          tool: cargo-generate

      - run: brew update && brew install filosottile/musl-cross/musl-cross llvm
        if: runner.os == 'macos'

      - run: cargo install bpf-linker --git https://github.com/aya-rs/bpf-linker.git --no-default-features
        if: runner.os == 'macos'

      - run: cargo install bpf-linker --git https://github.com/aya-rs/bpf-linker.git
        if: runner.os == 'Linux'

      - run: sudo apt update && sudo apt install expect
        if: runner.os == 'Linux'

      - run: ./test.sh ${{ github.workspace }} ${{ matrix.program }}