---
sudo: required
dist: focal

branches:
  only:
    - "master"

os: linux

language: c

compiler:
  - gcc

addons:
  apt:
    packages:
    - axel
    - luarocks
    - daemonize

cache:
  directories:
  - download-cache

env:
  global:
    - JOBS=2
    - NGX_BUILD_JOBS=$JOBS
    - LUAJIT_PREFIX=/opt/luajit21
    - LUAJIT_LIB=$LUAJIT_PREFIX/lib
    - LUAJIT_INC=$LUAJIT_PREFIX/include/luajit-2.1
    - LUA_INCLUDE_DIR=$LUAJIT_INC
    - LUA_CMODULE_DIR=/lib
    - PCRE_VER=8.45
    - PCRE2_VER=10.37
    - PCRE_PREFIX=/opt/pcre
    - PCRE2_PREFIX=/opt/pcre2
    - PCRE_LIB=$PCRE_PREFIX/lib
    - PCRE2_LIB=$PCRE2_PREFIX/lib
    - PCRE_INC=$PCRE_PREFIX/include
    - PCRE2_INC=$PCRE2_PREFIX/include
    - OPENSSL_PREFIX=/opt/ssl
    - OPENSSL_LIB=$OPENSSL_PREFIX/lib
    - OPENSSL_INC=$OPENSSL_PREFIX/include
    - LD_LIBRARY_PATH=$LUAJIT_LIB:$LD_LIBRARY_PATH
    - TEST_NGINX_SLEEP=0.005
    - TEST_NGINX_RANDOMIZE=1
    - LUACHECK_VER=0.21.1
  matrix:
    - NGINX_VERSION=1.27.0 OPENSSL_VER=1.1.1w OPENSSL_PATCH_VER=1.1.1f USE_PCRE2=Y
    - NGINX_VERSION=1.25.3 OPENSSL_VER=1.1.1w OPENSSL_PATCH_VER=1.1.1f

services:
  - memcache

before_install:
  - sudo luarocks install luacheck $LUACHECK_VER
  - luacheck --globals coroutine -q .
  - '! grep -n -P ''(?<=.{80}).+'' --color `find . -name ''*.lua''` || (echo "ERROR: Found Lua source lines exceeding 80 columns." > /dev/stderr; exit 1)'
  - '! grep -n -P ''\t+'' --color `find . -name ''*.lua''` || (echo "ERROR: Cannot use tabs." > /dev/stderr; exit 1)'
  - cpanm --sudo --notest Test::Nginx IPC::Run > build.log 2>&1 || (cat build.log && exit 1)

install:
  - if [ ! -d download-cache ]; then mkdir download-cache; fi
  - if [ ! -f download-cache/openssl-$OPENSSL_VER.tar.gz ]; then wget -P download-cache https://www.openssl.org/source/openssl-$OPENSSL_VER.tar.gz || wget -P download-cache https://www.openssl.org/source/old/${OPENSSL_VER//[a-z]/}/openssl-$OPENSSL_VER.tar.gz; fi
  - if [ "$USE_PCRE2" != "Y" ] && [ ! -f download-cache/pcre-$PCRE_VER.tar.gz ]; then wget -P download-cache http://ftp.cs.stanford.edu/pub/exim/pcre/pcre-$PCRE_VER.tar.gz; fi
  - if [ "$USE_PCRE2" = "Y" ] && [ ! -f download-cache/pcre2-$PCRE2_VER.tar.gz ]; then wget -P download-cache https://downloads.sourceforge.net/project/pcre/pcre2/${PCRE2_VER}/pcre2-${PCRE2_VER}.tar.gz; fi
  - git clone https://github.com/openresty/openresty.git ../openresty
  - git clone https://github.com/openresty/openresty-devel-utils.git
  - git clone https://github.com/simpl/ngx_devel_kit.git ../ndk-nginx-module
  - git clone https://github.com/openresty/lua-nginx-module.git ../lua-nginx-module
  - git clone https://github.com/openresty/no-pool-nginx.git ../no-pool-nginx
  - git clone https://github.com/openresty/echo-nginx-module.git ../echo-nginx-module
  - git clone https://github.com/openresty/lua-resty-lrucache.git
  - git clone https://github.com/openresty/headers-more-nginx-module.git ../headers-more-nginx-module
  - git clone -b v2.1-agentzh https://github.com/openresty/luajit2.git luajit2
  - git clone https://github.com/openresty/set-misc-nginx-module.git ../set-misc-nginx-module
  - git clone https://github.com/openresty/mockeagain.git
  - git clone https://github.com/openresty/test-nginx.git
  - git clone https://github.com/openresty/stream-lua-nginx-module.git ../stream-lua-nginx-module

script:
  - cd luajit2/
  - make -j$JOBS CCDEBUG=-g Q= PREFIX=$LUAJIT_PREFIX CC=$CC XCFLAGS='-DLUA_USE_APICHECK -DLUA_USE_ASSERT -msse4.2 -O1 -DLUAJIT_SECURITY_STRID=0 -DLUAJIT_SECURITY_STRHASH=0 -DLUAJIT_SECURITY_PRNG=0 -DLUAJIT_SECURITY_MCODE=0 -DLUAJIT_TEST_FIXED_ORDER' > build.log 2>&1 || (cat build.log && exit 1)
  - sudo make install PREFIX=$LUAJIT_PREFIX > build.log 2>&1 || (cat build.log && exit 1)
  - cd ..
  - cd lua-resty-lrucache && sudo make DESTDIR=$LUAJIT_PREFIX LUA_LIB_DIR=/share/lua/5.1 install && cd ..
  - tar zxf download-cache/openssl-$OPENSSL_VER.tar.gz
  - cd openssl-$OPENSSL_VER/
  - if [ -n "$OPENSSL_PATCH_VER" ]; then patch -p1 < ../../openresty/patches/openssl-$OPENSSL_PATCH_VER-sess_set_get_cb_yield.patch; fi
  - ./config no-threads shared enable-ssl3 enable-ssl3-method -g --prefix=$OPENSSL_PREFIX -DPURIFY > build.log 2>&1 || (cat build.log && exit 1)
  - make -j$JOBS > build.log 2>&1 || (cat build.log && exit 1)
  - sudo make PATH=$PATH install_sw > build.log 2>&1 || (cat build.log && exit 1)
  - cd ../mockeagain/ && make CC=$CC -j$JOBS && cd ..
  - if [ "$USE_PCRE2" != "Y" ]; then tar zxf download-cache/pcre-$PCRE_VER.tar.gz; cd pcre-$PCRE_VER/; ./configure --prefix=$PCRE_PREFIX --enable-jit --enable-utf --enable-unicode-properties > build.log 2>&1 || (cat build.log && exit 1); make -j$JOBS > build.log 2>&1 || (cat build.log && exit 1); sudo PATH=$PATH make install > build.log 2>&1 || (cat build.log && exit 1); cd ..; fi
  - if [ "$USE_PCRE2" = "Y" ]; then tar zxf download-cache/pcre2-$PCRE2_VER.tar.gz; cd pcre2-$PCRE2_VER/; ./configure --prefix=$PCRE2_PREFIX --enable-jit --enable-utf > build.log 2>&1 || (cat build.log && exit 1); make -j$JOBS > build.log 2>&1 || (cat build.log && exit 1); sudo PATH=$PATH make install > build.log 2>&1 || (cat build.log && exit 1); cd ..; fi
  - export PATH=$PWD/work/nginx/sbin:$PWD/openresty-devel-utils:$PATH
  - export LD_PRELOAD=$PWD/mockeagain/mockeagain.so
  - export LD_LIBRARY_PATH=$PWD/mockeagain:$LD_LIBRARY_PATH
  - export TEST_NGINX_RESOLVER=8.8.4.4
  - export NGX_BUILD_CC=$CC
  - export add_http3_module=--with-http_v3_module
  - export disable_pcre2=--without-pcre2
  - answer=`util/ver-ge "$NGINX_VERSION" 1.25.1`
  - if [ "$OPENSSL_VER" = "1.1.0l" ] || [ "$answer" = "N" ]; then add_http3_module=""; fi
  - if [ "$answer" = "N" ] || [ "$USE_PCRE2" = "Y" ]; then disable_pcre2=""; fi
  - if [ "$USE_PCRE2" = "Y" ]; then PCRE_INC=$PCRE2_INC; PCRE_LIB=$PCRE2_LIB; fi
  - ngx-build $NGINX_VERSION $disable_pcre2 $add_http3_module --with-http_v2_module --with-http_realip_module --with-http_ssl_module --with-pcre-jit --with-cc-opt="-I$OPENSSL_INC -I$PCRE_INC" --with-ld-opt="-L$OPENSSL_LIB -Wl,-rpath,$OPENSSL_LIB -L$PCRE_LIB -Wl,-rpath,$PCRE_LIB" --add-module=../ndk-nginx-module --add-module=../echo-nginx-module --add-module=../set-misc-nginx-module --add-module=../headers-more-nginx-module --add-module=../lua-nginx-module --with-debug --with-stream_ssl_module --with-stream --with-ipv6 --add-module=../stream-lua-nginx-module > build.log 2>&1 || (cat build.log && exit 1)
  - nginx -V
  - ldd `which nginx`|grep -E 'luajit|ssl|pcre'
  - prove -I. -Itest-nginx/lib -j$JOBS -r t
