name: CI

on:
  workflow_dispatch:
  workflow_call:
    inputs:
      additional_key:
        required: true
        type: string
        default: ""
  merge_group:
  pull_request:
    types:
      - opened
      - reopened
      - synchronize
  push:
    branches:
      - main
    paths-ignore:
      - README.md
      - SUPPORT.md

concurrency:
  group: ${{ github.workflow }}-${{ github.ref || github.event.pull_request.number || github.sha }}-${{ inputs.additional_key }}
  cancel-in-progress: true

env:
  CARGO_TERM_COLOR: always
  CARGO_REGISTRIES_CRATES_IO_PROTOCOL: sparse
  JUST_ENABLE_H3: true
  CARGO_PROFILE_RELEASE_CODEGEN_UNITS: 4
  CARGO_PROFILE_DEV_CODEGEN_UNITS: 4
  CARGO_PROFILE_CHECK_ONLY_CODEGEN_UNITS: 4

jobs:
  test:
    strategy:
      fail-fast: false
      matrix:
        include:
          - target: x86_64-apple-darwin
            os: macos-latest
          - target: x86_64-unknown-linux-gnu
            os: ubuntu-latest
          - target: x86_64-pc-windows-msvc
            os: windows-latest

    runs-on: ${{ matrix.os }}
    env:
      CARGO_BUILD_TARGET: ${{ matrix.target }}

    steps:
    - uses: actions/checkout@v4
    - uses: ./.github/actions/just-setup
      env:
        # just-setup use binstall to install sccache,
        # which works better when we provide it with GITHUB_TOKEN.
        GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

    - run: just ci-install-deps
    - run: just test
      env:
        GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

  cross-check:
    strategy:
      fail-fast: false
      matrix:
        include:
          - target: armv7-unknown-linux-musleabihf
            os: ubuntu-latest
          - target: armv7-unknown-linux-gnueabihf
            os: ubuntu-latest
          - target: aarch64-unknown-linux-musl
            os: ubuntu-latest
          - target: aarch64-unknown-linux-gnu
            os: ubuntu-latest
          - target: x86_64-unknown-linux-musl
            os: ubuntu-latest
          - target: aarch64-apple-darwin
            os: macos-latest
          - target: aarch64-pc-windows-msvc
            os: windows-latest
    runs-on: ${{ matrix.os }}
    env:
      CARGO_BUILD_TARGET: ${{ matrix.target }}
    steps:
    - uses: actions/checkout@v4
    - uses: ./.github/actions/just-setup
      with:
        tools: cargo-hack
      env:
        # just-setup use binstall to install sccache,
        # which works better when we provide it with GITHUB_TOKEN.
        GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

    - name: Enable cargo-zigbuild
      if: matrix.os == 'ubuntu-latest'
      run: echo JUST_USE_CARGO_ZIGBUILD=true >> "$GITHUB_ENV"

    - run: just ci-install-deps
    - run: just avoid-dev-deps
    - run: just check

  lint:
    strategy:
      fail-fast: false
      matrix:
        include:
          - target: x86_64-apple-darwin
            os: macos-latest
          - target: x86_64-unknown-linux-gnu
            os: ubuntu-latest
          - target: x86_64-pc-windows-msvc
            os: windows-latest

    runs-on: ${{ matrix.os }}
    steps:
    - uses: actions/checkout@v4
    - uses: ./.github/actions/just-setup

    - run: just toolchain rustfmt,clippy
    - run: just ci-install-deps
    - run: just avoid-dev-deps
    - run: just lint

  release-builds:
    uses: ./.github/workflows/release-build.yml
    with:
      CARGO_PROFILE_RELEASE_LTO: no
      CARGO_PROFILE_RELEASE_CODEGEN_UNITS: 4

  detect-targets-alpine-test:
    runs-on: ubuntu-latest
    env:
      CARGO_BUILD_TARGET: x86_64-unknown-linux-musl
      TARGET: x86_64-unknown-linux-musl
    steps:
    - uses: actions/checkout@v4
    - name: Install x86_64-unknown-linux-musl target
      run: rustup target add $TARGET
    - uses: Swatinem/rust-cache@v2
    - name: Build detect-targets
      run: |
        pip3 install -r zigbuild-requirements.txt
        cd crates/detect-targets && cargo zigbuild --target $TARGET
    - name: Run test in alpine
      run: |
        docker run --rm \
          --mount src="$PWD/target/$TARGET/debug/detect-targets",dst=/usr/local/bin/detect-targets,type=bind \
          --mount src="$PWD/test-detect-targets-musl.sh",dst=/usr/local/bin/test.sh,type=bind \
          alpine test.sh "$TARGET"

  detect-targets-ubuntu-test:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v4
    - uses: Swatinem/rust-cache@v2
    - name: Build detect-targets
      run: cargo build --bin detect-targets
    - name: Run test in ubuntu
      run: |
        set -exuo pipefail
        [ "$(./target/debug/detect-targets)" = "$(printf '%s\n%s' x86_64-unknown-linux-gnu x86_64-unknown-linux-musl)" ]

  # Dummy job to have a stable name for the "all tests pass" requirement
  tests-pass:
    name: Tests pass
    needs:
    - test
    - cross-check
    - lint
    - release-builds
    - detect-targets-alpine-test
    - detect-targets-ubuntu-test
    if: always() # always run even if dependencies fail
    runs-on: ubuntu-latest
    steps:
    # fail if ANY dependency has failed or been skipped or cancelled
    - if: "contains(needs.*.result, 'failure') || contains(needs.*.result, 'skipped') || contains(needs.*.result, 'cancelled')"
      run: exit 1
    - run: exit 0