name: Cloud Hypervisor Build
on: [pull_request, merge_group]
concurrency:
  group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }}-${{ github.event_name }}
  cancel-in-progress: true

jobs:
  build:
    name: Build
    runs-on: ubuntu-latest
    strategy:
      fail-fast: false
      matrix:
        rust:
          - stable
          - beta
          - nightly
          - "1.89.0"
        target:
          - x86_64-unknown-linux-gnu
          - x86_64-unknown-linux-musl
    steps:
      - name: Code checkout
        uses: actions/checkout@v6
        with:
          fetch-depth: 0

      - name: Install musl-gcc
        run: sudo apt install -y musl-tools

      - name: Install Rust toolchain (${{ matrix.rust }})
        uses: dtolnay/rust-toolchain@stable
        with:
          toolchain: ${{ matrix.rust }}
          target: ${{ matrix.target }}

      - name: Build (default features)
        run: cargo build --locked --bin cloud-hypervisor

      - name: Build (kvm)
        run: cargo build --locked --bin cloud-hypervisor --no-default-features --features "kvm"

      - name: Build (default features + tdx)
        run: cargo build --locked --bin cloud-hypervisor --features "tdx"

      - name: Build (default features + dbus_api)
        run: cargo build --locked --bin cloud-hypervisor --features "dbus_api"

      - name: Build (default features + guest_debug)
        run: cargo build --locked --bin cloud-hypervisor --features "guest_debug"

      - name: Build (default features + pvmemcontrol)
        run: cargo build --locked --bin cloud-hypervisor --features "pvmemcontrol"

      - name: Build (default features + fw_cfg)
        run: cargo build --locked --bin cloud-hypervisor --features "fw_cfg"

      - name: Build (default features + ivshmem)
        run: cargo build --locked --bin cloud-hypervisor --features "ivshmem"

      - name: Build (mshv)
        run: cargo build --locked --bin cloud-hypervisor --no-default-features --features "mshv"

      - name: Build (sev_snp)
        run: cargo build --locked --bin cloud-hypervisor --no-default-features --features "sev_snp"

      - name: Build (kvm + igvm + sev_snp + fw_cfg)
        run: cargo build --locked --bin cloud-hypervisor --no-default-features --features "kvm,igvm,sev_snp,fw_cfg"

      - name: Build (igvm)
        run: cargo build --locked --bin cloud-hypervisor --no-default-features --features "igvm"

      - name: Build (mshv + kvm)
        run: cargo build --locked --bin cloud-hypervisor --no-default-features --features "mshv,kvm"

      - name: Release Build (default features)
        run: cargo build --locked --all --release --target=${{ matrix.target }}

      - name: Check build did not modify any files
        run: test -z "$(git status --porcelain)"
