#***************************************************************************
#                                  _   _ ____  _
#  Project                     ___| | | |  _ \| |
#                             / __| | | | |_) | |
#                            | (__| |_| |  _ <| |___
#                             \___|\___/|_| \_\_____|
#
# Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
#
# This software is licensed as described in the file COPYING, which
# you should have received as part of this distribution. The terms
# are also available at https://curl.se/docs/copyright.html.
#
# You may opt to use, copy, modify, merge, publish, distribute and/or sell
# copies of the Software, and permit persons to whom the Software is
# furnished to do so, under the terms of the COPYING file.
#
# This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
# KIND, either express or implied.
#
# SPDX-License-Identifier: curl
#
###########################################################################

# View these jobs in the browser: https://app.circleci.com/pipelines/github/curl/curl

# Use the latest 2.1 version of CircleCI pipeline process engine. See: https://circleci.com/docs/2.0/configuration-reference
version: 2.1

commands:
  configure:
    steps:
      - run:
          command: |
            autoreconf -fi
            ./configure --enable-warnings --enable-werror --with-openssl

  configure-openssl-no-verbose:
    steps:
      - run:
          command: |
            autoreconf -fi
            ./configure --disable-verbose --enable-werror --with-openssl

  configure-no-proxy:
    steps:
      - run:
          command: |
            autoreconf -fi
            ./configure --disable-proxy --enable-werror --with-openssl

  configure-macos-normal:
    steps:
      - run:
          command: |
            autoreconf -fi
            ./configure --enable-warnings --enable-websockets --without-ssl CFLAGS='-Wno-vla -mmacosx-version-min=10.9'

  configure-macos-debug:
    steps:
      - run:
          command: |
            autoreconf -fi
            ./configure --enable-warnings --enable-websockets --without-ssl --enable-debug CFLAGS='-Wno-vla -mmacosx-version-min=10.9'

  configure-macos-libssh2:
    steps:
      - run:
          command: |
            autoreconf -fi
            ./configure --enable-warnings --enable-websockets --without-ssl --with-libssh2=/opt/homebrew/opt/libssh2 --enable-debug CFLAGS='-Wno-vla -mmacosx-version-min=10.9'

  configure-macos-libssh-c-ares:
    steps:
      - run:
          command: |
            autoreconf -fi
            ./configure --enable-warnings --enable-websockets --with-openssl --with-libssh --enable-ares --enable-debug PKG_CONFIG_PATH="$(brew --prefix openssl)/lib/pkgconfig" CFLAGS='-Wno-vla -mmacosx-version-min=10.9'

  configure-macos-libssh:
    steps:
      - run:
          command: |
            autoreconf -fi
            ./configure --enable-warnings --enable-websockets --with-openssl --with-libssh --enable-debug PKG_CONFIG_PATH="$(brew --prefix openssl)/lib/pkgconfig" CFLAGS='-Wno-vla -mmacosx-version-min=10.9'

  configure-macos-c-ares:
    steps:
      - run:
          command: |
            autoreconf -fi
            ./configure --enable-warnings --enable-websockets --without-ssl --enable-ares --enable-debug CFLAGS='-Wno-vla -mmacosx-version-min=10.9'

  configure-macos-http-only:
    steps:
      - run:
          command: |
            autoreconf -fi
            ./configure --enable-warnings --enable-maintainer-mode --disable-dict --disable-file --disable-ftp --disable-gopher --disable-imap --disable-ldap --disable-pop3 --disable-rtmp --disable-rtsp --disable-scp --disable-sftp --disable-smb --disable-smtp --disable-telnet --disable-tftp --disable-unix-sockets --disable-shared --without-brotli --without-gssapi --without-libidn2 --without-libpsl --without-librtmp --without-libssh2 --without-nghttp2 --without-ntlm-auth --without-ssl --without-zlib --enable-debug CFLAGS='-Wno-vla -mmacosx-version-min=10.15'

  configure-macos-securetransport-http2:
    steps:
      - run:
          command: |
            autoreconf -fi
            ./configure --enable-warnings --enable-websockets --with-secure-transport CFLAGS='-Wno-vla -mmacosx-version-min=10.8'

  configure-macos-openssl-http2:
    steps:
      - run:
          command: |
            autoreconf -fi
            ./configure --enable-warnings --enable-websockets --with-openssl --enable-debug PKG_CONFIG_PATH="$(brew --prefix openssl)/lib/pkgconfig" CFLAGS='-Wno-vla -mmacosx-version-min=10.9'

  configure-macos-libressl-http2:
    steps:
      - run:
          command: |
            autoreconf -fi
            ./configure --enable-warnings --enable-websockets --with-openssl --enable-debug PKG_CONFIG_PATH="$(brew --prefix libressl)/lib/pkgconfig" CFLAGS='-Wno-vla -mmacosx-version-min=10.9'

  configure-macos-torture:
    steps:
      - run:
          command: |
            autoreconf -fi
            ./configure --enable-warnings --enable-websockets --disable-shared --disable-threaded-resolver --with-openssl --enable-debug PKG_CONFIG_PATH="$(brew --prefix openssl)/lib/pkgconfig" CFLAGS='-Wno-vla -mmacosx-version-min=10.9'

  configure-macos-torture-ftp:
    steps:
      - run:
          command: |
            autoreconf -fi
            ./configure --enable-warnings --enable-websockets --disable-shared --disable-threaded-resolver --with-openssl --enable-debug PKG_CONFIG_PATH="$(brew --prefix openssl)/lib/pkgconfig" CFLAGS='-Wno-vla -mmacosx-version-min=10.9'

  install-cares:
    steps:
      - run:
          command: |
            sudo apt-get update && sudo apt-get install -y libc-ares-dev

  install-libssh:
    steps:
      - run:
          command: |
            sudo apt-get update && sudo apt-get install -y libssh-dev

  install-deps:
    steps:
      - run:
          command: |
            sudo apt-get update && sudo apt-get install -y libpsl-dev libbrotli-dev libzstd-dev zlib1g-dev python3-pip
            sudo python3 -m pip install impacket

  install-deps-brew:
    steps:
      - run:
          command: |
            # Drop libressl as long as we're not trying to build it
            echo libtool autoconf automake pkg-config nghttp2 libssh2 openssl libssh c-ares | xargs -Ix -n1 echo brew '"x"' > /tmp/Brewfile
            while [ $? -eq 0 ]; do for i in 1 2 3; do brew update && brew bundle install --no-lock --file /tmp/Brewfile && break 2 || { echo Error: wait to try again; sleep 10; } done; false Too many retries; done
            sudo python3 -m pip install impacket

  configure-libssh:
    steps:
      - run:
          command: |
            autoreconf -fi
            ./configure --enable-warnings --enable-werror --with-openssl --with-libssh

  install-wolfssl:
    steps:
      - run:
          command: |
            WOLFSSL_VER=5.6.0
            curl -LOsSf --retry 6 --retry-connrefused --max-time 999 https://github.com/wolfSSL/wolfssl/archive/v$WOLFSSL_VER-stable.tar.gz
            tar -xzf v$WOLFSSL_VER-stable.tar.gz
            cd wolfssl-$WOLFSSL_VER-stable
            ./autogen.sh
            ./configure --enable-tls13 --enable-all --enable-harden --prefix=$HOME/wssl
            make install

  install-wolfssh:
    steps:
      - run:
          command: |
            WOLFSSH_VER=1.4.12
            curl -LOsSf --retry 6 --retry-connrefused --max-time 999 https://github.com/wolfSSL/wolfssh/archive/v$WOLFSSH_VER-stable.tar.gz
            tar -xzf v$WOLFSSH_VER-stable.tar.gz
            cd wolfssh-$WOLFSSH_VER-stable
            ./autogen.sh
            ./configure --with-wolfssl=$HOME/wssl --prefix=$HOME/wssh --enable-scp --enable-sftp --disable-examples
            make install

  configure-cares:
    steps:
      - run:
          command: |
            autoreconf -fi
            ./configure --enable-warnings --enable-werror --with-openssl --enable-ares

  configure-wolfssh:
    steps:
      - run:
          command: |
            autoreconf -fi
            LDFLAGS="-Wl,-rpath,$HOME/wssh/lib" ./configure --enable-warnings --enable-werror --with-wolfssl=$HOME/wssl --with-wolfssh=$HOME/wssh

  configure-cares-debug:
    steps:
      - run:
          command: |
            autoreconf -fi
            ./configure --enable-debug --enable-werror --with-openssl --enable-ares

  build:
    steps:
      - run: make -j3 V=1
      - run: make -j3 V=1 examples

  build-macos:
    steps:
      - run: make -j7 V=1
      - run: make -j7 V=1 examples

  test:
    steps:
      - run: make -j3 V=1 test-ci

  test-macos:
    steps:
      - run: make -j7 V=1 test-ci

  test-torture:
    steps:
      - run: make -j5 V=1 test-ci TFLAGS="-n -t --shallow=25 !FTP"

  test-torture-ftp:
    steps:
      - run: make -j5 V=1 test-ci TFLAGS="-n -t --shallow=20 FTP"

executors:
  ubuntu:
    machine:
      image: ubuntu-2004:202010-01

jobs:
  basic:
    executor: ubuntu
    steps:
      - checkout
      - configure
      - build
      - test

  no-verbose:
    executor: ubuntu
    steps:
      - checkout
      - install-deps
      - configure-openssl-no-verbose
      - build

  wolfssh:
    executor: ubuntu
    steps:
      - checkout
      - install-deps
      - install-wolfssl
      - install-wolfssh
      - configure-wolfssh
      - build

  no-proxy:
    executor: ubuntu
    steps:
      - checkout
      - install-deps
      - configure-no-proxy
      - build
      - test

  cares:
    executor: ubuntu
    steps:
      - checkout
      - install-cares
      - configure-cares
      - build
      - test

  libssh:
    executor: ubuntu
    steps:
      - checkout
      - install-libssh
      - configure-libssh
      - build
      - test

  arm:
    machine:
      image: ubuntu-2004:202101-01
    resource_class: arm.medium
    steps:
      - checkout
      - configure
      - build
      - test

  arm-cares:
    machine:
      image: ubuntu-2004:202101-01
    resource_class: arm.medium
    steps:
      - checkout
      - install-cares
      - configure-cares-debug
      - build
      - test

  # TODO: All builds with "macos.x86.medium.gen2" must be changed to
  # "macos.m1.medium.gen1" in January 2024 because the former will be removed
  # (the names should also be changed from macos-x86-* to macos-arm-*).  We
  # want the M1 (ARM) machines anyway, for platform diversity.
  # See https://circleci.com/docs/configuration-reference/#macos-execution-environment
  macos-x86-normal:
    macos:
      xcode: 15.0.0
    resource_class: macos.x86.medium.gen2
    steps:
      - checkout
      - install-deps-brew
      - configure-macos-normal
      - build-macos
      - test-macos

  macos-x86-debug:
    macos:
      xcode: 15.0.0
    resource_class: macos.x86.medium.gen2
    steps:
      - checkout
      - install-deps-brew
      - configure-macos-debug
      - build-macos
      - test-macos

  macos-x86-libssh2:
    macos:
      xcode: 15.0.0
    resource_class: macos.x86.medium.gen2
    steps:
      - checkout
      - install-deps-brew
      - configure-macos-libssh2
      - build-macos
      - test-macos

  macos-x86-libssh-c-ares:
    macos:
      xcode: 15.0.0
    resource_class: macos.x86.medium.gen2
    steps:
      - checkout
      - install-deps-brew
      - configure-macos-libssh-c-ares
      - build-macos
      - test-macos

  macos-x86-libssh:
    macos:
      xcode: 15.0.0
    resource_class: macos.x86.medium.gen2
    steps:
      - checkout
      - install-deps-brew
      - configure-macos-libssh
      - build-macos
      - test-macos

  macos-x86-c-ares:
    macos:
      xcode: 15.0.0
    resource_class: macos.x86.medium.gen2
    steps:
      - checkout
      - install-deps-brew
      - configure-macos-c-ares
      - build-macos
      - test-macos

  macos-x86-http-only:
    macos:
      xcode: 15.0.0
    resource_class: macos.x86.medium.gen2
    steps:
      - checkout
      - install-deps-brew
      - configure-macos-http-only
      - build-macos
      - test-macos

  macos-x86-http-securetransport-http2:
    macos:
      xcode: 15.0.0
    resource_class: macos.x86.medium.gen2
    steps:
      - checkout
      - install-deps-brew
      - configure-macos-securetransport-http2
      - build-macos
      - test-macos

  macos-x86-http-openssl-http2:
    macos:
      xcode: 15.0.0
    resource_class: macos.x86.medium.gen2
    steps:
      - checkout
      - install-deps-brew
      - configure-macos-openssl-http2
      - build-macos
      - test-macos

  macos-x86-http-libressl-http2:
    macos:
      xcode: 15.0.0
    resource_class: macos.x86.medium.gen2
    steps:
      - checkout
      - install-deps-brew
      - configure-macos-libressl-http2
      - build-macos
      - test-macos

  macos-x86-http-torture:
    macos:
      xcode: 15.0.0
    resource_class: macos.x86.medium.gen2
    steps:
      - checkout
      - install-deps-brew
      - configure-macos-torture
      - build-macos
      - test-torture

  macos-x86-http-torture-ftp:
    macos:
      xcode: 15.0.0
    resource_class: macos.x86.medium.gen2
    steps:
      - checkout
      - install-deps-brew
      - configure-macos-torture-ftp
      - build-macos
      - test-torture-ftp

workflows:
  x86-openssl:
    jobs:
      - basic

  openssl-c-ares:
    jobs:
      - cares

  openssl-libssh:
    jobs:
      - libssh

  openssl-no-proxy:
    jobs:
      - no-proxy

  openssl-no-verbose:
    jobs:
      - no-verbose

  wolfssl-wolfssh:
    jobs:
      - wolfssh

  arm-openssl:
    jobs:
      - arm

  arm-openssl-c-ares:
    jobs:
      - arm-cares

  macos-x86-normal:
    jobs:
      - macos-x86-normal

  macos-x86-debug:
    jobs:
      - macos-x86-debug

  macos-x86-libssh2:
    jobs:
      - macos-x86-libssh2

  macos-x86-libssh-c-ares:
    jobs:
      - macos-x86-libssh-c-ares

  macos-x86-libssh:
    jobs:
      - macos-x86-libssh

  macos-x86-c-ares:
    jobs:
      - macos-x86-c-ares

  macos-x86-http-only:
    jobs:
      - macos-x86-http-only

  macos-x86-http-securetransport-http2:
    jobs:
      - macos-x86-http-securetransport-http2

  macos-x86-http-openssl-http2:
    jobs:
      - macos-x86-http-openssl-http2

  # There are problem linking with LibreSSL on the CI boxes that prevent this
  # from working.
  #macos-x86-http-libressl-http2:
  #  jobs:
  #    - macos-x86-http-libressl-http2

  macos-x86-http-torture:
    jobs:
      - macos-x86-http-torture

  macos-x86-http-torture-ftp:
    jobs:
      - macos-x86-http-torture-ftp
