name: devel
on:
  merge_group:
    branches:
      - 2.x
  pull_request:
    paths:
      - .github/workflows/devel.yml
      - .cargo/config.toml
      - Cargo.lock
      - Cargo.toml
      - src/**
      - tests/**
  push:
    branches:
    - 2.x
    paths:
      - .github/workflows/devel.yml
      - .cargo/config.toml
      - Cargo.lock
      - Cargo.toml
      - src/**
      - tests/**
  schedule:
    - cron: '30 01 * * *' # Every day at 01:30 UTC

jobs:
  checks:
    name: Check
    runs-on: ubuntu-24.04
    env:
      RUSTFLAGS: "--cfg tokio_unstable"
    strategy:
      matrix:
        modes:
          - no-defaults
          - all
          - fips

        include:
          - modes: no-defaults
            features: "--no-default-features"
          - modes: all
            features: "--features all"
          - modes: fips
            features: "--no-default-features --features all-fips"

    steps:
    - name: Checkout repository
      uses: actions/checkout@v4
      with:
        fetch-depth: 1

    - name: Install stable toolchain
      uses: dtolnay/rust-toolchain@stable
      with:
        toolchain: stable
        components: rustfmt, clippy

    - name: Install FIPS build dependencies
      if: ${{ matrix.modes == 'fips' }}
      run: |
        sudo apt-get update
        sudo apt-get install -y cmake golang libclang-dev musl-tools
        sudo ln -s /usr/bin/musl-gcc /usr/local/bin/x86_64-linux-musl-gcc

    - name: Check formatting
      run: |
        cargo fmt --all -- --check tests/*.rs

    # The `all` feature alias selects exactly one TLS crypto provider
    # (http2-ring, the default). Using `--all-features` would enable both
    # http2-ring and http2-fips simultaneously, which is rejected by a
    # compile_error in src/tls.rs because the two providers are mutually
    # exclusive. The FIPS provider is checked separately by devel-fips.
    - name: Check via Clippy
      run: |
        cargo clippy ${{ matrix.features }} -- -D warnings

    - name: Check via Clippy (tests)
      run: |
        cargo clippy ${{ matrix.features }} --tests -- -D warnings

  test:
    name: Test
    runs-on: ${{ matrix.os }}
    env:
      # Cargo binary
      CARGO_BIN: cargo
      # When CARGO_BIN is set to CROSS, this is set to `--target matrix.target`
      TARGET_FLAGS: ""
      # When CARGO_BIN is set to CROSS, TARGET_DIR includes matrix.target
      TARGET_DIR: ./target
      # Emit backtraces on panics
      RUST_BACKTRACE: 1
      # Skip tests
      SKIP_TESTS: ""
      # SWS features for Cargo build
      CARGO_FEATURES: "--features=all"
    strategy:
      matrix:
        build:
          - pinned
          - linux-musl
          - linux-musl-arm64
          - linux-musl-i686
          - linux-gnu
          - linux-gnu-arm64
          - linux-gnu-i686
          - linux-arm-gnueabihf
          - linux-musl-armv6
          - linux-musl-armv7
          - linux-gnu-armv7
          - linux-ppc64le
          - linux-s390x
          # NOTE: looks like not supported by `listenfd` crate
          # - linux-android-armv7
          - linux-android-arm64
          - netbsd
          - illumos
          - macos
          - macos-arm64
          - windows-msvc
          - windows-msvc-i686
          - windows-msvc-arm64
          - windows-pc-gnu
          # FIPS builds
          - linux-gnu-fips
          - linux-gnu-arm64-fips
          - linux-musl-fips
          - linux-musl-arm64-fips
          - freebsd
          - freebsd-i686
        include:
          # Specific Rust channels.
          # We test against the latest and minimum Rust stable version.
          - build: pinned
            native: true
            os: ubuntu-24.04
            rust: 1.88.0
          # Some of our release builds are generated by a nightly compiler to take
          # advantage of the latest optimizations/compile time improvements.
          - build: linux-musl
            native: true
            os: ubuntu-24.04
            rust: stable
            target: x86_64-unknown-linux-musl
          - build: linux-musl-i686
            os: ubuntu-24.04
            rust: stable
            target: i686-unknown-linux-musl
          - build: linux-musl-arm64
            native: true
            os: ubuntu-24.04-arm
            rust: stable
            target: aarch64-unknown-linux-musl
          - build: linux-gnu
            native: true
            os: ubuntu-24.04
            rust: stable
            target: x86_64-unknown-linux-gnu
          - build: linux-gnu-i686
            os: ubuntu-24.04
            rust: stable
            target: i686-unknown-linux-gnu
          - build: linux-gnu-arm64
            native: true
            os: ubuntu-24.04-arm
            rust: stable
            target: aarch64-unknown-linux-gnu
          - build: linux-arm-gnueabihf
            os: ubuntu-24.04
            rust: stable
            target: arm-unknown-linux-gnueabihf
          - build: linux-musl-armv6
            os: ubuntu-24.04
            rust: stable
            target: arm-unknown-linux-musleabihf
          - build: linux-musl-armv7
            os: ubuntu-24.04
            rust: stable
            target: armv7-unknown-linux-musleabihf
          - build: linux-gnu-armv7
            os: ubuntu-24.04
            rust: stable
            target: armv7-unknown-linux-gnueabihf
          - build: linux-ppc64le
            os: ubuntu-24.04
            rust: stable
            target: powerpc64le-unknown-linux-gnu
          - build: linux-s390x
            os: ubuntu-24.04
            rust: stable
            target: s390x-unknown-linux-gnu
          #  NOTE: looks like not supported by `listenfd` crate
          # - build: linux-android-armv7
          #   os: ubuntu-24.04
          #   rust: stable
          #   target: armv7-linux-androideabi
          - build: linux-android-arm64
            os: ubuntu-24.04
            rust: stable
            target: aarch64-linux-android
          - build: netbsd
            os: ubuntu-24.04
            rust: stable
            target: x86_64-unknown-netbsd
          - build: illumos
            os: ubuntu-24.04
            rust: stable
            target: x86_64-unknown-illumos
          - build: macos
            native: true
            os: macos-26
            rust: stable
            target: x86_64-apple-darwin
          - build: macos-arm64
            native: true
            os: macos-26
            rust: stable
            target: aarch64-apple-darwin
          - build: windows-msvc
            native: true
            os: windows-2025
            rust: stable
            target: x86_64-pc-windows-msvc
          - build: windows-msvc-i686
            native: true
            os: windows-2025
            rust: stable
            target: i686-pc-windows-msvc
          - build: windows-msvc-arm64
            native: true
            os: windows-11-arm
            rust: stable
            target: aarch64-pc-windows-msvc
          - build: windows-pc-gnu
            native: true
            os: windows-2025
            rust: stable
            target: x86_64-pc-windows-gnu
          # FIPS builds
          - build: linux-gnu-fips
            native: true
            os: ubuntu-24.04
            rust: stable
            target: x86_64-unknown-linux-gnu
          - build: linux-musl-fips
            native: true
            os: ubuntu-24.04
            rust: stable
            target: x86_64-unknown-linux-musl
          - build: linux-gnu-arm64-fips
            native: true
            os: ubuntu-24.04-arm
            rust: stable
            target: aarch64-unknown-linux-gnu
          - build: linux-musl-arm64-fips
            native: true
            os: ubuntu-24.04-arm
            rust: stable
            target: aarch64-unknown-linux-musl
          - build: freebsd
            os: ubuntu-24.04
            rust: stable
            target: x86_64-unknown-freebsd
          - build: freebsd-i686
            os: ubuntu-24.04
            rust: stable
            target: i686-unknown-freebsd

    steps:
    - name: Checkout repository
      uses: actions/checkout@v4
      with:
        fetch-depth: 1

    - name: Cache Rust Cargo/Toolchain
      uses: actions/cache@v4
      with:
        path: |
          ~/.cargo
          **/target
        key: rust-${{matrix.os}}-${{ matrix.build }}-${{ matrix.target }}-${{ hashFiles('**/Cargo.lock') }}
        restore-keys: |
          rust-${{matrix.os}}-${{ matrix.build }}-${{ matrix.target }}-

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

    - name: Set up Cross
      if: ${{ !matrix.native && matrix.target != '' }}
      shell: bash
      run: |
        echo "Configuring Cross..."
        # Use 'cargo.sh' script for Android targets only
        if [[ "${{ matrix.build }}" == "linux-android-armv7" ]] || [[ "${{ matrix.build }}" == "linux-android-arm64" ]]; then
          echo "CARGO_BIN=scripts/ci/cargo.sh" >> $GITHUB_ENV
        else
          target=''
          case "${{ matrix.os }}" in
            *macos*)
              target=x86_64-apple-darwin
            ;;
            *)
              target=x86_64-unknown-linux-musl
            ;;
          esac

          echo "Installing cross..."
          curl -sSL \
            "https://github.com/cross-rs/cross/releases/download/v0.2.5/cross-$target.tar.gz" \
            | sudo tar zxf - -C /usr/local/bin/ cross cross-util
          cross -V
          echo "CARGO_BIN=/usr/local/bin/cross" >> $GITHUB_ENV
        fi

    - name: Setup Cargo
      shell: bash
      run: |
        if [[ "${{ matrix.target }}" != "" ]]; then
          echo "TARGET_FLAGS=--target=${{ matrix.target }}" >> $GITHUB_ENV
          echo "TARGET_DIR=./target/${{ matrix.target }}" >> $GITHUB_ENV
        fi
        echo "cargo command is: ${{ env.CARGO_BIN }}"
        echo "target flag is: ${{ env.TARGET_FLAGS }}"
        echo "target dir is: ${{ env.TARGET_DIR }}"

    - name: Setup Windows ARM64
      if: ${{ contains(matrix.build, 'windows-msvc-arm64') }}
      run: |
        # ring crate: add Visual Studio Build Tools "VS 2022 C++ ARM64 build tools" and "clang" components
        $env:Path += ";C:\Program Files (x86)\Microsoft Visual Studio\2022\Enterprise\VC\Tools\Llvm\x64\bin"

    - name: Setup Linux/BSD tools
      shell: bash
      if: ${{ !startsWith(matrix.os, 'windows') }}
      run: |
        scripts/ci/install_tools.sh --build=${{ matrix.build }} --target=${{ matrix.target }}

    - name: Skip tests
      shell: bash
      if: ${{ contains(fromJSON('["macos-arm64", "linux-android-arm64", "linux-android-armv7", "netbsd", "illumos", "windows-msvc-arm64", "freebsd", "freebsd-i686"]'), matrix.build) }}
      run: |
        # Can't run tests: cross-compiling
        echo "SKIP_TESTS=--no-run" >> $GITHUB_ENV

    - name: Run tests
      shell: bash
      run: |
        ${{ env.CARGO_BIN }} test --verbose ${{ env.CARGO_FEATURES }} ${{ env.TARGET_FLAGS }} ${{ env.SKIP_TESTS }}

    - name: Run build
      shell: bash
      run: |
        ${{ env.CARGO_BIN }} build --bin static-web-server -vv ${{ env.CARGO_FEATURES }} ${{ env.TARGET_FLAGS }}

    - name: Run executable
      shell: bash
      run: |
        if [[ "${{ matrix.native }}" == "true" ]]; then
          ext=""
          if [[ "${{ matrix.os }}" == windows* ]]; then ext=".exe"; fi
          target/${{ matrix.target }}/debug/static-web-server$ext -V
          target/${{ matrix.target }}/debug/static-web-server$ext -h
        else
          echo "WARN: some arm, bsd, illumos, ppc64 or s390x targets are unable to execute on CI for now!"
        fi

    - name: Turn off default features
      shell: bash
      run: |
        echo "CARGO_FEATURES=--no-default-features" >> $GITHUB_ENV

    - name: Run tests without default features
      shell: bash
      run: |
        ${{ env.CARGO_BIN }} test --verbose ${{ env.CARGO_FEATURES }} ${{ env.TARGET_FLAGS }} ${{ env.SKIP_TESTS }}

    - name: Run build without default features
      shell: bash
      run: |
        ${{ env.CARGO_BIN }} build --bin static-web-server -vv ${{ env.CARGO_FEATURES }} ${{ env.TARGET_FLAGS }}

    - name: Run executable without default features
      shell: bash
      run: |
        if [[ "${{ matrix.native }}" == "true" ]]; then
          ext=""
          if [[ "${{ matrix.os }}" == windows* ]]; then ext=".exe"; fi
          target/${{ matrix.target }}/debug/static-web-server$ext -V
          target/${{ matrix.target }}/debug/static-web-server$ext -h
        else
          echo "WARN: some arm, bsd, illumos, ppc64 or s390x targets are unable to execute on CI for now!"
        fi

  docs-check:
    name: Docs check
    runs-on: ubuntu-24.04

    steps:
    - name: Checkout repository
      uses: actions/checkout@v4
      with:
        fetch-depth: 1

    - name: Install nightly toolchain
      uses: dtolnay/rust-toolchain@stable
      with:
        toolchain: nightly
        components: rustfmt, clippy

    # `--features all` rather than `--all-features` for the same reason as
    # the clippy job above: the http2-ring and http2-fips providers are
    # mutually exclusive. FIPS rustdoc coverage lives in devel-fips.
    - name: Rustdoc build
      run: |
        cargo +nightly rustdoc --lib -Zrustdoc-map --features all \
            --config "build.rustflags=[\"--cfg\", \"tokio_unstable\"]" \
            -Zhost-config -Ztarget-applies-to-host \
            --config "host.rustflags=[\"--cfg\", \"tokio_unstable\"]" \
            --config "build.rustdocflags=[\"--cfg\", \"docsrs\", \"--cfg\", \"docsrs\", \"--cfg\", \"tokio_unstable\", \"-Z\", \"unstable-options\", \"--cap-lints\", \"warn\", \"--extern-html-root-takes-precedence\"]" \
            -Zunstable-options -- --document-private-items
