# Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
#
# SPDX-License-Identifier: curl

name: quiche

on:
  push:
    branches:
      - master
      - '*/ci'
    paths-ignore:
      - '**/*.md'
      - '**/CMakeLists.txt'
      - '.azure-pipelines.yml'
      - '.circleci/**'
      - '.cirrus.yml'
      - 'appveyor.*'
      - 'CMake/**'
      - 'packages/**'
      - 'plan9/**'
      - 'projects/**'
      - 'winbuild/**'
  pull_request:
    branches:
      - master
    paths-ignore:
      - '**/*.md'
      - '**/CMakeLists.txt'
      - '.azure-pipelines.yml'
      - '.circleci/**'
      - '.cirrus.yml'
      - 'appveyor.*'
      - 'CMake/**'
      - 'packages/**'
      - 'plan9/**'
      - 'projects/**'
      - 'winbuild/**'

concurrency:
  # Hardcoded workflow filename as workflow name above is just Linux again
  group: quiche-${{ github.event.pull_request.number || github.sha }}
  cancel-in-progress: true

permissions: {}

env:
  MAKEFLAGS: -j 3
  openssl-version: 3.1.4+quic
  nghttp3-version: v1.1.0
  ngtcp2-version: v1.2.0
  nghttp2-version: v1.59.0
  quiche-version: 0.20.0
  mod_h2-version: v2.0.26

jobs:
  autotools:
    name: ${{ matrix.build.name }}
    runs-on: 'ubuntu-latest'
    timeout-minutes: 60
    strategy:
      fail-fast: false
      matrix:
        build:
          - name: quiche
            install: >-
              libpsl-dev libbrotli-dev libzstd-dev zlib1g-dev libev-dev libc-ares-dev
            install_steps: pytest
            configure: >-
              LDFLAGS="-Wl,-rpath,/home/runner/quiche/target/release"
              --with-openssl=/home/runner/quiche/quiche/deps/boringssl/src
              --enable-debug
              --with-quiche=/home/runner/quiche/target/release
              --with-test-nghttpx="$HOME/nghttpx/bin/nghttpx"
              --with-ca-fallback

    steps:
      - run: |
          sudo apt-get update
          sudo apt-get install libtool autoconf automake pkg-config stunnel4 ${{ matrix.build.install }}
          sudo apt-get install apache2 apache2-dev libnghttp2-dev
        name: 'install prereqs and impacket, pytest, crypto'

      - name: cache nghttpx
        uses: actions/cache@v4
        id: cache-nghttpx
        env:
          cache-name: cache-nghttpx
        with:
          path: /home/runner/nghttpx
          key: ${{ runner.os }}-build-${{ env.cache-name }}-openssl-${{ env.openssl-version }}-nghttp3-${{ env.nghttp3-version }}-ngtcp2-${{ env.ngtcp2-version }}-nghttp2-${{ env.nghttp2-version }}

      - if: steps.cache-nghttpx.outputs.cache-hit != 'true'
        run: |
          git clone --quiet --depth=1 -b openssl-${{ env.openssl-version }} https://github.com/quictls/openssl
          cd openssl
          ./config --prefix=$HOME/nghttpx --libdir=$HOME/nghttpx/lib
          make -j1 install_sw
        name: 'install quictls'

      - if: steps.cache-nghttpx.outputs.cache-hit != 'true'
        run: |
          git clone --quiet --depth=1 -b ${{ env.nghttp3-version }} https://github.com/ngtcp2/nghttp3
          cd nghttp3
          autoreconf -fi
          ./configure --prefix=$HOME/nghttpx PKG_CONFIG_PATH="$HOME/nghttpx/lib/pkgconfig" --enable-lib-only
          make install
        name: 'install nghttp3'

      - if: steps.cache-nghttpx.outputs.cache-hit != 'true'
        run: |
          git clone --quiet --depth=1 -b ${{ env.ngtcp2-version }} https://github.com/ngtcp2/ngtcp2
          cd ngtcp2
          autoreconf -fi
          ./configure --prefix=$HOME/nghttpx PKG_CONFIG_PATH="$HOME/nghttpx/lib/pkgconfig" --enable-lib-only --with-openssl
          make install
        name: 'install ngtcp2'

      - if: steps.cache-nghttpx.outputs.cache-hit != 'true'
        run: |
          git clone --quiet --depth=1 -b ${{ env.nghttp2-version }} https://github.com/nghttp2/nghttp2
          cd nghttp2
          autoreconf -fi
          ./configure --prefix=$HOME/nghttpx PKG_CONFIG_PATH="$HOME/nghttpx/lib/pkgconfig" --enable-http3
          make install
        name: 'install nghttp2'

      - name: cache quiche
        uses: actions/cache@v4
        id: cache-quiche
        env:
          cache-name: cache-quiche
        with:
          path: /home/runner/quiche
          key: ${{ runner.os }}-build-${{ env.cache-name }}-quiche-${{ env.quiche-version }}

      - if: steps.cache-quiche.outputs.cache-hit != 'true'
        run: |
          cd $HOME
          git clone --quiet --depth=1 -b ${{ env.quiche-version }} --recursive https://github.com/cloudflare/quiche.git
          cd quiche
          #### Work-around https://github.com/curl/curl/issues/7927 #######
          #### See https://github.com/alexcrichton/cmake-rs/issues/131 ####
          sed -i -e 's/cmake = "0.1"/cmake = "=0.1.45"/' quiche/Cargo.toml

          cargo build -v --package quiche --release --features ffi,pkg-config-meta,qlog --verbose
          mkdir -v quiche/deps/boringssl/src/lib
          ln -vnf $(find target/release -name libcrypto.a -o -name libssl.a) quiche/deps/boringssl/src/lib/

          # include dir
          # /home/runner/quiche/quiche/deps/boringssl/src/include
          # lib dir
          # /home/runner/quiche/quiche/deps/boringssl/src/lib
        name: 'build quiche and boringssl'

      - name: cache mod_h2
        uses: actions/cache@v4
        id: cache-mod_h2
        env:
          cache-name: cache-mod_h2
        with:
          path: /home/runner/mod_h2
          key: ${{ runner.os }}-build-${{ env.cache-name }}-${{ env.mod_h2-version }}

      - if: steps.cache-mod_h2.outputs.cache-hit != 'true'
        run: |
          cd $HOME
          git clone --quiet --depth=1 -b  ${{ env.mod_h2-version }} https://github.com/icing/mod_h2
          cd mod_h2
          autoreconf -fi
          ./configure
          make
        name: 'build mod_h2'

      - run: |
          cd $HOME/mod_h2
          sudo make install
        name: 'install mod_h2'

      - uses: actions/checkout@v4

      - run: |
          sudo python3 -m pip install -r tests/requirements.txt -r tests/http/requirements.txt
        name: 'install python test prereqs'

      - run: autoreconf -fi
        name: 'autoreconf'

      - run: ./configure ${{ matrix.build.configure }}
        name: 'configure'

      - run: make V=1
        name: 'make'

      - run: make V=1 examples
        name: 'make examples'

      - run: make V=1 -C tests
        name: 'make tests'

      - run: make V=1 test-ci
        name: 'run tests'
        env:
          TFLAGS: "${{ matrix.build.tflags }}"

      - run: pytest -v tests
        name: 'run pytest'
        env:
          TFLAGS: "${{ matrix.build.tflags }}"
          CURL_CI: github