# Copyright (C) The c-ares project and its contributors
# SPDX-License-Identifier: MIT

env:
  CIRRUS_CLONE_DEPTH: 1
  CMAKE_FLAGS: "-DCMAKE_BUILD_TYPE=DEBUG -DCARES_STATIC=ON -DCARES_STATIC_PIC=ON -DCARES_BUILD_TESTS=ON"

task:
  matrix:
    - name: "Debian amd64"
      env:
        DIST: "DEBIAN"
        LC_ALL: "C"
        SCAN_BUILD: "scan-build"
        MAKE: "make"
        BUILD_ASAN: "yes"
        BUILD_UBSAN: "yes"
        BUILD_LSAN: "yes"
        BUILD_VALGRIND: "yes"
        BUILD_COVERAGE: "yes"
        BUILD_ANALYZE: "yes"
      container:
        image: debian:11
    - name: "Debian arm64"
      env:
        DIST: "DEBIAN-ARM"
        LC_ALL: "C"
        SCAN_BUILD: "scan-build"
        MAKE: "make"
        BUILD_ASAN: "yes"
        BUILD_UBSAN: "yes"
        BUILD_LSAN: "yes"
        BUILD_ANALYZE: "yes"
      arm_container:
        image: debian:latest
    - name: "FreeBSD amd64"
      env:
        DIST: "FREEBSD"
        SCAN_BUILD: "scan-build"
        MAKE: "gmake"
        BUILD_ANALYZE: "yes"
      freebsd_instance:
        image_family: freebsd-13-1
    - name: "MacOS"
      env:
        DIST: "MACOS"
        SCAN_BUILD: "/opt/homebrew/opt/llvm/bin/scan-build-py"
        MAKE: "make"
        BUILD_ASAN: "yes"
        BUILD_ANALYZE: "yes"
      macos_instance:
        image: ghcr.io/cirruslabs/macos-ventura-xcode:latest
    - name: "iOS"
      env:
        DIST: "iOS"
        MAKE: "make"
        CMAKE_FLAGS: "-DCMAKE_BUILD_TYPE=DEBUG -DCARES_STATIC=ON -DCARES_STATIC_PIC=ON -DCARES_BUILD_TESTS=OFF -DCMAKE_OSX_DEPLOYMENT_TARGET=10.0 -DCMAKE_OSX_ARCHITECTURES=armv7;armv7s;arm64"
        CFLAGS: "-arch armv7 -arch armv7s -arch arm64 -miphoneos-version-min=10.0"
        CONFIG_OPTS: "--host=arm-apple-darwin10 --disable-tests"
      macos_instance:
        image: ghcr.io/cirruslabs/macos-ventura-xcode:latest
  matrix:
    - name: "CMAKE"
      env:
        BUILD_TYPE: "cmake"
    - name: "AUTOTOOLS"
      env:
        BUILD_TYPE: "autotools"
    - name: "ASAN"
      # FreeBSD just hangs trying to run tests, think it may be trying to run leak sanitizer
      only_if: $BUILD_ASAN == 'yes'
      env:
        BUILD_TYPE: "asan"
        CC: "clang"
        CONFIG_OPTS: "--enable-debug"
        CFLAGS: "-fsanitize=address"
        CXXFLAGS: "-fsanitize=address"
        LDFLAGS: "-fsanitize=address -lpthread"
    - name: "UBSAN"
      # FreeBSD just hangs trying to run tests, think it may be trying to run leak sanitizer
      only_if: $BUILD_UBSAN == 'yes'
      env:
        BUILD_TYPE: "ubsan"
        CC: "clang"
        CONFIG_OPTS: "--enable-debug"
        CFLAGS: "-fsanitize=undefined -fno-sanitize-recover"
        CXXFLAGS: "-fsanitize=undefined -fno-sanitize-recover"
        LDFLAGS: "-fsanitize=undefined -lpthread"
    - name: "LSAN"
      # lsan only works on debian x86/x64
      only_if: $BUILD_LSAN == 'yes'
      env:
        BUILD_TYPE: "lsan"
        CC: "clang"
        CONFIG_OPTS: "--enable-debug"
        CFLAGS: "-fsanitize=leak"
        CXXFLAGS: "-fsanitize=leak"
        LDFLAGS: "-fsanitize=leak -lpthread"
    - name: "ANALYZE"
      only_if: $BUILD_ANALYZE == 'yes'
      env:
        BUILD_TYPE: "analyze"
        CC: "clang"
        SCAN_WRAP: "${SCAN_BUILD} -v --status-bugs"
        CONFIG_OPTS: "--enable-debug --disable-tests"
    - name: "VALGRIND"
      # FreeBSD just hangs trying to run tests, think it may be trying to run leak sanitizer
      only_if: $BUILD_VALGRIND == 'yes'
      env:
        BUILD_TYPE: "valgrind"
        TEST_WRAP: "valgrind --leak-check=full"
        TEST_FILTER: "--gtest_filter=-*Container*:-*LiveSearchANY*"
    - name: "COVERAGE"
      only_if: $BUILD_COVERAGE == 'yes'
      env:
        BUILD_TYPE: "coverage"
        CI_NAME: "cirrus-ci"
        CI_BUILD_NUMBER: "${CIRRUS_TASK_ID}"
        CI_BUILD_URL: "https://cirrus-ci.com/task/${CIRRUS_TASK_ID}"
        CI_BRANCH: "${CIRRUS_BRANCH}"
        CI_PULL_REQUEST: "${CIRRUS_PR}"
        COVERALLS_REPO_TOKEN: "ENCRYPTED[a46bc6ca908e9e74f102c686cde67cae21d3e792dff46a60e860d1179d9349e8eb5a529132d0effefebdb0dbc3f95810]"
        CONFIG_OPTS: "--enable-debug --disable-shared --enable-code-coverage --enable-tests"

  install_script:
    - |
      case "${DIST}" in
        DEBIAN*)
          apt-get update && \
          apt-get install -y cmake ninja-build autoconf automake libtool g++ pip
          case "${BUILD_TYPE}" in
            asan|lsan|ubsan)
              apt-get install -y clang
              ;;
            analyze)
              apt-get install -y clang clang-tools
              ;;
            valgrind)
              apt-get install -y valgrind
              ;;
            coverage)
              apt-get install -y lcov git
              pip3 install --user cpp-coveralls
              ;;
          esac
          ;;
        FREEBSD)
          # pkg upgrade -y && \
          pkg install -y cmake ninja
          case "${BUILD_TYPE}" in
            asan|analyze|ubsan|lsan)
              pkg install -y llvm autoconf automake libtool gmake
              ;;
            autotools)
              pkg install -y autoconf automake libtool gmake
              ;;
          esac
          ;;
        MACOS|iOS)
          brew update && \
          brew install ninja cmake
          case "${BUILD_TYPE}" in
            asan|ubsan|lsan|analyze)
              brew install llvm autoconf automake libtool make
              ;;
            autotools)
              brew install autoconf automake libtool make
              ;;
          esac
          ;;
      esac

  script:
    - ./ci/build.sh
    - ./ci/test.sh
    - if [ "$BUILD_TYPE" = "autotools" -a "$DIST" = "DEBIAN" ]; then ./ci/distcheck.sh ; fi
    - if [ "$BUILD_TYPE" = "coverage" ]; then ./ci/covupload.sh ; fi

