name: Rust

on:
  push:
    branches: [ "main" ]
  pull_request:
    branches: [ "main" ]

env:
  CARGO_TERM_COLOR: always

  # Make sure CI fails on all warnings, including Clippy lints
  RUSTFLAGS: "-Dwarnings"

jobs:
  build-gnu-apt:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4

      - name: Update repos
        run: sudo apt-get update -qq
      - name: Install toolchain
        run: sudo apt-get install -qy cargo rust-clippy liblzma-dev gcc make pkgconf

      - name: Run Clippy
        run: cargo clippy

      - name: build
        run: cargo build

      - name: generate bindings
        run: ./generate-binding.sh
      - name: check that generated files did not change
        run: output=$(git status --porcelain) && [ -z "$output" ]

      - name: build example -- glibc release
        run: cd example && make clean && make TARGET=aarch64-unknown-linux-gnu PROFILE=release
      - name: build example -- glibc debug
        run: cd example && make clean && make TARGET=aarch64-unknown-linux-gnu PROFILE=debug

  # this _should_ have been -stable, but there is a bug when running in the container ("Invalid cross-device link")
  build-musl-beta:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4

      - name: Update repos
        run: sudo apt-get update -qq
      - name: Install toolchain
        run: sudo apt-get install -qy liblzma-dev gcc make pkgconf musl-dev musl-tools rustup
      - name: install rust target
        run: rustup toolchain install beta -t aarch64-unknown-linux-musl -c rust-src --no-self-update && rustup component add --toolchain aarch64-unknown-linux-musl rust-src

      - name: build example -- musl release
        run: cd example && make clean && make TARGET=aarch64-unknown-linux-musl PROFILE=release MUSL=1 CARGO="rustup run beta cargo"
      - name: build example -- musl debug
        run: cd example && make clean && make TARGET=aarch64-unknown-linux-musl PROFILE=debug MUSL=1 CARGO="rustup run beta cargo"

  build-size-gnu-unstable:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4

      - name: Update repos
        run: sudo apt-get update -qq
      - name: Install toolchain
        run: sudo apt-get install -qy liblzma-dev gcc make pkgconf rustup
      - name: install rust targets
        run: rustup toolchain install nightly -t aarch64-unknown-linux-gnu -c rust-src --no-self-update

      - name: build example -- glibc size-optimized
        run: cd example && make clean && make TARGET=aarch64-unknown-linux-gnu PROFILE=size-optimized CARGO="rustup run nightly cargo"


  build-size-musl-unstable:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4

      - name: Update repos
        run: sudo apt-get update -qq
      - name: Install toolchain
        run: sudo apt-get install -qy liblzma-dev gcc make pkgconf musl-dev musl-tools rustup
      - name: install rust targets
        run: rustup toolchain install nightly -t aarch64-unknown-linux-musl -c rust-src --no-self-update

      - name: build example -- musl size-optimized
        run: cd example && make clean && make TARGET=aarch64-unknown-linux-musl PROFILE=size-optimized MUSL=1 LTO=1 CARGO="rustup run nightly cargo"