feat(tfut): add support for building code coverage reports

Added support to build code coverage reports using lcov.

Signed-off-by: Edward Potapov <edward.potapov@arm.com>
Change-Id: I71d02599bb96edf4fc0cdd763b08c39c55f65c2e
diff --git a/job/tf-ci-gateway/generate_report.sh b/job/tf-ci-gateway/generate_report.sh
index aac04f0..a60a80d 100755
--- a/job/tf-ci-gateway/generate_report.sh
+++ b/job/tf-ci-gateway/generate_report.sh
@@ -62,8 +62,8 @@
 		--meta-data html:coverity.data
 
     # Only call to merge reports if the test groups are for code coverage
-    if [[ $TEST_GROUPS == *"code-coverage"* ]] ||
-    [[ $TEST_GROUPS == "scp-boot-tests-coverage" ]]; then
+    if [[ $TEST_GROUPS == *"coverage"* ]] ||
+       [[ $TEST_GROUPS == "scp-boot-tests-coverage" ]]; then
   	   source $CI_ROOT/script/gen_merge_report.sh "${WORKSPACE}/report.json" \
   		   "${WORKSPACE}/report.html"
       echo "Finished merging reports..."
diff --git a/run_config_tfut/coverage b/run_config_tfut/coverage
index c9506de..f491b1c 100644
--- a/run_config_tfut/coverage
+++ b/run_config_tfut/coverage
@@ -6,6 +6,6 @@
 #
 
 pre_tfut_build() {
-	set_hook_var "COVERAGE" "1"
+	set_hook_var "COVERAGE" "ON"
 }
 
diff --git a/run_config_tfut/no_coverage b/run_config_tfut/no_coverage
index b24f07d..d5ec0fa 100644
--- a/run_config_tfut/no_coverage
+++ b/run_config_tfut/no_coverage
@@ -6,6 +6,6 @@
 #
 
 pre_tfut_build() {
-	set_hook_var "COVERAGE" "0"
+	set_hook_var "COVERAGE" "OFF"
 }
 
diff --git a/script/build_package.sh b/script/build_package.sh
index e7e1fe7..6e908d8 100755
--- a/script/build_package.sh
+++ b/script/build_package.sh
@@ -211,6 +211,16 @@
 	popd
 }
 
+collect_tfut_coverage() {
+	if [ "$coverage" != "ON" ]; then
+                return
+        fi
+
+	pushd "$tfut_root/build"
+	touch "${to:?}/tfut_coverage.txt"
+	popd
+}
+
 # Map the UART ID used for expect with the UART descriptor and port
 # used by the FPGA automation tools.
 map_uart() {
@@ -806,12 +816,13 @@
         cat <<EOF | log_separator >/dev/null
 
 Build command line:
-	cmake $(echo "$cmake_config") -G"Unix Makefiles" --debug-output -DCMAKE_VERBOSE_MAKEFILE -DUNIT_TEST_PROJECT_PATH="$tf_root" ..
+cmake $(echo "$cmake_config") -G"Unix Makefiles" --debug-output -DCMAKE_VERBOSE_MAKEFILE -DCOVERAGE="$COVERAGE" -DUNIT_TEST_PROJECT_PATH="$tf_root" ..
         make $(echo "$config" | tr '\n' ' ') DEBUG=$DEBUG V=1 $build_targets
 
 EOF
 	cmake $(echo "$cmake_config") -G"Unix Makefiles" --debug-output \
 		-DCMAKE_VERBOSE_MAKEFILE=ON 				\
+		-DCOVERAGE="$COVERAGE" 					\
 		-DUNIT_TEST_PROJECT_PATH="$tf_root" 			\
 		.. &>> "$build_log" || fail_build
 	echo "Done with cmake" >> "$build_log"
@@ -1586,6 +1597,8 @@
 
 		from="$tfut_build_root" to="$archive" collect_tfut_artefacts
 
+		to="$archive" coverage="$COVERAGE" collect_tfut_coverage
+
 		echo "##########"
 		echo
 		)
diff --git a/script/gen_merge_report.sh b/script/gen_merge_report.sh
index d25ebed..a94c704 100644
--- a/script/gen_merge_report.sh
+++ b/script/gen_merge_report.sh
@@ -45,7 +45,7 @@
 test_files = data['test_files']
 for index, build_number in enumerate(test_results):
     if ("bmcov" in test_files[index] or
-    "code-coverage" in test_files[index]) and test_results[build_number] == "SUCCESS":
+    "coverage" in test_files[index]) and test_results[build_number] == "SUCCESS":
         merge_number += 1
         base_url = "{}job/{}/{}/{}".format(
                         server, data['job'], build_number, "$ARTIFACT_PATH")
@@ -267,12 +267,18 @@
 
 OUTDIR=""
 index=""
+ls -al
 case "$TEST_GROUPS" in
     scp*)
             project="scp"
             jenkins_archive_folder=reports
             individual_report_folder=html/qa-code-coverage/lcov/index.html
             ;;
+    tfut*)
+            project="tfut"
+            jenkins_archive_folder=merge/outdir
+            individual_report_folder=unit_tests/trusted-firmware-a-coverage/index.html
+            ;;
     tf*)
             project="trusted_firmware"
             jenkins_archive_folder=merge/outdir
diff --git a/script/run_unit_tests.sh b/script/run_unit_tests.sh
index 8847cbc..7d4384d 100644
--- a/script/run_unit_tests.sh
+++ b/script/run_unit_tests.sh
@@ -26,7 +26,7 @@
 export run_root
 export pid_dir
 
-export tfut_root="${tfut_root:-$workspace/tfut}"
+export tfut_root="${tfut_root:-$workspace/tf-a-unit-tests}"
 
 kill_and_reap() {
 	local gid
@@ -67,6 +67,43 @@
 	popd
 }
 
+# Check coverage and make reports
+run_coverage() {
+	if [ -f "tfut_coverage.txt" ]; then
+		COVERAGE="ON"
+	else
+		COVERAGE="OFF"
+	fi
+
+	# Switch to the tfut root directory to check for coverage
+	pushd "$tfut_root/build"
+	if [ "$COVERAGE" = "ON" ]; then
+		echo "Generating coverage report..."
+		make coverage_report
+		cp -r ./tf-a-unit-tests-coverage "$workspace/unit_tests/"
+		cp -r  ./trusted-firmware-a-coverage  "$workspace/unit_tests/"
+		mkdir "$workspace/trace_report/"
+		cp -r  ./trusted-firmware-a-coverage/* "$workspace/trace_report/"
+		cp ./coverage.info "$workspace/trace_report/"
+		cat > "$workspace/config_file.json" <<EOF
+{
+	"configuration": {
+		"sources": [
+		{
+			"type": "git",
+			"URL": "https://review.trustedfirmware.org/TF-A/trusted-firmware-a",
+			"LOCATION": "trusted-firmware-a"
+		}
+		]
+	}
+}
+EOF
+	else
+		echo "Code coverage is disabled."
+	fi
+	popd
+}
+
 # Cleanup actions
 trap_with_sig cleanup SIGINT SIGHUP SIGTERM EXIT
 
@@ -107,6 +144,7 @@
 if upon "$test_run"; then
 	echo
         "$run_sh" "$@" -v -c
+        run_coverage
         exit 0
 fi
 
@@ -120,6 +158,7 @@
 else
 	"$run_sh" "$@" -c -v
 fi
+run_coverage
 batch_pid=$!
 
 # Wait for all children. Note that the wait below is *not* a timed wait.