Building via the CI

Change-Id: Icf5840a020b76ccfa2cf2c7e759b4355abfac4fa
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 6c72b8d..5fa2341 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -2,3 +2,34 @@
 cmake_minimum_required(VERSION 3.0)
 set(CMAKE_CXX_STANDARD 14)
 set(CMAKE_CXX_STANDARD_REQUIRED ON)
+
+include(GNUInstallDirs)
+
+# Set a default build type if none was specified. This was shamelessly stolen
+# from VTK's cmake setup because these guys produce both CMake and a project that
+# manipulates this variable, and the web is full of posts where people say that
+# it is apparently evil to just set the build type in a way an earlier version of
+# this patch did. Oh, and the location of this check/update matters, apparently.
+#
+# Yes, this is just plain crazy.
+if(NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES)
+    message(STATUS "Setting build type to 'RelWithDebInfo' as none was specified.")
+    set(CMAKE_BUILD_TYPE RelWithDebInfo CACHE STRING "Choose the type of build." FORCE)
+    # Set the possible values of build type for cmake-gui
+    set_property(CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS "Debug" "Release" "MinSizeRel" "RelWithDebInfo")
+endif()
+
+# -Werror is not a default for sanity reasons (one cannot know what warnings a future compiler
+# might bring along), but it's a default in debug mode. The idea is that developers should care
+# about a warning-free build, and that this is easier than messing with yet another configure option.
+set(CMAKE_CXX_FLAGS_DEBUG "-Werror ${CMAKE_CXX_FLAGS_DEBUG}")
+
+# I don't want to duplicate the compiler's optimizations
+set(CMAKE_CXX_FLAGS "-O2 ${CMAKE_CXX_FLAGS}")
+
+# Build warnings are useful tools (and this project should be warning-free anyway), enable them on all
+# configurations. They are warnings, not errors.
+set(CMAKE_CXX_FLAGS "-Wall -Wextra -pedantic ${CMAKE_CXX_FLAGS}")
+
+find_package(docopt REQUIRED)
+find_package(spdlog REQUIRED)
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 \