feat(ci): build tfa and tftf in the job's workspace directory

This is useful when running locally - avoid polluting the source
directory with artefacts and allows more than 1 job to be built at the
same time.

Signed-off-by: Boyan Karatotev <boyan.karatotev@arm.com>
Change-Id: I8e8e0ff4741d5454706fd96a55936d56c3a6d22e
diff --git a/juno_utils.sh b/juno_utils.sh
index 476a6fd..e14c90f 100644
--- a/juno_utils.sh
+++ b/juno_utils.sh
@@ -111,7 +111,7 @@
 
 	# Copy BL32 to a temporary directoy, and update it in the FIP
 	local tmpdir="$(mktempdir)"
-	from="$tf_root/build/juno/$mode" to="$tmpdir" collect_build_artefacts
+	from="$tf_build_root/juno/$mode" to="$tmpdir" collect_build_artefacts
 	bin_name="tos-fw" src="$tmpdir/bl32.bin" fip_update
 }
 
diff --git a/run_config/fvp-spm.optee.sp b/run_config/fvp-spm.optee.sp
index 6f423ad..bf22472 100644
--- a/run_config/fvp-spm.optee.sp
+++ b/run_config/fvp-spm.optee.sp
@@ -9,9 +9,9 @@
 	url="$tfa_downloads/spm/07-29-2021/spmc_sel2_optee_sel1.bin" fetch_file
 
 	archive_file "spmc_sel2_optee_sel1.bin"
-	cp "${archive}/spmc_sel2_optee_sel1.bin" "${tf_root}/build/fvp/${bin_mode}"
+	cp "${archive}/spmc_sel2_optee_sel1.bin" "${archive}/build/tfa/fvp/${bin_mode}"
 
-	cat <<-EOF >"${tf_root}/build/fvp/${bin_mode}/optee_sp_layout.json"
+	cat <<-EOF >"${archive}/build/tfa/fvp/${bin_mode}/optee_sp_layout.json"
 		{
 			"op-tee" : {
 				"image": "spmc_sel2_optee_sel1.bin",
diff --git a/run_config/fvp-spm.optee.sp.bmcov b/run_config/fvp-spm.optee.sp.bmcov
index 7c2df87..2ef3708 100644
--- a/run_config/fvp-spm.optee.sp.bmcov
+++ b/run_config/fvp-spm.optee.sp.bmcov
@@ -9,9 +9,9 @@
 	url="$tfa_downloads/spm/07-29-2021/spmc_sel2_optee_sel1.bin" fetch_file
 
 	archive_file "spmc_sel2_optee_sel1.bin"
-	cp "${archive}/spmc_sel2_optee_sel1.bin" "${tf_root}/build/fvp/${bin_mode}"
+	cp "${archive}/spmc_sel2_optee_sel1.bin" "${archive}/build/tfa/fvp/${bin_mode}"
 
-	cat <<-EOF >"${tf_root}/build/fvp/${bin_mode}/optee_sp_layout.json"
+	cat <<-EOF >"${archive}/build/tfa/fvp/${bin_mode}/optee_sp_layout.json"
 		{
 			"op-tee" : {
 				"image": "spmc_sel2_optee_sel1.bin",
diff --git a/run_config/fvp-spm.trp.tftf b/run_config/fvp-spm.trp.tftf
index 21a294a..520ed07 100644
--- a/run_config/fvp-spm.trp.tftf
+++ b/run_config/fvp-spm.trp.tftf
@@ -8,7 +8,7 @@
 # Build TF-A with tf-a-tests, TRP and Hafnium
 post_tf_build() {
 	# Stash the TRP binary
-	archive_file "${tf_root}/build/fvp/${bin_mode}/rmm.bin"
+	archive_file "${tf_build_root}/fvp/${bin_mode}/rmm.bin"
 
 	build_fip BL33="$archive/tftf.bin" BL32="$archive/secure_hafnium.bin" \
 		RMM="$archive/rmm.bin"
diff --git a/run_config/fvp-spm.trp.tftf.bmcov b/run_config/fvp-spm.trp.tftf.bmcov
index 891c7c5..65c5fad 100644
--- a/run_config/fvp-spm.trp.tftf.bmcov
+++ b/run_config/fvp-spm.trp.tftf.bmcov
@@ -8,7 +8,7 @@
 # Build TF-A with tf-a-tests, TRP and Hafnium
 post_tf_build() {
 	# Stash the TRP binary
-	archive_file "${tf_root}/build/fvp/${bin_mode}/rmm.bin"
+	archive_file "${tf_build_root}/fvp/${bin_mode}/rmm.bin"
 
 	build_fip BL33="$archive/tftf.bin" BL32="$archive/secure_hafnium.bin" \
 		RMM="$archive/rmm.bin"
diff --git a/run_config/fvp-trp.tftf b/run_config/fvp-trp.tftf
index 0793c8f..89a9597 100644
--- a/run_config/fvp-trp.tftf
+++ b/run_config/fvp-trp.tftf
@@ -8,7 +8,7 @@
 # Build TF-A with tf-a-tests and TRP
 post_tf_build() {
 	# Stash the TRP binary
-	archive_file "${tf_root}/build/fvp/${bin_mode}/rmm.bin"
+	archive_file "${tf_build_root}/fvp/${bin_mode}/rmm.bin"
 
 	build_fip BL33="$archive/tftf.bin" RMM="$archive/rmm.bin"
 }
diff --git a/script/build_package.sh b/script/build_package.sh
index f6e63bb..851d840 100755
--- a/script/build_package.sh
+++ b/script/build_package.sh
@@ -42,7 +42,7 @@
 run_config="${RUN_CONFIG:?}"
 cc_config="${CC_ENABLE:-}"
 
-archive="$artefacts"
+export archive="$artefacts"
 build_log="$artefacts/build.log"
 fiptool="$tf_root/tools/fiptool/fiptool"
 cert_create="$tf_root/tools/cert_create/cert_create"
@@ -345,7 +345,7 @@
 		set +a
 	fi
 
-	make -C "$tf_root" $(cat "$tf_config_file") DEBUG="$DEBUG" V=1 "$@" \
+	make -C "$tf_root" $(cat "$tf_config_file") DEBUG="$DEBUG" BUILD_BASE=$tf_build_root V=1 "$@" \
 		${fip_targets:-fip} &>>"$build_log" || fail_build
 	)
 }
@@ -368,7 +368,7 @@
 		set +a
 	fi
 
-	make -C "$tf_root" $(cat "$tf_config_file") DEBUG="$DEBUG" V=1 "$@" \
+	make -C "$tf_root" $(cat "$tf_config_file") DEBUG="$DEBUG" V=1 BUILD_BASE=$tf_build_root "$@" \
 		${tf_extra_rules} &>>"$build_log" || fail_build
 	)
 }
@@ -582,12 +582,12 @@
 		extend_path "PATH" "path_list"
 	fi
 
-	cd "$tf_root"
+	pushd "$tf_root"
 
 	# Always distclean when running on Jenkins. Skip distclean when running
 	# locally and explicitly requested.
 	if upon "$jenkins_run" || not_upon "$dont_clean"; then
-		make distclean &>>"$build_log" || fail_build
+		make distclean BUILD_BASE=$tf_build_root &>>"$build_log" || fail_build
 	fi
 
 	# Log build command line. It is left unfolded on purpose to assist
@@ -595,7 +595,7 @@
 	cat <<EOF | log_separator >/dev/null
 
 Build command line:
-	$tf_build_wrapper make $make_j_opts $(cat "$config_file" | tr '\n' ' ') DEBUG=$DEBUG V=1 $build_targets
+	$tf_build_wrapper make $make_j_opts $(cat "$config_file" | tr '\n' ' ') DEBUG=$DEBUG V=1 BUILD_BASE=$tf_build_root $build_targets
 
 CC version:
 $(${CC-${CROSS_COMPILE}gcc} -v 2>&1)
@@ -608,7 +608,7 @@
 	# Build TF. Since build output is being directed to the build log, have
 	# descriptor 3 point to the current terminal for build wrappers to vent.
 	$tf_build_wrapper poetry run make $make_j_opts $(cat "$config_file") \
-		DEBUG="$DEBUG" V=1 SPIN_ON_BL1_EXIT="$connect_debugger" \
+		DEBUG="$DEBUG" V=1 BUILD_BASE="$tf_build_root" SPIN_ON_BL1_EXIT="$connect_debugger" \
 		$build_targets 3>&1 &>>"$build_log" || fail_build
 
         if [ "$build_targets" != "doc" ]; then
@@ -618,6 +618,7 @@
                     (poetry run memory --root "${tf_build_root}" summary "${map}" 2>&1 || true) | tee -a "${build_log}"
                 done
         fi
+	popd
 	)
 }
 
@@ -635,7 +636,7 @@
 	# Always distclean when running on Jenkins. Skip distclean when running
 	# locally and explicitly requested.
 	if upon "$jenkins_run" || not_upon "$dont_clean"; then
-		make distclean &>>"$build_log" || fail_build
+		make distclean BUILD_BASE="$tftf_build_root" &>>"$build_log" || fail_build
 	fi
 
 	# TFTF build system cannot reliably deal with -j option, so we avoid
@@ -645,11 +646,11 @@
 	cat <<EOF | log_separator >/dev/null
 
 Build command line:
-	make $make_j_opts $(cat "$config_file" | tr '\n' ' ') DEBUG=$DEBUG V=1 $build_targets
+	make $make_j_opts $(cat "$config_file" | tr '\n' ' ') DEBUG=$DEBUG V=1 BUILD_BASE="$tftf_build_root" $build_targets
 
 EOF
 
-	make $make_j_opts $(cat "$config_file") DEBUG="$DEBUG" V=1 \
+	make $make_j_opts $(cat "$config_file") DEBUG="$DEBUG" V=1 BUILD_BASE="$tftf_build_root" \
 		$build_targets &>>"$build_log" || fail_build
 	)
 }
@@ -1562,7 +1563,8 @@
 		fi
 
 		archive="$build_archive"
-		tftf_build_root="$tftf_root/build"
+		tftf_build_root="$archive/build/tftf"
+		mkdir -p ${tftf_build_root}
 
 		echo "Building Trusted Firmware TF ($mode) ..." |& log_separator
 
@@ -1698,7 +1700,8 @@
 		poetry -C "$tf_root" install --without docs
 
 		archive="$build_archive"
-		tf_build_root="$tf_root/build"
+		tf_build_root="$archive/build/tfa"
+		mkdir -p ${tf_build_root}
 
 		echo "Building Trusted Firmware ($mode) ..." |& log_separator
 
diff --git a/tf_config/fvp-spm b/tf_config/fvp-spm
index 047fd17..1181dd0 100644
--- a/tf_config/fvp-spm
+++ b/tf_config/fvp-spm
@@ -8,7 +8,7 @@
 CTX_INCLUDE_PAUTH_REGS=1
 ENABLE_FEAT_MTE2=1
 BRANCH_PROTECTION=1
-SP_LAYOUT_FILE=${tftf_root}/build/fvp/${bin_mode}/sp_layout.json
-ARM_BL2_SP_LIST_DTS=${tf_root}/build/fvp/${bin_mode}/sp_list_fragment.dts
+SP_LAYOUT_FILE=${archive}/${mode}build/tftf/fvp/${bin_mode}/sp_layout.json
+ARM_BL2_SP_LIST_DTS=${archive}/${mode}build/tfa/fvp/${bin_mode}/sp_list_fragment.dts
 GIC_EXT_INTID=1
 PLAT_TEST_SPM=1
diff --git a/tf_config/fvp-spm-hyp b/tf_config/fvp-spm-hyp
index fcc6b10..4f5191e 100644
--- a/tf_config/fvp-spm-hyp
+++ b/tf_config/fvp-spm-hyp
@@ -10,6 +10,6 @@
 CTX_INCLUDE_PAUTH_REGS=1
 ENABLE_FEAT_MTE2=1
 BRANCH_PROTECTION=1
-SP_LAYOUT_FILE=${tftf_root}/build/fvp/${bin_mode}/sp_layout.json
+SP_LAYOUT_FILE=${archive}/${mode}build/tftf/fvp/${bin_mode}/sp_layout.json
 GIC_EXT_INTID=1
 PLAT_TEST_SPM=1
diff --git a/tf_config/fvp-spm-measured-boot b/tf_config/fvp-spm-measured-boot
index c357f34..d700526 100644
--- a/tf_config/fvp-spm-measured-boot
+++ b/tf_config/fvp-spm-measured-boot
@@ -8,7 +8,7 @@
 CTX_INCLUDE_PAUTH_REGS=1
 ENABLE_FEAT_MTE2=1
 BRANCH_PROTECTION=1
-SP_LAYOUT_FILE=${tftf_root}/build/fvp/${bin_mode}/sp_layout.json
+SP_LAYOUT_FILE=${archive}/${mode}build/tftf/fvp/${bin_mode}/sp_layout.json
 MEASURED_BOOT=1
 USE_ROMLIB=1
 FVP_TRUSTED_SRAM_SIZE=384
diff --git a/tf_config/fvp-spm-optee-sp b/tf_config/fvp-spm-optee-sp
index 4f1e7d8..786fc48 100644
--- a/tf_config/fvp-spm-optee-sp
+++ b/tf_config/fvp-spm-optee-sp
@@ -8,7 +8,7 @@
 CTX_INCLUDE_EL2_REGS=1
 PLAT=fvp
 SPD=spmd
-SP_LAYOUT_FILE=${tf_root}/build/fvp/${bin_mode}/optee_sp_layout.json
+SP_LAYOUT_FILE=${archive}/${mode}build/tfa/fvp/${bin_mode}/optee_sp_layout.json
 SPMD_SPM_AT_SEL2=1
 GIC_EXT_INTID=1
 PLAT_TEST_SPM=1
diff --git a/tf_config/fvp-spm-report-max-ctx-mem-use b/tf_config/fvp-spm-report-max-ctx-mem-use
index 315d4f8..136807b 100644
--- a/tf_config/fvp-spm-report-max-ctx-mem-use
+++ b/tf_config/fvp-spm-report-max-ctx-mem-use
@@ -15,4 +15,4 @@
 PLATFORM_REPORT_CTX_MEM_USE=1
 SPD=spmd
 SPMD_SPM_AT_SEL2=1
-SP_LAYOUT_FILE=${tftf_root}/build/fvp/${bin_mode}/sp_layout.json
+SP_LAYOUT_FILE=${archive}/${mode}build/tftf/fvp/${bin_mode}/sp_layout.json
diff --git a/tf_config/fvp-spm-rme b/tf_config/fvp-spm-rme
index f93d835..9137000 100644
--- a/tf_config/fvp-spm-rme
+++ b/tf_config/fvp-spm-rme
@@ -8,6 +8,6 @@
 BRANCH_PROTECTION=1
 CTX_INCLUDE_PAUTH_REGS=1
 ENABLE_FEAT_MTE2=1
-SP_LAYOUT_FILE=${tftf_root}/build/fvp/${bin_mode}/sp_layout.json
+SP_LAYOUT_FILE=${archive}/${mode}build/tftf/fvp/${bin_mode}/sp_layout.json
 GIC_EXT_INTID=1
 PLAT_TEST_SPM=1
diff --git a/tf_config/fvp-spm-rst-bl31 b/tf_config/fvp-spm-rst-bl31
index a840c90..16fd679 100644
--- a/tf_config/fvp-spm-rst-bl31
+++ b/tf_config/fvp-spm-rst-bl31
@@ -12,6 +12,6 @@
 CTX_INCLUDE_PAUTH_REGS=1
 ENABLE_FEAT_MTE2=1
 BRANCH_PROTECTION=1
-SP_LAYOUT_FILE=${tftf_root}/build/fvp/${bin_mode}/sp_layout.json
+SP_LAYOUT_FILE=${archive}/${mode}build/tftf/fvp/${bin_mode}/sp_layout.json
 GIC_EXT_INTID=1
 PLAT_TEST_SPM=1
diff --git a/tf_config/fvp-spm-spmd-lsp b/tf_config/fvp-spm-spmd-lsp
index 7b43eb5..b11838c 100644
--- a/tf_config/fvp-spm-spmd-lsp
+++ b/tf_config/fvp-spm-spmd-lsp
@@ -7,6 +7,6 @@
 CTX_INCLUDE_PAUTH_REGS=1
 ENABLE_FEAT_MTE2=1
 BRANCH_PROTECTION=1
-SP_LAYOUT_FILE=${tftf_root}/build/fvp/${bin_mode}/sp_layout.json
+SP_LAYOUT_FILE=${archive}/${mode}build/tftf/fvp/${bin_mode}/sp_layout.json
 GIC_EXT_INTID=1
 PLAT_TEST_SPM=1
diff --git a/tf_config/fvp-spm-sve b/tf_config/fvp-spm-sve
index 6ad8ebc..91600f1 100644
--- a/tf_config/fvp-spm-sve
+++ b/tf_config/fvp-spm-sve
@@ -5,7 +5,7 @@
 BRANCH_PROTECTION=1
 ENABLE_FEAT_MTE2=1
 ENABLE_SVE_FOR_SWD=1
-SP_LAYOUT_FILE=${tftf_root}/build/fvp/${bin_mode}/sp_layout.json
+SP_LAYOUT_FILE=${archive}/${mode}build/tftf/fvp/${bin_mode}/sp_layout.json
 SPD=spmd
 SPMD_SPM_AT_SEL2=1
 GIC_EXT_INTID=1
diff --git a/tf_config/fvp-spm-sve+sme b/tf_config/fvp-spm-sve+sme
index eebe2d3..f6e7939 100644
--- a/tf_config/fvp-spm-sve+sme
+++ b/tf_config/fvp-spm-sve+sme
@@ -9,4 +9,4 @@
 PLAT=fvp
 PLAT_TEST_SPM=1
 SPD=spmd
-SP_LAYOUT_FILE=${tftf_root}/build/fvp/${bin_mode}/sp_layout.json
+SP_LAYOUT_FILE=${archive}/${mode}build/tftf/fvp/${bin_mode}/sp_layout.json
diff --git a/tf_config/fvp-spm-tbb b/tf_config/fvp-spm-tbb
index 06ca8da..550904f 100644
--- a/tf_config/fvp-spm-tbb
+++ b/tf_config/fvp-spm-tbb
@@ -8,7 +8,7 @@
 CTX_INCLUDE_PAUTH_REGS=1
 ENABLE_FEAT_MTE2=1
 BRANCH_PROTECTION=1
-SP_LAYOUT_FILE=${tftf_root}/build/fvp/${bin_mode}/sp_layout.json
+SP_LAYOUT_FILE=${archive}/${mode}build/tftf/fvp/${bin_mode}/sp_layout.json
 ARM_ROTPK_LOCATION=devel_rsa
 GENERATE_COT=1
 ROT_KEY=plat/arm/board/common/rotpk/arm_rotprivk_rsa.pem
diff --git a/tf_config/fvp-spm-tbb-dualroot b/tf_config/fvp-spm-tbb-dualroot
index 6c87551..a46656b 100644
--- a/tf_config/fvp-spm-tbb-dualroot
+++ b/tf_config/fvp-spm-tbb-dualroot
@@ -8,7 +8,7 @@
 CTX_INCLUDE_PAUTH_REGS=1
 ENABLE_FEAT_MTE2=1
 BRANCH_PROTECTION=1
-SP_LAYOUT_FILE=${tftf_root}/build/fvp/${bin_mode}/sp_layout.json
+SP_LAYOUT_FILE=${archive}/${mode}build/tftf/fvp/${bin_mode}/sp_layout.json
 ARM_ROTPK_LOCATION=devel_rsa
 GENERATE_COT=1
 COT=dualroot
diff --git a/tf_config/fvp-tbb-mbedtls-mb-spm-rme b/tf_config/fvp-tbb-mbedtls-mb-spm-rme
index 27c72ba..38657a4 100644
--- a/tf_config/fvp-tbb-mbedtls-mb-spm-rme
+++ b/tf_config/fvp-tbb-mbedtls-mb-spm-rme
@@ -9,7 +9,7 @@
 MEASURED_BOOT=1
 PLAT=fvp
 ROT_KEY=plat/arm/board/common/rotpk/arm_rotprivk_rsa.pem
-SP_LAYOUT_FILE=${tftf_root}/build/fvp/${bin_mode}/sp_layout.json
+SP_LAYOUT_FILE=${archive}/${mode}build/tftf/fvp/${bin_mode}/sp_layout.json
 SPD=spmd
 TRUSTED_BOARD_BOOT=1
 GIC_EXT_INTID=1
diff --git a/tf_config/fvp-tbb-psa-mbedtls-mb-spm-rme b/tf_config/fvp-tbb-psa-mbedtls-mb-spm-rme
index ef285a5..9245f43 100644
--- a/tf_config/fvp-tbb-psa-mbedtls-mb-spm-rme
+++ b/tf_config/fvp-tbb-psa-mbedtls-mb-spm-rme
@@ -9,7 +9,7 @@
 MEASURED_BOOT=1
 PLAT=fvp
 ROT_KEY=plat/arm/board/common/rotpk/arm_rotprivk_rsa.pem
-SP_LAYOUT_FILE=${tftf_root}/build/fvp/${bin_mode}/sp_layout.json
+SP_LAYOUT_FILE=${archive}/${mode}build/tftf/fvp/${bin_mode}/sp_layout.json
 SPD=spmd
 TRUSTED_BOARD_BOOT=1
 GIC_EXT_INTID=1
diff --git a/tf_config/fvp-tc4-spm b/tf_config/fvp-tc4-spm
index 5a2da09..641e325 100644
--- a/tf_config/fvp-tc4-spm
+++ b/tf_config/fvp-tc4-spm
@@ -3,7 +3,7 @@
 PLAT=tc
 SCP_BL2=/dev/null
 SPD=spmd
-SP_LAYOUT_FILE=${tftf_root}/build/tc/${bin_mode}/sp_layout.json
+SP_LAYOUT_FILE=${archive}/${mode}build/tftf/tc/${bin_mode}/sp_layout.json
 TARGET_PLATFORM=4
 CSS_LOAD_SCP_IMAGES=0
 ENABLE_STACK_PROTECTOR=strong