CI: start building with coverage profiling

This has to happen in two commits because the upcoming build job which
performs the comparison also builds the content of the repo from the
*previous* commit as-is. This means that this commit has to:

- enable `--coverage`,
- start generating that `coverage.xml`,
- teach artifact fetching code that the "-coverage-diff" suffix won't be
significant

Also remove one leftover which is no longer needed, really.

Change-Id: Ia8c73905f9f9eceb8347f8e83f402268d5cde1d0
diff --git a/ci/build.sh b/ci/build.sh
index 059c265..8b08c3b 100755
--- a/ci/build.sh
+++ b/ci/build.sh
@@ -1,7 +1,7 @@
 #!/bin/bash
 
 set -eux -o pipefail
-shopt -s failglob
+shopt -s failglob extglob
 
 ZUUL_JOB_NAME=$(jq < ~/zuul-env.json -r '.job')
 ZUUL_TENANT=$(jq < ~/zuul-env.json -r '.tenant')
@@ -50,6 +50,12 @@
     CMAKE_OPTIONS="${CMAKE_OPTIONS} -DWITH_PYTHON_BINDINGS=ON"
 fi
 
+if [[ $ZUUL_JOB_NAME =~ .*-cover.* ]]; then
+    export CFLAGS="${CFLAGS} --coverage"
+    export CXXFLAGS="${CXXFLAGS} --coverage"
+    export LDFLAGS="${LDFLAGS} --coverage"
+fi
+
 PREFIX=~/target
 mkdir ${PREFIX}
 BUILD_DIR=~/build
@@ -69,7 +75,7 @@
 
 if [[ -z "${ARTIFACT_URL}" ]]; then
     # fallback to a promoted artifact
-    ARTIFACT_URL="https://object-store.cloud.muni.cz/swift/v1/ci-artifacts-${ZUUL_TENANT}/${ZUUL_GERRIT_HOSTNAME}/CzechLight/dependencies/${ZUUL_JOB_NAME}/${DEP_SUBMODULE_COMMIT}.tar.xz"
+    ARTIFACT_URL="https://object-store.cloud.muni.cz/swift/v1/ci-artifacts-${ZUUL_TENANT}/${ZUUL_GERRIT_HOSTNAME}/CzechLight/dependencies/${ZUUL_JOB_NAME%%-cover?(-diff)}/${DEP_SUBMODULE_COMMIT}.tar.xz"
 fi
 
 ARTIFACT_FILE=$(basename ${ARTIFACT_URL})
@@ -82,9 +88,6 @@
 tar -C ${PREFIX} -xf ${ARTIFACT_FILE}
 rm ${ARTIFACT_FILE}
 
-# TODO: move this to CzechLight/dependencies
-mkdir -p /home/ci/target/var-run
-
 cd ${BUILD_DIR}
 cmake -GNinja -DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE:-Debug} -DCMAKE_INSTALL_PREFIX=${PREFIX} ${CMAKE_OPTIONS} ${ZUUL_PROJECT_SRC_DIR}
 ninja-build
@@ -96,3 +99,7 @@
     zip -r ~/zuul-output/docs/html.zip .
     popd
 fi
+
+if [[ $LDFLAGS =~ .*--coverage.* ]]; then
+    gcovr -j ${CI_PARALLEL_JOBS} --object-directory ${BUILD_DIR} --root ${ZUUL_PROJECT_SRC_DIR} --xml --output ${BUILD_DIR}/coverage.xml
+fi