
##############################################################################
# Project specific settings
##############################################################################

language: c

cache:
  directories:
  - $HOME/third-party

osx_image: xcode9

addons:
  apt:
    packages:
      - cmake
      - openssl
      - libssl-dev
      - gdb
    sources:
      - kubuntu-backports


before_install:
  - if [ "${TRAVIS_OS_NAME}" == "linux" ]; then
      mkdir $HOME/usr;
      export PATH="$HOME/usr/bin:$PATH";
      wget https://cmake.org/files/v3.7/cmake-3.7.2-Linux-x86_64.sh --no-check-certificate;
      chmod +x cmake-3.7.2-Linux-x86_64.sh;
      ./cmake-3.7.2-Linux-x86_64.sh --prefix=$HOME/usr --exclude-subdir --skip-license;
    fi
  - cmake --version


install:
  - if [ "${BUILD_TYPE}" == "Coverage" -a "${TRAVIS_OS_NAME}" == "linux" ]; then
      PATH=~/.local/bin:${PATH};
      pip install --user --upgrade pip;
      pip install --user cpp-coveralls;
      pip install --user codecov;
      pip install --user coverage;
    fi

before_script:
  # Add an IPv6 config - see the corresponding Travis issue
  # https://github.com/travis-ci/travis-ci/issues/8361
  - if [ "${ENABLE_IPV6}" == "YES" -a "${TRAVIS_OS_NAME}" == "linux" ]; then
      echo "Activating IPv6 on Travis";
      sudo sh -c 'echo 0 > /proc/sys/net/ipv6/conf/all/disable_ipv6';
    fi
  # Check some settings of the build server (operating system, IPv6 availability, directory)
  - uname -a
  - if [ "${TRAVIS_OS_NAME}" == "linux" ]; then
      lsb_release -a;
      cat /etc/network/interfaces || true;
    fi
  - ifconfig
  - pwd
  - ls -la
  - if [ "${TRAVIS_OS_NAME}" == "linux" ]; then
      apt-cache search gcc | grep "GNU C compiler";
      apt-cache search clang | grep compiler;
    fi
  - if [[ "${BUILD_TYPE}" == "OSX_OPENSSL_1_1" ]]; then HOMEBREW_NO_AUTO_UPDATE=1 brew install openssl@1.1 ;fi
  # Generate the build scripts with CMake
  - mkdir output
  - openssl version
  - gcc --version
  - cmake --version
  - gcc unittest/cgi_test.c -o output/cgi_test.cgi
  - cd output
  - echo cmake
    -G "Unix Makefiles"
    -DCMAKE_BUILD_TYPE=${BUILD_TYPE}
    -DBUILD_SHARED_LIBS=${BUILD_SHARED}
    "-DCIVETWEB_THIRD_PARTY_DIR=${HOME}/third-party"
    -DCIVETWEB_ENABLE_THIRD_PARTY_OUTPUT=YES
    -DCIVETWEB_ENABLE_SSL=${ENABLE_SSL}
    -DCIVETWEB_DISABLE_CGI=${NO_CGI}
    -DCIVETWEB_SERVE_NO_FILES=${NO_FILES}
    -DCIVETWEB_ENABLE_SSL_DYNAMIC_LOADING=${ENABLE_SSL_DYNAMIC_LOADING}
    -DCIVETWEB_SSL_OPENSSL_API_1_0=${OPENSSL_1_0}
    -DCIVETWEB_SSL_OPENSSL_API_1_1=${OPENSSL_1_1}
    -DCIVETWEB_ENABLE_WEBSOCKETS=${ENABLE_WEBSOCKETS}
    -DCIVETWEB_ENABLE_CXX=${ENABLE_CXX}
    -DCIVETWEB_ENABLE_SERVER_STATS=${ENABLE_SERVER_STATS}
    -DCIVETWEB_ENABLE_LUA=${ENABLE_LUA}
    -DCIVETWEB_ENABLE_LUA_SHARED=${ENABLE_LUA_SHARED}
    -DCIVETWEB_ENABLE_DUKTAPE=${ENABLE_DUKTAPE}
    -DCIVETWEB_DISABLE_CACHING=${NO_CACHING}
    -DCIVETWEB_C_STANDARD=${C_STANDARD}
    -DCIVETWEB_CXX_STANDARD=${CXX_STANDARD}
    -DCIVETWEB_ALLOW_WARNINGS=${ALLOW_WARNINGS}
    -DCIVETWEB_ENABLE_IPV6=${ENABLE_IPV6}
    ${ADDITIONAL_CMAKE_ARGS}
    ..
  - cmake
    -G "Unix Makefiles"
    -DCMAKE_BUILD_TYPE=${BUILD_TYPE}
    -DBUILD_SHARED_LIBS=${BUILD_SHARED}
    "-DCIVETWEB_THIRD_PARTY_DIR=${HOME}/third-party"
    -DCIVETWEB_ENABLE_THIRD_PARTY_OUTPUT=YES
    -DCIVETWEB_ENABLE_SSL=${ENABLE_SSL}
    -DCIVETWEB_DISABLE_CGI=${NO_CGI}
    -DCIVETWEB_SERVE_NO_FILES=${NO_FILES}
    -DCIVETWEB_ENABLE_SSL_DYNAMIC_LOADING=${ENABLE_SSL_DYNAMIC_LOADING}
    -DCIVETWEB_SSL_OPENSSL_API_1_0=${OPENSSL_1_0}
    -DCIVETWEB_SSL_OPENSSL_API_1_1=${OPENSSL_1_1}
    -DCIVETWEB_ENABLE_WEBSOCKETS=${ENABLE_WEBSOCKETS}
    -DCIVETWEB_ENABLE_CXX=${ENABLE_CXX}
    -DCIVETWEB_ENABLE_SERVER_STATS=${ENABLE_SERVER_STATS}
    -DCIVETWEB_ENABLE_LUA=${ENABLE_LUA}
    -DCIVETWEB_ENABLE_LUA_SHARED=${ENABLE_LUA_SHARED}
    -DCIVETWEB_ENABLE_DUKTAPE=${ENABLE_DUKTAPE}
    -DCIVETWEB_DISABLE_CACHING=${NO_CACHING}
    -DCIVETWEB_C_STANDARD=${C_STANDARD}
    -DCIVETWEB_CXX_STANDARD=${CXX_STANDARD}
    -DCIVETWEB_ALLOW_WARNINGS=${ALLOW_WARNINGS}
    -DCIVETWEB_ENABLE_IPV6=${ENABLE_IPV6}
    ${ADDITIONAL_CMAKE_ARGS}
    ..
  - ls -la
  - ulimit -c unlimited -S

after_failure:
  - COREFILE=$(find . -maxdepth 1 -name "core*" | head -n 1) # find core file
  - if [[ -f "$COREFILE" ]]; then gdb -c "$COREFILE" example -ex "thread apply all bt" -ex "set pagination 0" -batch; fi


# Modifications due to Travis IPv6 issues:
# https://github.com/travis-ci/travis-ci/issues/8711
# https://github.com/travis-ci/travis-ci/issues/8361
# DCIVETWEB_ENABLE_IPV6=${ENABLE_IPV6} or =NO

script:
  - if [ "${MACOSX_PACKAGE}" == "1" ]; then
      cd "${TRAVIS_BUILD_DIR}";
      make -f Makefile.osx package;
    fi
  - if [ "${RUN_UNITTEST}" == "1" ]; then
      CTEST_OUTPUT_ON_FAILURE=1  CK_FORK=yes  make all test;
    fi
  - pwd
  - ls -la unittest
  - echo "Show all test logs:"
  - if [ "${RUN_UNITTEST}" == "1" ]; then
      for f in unittest/test-*.log; do printf "\n$f:\n"; cat $f; done;
      for f in unittest/test-*.xml; do printf "\n$f:\n"; cat $f; done;
    fi
  - echo "Build and test script DONE"

# Coveralls options: https://github.com/eddyxu/cpp-coveralls/blob/master/README.md
after_success:
  - if [ "${BUILD_TYPE}" == "Coverage" -a "${TRAVIS_OS_NAME}" == "linux" ]; then
      echo "Preparing coverage tests";
      echo "Creating coveralls coverage report";
      coveralls --include src --exclude src/main.c --exclude src/third_party --include include --gcov-options '\-lp' --root .. --build-root .;
      echo "Creating codecov coverage report";
      bash <(curl -s https://codecov.io/bash);
      echo "All coverage reports created";
    fi


#########################################################################################
#########################################################################################
#####   START OF BUILD MATRIX   #########################################################
#########################################################################################
#########################################################################################

matrix:
  fast_finish: true
  include:


#########################################################################################
#####   TRUSTY   ########################################################################
#########################################################################################

  - dist: trusty
    sudo: false
    os: linux
    compiler: clang
    addons:
      apt:
        sources:
          - ubuntu-toolchain-r-test
          - llvm-toolchain-precise-3.8
        packages:
          - clang-3.8
    env:
      idx=1
      N=Clang3.8-Linux-Minimal-Debug
      MATRIX_EVAL="CC=clang-3.8 && CXX=clang++-3.8"
      BUILD_TYPE=Debug
      ENABLE_SSL_DYNAMIC_LOADING=YES
      OPENSSL_1_0=NO
      OPENSSL_1_1=NO
      ENABLE_CXX=NO
      ENABLE_LUA_SHARED=NO
      C_STANDARD=auto
      CXX_STANDARD=auto
      BUILD_SHARED=NO
      NO_FILES=YES
      ENABLE_SSL=NO
      NO_CGI=YES
      ENABLE_IPV6=NO
      ENABLE_WEBSOCKETS=NO
      ENABLE_SERVER_STATS=NO
      ENABLE_LUA=NO
      ENABLE_DUKTAPE=NO
      NO_CACHING=NO
      ALLOW_WARNINGS=YES

  - dist: trusty
    sudo: false
    os: linux
    compiler: clang
    addons:
      apt:
        sources:
          - ubuntu-toolchain-r-test
          - llvm-toolchain-precise-3.8
        packages:
          - clang-3.8
    env:
      idx=3
      N=Clang3.8-Linux-Default-Release
      MATRIX_EVAL="CC=clang-3.8 && CXX=clang++-3.8"
      BUILD_TYPE=Release
      ENABLE_SSL_DYNAMIC_LOADING=YES
      OPENSSL_1_0=YES
      OPENSSL_1_1=NO
      ENABLE_CXX=NO
      ENABLE_LUA_SHARED=NO
      C_STANDARD=auto
      CXX_STANDARD=auto
      BUILD_SHARED=NO
      NO_FILES=NO
      ENABLE_SSL=YES
      NO_CGI=NO
      ENABLE_IPV6=NO
      ENABLE_WEBSOCKETS=NO
      ENABLE_SERVER_STATS=NO
      ENABLE_LUA=NO
      ENABLE_DUKTAPE=NO
      NO_CACHING=NO
      ALLOW_WARNINGS=YES

  - dist: trusty
    sudo: required
    os: linux
    compiler: gcc
    addons:
      apt:
        sources:
          - ubuntu-toolchain-r-test
        packages:
          - g++-5
    env:
      idx=5
      N=GCC5-Linux-Complete-NoLua-Release
      MATRIX_EVAL="CC=gcc-5 && CXX=g++-5"
      BUILD_TYPE=Release
      ENABLE_SSL_DYNAMIC_LOADING=YES
      OPENSSL_1_0=YES
      OPENSSL_1_1=NO
      ENABLE_CXX=NO
      ENABLE_LUA_SHARED=NO
      C_STANDARD=auto
      CXX_STANDARD=auto
      BUILD_SHARED=NO
      NO_FILES=NO
      ENABLE_SSL=YES
      NO_CGI=NO
      ENABLE_IPV6=YES
      ENABLE_WEBSOCKETS=YES
      ENABLE_SERVER_STATS=YES
      ENABLE_LUA=NO
      ENABLE_DUKTAPE=NO
      NO_CACHING=YES
      ALLOW_WARNINGS=YES
      RUN_UNITTEST=1


#########################################################################################
#####   COVERAGE   ######################################################################
#########################################################################################

  - os: linux
    sudo: required
    compiler: clang
    env:
      idx=6
      N=GCCAnyVersion-Linux-Coverage
      BUILD_TYPE=Coverage
      ENABLE_SSL_DYNAMIC_LOADING=YES
      OPENSSL_1_0=YES
      OPENSSL_1_1=NO
      ENABLE_CXX=NO
      ENABLE_LUA_SHARED=NO
      C_STANDARD=auto
      CXX_STANDARD=auto
      BUILD_SHARED=NO
      NO_FILES=NO
      ENABLE_SSL=YES
      NO_CGI=NO
      ENABLE_IPV6=YES
      ENABLE_WEBSOCKETS=YES
      ENABLE_SERVER_STATS=YES
      ENABLE_LUA=NO
      ENABLE_DUKTAPE=NO
      NO_CACHING=NO
      ALLOW_WARNINGS=YES
      RUN_UNITTEST=1

#########################################################################################
#####   SHARED   ########################################################################
#########################################################################################

  - sudo: false
    os: linux
    compiler: clang
    env:
      idx=9
      N=Clang-Linux-Default-Shared
      BUILD_TYPE=Debug
      ENABLE_SSL_DYNAMIC_LOADING=YES
      OPENSSL_1_0=NO
      OPENSSL_1_1=YES
      ENABLE_CXX=NO
      ENABLE_LUA_SHARED=NO
      C_STANDARD=auto
      CXX_STANDARD=auto
      BUILD_SHARED=YES
      NO_FILES=NO
      ENABLE_SSL=YES
      NO_CGI=NO
      ENABLE_IPV6=NO
      ENABLE_WEBSOCKETS=NO
      ENABLE_SERVER_STATS=NO
      ENABLE_LUA=NO
      ENABLE_DUKTAPE=NO
      NO_CACHING=NO
      ALLOW_WARNINGS=YES


#########################################################################################
#####   BUILD TYPES   ###################################################################
#########################################################################################

# According to CMakeLists, options are:
# None Debug Release RelWithDebInfo MinSizeRel Coverage

  -
    os: linux
    compiler: gcc
    env:
      idx=15
      N=GCCLinuxDefault_RelWithDebInfo
      BUILD_TYPE=RelWithDebInfo
      ENABLE_SSL_DYNAMIC_LOADING=YES
      OPENSSL_1_0=YES
      OPENSSL_1_1=NO
      ENABLE_CXX=NO
      ENABLE_LUA_SHARED=NO
      C_STANDARD=auto
      CXX_STANDARD=auto
      BUILD_SHARED=NO
      NO_FILES=NO
      ENABLE_SSL=YES
      NO_CGI=NO
      ENABLE_IPV6=NO
      ENABLE_WEBSOCKETS=NO
      ENABLE_LUA=NO
      ENABLE_DUKTAPE=NO
      NO_CACHING=NO
      ALLOW_WARNINGS=YES

  -
    os: linux
    compiler: gcc
    env:
      idx=16
      N=GCCLinuxDefault_MinSizeRel
      BUILD_TYPE=MinSizeRel
      ENABLE_SSL_DYNAMIC_LOADING=YES
      OPENSSL_1_0=YES
      OPENSSL_1_1=NO
      ENABLE_CXX=NO
      ENABLE_LUA_SHARED=NO
      C_STANDARD=auto
      CXX_STANDARD=auto
      BUILD_SHARED=NO
      NO_FILES=NO
      ENABLE_SSL=YES
      NO_CGI=NO
      ENABLE_IPV6=NO
      ENABLE_WEBSOCKETS=NO
      ENABLE_LUA=NO
      ENABLE_DUKTAPE=NO
      NO_CACHING=NO
      ALLOW_WARNINGS=YES

  -
    os: linux
    compiler: gcc
    env:
      idx=17
      N=GCCLinuxDefault_None
      BUILD_TYPE=None
      ENABLE_SSL_DYNAMIC_LOADING=YES
      OPENSSL_1_0=YES
      OPENSSL_1_1=NO
      ENABLE_CXX=NO
      ENABLE_LUA_SHARED=NO
      C_STANDARD=auto
      CXX_STANDARD=auto
      BUILD_SHARED=NO
      NO_FILES=NO
      ENABLE_SSL=YES
      NO_CGI=NO
      ENABLE_IPV6=NO
      ENABLE_WEBSOCKETS=NO
      ENABLE_LUA=NO
      ENABLE_DUKTAPE=NO
      NO_CACHING=NO
      ALLOW_WARNINGS=YES

#########################################################################################
#####   XENIAL, BIONIC, FOCAL   #########################################################
#########################################################################################

  -
    os: linux
    compiler: gcc
    dist: xenial
    env:
      idx=20
      N=GCCLinuxDefault_xenial
      BUILD_TYPE=Release
      ENABLE_SSL_DYNAMIC_LOADING=YES
      OPENSSL_1_0=YES
      OPENSSL_1_1=NO
      ENABLE_CXX=NO
      ENABLE_LUA_SHARED=NO
      C_STANDARD=auto
      CXX_STANDARD=auto
      BUILD_SHARED=NO
      NO_FILES=NO
      ENABLE_SSL=YES
      NO_CGI=NO
      ENABLE_IPV6=NO
      ENABLE_WEBSOCKETS=NO
      ENABLE_LUA=NO
      ENABLE_DUKTAPE=NO
      NO_CACHING=NO
      ALLOW_WARNINGS=YES

  -
    os: linux
    compiler: gcc
    dist: bionic
    env:
      idx=21
      N=GCCLinuxDefault_bionic
      BUILD_TYPE=Release
      ENABLE_SSL_DYNAMIC_LOADING=YES
      OPENSSL_1_0=NO
      OPENSSL_1_1=YES
      ENABLE_CXX=NO
      ENABLE_LUA_SHARED=NO
      C_STANDARD=auto
      CXX_STANDARD=auto
      BUILD_SHARED=NO
      NO_FILES=NO
      ENABLE_SSL=YES
      NO_CGI=NO
      ENABLE_IPV6=NO
      ENABLE_WEBSOCKETS=NO
      ENABLE_LUA=NO
      ENABLE_DUKTAPE=NO
      NO_CACHING=NO
      ALLOW_WARNINGS=YES

  -
    os: linux
    compiler: gcc
    dist: focal
    addons:
      apt:
        packages:
          - lsb-core
    env:
      idx=23
      N=GCCLinuxDefault_focal
      BUILD_TYPE=Release
      ENABLE_SSL_DYNAMIC_LOADING=YES
      OPENSSL_1_0=NO
      OPENSSL_1_1=YES
      ENABLE_CXX=NO
      ENABLE_LUA_SHARED=NO
      C_STANDARD=auto
      CXX_STANDARD=auto
      BUILD_SHARED=NO
      NO_FILES=NO
      ENABLE_SSL=YES
      NO_CGI=NO
      ENABLE_IPV6=NO
      ENABLE_WEBSOCKETS=NO
      ENABLE_LUA=NO
      ENABLE_DUKTAPE=NO
      NO_CACHING=NO
      ALLOW_WARNINGS=YES
      RUN_UNITTEST=1


#########################################################################################
#####   FREEBSD BUILD   ###########=#####################################################
#########################################################################################

  -
    os: freebsd
    sudo: required
    compiler: clang
    env:
      idx=20
      N=Clang-FREEBSD-Complete-NoLua-Release
      BUILD_TYPE=Release
      ENABLE_SSL_DYNAMIC_LOADING=YES
      OPENSSL_1_0=NO
      OPENSSL_1_1=YES
      ENABLE_CXX=NO
      ENABLE_LUA_SHARED=NO
      C_STANDARD=auto
      CXX_STANDARD=auto
      BUILD_SHARED=NO
      NO_FILES=NO
      ENABLE_SSL=YES
      NO_CGI=NO
      ENABLE_IPV6=YES
      ENABLE_WEBSOCKETS=YES
      ENABLE_SERVER_STATS=YES
      ENABLE_LUA=NO
      ENABLE_DUKTAPE=NO
      NO_CACHING=NO
      ALLOW_WARNINGS=YES
      RUN_UNITTEST=1


#########################################################################################
#####   OSX BUILD   #####################################################################
#########################################################################################

  -
    os: osx
    sudo: required
    compiler: clang
    env:
      idx=8
      N=Clang-OSX-Complete-NoLua-Release-OpenSSL_1_1_NoDynLoad
      BUILD_TYPE=Release
      ENABLE_SSL_DYNAMIC_LOADING=NO
      OPENSSL_1_0=NO
      OPENSSL_1_1=YES
      ENABLE_CXX=NO
      ENABLE_LUA_SHARED=NO
      C_STANDARD=auto
      CXX_STANDARD=auto
      BUILD_SHARED=NO
      NO_FILES=NO
      ENABLE_SSL=YES
      NO_CGI=NO
      ENABLE_IPV6=YES
      ENABLE_WEBSOCKETS=YES
      ENABLE_SERVER_STATS=YES
      ENABLE_LUA=NO
      ENABLE_DUKTAPE=NO
      NO_CACHING=YES
      ALLOW_WARNINGS=YES
      OPENSSL_ROOT_DIR="/usr/local/opt/openssl@1.1"
      LDFLAGS="-L${OPENSSL_ROOT_DIR}/lib"
      CFLAGS="-I${OPENSSL_ROOT_DIR}/include"
      ADDITIONAL_CMAKE_ARGS="-DCMAKE_SHARED_LINKER_FLAGS=${LDFLAGS} -DCMAKE_C_FLAGS=${CFLAGS}"
      PATH="${OPENSSL_ROOT_DIR}/bin:$PATH"
      DYLD_LIBRARY_PATH="${OPENSSL_ROOT_DIR}/lib:${DYLD_LIBRARY_PATH}"
      RUN_UNITTEST=1

  -
    os: osx
    sudo: required
    compiler: clang
    env:
      idx=11
      N=OSX-Package
      BUILD_TYPE=Release
      ENABLE_SSL_DYNAMIC_LOADING=YES
      OPENSSL_1_0=YES
      OPENSSL_1_1=NO
      ENABLE_CXX=NO
      ENABLE_LUA_SHARED=NO
      C_STANDARD=auto
      CXX_STANDARD=auto
      BUILD_SHARED=NO
      NO_FILES=NO
      ENABLE_SSL=YES
      NO_CGI=NO
      ENABLE_IPV6=YES
      ENABLE_WEBSOCKETS=YES
      ENABLE_SERVER_STATS=NO
      ENABLE_LUA=NO
      ENABLE_DUKTAPE=NO
      NO_CACHING=NO
      ALLOW_WARNINGS=YES
      MACOSX_PACKAGE=1

#########################################################################################
#########################################################################################
#####   END OF BUILD MATRIX   ###########################################################
#########################################################################################
#########################################################################################

# Remove Lua build, until someone knows how to fix the CMake files
#
#  - dist: trusty
#    sudo: required
#    os: linux
#    compiler: clang
#    addons:
#      apt:
#        sources:
#          - ubuntu-toolchain-r-test
#          - llvm-toolchain-precise-3.8
#        packages:
#          - clang-3.8
#          - lua5.2
#    env:
#      idx=99
#      N=Clang3.8-Linux-Complete-WithLua-Debug
#      MATRIX_EVAL="CC=clang-3.8 && CXX=clang++-3.8"
#      BUILD_TYPE=Debug
#      ENABLE_SSL_DYNAMIC_LOADING=YES
#      OPENSSL_1_0=NO
#      OPENSSL_1_1=YES
#      ENABLE_CXX=NO
#      ENABLE_LUA_SHARED=YES
#      C_STANDARD=auto
#      CXX_STANDARD=auto
#      BUILD_SHARED=NO
#      NO_FILES=NO
#      ENABLE_SSL=YES
#      NO_CGI=NO
#      ENABLE_IPV6=YES
#      ENABLE_WEBSOCKETS=YES
#      ENABLE_SERVER_STATS=YES
#      ENABLE_LUA=YES
#      ENABLE_LUA_SHARED=YES
#      ENABLE_DUKTAPE=NO
#      NO_CACHING=YES
#      ALLOW_WARNINGS=YES
