blob: ebb1b05e228ec8e4e6756297306b869a04b4d4e3 [file] [log] [blame]
Leonardo Sandoval9dfdd1b2020-08-06 17:08:11 -05001#!/usr/bin/env bash
Fathi Boudra422bf772019-12-02 11:10:16 +02002#
Leonardo Sandoval579c7372020-10-23 15:23:32 -05003# Copyright (c) 2019-2020 Arm Limited. All rights reserved.
Fathi Boudra422bf772019-12-02 11:10:16 +02004#
5# SPDX-License-Identifier: BSD-3-Clause
6#
7#
8# Clone and sync all Trusted Firmware repositories.
9#
Fathi Boudra422bf772019-12-02 11:10:16 +020010# For every cloned repository, set its location to a variable so that the
11# checked out location can be passed down to sub-jobs.
12#
13# Generate an environment file that can then be sourced by the caller.
14
15set -e
16
17ci_root="$(readlink -f "$(dirname "$0")/..")"
18source "$ci_root/utils.sh"
19
20clone_log="$workspace/clone_repos.log"
21clone_data="$workspace/clone.data"
22override_data="$workspace/override.data"
Fathi Boudra422bf772019-12-02 11:10:16 +020023inject_data="$workspace/inject.data"
24
25# File containing parameters for sub jobs
26param_file="$workspace/env.param"
27
28# Emit a parameter to sub jobs
29emit_param() {
30 echo "$1=$2" >> "$param_file"
31}
32
Zelalem219df412020-05-17 19:21:20 -050033# Emit a parameter for code coverage metadata
34code_cov_emit_param() {
35 emit_param "CC_$(echo ${1^^} | tr '-' _)_$2" "$3"
36}
37
Fathi Boudra422bf772019-12-02 11:10:16 +020038meta_data() {
39 echo "$1" >> "$clone_data"
40}
41
42# Path into the project filer where various pieces of scripts that override
43# some CI environment variables are stored.
44ci_overrides="$project_filer/ci-overrides"
45
46display_override() {
47 echo
48 echo -n "Override: "
49 # Print the relative path of the override file.
50 echo "$1" | sed "s#$ci_overrides/\?##"
51}
52
53strip_var() {
54 local var="$1"
55 local val="$(echo "${!var}" | sed 's#^\s*\|\s*$##g')"
56 eval "$var=\"$val\""
57}
58
59prefix_tab() {
60 sed 's/^/\t/g' < "${1:?}"
61}
62
63prefix_arrow() {
64 sed 's/^/ > /g' < "${1:?}"
65}
66
67test_source() {
68 local file="${1:?}"
69 if ! bash -c "source $file" &>/dev/null; then
70 return 1
71 fi
72
73 source "$file"
74 return 0
75}
76
Fathi Boudra422bf772019-12-02 11:10:16 +020077# Whether we've overridden some CI environment variables.
78has_overrides=0
79
80# Whether we've injected environment via. Jenkins
81has_env_inject=0
82
Fathi Boudra422bf772019-12-02 11:10:16 +020083clone_and_sync() {
84 local stat
85 local topic
86 local refspec="${!ref}"
87 local s_before s_after s_diff
88 local reference_dir="$project_filer/ref-repos/${name?}"
89 local ref_repo
90 local ret
Fathi Boudra422bf772019-12-02 11:10:16 +020091
92 strip_var refspec
93 strip_var url
94
Fathi Boudra422bf772019-12-02 11:10:16 +020095 # Clone in the filter workspace
96 mkdir -p "$ci_scratch"
97 pushd "$ci_scratch"
98
99 # Seconds before
100 s_before="$(date +%s)"
101
102 # Clone repository to the directory same as its name; HEAD stays at
103 # master.
104 if [ -d "$reference_dir" ]; then
105 ref_repo="--reference $reference_dir"
106 fi
Zelalem219df412020-05-17 19:21:20 -0500107 echo "$ref_repo $url $name $branch"
Fathi Boudra422bf772019-12-02 11:10:16 +0200108 git clone -q $ref_repo "$url" "$name" &>"$clone_log"
Zelalem219df412020-05-17 19:21:20 -0500109 code_cov_emit_param "${name}" "URL" "${url}"
Fathi Boudra422bf772019-12-02 11:10:16 +0200110 stat="on branch master"
111
112 pushd "$name"
113
114 if [ "$refspec" ] && [ "$refspec" != "master" ]; then
115 # If a specific revision is specified, always use that.
116 git fetch -q origin "$refspec" &>"$clone_log"
117 git checkout -q FETCH_HEAD &>"$clone_log"
118 stat="refspec $refspec"
119
120 # If it's not a commit hash, have the refspec replicated on the
121 # clone so that downstream jobs can clone from this one using
122 # the same refspec.
123 if echo "$refspec" | grep -qv '^[a-f0-9]\+$'; then
Saul Romero06717eb2023-08-18 11:20:38 +0100124 git branch -f "$refspec" FETCH_HEAD
Fathi Boudra422bf772019-12-02 11:10:16 +0200125 fi
Fathi Boudra422bf772019-12-02 11:10:16 +0200126 fi
127
Zelalem219df412020-05-17 19:21:20 -0500128 code_cov_emit_param "${name}" "REFSPEC" "${refspec}"
Fathi Boudra422bf772019-12-02 11:10:16 +0200129 # Generate meta data. Eliminate any quoting in commit subject as it
130 # might cause problems when reporting back to Gerrit.
131 meta_data "$name: $stat"
132 meta_data " $(git show --quiet --format=%H): $(git show --quiet --format=%s | sed "s/[\"']/ /g")"
133 meta_data " Commit date: $(git show --quiet --format=%cd)"
134 meta_data
Zelalem219df412020-05-17 19:21:20 -0500135 code_cov_emit_param "${name}" "COMMIT" "$(git show --quiet --format=%H)"
Fathi Boudra422bf772019-12-02 11:10:16 +0200136
137 # Calculate elapsed seconds
138 s_after="$(date +%s)"
139 let "s_diff = $s_after - $s_before" || true
140
141 echo
142 echo "Repository: $url ($stat)"
143 prefix_arrow <(git show --quiet)
144 echo "Cloned in $s_diff seconds"
145 echo
146
147 popd
148 popd
149
150 emit_env "$loc" "$ci_scratch/$name"
151 emit_env "$ref" "$refspec"
Fathi Boudra422bf772019-12-02 11:10:16 +0200152}
153
Fathi Boudra422bf772019-12-02 11:10:16 +0200154# Environment file in Java property file format, that's soured in Jenkins job
155env_file="$workspace/env"
156rm -f "$env_file"
157
158# Workspace on external filer where all repositories gets cloned so that they're
159# accessible to all Jenkins slaves.
160if upon "$local_ci"; then
161 ci_scratch="$workspace/filer"
162else
163 scratch_owner="${JOB_NAME:?}-${BUILD_NUMBER:?}"
164 ci_scratch="$project_scratch/$scratch_owner"
165 tforg_key="$CI_BOT_KEY"
166 tforg_user="$CI_BOT_USERNAME"
167fi
168
169if [ -d "$ci_scratch" ]; then
170 # This could be because of jobs of same name running from
171 # production/staging/temporary VMs
172 echo "Scratch space $ci_scratch already exists; removing."
173 rm -rf "$ci_scratch"
174fi
175mkdir -p "$ci_scratch"
176
Fathi Boudra422bf772019-12-02 11:10:16 +0200177# Set CI_SCRATCH so that it'll be injected when sub-jobs are triggered.
178emit_param "CI_SCRATCH" "$ci_scratch"
179
180# However, on Jenkins v2, injected environment variables won't override current
181# job's parameters. This means that the current job (the scratch owner, the job
182# that's executing this script) would always observe CI_SCRATCH as empty, and
183# therefore won't be able to remove it. Therefore, use a different variable
184# other than CI_SCRATCH parameter for the current job to refer to the scratch
185# space (although they both will have the same value!)
186emit_env "SCRATCH_OWNER" "$scratch_owner"
187emit_env "SCRATCH_OWNER_SPACE" "$ci_scratch"
188
189strip_var CI_ENVIRONMENT
190if [ "$CI_ENVIRONMENT" ]; then
191 {
192 echo
193 echo "Injected environment:"
194 prefix_tab <(echo "$CI_ENVIRONMENT")
195 echo
196 } >> "$inject_data"
197
198 cat "$inject_data"
199
200 tmp_env=$(mktempfile)
201 echo "$CI_ENVIRONMENT" > "$tmp_env"
202 source "$tmp_env"
203 cat "$tmp_env" >> "$env_file"
204
205 has_env_inject=1
206fi
207
Fathi Boudra422bf772019-12-02 11:10:16 +0200208TF_REFSPEC="${tf_refspec:-$TF_REFSPEC}"
209if not_upon "$no_tf"; then
210 # Clone Trusted Firmware repository
211 url="$tf_src_repo_url" name="trusted-firmware" ref="TF_REFSPEC" \
212 loc="TF_CHECKOUT_LOC" \
Fathi Boudra422bf772019-12-02 11:10:16 +0200213 clone_and_sync
214fi
215
216TFTF_REFSPEC="${tftf_refspec:-$TFTF_REFSPEC}"
217if not_upon "$no_tftf"; then
218 # Clone Trusted Firmware TF repository
219 url="$tftf_src_repo_url" name="trusted-firmware-tf" ref="TFTF_REFSPEC" \
220 loc="TFTF_CHECKOUT_LOC" \
Fathi Boudra422bf772019-12-02 11:10:16 +0200221 clone_and_sync
222fi
223
Zelalem219df412020-05-17 19:21:20 -0500224# Clone code coverage repository if code coverage is enabled
225if not_upon "$no_cc"; then
226 pushd "$ci_scratch"
227 git clone -q $cc_src_repo_url cc_plugin -b $cc_src_repo_tag 2> /dev/null
228 popd
229fi
230
Fathi Boudra422bf772019-12-02 11:10:16 +0200231SCP_REFSPEC="${scp_refspec:-$SCP_REFSPEC}"
232if upon "$clone_scp"; then
233 # Clone SCP Firmware repository
234 # NOTE: currently scp/firmware:master is not tracking the upstream.
235 # Therefore, if the url is gerrit.oss.arm.com/scp/firmware and there is
236 # no ref_spec, then set the ref_spec to master-upstream.
Fathi Boudra422bf772019-12-02 11:10:16 +0200237 if [ "$scp_src_repo_url" = "$scp_src_repo_default" ]; then
238 SCP_REFSPEC="${SCP_REFSPEC:-master-upstream}"
239 fi
240
241 url="$scp_src_repo_url" name="scp" ref="SCP_REFSPEC" \
242 loc="SCP_CHECKOUT_LOC" clone_and_sync
243
244 pushd "$ci_scratch/scp"
245
246 # Edit the submodule URL to point to the reference repository so that
247 # all submodule update pick from the reference repository instead of
248 # Github.
249 cmsis_ref_repo="${cmsis_root:-$project_filer/ref-repos/cmsis}"
250 if [ -d "$cmsis_ref_repo" ]; then
251 cmsis_reference="--reference $cmsis_ref_repo"
252 fi
Fathi Boudra422bf772019-12-02 11:10:16 +0200253 git submodule -q update $cmsis_reference --init
Girish Pathak14c45c82021-03-03 11:18:22 +0000254
Brett Warren821799a2022-08-19 12:23:10 +0100255 eval cmsis_dir="$(git submodule status | grep cmsis | awk 'NR==1{print $2}')"
Girish Pathak14c45c82021-03-03 11:18:22 +0000256
Zelalem1af7a7b2020-08-04 17:34:32 -0500257 # Workaround while fixing permissions on /arm/projectscratch/ssg/trusted-fw/ref-repos/cmsis
Girish Pathak14c45c82021-03-03 11:18:22 +0000258 cd $cmsis_dir
Zelalem1af7a7b2020-08-04 17:34:32 -0500259 code_cov_emit_param "CMSIS" "URL" "$(git remote -v | grep fetch | awk '{print $2}')"
260 code_cov_emit_param "CMSIS" "COMMIT" "$(git rev-parse HEAD)"
261 code_cov_emit_param "CMSIS" "REFSPEC" "master"
262 cd ..
263 ########################################
Fathi Boudra422bf772019-12-02 11:10:16 +0200264 popd
265fi
266
Olivier Depreze52d6fc2019-12-17 17:48:19 +0100267SPM_REFSPEC="${spm_refspec:-$SPM_REFSPEC}"
268if not_upon "$no_spm"; then
269 # Clone SPM repository
270 url="$spm_src_repo_url" name="spm" ref="SPM_REFSPEC" \
271 loc="SPM_CHECKOUT_LOC" clone_and_sync
272fi
273
Fathi Boudra422bf772019-12-02 11:10:16 +0200274CI_REFSPEC="${ci_refspec:-$CI_REFSPEC}"
275if not_upon "$no_ci"; then
276 # Clone Trusted Firmware CI repository
277 url="$tf_ci_repo_url" name="trusted-firmware-ci" ref="CI_REFSPEC" \
Chris Kaye38de7a2025-08-04 19:06:35 +0100278 loc="CI_ROOT" clone_and_sync
Fathi Boudra422bf772019-12-02 11:10:16 +0200279fi
280
Jimmy Brisson0d5e12c2023-05-16 14:51:51 -0500281TF_M_TESTS_REFSPEC="${tf_m_tests_refspec:-$TF_M_TESTS_REFSPEC}"
282if not_upon "$no_tfm_tests"; then
283 url="$tf_m_tests_src_repo_url" name="tf-m-tests" ref="TF_M_TESTS_REFSPEC" \
284 loc="TF_M_TESTS_PATH" clone_and_sync
285fi
286
287TF_M_EXTRAS_REFSPEC="${tf_m_extras_refspec:-$TF_M_EXTRAS_REFSPEC}"
288if not_upon "$no_tfm_extras"; then
289 url="$tf_m_extras_src_repo_url" name="tf-m-extras" ref="TF_M_EXTRAS_REFSPEC" \
290 loc="TF_M_EXTRAS_PATH" clone_and_sync
291fi
292
Manish V Badarkhe41909452025-04-11 12:06:45 +0100293RMM_REFSPEC="${rmm_refspec:-$RMM_REFSPEC}"
Manish V Badarkhed62aa5f2025-03-18 21:18:14 +0000294if not_upon "$no_rmm"; then
295 url="$rmm_src_repo_url" name="tf-rmm" ref="RMM_REFSPEC" \
296 loc="RMM_PATH" clone_and_sync
297fi
298
Zelalem219df412020-05-17 19:21:20 -0500299echo "SCP_TOOLS_COMMIT=$SCP_TOOLS_COMMIT" >> "$param_file"
300
Fathi Boudra422bf772019-12-02 11:10:16 +0200301# Copy environment file to ci_scratch for sub-jobs' access
302cp "$env_file" "$ci_scratch"
303cp "$param_file" "$ci_scratch"
304
305# Copy clone data so that it's available for sub-jobs' HTML reporting
306if [ -f "$clone_data" ]; then
307 cp "$clone_data" "$ci_scratch"
308fi
309
310# vim: set tw=80 sw=8 noet: