Building via the CI

Change-Id: Icf5840a020b76ccfa2cf2c7e759b4355abfac4fa
diff --git a/ci/do-build.sh b/ci/do-build.sh
index fb8346a..1c0ca6b 100755
--- a/ci/do-build.sh
+++ b/ci/do-build.sh
@@ -20,6 +20,63 @@
 # - libyang and libnetconf2 copmare CMAKE_BUILD_TYPE to lowercase "debug"...
 CMAKE_OPTIONS="${CMAKE_OPTIONS} -DENABLE_BUILD_TESTS=ON -DENABLE_VALGRIND_TESTS=OFF"
 
+build_dep_cmake() {
+    pushd ${TH_JOB_WORKING_DIR}
+    mkdir build-$1
+    pushd build-$1
+    ${CMAKE} -GNinja ${CMAKE_OPTIONS} -DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE:-Debug} -DCMAKE_INSTALL_PREFIX=${PREFIX} ${TH_GIT_PATH}/submodules/$1
+    ninja-build install
+    popd
+    popd
+}
+
+do_test_dep_cmake() {
+    pushd ${TH_JOB_WORKING_DIR}/build-$1
+    shift
+    ${CTEST} --output-on-failure "$@"
+    popd
+}
+
+emerge_dep() {
+    if [[ -f ${TH_GIT_PATH}/submodules/$1/CMakeLists.txt ]]; then
+        build_dep_cmake $1
+    else
+        echo "Unrecognized buildsystem for $1"
+        exit 1
+    fi
+}
+
+ARTIFACT=netconf-cli-$(git --git-dir ${TH_GIT_PATH}/.git rev-parse HEAD:submodules/).tar.xz
+
+scp th-ci-logs@ci-logs.gerrit.cesnet.cz:artifacts/${TH_JOB_NAME}/${ARTIFACT} . \
+    || true # ignore network errors
+
+if [[ -f ${TH_JOB_WORKING_DIR}/${ARTIFACT} ]]; then
+    tar -C ~/target -xvJf ${TH_JOB_WORKING_DIR}/${ARTIFACT}
+else
+    # rebuild everything from scratch
+
+    emerge_dep Catch
+    do_test_dep_cmake Catch -j${CI_PARALLEL_JOBS}
+
+    # Trompeloeil is a magic snowflake because it attempts to download and build Catch and kcov when building in a debug mode...
+    CMAKE_BUILD_TYPE=Release emerge_dep trompeloeil
+
+    emerge_dep docopt.cpp
+    do_test_dep_cmake docopt.cpp -j${CI_PARALLEL_JOBS}
+
+    emerge_dep spdlog
+    do_test_dep_cmake spdlog -j${CI_PARALLEL_JOBS}
+
+    # boost-spirit doesn't require installation
+
+    tar -C ~/target -cvJf ${TH_JOB_WORKING_DIR}/${ARTIFACT} .
+    ssh th-ci-logs@ci-logs.gerrit.cesnet.cz mkdir -p artifacts/${TH_JOB_NAME} \
+        || true # ignore network errors
+    rsync ${TH_JOB_WORKING_DIR}/${ARTIFACT} th-ci-logs@ci-logs.gerrit.cesnet.cz:artifacts/${TH_JOB_NAME}/ \
+        || true # ignore network errors
+fi
+
 mkdir -p ${ZUUL_PROJECT}/build
 cd ${ZUUL_PROJECT}/build
 ${CMAKE} -GNinja \