Sync build system and CI scripts with ndn-tools

Change-Id: I21dc5349f3f3c819bf8590fe709157ce0bc26b44
diff --git a/.jenkins.d/00-deps.sh b/.jenkins.d/00-deps.sh
new file mode 100755
index 0000000..95c2447
--- /dev/null
+++ b/.jenkins.d/00-deps.sh
@@ -0,0 +1,29 @@
+#!/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
+    FORMULAE=(boost openssl pkg-config)
+    brew update
+    if [[ -n $TRAVIS ]]; then
+        # travis images come with a large number of brew packages
+        # pre-installed, don't waste time upgrading all of them
+        for FORMULA in "${FORMULAE[@]}"; do
+            brew outdated $FORMULA || brew upgrade $FORMULA
+        done
+    else
+        brew upgrade
+    fi
+    brew install "${FORMULAE[@]}"
+    brew cleanup
+fi
+
+if has Ubuntu $NODE_LABELS; then
+    sudo apt-get -qq update
+    sudo apt-get -qy install build-essential pkg-config libboost-all-dev \
+                             libsqlite3-dev libssl-dev
+fi
diff --git a/.jenkins.d/01-ndn-cxx.sh b/.jenkins.d/01-ndn-cxx.sh
new file mode 100755
index 0000000..c9e2a63
--- /dev/null
+++ b/.jenkins.d/01-ndn-cxx.sh
@@ -0,0 +1,56 @@
+#!/usr/bin/env bash
+set -e
+
+JDIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
+source "$JDIR"/util.sh
+
+set -x
+
+pushd "${CACHE_DIR:-/tmp}" >/dev/null
+
+INSTALLED_VERSION=
+if has OSX $NODE_LABELS; then
+    BOOST=$(brew ls --versions boost)
+    OLD_BOOST=$(cat boost.txt || :)
+    if [[ $OLD_BOOST != $BOOST ]]; then
+        echo "$BOOST" > boost.txt
+        INSTALLED_VERSION=NONE
+    fi
+fi
+
+if [[ -z $INSTALLED_VERSION ]]; then
+    INSTALLED_VERSION=$(git -C ndn-cxx rev-parse HEAD 2>/dev/null || echo NONE)
+fi
+
+sudo rm -Rf ndn-cxx-latest
+
+git clone --depth 1 git://github.com/named-data/ndn-cxx ndn-cxx-latest
+
+LATEST_VERSION=$(git -C ndn-cxx-latest 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
+    sudo rm -Rf ndn-cxx-latest
+fi
+
+sudo rm -f /usr/local/bin/ndnsec*
+sudo rm -fr /usr/local/include/ndn-cxx
+sudo rm -f /usr/local/lib/libndn-cxx*
+sudo rm -f /usr/local/lib/pkgconfig/libndn-cxx.pc
+
+pushd ndn-cxx >/dev/null
+
+./waf configure --color=yes --enable-shared --disable-static --without-osx-keychain
+./waf build --color=yes -j${WAF_JOBS:-1}
+sudo env "PATH=$PATH" ./waf install --color=yes
+
+popd >/dev/null
+popd >/dev/null
+
+if has Linux $NODE_LABELS; then
+    sudo ldconfig
+elif has FreeBSD10 $NODE_LABELS; then
+    sudo ldconfig -m
+fi
diff --git a/.jenkins.d/10-build.sh b/.jenkins.d/10-build.sh
new file mode 100755
index 0000000..5a8a7f5
--- /dev/null
+++ b/.jenkins.d/10-build.sh
@@ -0,0 +1,10 @@
+#!/usr/bin/env bash
+set -e
+set -x
+
+sudo env "PATH=$PATH" ./waf --color=yes distclean
+
+./waf --color=yes configure
+./waf --color=yes build -j${WAF_JOBS:-1}
+
+sudo env "PATH=$PATH" ./waf --color=yes install
diff --git a/.jenkins.d/10-ndn-cxx.sh b/.jenkins.d/10-ndn-cxx.sh
deleted file mode 100755
index 951e3f9..0000000
--- a/.jenkins.d/10-ndn-cxx.sh
+++ /dev/null
@@ -1,39 +0,0 @@
-#!/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"
-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
-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
-
-./waf configure -j1 --color=yes --without-osx-keychain
-./waf -j1 --color=yes
-sudo ./waf install -j1 --color=yes
diff --git a/.jenkins.d/20-build.sh b/.jenkins.d/20-build.sh
deleted file mode 100755
index dc8af29..0000000
--- a/.jenkins.d/20-build.sh
+++ /dev/null
@@ -1,8 +0,0 @@
-#!/usr/bin/env bash
-set -x
-set -e
-
-sudo ./waf distclean -j1 --color=yes
-./waf configure -j1 --color=yes
-./waf -j1 --color=yes
-sudo ./waf install -j1 --color=yes
diff --git a/.jenkins.d/README.md b/.jenkins.d/README.md
new file mode 100644
index 0000000..6c0f5a9
--- /dev/null
+++ b/.jenkins.d/README.md
@@ -0,0 +1,34 @@
+CONTINUOUS INTEGRATION SCRIPTS
+==============================
+
+Environment Variables Used in Build Scripts
+-------------------------------------------
+
+- `NODE_LABELS`: the variable defines a list of OS properties.  The set values are used by the
+  build scripts to select proper behavior for different OS.
+
+  The list should include at least `[OS_TYPE]`, `[DISTRO_TYPE]`, and `[DISTRO_VERSION]`.
+
+  Possible values for Linux:
+
+  * `[OS_TYPE]`: `Linux`
+  * `[DISTRO_TYPE]`: `Ubuntu`
+  * `[DISTRO_VERSION]`: `Ubuntu-14.04`, `Ubuntu-16.04`
+
+  Possible values for OS X / macOS:
+
+  * `[OS_TYPE]`: `OSX`
+  * `[DISTRO_TYPE]`: `OSX` (can be absent)
+  * `[DISTRO_VERSION]`: `OSX-10.10`, `OSX-10.11`, `OSX-10.12`
+
+- `JOB_NAME`: optional variable to define type of the job.  Depending on the defined job type,
+  the build scripts can perform different tasks.
+
+  Possible values:
+
+  * empty: default build process
+
+- `CACHE_DIR`: the variable defines a path to folder containing cached files from previous builds,
+  e.g., a compiled version of ndn-cxx library.  If not set, `/tmp` is used.
+
+- `WAF_JOBS`: number of parallel build jobs used by waf, defaults to 1.
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}
+}