build+ci: Upgrade build scripts, enable coverage and sanitizer flags

Change-Id: I825ea4fdf4f40c4af06b5667af432f1c0eab368d
diff --git a/.jenkins.d/00-deps.sh b/.jenkins.d/00-deps.sh
new file mode 100755
index 0000000..41b93f0
--- /dev/null
+++ b/.jenkins.d/00-deps.sh
@@ -0,0 +1,20 @@
+#!/usr/bin/env bash
+set -e
+
+JDIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
+source "$JDIR"/util.sh
+
+set -x
+
+if has OSX $NODE_LABELS; then
+    brew update
+    brew upgrade
+    brew install boost pkg-config cryptopp openssl
+    brew cleanup
+fi
+
+if has Ubuntu $NODE_LABELS; then
+    sudo apt-get -qq update
+    sudo apt-get -qq install build-essential pkg-config libboost-all-dev \
+                             libcrypto++-dev libsqlite3-dev libssl-dev
+fi
diff --git a/.jenkins.d/10-ndn-cxx.sh b/.jenkins.d/10-ndn-cxx.sh
index fa278ac..e5f7585 100755
--- a/.jenkins.d/10-ndn-cxx.sh
+++ b/.jenkins.d/10-ndn-cxx.sh
@@ -1,39 +1,43 @@
 #!/usr/bin/env bash
-set -x
 set -e
 
-cd /tmp
-BUILD="no"
-if [ ! -d ndn-cxx ]; then
-    git clone --depth 1 git://github.com/named-data/ndn-cxx
-    cd ndn-cxx
-    BUILD="yes"
+JDIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
+source "$JDIR"/util.sh
+
+set -x
+
+pushd "${CACHE_DIR:-/tmp}" >/dev/null
+
+INSTALLED_VERSION=$((cd ndn-cxx && git rev-parse HEAD) 2>/dev/null || echo NONE)
+
+sudo rm -Rf ndn-cxx-latest
+
+git clone --depth 1 git://github.com/named-data/ndn-cxx ndn-cxx-latest
+
+LATEST_VERSION=$((cd ndn-cxx-latest && git rev-parse HEAD) 2>/dev/null || echo UNKNOWN)
+
+if [[ $INSTALLED_VERSION != $LATEST_VERSION ]]; then
+    sudo rm -Rf ndn-cxx
+    mv ndn-cxx-latest ndn-cxx
 else
-    cd ndn-cxx
-    INSTALLED_VERSION=`git rev-parse HEAD || echo NONE`
-    sudo rm -Rf latest-version
-    git clone --depth 1 git://github.com/named-data/ndn-cxx latest-version
-    cd latest-version
-    LATEST_VERSION=`git rev-parse HEAD || echo UNKNOWN`
-    cd ..
-    rm -Rf latest-version
-    if [ "$INSTALLED_VERSION" != "$LATEST_VERSION" ]; then
-        cd ..
-        sudo rm -Rf ndn-cxx
-        git clone --depth 1 git://github.com/named-data/ndn-cxx
-        cd ndn-cxx
-        BUILD="yes"
-    fi
+    sudo rm -Rf ndn-cxx-latest
 fi
 
 sudo rm -Rf /usr/local/include/ndn-cxx
 sudo rm -f /usr/local/lib/libndn-cxx*
 sudo rm -f /usr/local/lib/pkgconfig/libndn-cxx*
 
-if [ "$BUILD" = "yes" ]; then
-    sudo ./waf distclean -j1 --color=yes
-fi
+pushd ndn-cxx >/dev/null
 
-./waf configure -j1 --color=yes --enable-shared --disable-static --without-osx-keychain
-./waf -j1 --color=yes
-sudo ./waf install -j1 --color=yes
+./waf -j1 --color=yes configure --enable-shared --disable-static --without-osx-keychain
+./waf -j1 --color=yes build
+sudo ./waf -j1 --color=yes install
+
+popd >/dev/null
+popd >/dev/null
+
+if has Linux $NODE_LABELS; then
+    sudo ldconfig
+elif has FreeBSD $NODE_LABELS; then
+    sudo ldconfig -a
+fi
diff --git a/.jenkins.d/20-build.sh b/.jenkins.d/20-build.sh
index 8220c31..e281a7d 100755
--- a/.jenkins.d/20-build.sh
+++ b/.jenkins.d/20-build.sh
@@ -1,32 +1,50 @@
 #!/usr/bin/env bash
-set -x
 set -e
 
-sudo rm -Rf /usr/local/include/ChronoSync
-sudo rm -f /usr/local/lib/libChronoSync*
-sudo rm -f /usr/local/lib/pkgconfig/ChronoSync*
+JDIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
+source "$JDIR"/util.sh
+
+set -x
+
+git submodule init
+git submodule sync
+git submodule update
 
 # Cleanup
 sudo ./waf -j1 --color=yes distclean
 
-# Configure/build in debug mode
-./waf -j1 --color=yes configure --with-tests --debug
+if [[ "$JOB_NAME" != *"limited-build" ]]; then
+  # Configure/build in optimized mode with tests
+  ./waf -j1 --color=yes configure --with-tests
+  ./waf -j1 --color=yes build
+
+  # Cleanup
+  sudo ./waf -j1 --color=yes distclean
+
+  # Configure/build in optimized mode without tests
+  ./waf -j1 --color=yes configure
+  ./waf -j1 --color=yes build
+
+  # Cleanup
+  sudo ./waf -j1 --color=yes distclean
+fi
+
+# Configure/build in debug mode with tests and without precompiled headers
+if [[ "$JOB_NAME" == *"code-coverage" ]]; then
+    COVERAGE="--with-coverage"
+elif ! has OSX-10.9 $NODE_LABELS && ! has OSX-10.11 $NODE_LABELS; then
+    ASAN="--with-sanitizer=address"
+fi
+./waf -j1 --color=yes configure --debug --with-tests --without-pch $COVERAGE $ASAN
 ./waf -j1 --color=yes build
 
-# Cleanup
-sudo ./waf -j1 --color=yes distclean
-
-# Configure/build in optimized mode without tests
-./waf -j1 --color=yes configure
-./waf -j1 --color=yes build
-
-# Cleanup
-sudo ./waf -j1 --color=yes distclean
-
-# Configure/build in optimized mode with tests
-./waf -j1 --color=yes configure --with-tests
-./waf -j1 --color=yes build
+# (tests will be run against debug version)
 
 # Install
-sudo ./waf install -j1 --color=yes
-sudo ldconfig || true
+sudo ./waf -j1 --color=yes install
+
+if has Linux $NODE_LABELS; then
+    sudo ldconfig
+elif has FreeBSD $NODE_LABELS; then
+    sudo ldconfig -a
+fi
diff --git a/.jenkins.d/30-test.sh b/.jenkins.d/30-test.sh
index 7e6610e..9f7ec40 100755
--- a/.jenkins.d/30-test.sh
+++ b/.jenkins.d/30-test.sh
@@ -1,8 +1,44 @@
 #!/usr/bin/env bash
-set -x
 set -e
 
-# Prepare environment
-sudo rm -Rf ~/.ndn
+JDIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
+source "$JDIR"/util.sh
 
-./build/unit-tests -l test_suite
+set -x
+
+# Prepare environment
+rm -Rf ~/.ndn
+
+if has OSX $NODE_LABELS; then
+    security unlock-keychain -p named-data
+fi
+
+ndnsec-keygen "/tmp/jenkins/$NODE_NAME" | ndnsec-install-cert -
+
+BOOST_VERSION=$(python -c "import sys; sys.path.append('build/c4che'); import _cache; print(_cache.BOOST_VERSION_NUMBER);")
+
+ut_log_args() {
+    if (( BOOST_VERSION >= 106200 )); then
+        echo --logger=HRF,test_suite,stdout:XML,all,build/xunit-${1:-report}.xml
+    else
+        if [[ -n $XUNIT ]]; then
+            echo --log_level=all $( (( BOOST_VERSION >= 106000 )) && echo -- ) \
+                 --log_format2=XML --log_sink2=build/xunit-${1:-report}.xml
+        else
+            echo --log_level=test_suite
+        fi
+    fi
+}
+
+ASAN_OPTIONS="color=always"
+ASAN_OPTIONS+=":detect_leaks=false"
+ASAN_OPTIONS+=":detect_stack_use_after_return=true"
+ASAN_OPTIONS+=":check_initialization_order=true"
+ASAN_OPTIONS+=":strict_init_order=true"
+ASAN_OPTIONS+=":detect_invalid_pointer_pairs=1"
+ASAN_OPTIONS+=":detect_container_overflow=false"
+ASAN_OPTIONS+=":strict_string_checks=true"
+ASAN_OPTIONS+=":strip_path_prefix=${PWD}/"
+export ASAN_OPTIONS
+
+./build/unit-tests $(ut_log_args ChronoSync)
diff --git a/.jenkins.d/util.sh b/.jenkins.d/util.sh
new file mode 100644
index 0000000..a89bc27
--- /dev/null
+++ b/.jenkins.d/util.sh
@@ -0,0 +1,18 @@
+has() {
+    local saved_xtrace
+    [[ $- == *x* ]] && saved_xtrace=-x || saved_xtrace=+x
+    set +x
+
+    local p=$1
+    shift
+    local i ret=1
+    for i in "$@"; do
+        if [[ "${i}" == "${p}" ]]; then
+            ret=0
+            break
+        fi
+    done
+
+    set ${saved_xtrace}
+    return ${ret}
+}