blob: 4fc0d9ac21801fe8cb1d1abe86b24b5b47dc8cbc [file] [log] [blame]
Saul Romerocacda172023-03-10 14:23:41 +00001#!/usr/bin/env bash
2#
3# Copyright (c) 2023, Arm Limited. All rights reserved.
4#
5# SPDX-License-Identifier: BSD-3-Clause
6#
7
8# Include variables and functions to be used by these scripts
9source "$CI_ROOT/utils.sh"
10################################################################################
11# CI VARIABLES:
12# workspace, warehouse, artefacts
13# GLOBAL VARIABLES:
14# OUTDIR, PROJECT, FALLBACK_PLUGIN_URL, FALLBACK_FILES, PLUGIN_BINARY
15################################################################################
16# Defining constants
17GERRIT_URL=${GERRIT_URL:-https://gerrit.oss.arm.com}
18QA_REPO_USER=jenkins_auto
19QA_REPO_INTERNAL=${QA_REPO_INTERNAL:-https://${QA_REPO_USER}:${QA_REPO_TOKEN}@git.gitlab.arm.com/tooling/qa-tools-internal.git}
20QA_REPO_PUBLIC=${QA_REPO_PUBLIC:-https://git.gitlab.arm.com/tooling/qa-tools.git}
21QA_REPO_NAME=qa-tools
22# Internal globals
23CODE_COVERAGE_FOLDER="${OUTDIR:-$workspace}/qa-code-coverage"
24DEBUG_FOLDER=${artefacts}/debug
25RELEASE_FOLDER=${artefacts}/release
26TRACE_FILE_PREFIX=covtrace
27CONFIG_JSON=${CODE_COVERAGE_FOLDER}/configuration_file.json
28INTERMEDIATE_LAYER_FILE=${CODE_COVERAGE_FOLDER}/intermediate_layer.json
29INFO_FILE=${CODE_COVERAGE_FOLDER}/coverage.info
30REPORT_FOLDER=${CODE_COVERAGE_FOLDER}/lcov
31
32if echo "$JENKINS_URL" | grep -q "oss.arm.com"; then
33 QA_REPO=${QA_REPO_PUBLIC}
34 QA_REFSPEC=${QA_REFSPEC:-master}
35else
36 QA_REPO="https://review.trustedfirmware.org/ci/qa-tools"
37 QA_REFSPEC="openci"
38fi
39
40
41################################################################################
42# Deploy qa-tools into the current directory
43# GLOBALS:
44# QA_REPO, QA_REPO_NAME, QA_REFSPEC
45# ARGUMENTS:
46# None
47# OUTPUTS:
48# Clones the qa-tools repo from the global variables with the given
49# commit hash.
50# RETURN:
51# 0 if succeeds, non-zero on error.
52################################################################################
53deploy_qa_tools() {
54 git clone "${QA_REPO}" ${QA_REPO_NAME}
55 cd ${QA_REPO_NAME} && git checkout "${QA_REFSPEC}" && cd ..
56}
57
58
59################################################################################
60# Builds or downloads the QA Code Coverage Tool
61# GLOBALS:
62# CODE_COVERAGE_FOLDER, QA_REPO, QA_REPO_NAME, QA_REFSPEC, FALLBACK_PLUGIN_URL
63# ARGUMENTS:
64# None
65# OUTPUTS:
66# Creates coverage folder and builds/downloads there the plugin binaries.
67# It exports the binary plugin location to coverage_trace_plugin.
68# RETURN:
69# 0 if succeeds, non-zero on error.
70################################################################################
71build_tool() {
72 echo "Building QA Code coverage tool..."
73 PLUGIN_BINARY="${FALLBACK_FILES%%,*}" # The first in the list of the binary files
74 local PVLIB_HOME="warehouse/SysGen/PVModelLib/$model_version/$model_build/external"
75 local LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$CODE_COVERAGE_FOLDER
76 mkdir -p ${CODE_COVERAGE_FOLDER}
77 pushd "${CODE_COVERAGE_FOLDER}"
78 deploy_qa_tools
79 local cc_source=$(find . -type f -name 'coverage_trace.cc')
80 local fallback="wget -q ${FALLBACK_PLUGIN_URL}/{$FALLBACK_FILES}"
81 echo "Warehouse=${warehouse}"
82 eval "$fallback"
83 ls -al
84 export coverage_trace_plugin="${CODE_COVERAGE_FOLDER}/${PLUGIN_BINARY}"
85 popd
86}
87
88 ################################################################################
89 # Creates configuration file for intermediate layer generation
90 # GLOBALS:
91 # PROJECT, CONFIG_JSON, INTERMEDIATE_LAYER_FILE, CODE_COVERAGE_FOLDER
92 # ARGUMENTS:
93 # $1 Folder where are the elf/axf files.
94 # $2 List of elf/axf file names.
95 # $3 Path for trace files.
96 # $4 Root folder name where all the repos are cloned.
97 # OUTPUTS:
98 # Creates coverage folder and builds/downloads there the plugin binaries.
99 # RETURN:
100 # 0 if succeeds, non-zero on error.
101 ################################################################################
102create_config_json() {
103 set +e
104 if [ -z "$1" ] || [ -z "$2" ] || [ -z "$3" ]
105 then
106 cat << END
107Missing argument at '${FUNCNAME[0]}'.
108USAGE:
109 create_config_json ' Glob binaries' 'Glob trace files' 'Repos root folder name'
110 Example:
111 create_config_json 'bl1.elf bl2.elf' 'tf'
112END
113 exit 1
114 fi
115 local ELF_FOLDER=$1
116 local dwarf_array=($2)
117 local TRACE_FOLDER=$3
118 local root_repos_foolder="${4:-$workspace}"
119 local scm_sources=""
120
121 # Obtaining binaries from array
122 bin_section=""
123 for index in "${!dwarf_array[@]}"
124 do
125 local elf_file="${ELF_FOLDER}/${dwarf_array[$index]}"
126 cp "$elf_file" ${CODE_COVERAGE_FOLDER}/.
127 read -r -d '' bin_section << EOM
128${bin_section}
129 {
130 "name": "$elf_file",
131 "traces": [
132 "${TRACE_FOLDER}/${TRACE_FILE_PREFIX:-covtrace}-*.log"
133 ]
134 }
135EOM
136 if [ $index -lt $((${#dwarf_array[@]} - 1)) ];then
137 bin_section="${bin_section},"
138 fi
139 done
140
141 if [ "$PROJECT" = "SCP" ]; then
142 read -r -d '' scm_sources << EOM
143 [
144 {
145 "type": "git",
146 "URL": "$CC_SCP_URL",
147 "COMMIT": "$CC_SCP_COMMIT",
148 "REFSPEC": "$CC_SCP_REFSPEC",
149 "LOCATION": "scp"
150 },
151 {
152 "type": "git",
153 "URL": "$CC_CMSIS_URL",
154 "COMMIT": "$CC_CMSIS_COMMIT",
155 "REFSPEC": "$CC_CMSIS_REFSPEC",
156 "LOCATION": "scp/contrib/cmsis/git"
157 }
158 ]
159EOM
160elif [ "$PROJECT" = "TF-A" ]; then
161 read -r -d '' scm_sources << EOM
162 [
163 {
164 "type": "git",
165 "URL": "$CC_TRUSTED_FIRMWARE_URL",
166 "COMMIT": "$CC_TRUSTED_FIRMWARE_COMMIT",
167 "REFSPEC": "$CC_TRUSTED_FIRMWARE_REFSPEC",
168 "LOCATION": "trusted_firmware"
169 },
170 {
171 "type": "http",
172 "URL": "$mbedtls_archive",
173 "COMPRESSION": "xz",
174 "EXTRA_PARAMS": "--strip-components=1",
175 "LOCATION": "mbedtls"
176 }
177 ]
178EOM
179 else
180 echo "SCM sources not provided for project '${PROJECT}'"
181 exit 1
182 fi
183local metadata="\"BUILD_CONFIG\": \"${BUILD_CONFIG}\", \"RUN_CONFIG\": \"${RUN_CONFIG}\""
184cat <<EOF > "${CONFIG_JSON}"
185{
186 "configuration":
187 {
188 "remove_workspace": true,
189 "include_assembly": true
190 },
191 "parameters":
192 {
193 "objdump": "${OBJDUMP}",
194 "readelf": "${READELF}",
195 "sources": $scm_sources,
196 "workspace": "${root_repos_foolder}",
197 "output_file": "${INTERMEDIATE_LAYER_FILE}",
198 "metadata": {$metadata}
199 },
200 "elfs": [
201 ${bin_section}
202 ]
203}
204EOF
205
206}
207
208################################################################################
209# Creates intermediate layer json file with trace coverage data.
210#
211# Creates a configuration JSON file to be the input for the intermediate
212# layer file creation.
213# GLOBALS:
214# TRACE_FILE_PREFIX, CODE_COVERAGE_FOLDER
215# ARGUMENTS:
216# $1 Location of trace files.
217# $2 Location of elf/axf files.
218# $3 List of binaries to be checked the traces.
219# $4 Root folder name where all the repos are cloned.
220# OUTPUTS:
221# A configuration JSON file.
222# An intermediate layer JSON file.
223# RETURN:
224# 0 if succeeds, non-zero on error.
225################################################################################
226create_intermediate_layer() {
227 local TRACE_FOLDER="$1"
228 local ELF_FOLDER="$2"
229 local LIST_OF_BINARIES="$3"
230 local root_repos_foolder="$4"
231
232 # Copying trace files into the qa-tools executables folder
233 if [ $(ls -1 ${TRACE_FOLDER}/${TRACE_FILE_PREFIX}-* 2>/dev/null | wc -l) != 0 ]; then
234 cp ${TRACE_FOLDER}/${TRACE_FILE_PREFIX}-* ${CODE_COVERAGE_FOLDER}/.
235 else
236 echo "Trace files not present, aborting reports..."
237 ls -al ${TRACE_FOLDER}
238 exit -1
239 fi
240 create_config_json "${ELF_FOLDER}" "${LIST_OF_BINARIES}" "${TRACE_FOLDER}" "$root_repos_foolder"
241 python3 ${CODE_COVERAGE_FOLDER}/qa-tools/coverage-tool/coverage-reporting/intermediate_layer.py \
242 --config-json ${CONFIG_JSON}
243
244}
245
246
247################################################################################
248# Creates LCOV coverage report.
249# GLOBALS:
250# CODE_COVERAGE_FOLDER, workspace, INTERMEDIATE_LAYER_FILE, INFO_FILE,
251# REPORT_FOLDER
252# ARGUMENTS:
253# None
254# OUTPUTS:
255# A coverage info file.
256# LCOV HTML coverage report.
257# RETURN:
258# 0 if succeeds, non-zero on error.
259################################################################################
260create_coverage_report() {
261 python3 ${CODE_COVERAGE_FOLDER}/qa-tools/coverage-tool/coverage-reporting/generate_info_file.py \
262 --workspace ${workspace} --json ${INTERMEDIATE_LAYER_FILE} --info ${INFO_FILE}
263 genhtml --branch-coverage ${INFO_FILE} --output-directory ${REPORT_FOLDER}
264}