name: CI

on:
  workflow_dispatch:
  workflow_call:
  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 }}
  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@v3
    - 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@v3
    - 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@v3
    - 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

  # 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
    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