name: CI
on:
  push:

jobs:
  freebsd-cross-build:
    name: Cross-Build for FreeBSD
    runs-on: 'ubuntu-24.04'
    env:
      CARGO_NET_GIT_FETCH_WITH_CLI: true
    steps:
    - uses: actions/checkout@v4

    - name: Install dependencies
      run: |
        sudo apt-get update
        sudo apt-get install -y libudev-dev libhidapi-dev

    - name: Setup Rust toolchain
      run: |
        rm rust-toolchain.toml
        rustup target add x86_64-unknown-freebsd
        rustup show

    - name: Install cross compilation tool
      run: cargo install cross

    - name: Build FreeBSD tool
      run: cross build --target=x86_64-unknown-freebsd --no-default-features -p framework_lib

    - name: Upload FreeBSD App
      uses: actions/upload-artifact@v4
      with:
        name: framework_tool_freebsd
        path: target/x86_64-unknown-freebsd/debug/framework_tool

  build:
    name: Build Linux
    runs-on: ubuntu-24.04
    env:
      CARGO_NET_GIT_FETCH_WITH_CLI: true
    steps:
    - uses: actions/checkout@v4

    - name: Install dependencies
      run: |
        sudo apt-get update
        sudo apt-get install -y libudev-dev

    - name: Setup Rust toolchain
      run: rustup show

    - name: Build library (Linux)
      run: cargo build -p framework_lib

    - name: Build Linux tool
      run: cargo build -p framework_tool

    - name: Check if Linux tool can start
      run: cargo run -- --help

    - name: Upload Linux App
      uses: actions/upload-artifact@v4
      with:
        name: framework_tool
        path: target/debug/framework_tool

  build-uefi:
    name: Build UEFI
    runs-on: ubuntu-24.04
    env:
      CARGO_NET_GIT_FETCH_WITH_CLI: true
    steps:
    - uses: actions/checkout@v4

    - name: Setup Rust toolchain
      run: rustup show

    - name: Build UEFI application (no ESP)
      run: make -C framework_uefi build/x86_64-unknown-uefi/boot.efi

    - name: Upload UEFI App
      uses: actions/upload-artifact@v4
      with:
        name: framework.efi
        path: framework_uefi/build/x86_64-unknown-uefi/boot.efi

    - name: Install mtools to build ESP and ISO (Linux)
      run: sudo apt-get install -y mtools genisoimage

    - name: Build ESP (Linux)
      run: make -C framework_uefi

    - name: Build ISO (Linux)
      run: make -C framework_uefi iso

    - name: Upload UEFI App ISO
      uses: actions/upload-artifact@v4
      with:
        name: UEFI-Shell-fwk.iso
        path: framework_uefi/build/x86_64-unknown-uefi/UEFI-Shell-fwk.iso

  build-windows:
    name: Build Windows
    runs-on: windows-2022
    env:
      CARGO_NET_GIT_FETCH_WITH_CLI: true
    steps:
    - uses: actions/checkout@v4

    - name: Setup Rust toolchain
      run: rustup show

    # Build debug library first to fail fast
    - name: Build library (Windows)
      run: cargo build -p framework_lib

    - name: Build Windows tool
      run: |
        cargo build -p framework_tool
        cargo build -p framework_tool --release

    - name: Check if Windows tool can start
      run: cargo run -- --help --release

    # Upload release build so that vcruntime is statically linked
    - name: Upload Windows App
      uses: actions/upload-artifact@v4
      with:
        name: framework_tool.exe
        path: target/release/framework_tool.exe


  test:
    name: Test Suite
    runs-on: ubuntu-latest
    env:
      CARGO_NET_GIT_FETCH_WITH_CLI: true
    steps:
      - name: Checkout sources
        uses: actions/checkout@v4

      - name: Install dependencies
        run: |
          sudo apt-get update
          sudo apt-get install -y libudev-dev

      - name: Setup Rust toolchain
        run: rustup show

      - name: Run cargo test
        run: cargo test -p framework_lib

  lints:
    name: Lints
    runs-on: ubuntu-24.04
    env:
      CARGO_NET_GIT_FETCH_WITH_CLI: true
    steps:
    - uses: actions/checkout@v4

    - name: Install dependencies
      run: |
        sudo apt-get update
        sudo apt-get install -y libudev-dev

    - name: Setup Rust toolchain
      run: rustup show

    - name: Run cargo fmt
      run: cargo fmt --all -- --check

    - name: Run cargo clippy
      run: cargo clippy -p framework_lib -p framework_tool -- -D warnings

  # Just make sure doc generation works
  doc:
    name: Generate docs
    runs-on: ubuntu-24.04
    env:
      CARGO_NET_GIT_FETCH_WITH_CLI: true
      # Fail if warnings are produced
      RUSTDOCFLAGS: -Dwarnings
    steps:
    - uses: actions/checkout@v4

    - name: Install dependencies
      run: |
        sudo apt-get update
        sudo apt-get install -y libudev-dev

    - name: Setup Rust toolchain
      run: rustup show

    - name: Run cargo doc
      run: cargo doc
