ci: add capability to clone and build RF-A

 * Add support to clone the RF-A repository using
   local CI and also add necessary hooks to build RF-A.
 * Update run_local_ci script to pass missing paramters
   like no_rfa.

Change-Id: Icad55bc3321f524009636f554f6d751cc536eaf6
Signed-off-by: Tomás González <tomasagustin.gonzalezorlando@arm.com>
diff --git a/script/build_package.sh b/script/build_package.sh
index bb01678..8d3723e 100755
--- a/script/build_package.sh
+++ b/script/build_package.sh
@@ -19,6 +19,7 @@
 
 # Directory to where the source code e.g. for Trusted Firmware is checked out.
 export tf_root="${tf_root:-$workspace/trusted_firmware}"
+export rfa_root="${rfa_root:-$workspace/rusted-firmware-a}"
 export tftf_root="${tftf_root:-$workspace/trusted_firmware_tf}"
 export scp_root="${scp_root:-$workspace/scp}"
 scp_tools_root="${scp_tools_root:-$workspace/scp_tools}"
@@ -35,6 +36,7 @@
 scp_tools_commit="${SCP_TOOLS_COMMIT:-master}"
 spm_refspec="$SPM_REFSPEC"
 rmm_refspec="$RMM_REFSPEC"
+rfa_refspec="$RFA_REFSPEC"
 
 test_config="${TEST_CONFIG:?}"
 test_group="${TEST_GROUP:?}"
@@ -527,6 +529,16 @@
 	)
 }
 
+get_rfa_opt() {
+	(
+	name="${1:?}"
+	if config_valid "$rfa_config_file"; then
+		source "$rfa_config_file"
+		echo "${!name}"
+	fi
+	)
+}
+
 get_rmm_opt() {
         (
         name="${1:?}"
@@ -624,20 +636,20 @@
 
 build_rfa() {
 	(
-	config_file="${tf_build_config:-$tf_config_file}"
+	config_file="${rfa_build_config:-$rfa_config_file}"
 
 	# Build the 'all' target by default.
-	build_targets="${tf_build_targets:-all}"
+	build_targets="${rfa_build_targets:-all}"
 
 	source "$config_file"
 
-	cd "$tf_root"
+	cd "$rfa_root"
 
 	# Always distclean when running on Jenkins. Skip distclean when running
 	# locally and explicitly requested.
 	if upon "$jenkins_run" || not_upon "$dont_clean"; then
 		echo "Cleaning TF-A..."
-		make -C "$TFA" distclean &>>"$build_log" || fail_build
+		make -C "$tf_root" distclean &>>"$build_log" || fail_build
 
 		echo 'Cleaning RF-A...'
 		cargo clean &>>"$build_log" || fail_build
@@ -1019,6 +1031,11 @@
 	set_hook_var "tf_build_targets" "$targets"
 }
 
+set_rfa_build_targets() {
+	echo "Set build target to '${targets:?}'"
+	set_hook_var "rfa_build_targets" "$targets"
+}
+
 set_tftf_build_targets() {
 	echo "Set build target to '${targets:?}'"
 	set_hook_var "tftf_build_targets" "$targets"
@@ -1284,10 +1301,13 @@
 scp_tools_config_file="$ci_root/scp_tools_config/$scp_tools_config"
 spm_config_file="$ci_root/spm_config/$spm_config"
 rmm_config_file="$ci_root/rmm_config/$rmm_config"
+rfa_config_file="$ci_root/rfa_config/$rfa_config"
+
 
 # File that keeps track of applied patches
 tf_patch_record="$workspace/tf_patches"
 tftf_patch_record="$workspace/tftf_patches"
+rfa_patch_record="$workspace/rfa_patches"
 
 pushd "$workspace"
 
@@ -1300,6 +1320,15 @@
 	echo
 fi
 
+if ! config_valid "$rfa_config"; then
+	rfa_config=
+else
+	echo "RF-A config:"
+	echo
+	sort "$rfa_config_file" | sed '/^\s*$/d;s/^/\t/'
+	echo
+fi
+
 if ! config_valid "$tftf_config"; then
 	tftf_config=
 else
@@ -1360,6 +1389,16 @@
 	show_head "$tf_root"
 fi
 
+if [ "$rfa_config" ] && assert_can_git_clone "rfa_root"; then
+	# If the Rusted Firmware repository has already been checked out, use
+	# that location. Otherwise, clone one ourselves.
+	echo "Cloning Rusted Firmware..."
+
+	clone_url="${RFA_CHECKOUT_LOC:-$rfa_src_repo_url}" where="$rfa_root" \
+	    refspec="$RFA_REFSPEC" clone_repo &>>"$build_log"
+	show_head "$rfa_root"
+fi
+
 if [ "$tftf_config" ] && assert_can_git_clone "tftf_root"; then
 	# If the Trusted Firmware TF repository has already been checked out,
 	# use that location. Otherwise, clone one ourselves.
@@ -1709,6 +1748,84 @@
 			)
 	fi
 
+	# RF-A build
+	if config_valid "$rfa_config"; then
+		(
+		echo "##########"
+
+		plat="$(get_rfa_opt PLAT)"
+		plat_utils="$ci_root/${plat}_utils.sh"
+		if [ -f "$plat_utils" ]; then
+		    source "$plat_utils"
+		fi
+
+		fvp_tsram_size="$(get_rfa_opt FVP_TRUSTED_SRAM_SIZE)"
+		fvp_tsram_size="${fvp_tsram_size:-256}"
+
+		archive="$build_archive"
+
+		# Clone TF-A repo if required. Save its path into the
+		# special variable "TFA", which is used by RF-A build
+		# system.
+		export TFA="${TFA:-$tf_root}"
+		if assert_can_git_clone "TFA"; then
+			echo "Cloning TF-A..."
+			clone_url="$tf_src_repo_url" where="$TFA" clone_repo
+		fi
+		show_head "$TFA"
+		poetry -C "$TFA" install --without docs
+
+		rfa_build_root="$rfa_root/target"
+		echo "Building Rusted Firmware ($mode) ..." |& log_separator
+
+		if not_upon "$local_ci"; then
+			# In the CI Dockerfile, rustup is installed by the root user in the
+			# non-default location /usr/local/rustup, so $RUSTUP_HOME is required to
+			# access rust config e.g. default toolchains and run cargo
+			#
+			# Leave $CARGO_HOME blank so when this script is run in CI by the buildslave
+			# user, it uses the default /home/buildslave/.cargo directory which it has
+			# write permissions for - that allows it to download new crates during
+			# compilation
+			#
+			# The buildslave user does not have write permissions to the default
+			# $CARGO_HOME=/usr/local/cargo dir and so will error when trying to download
+			# new crates otherwise
+			#
+			# note: $PATH still contains /usr/local/cargo/bin at this point so cargo is
+			# still run via the root installation
+			#
+			# see https://github.com/rust-lang/rustup/issues/1085
+			set_hook_var "RUSTUP_HOME" "/usr/local/rustup"
+		fi
+
+		# Call pre-build hook
+		call_hook pre_rfa_build
+
+		build_rfa
+
+		# Call post-build hook
+		call_hook post_rfa_build
+
+		# Pre-archive hook
+		call_hook pre_rfa_archive
+
+		from="$rfa_build_root" to="$archive" collect_rfa_artefacts
+
+		# Post-archive hook
+		call_hook post_rfa_archive
+
+		call_hook fetch_rfa_resource
+		call_hook post_fetch_rfa_resource
+
+		# Generate LAVA job files if necessary
+		call_hook generate_lava_job_template
+		call_hook generate_lava_job
+
+		echo "##########"
+		)
+	fi
+
 	# TF build
 	if config_valid "$tf_config"; then
 		(
@@ -1734,60 +1851,17 @@
 		fvp_tsram_size="$(get_tf_opt FVP_TRUSTED_SRAM_SIZE)"
 		fvp_tsram_size="${fvp_tsram_size:-256}"
 
-		if not_upon "$(get_tf_opt RUST)"; then
-			poetry -C "$tf_root" install --without docs
-		fi
+		poetry -C "$tf_root" install --without docs
 
 		archive="$build_archive"
 
-		if not_upon "$(get_tf_opt RUST)"; then
-			tf_build_root="$tf_root/build"
-			echo "Building Trusted Firmware ($mode) ..." |& log_separator
-		else
-			# Clone TF-A repo if required. Save its path into the
-			# special variable "TFA", which is used by RF-A build
-			# system.
-			export TFA="${TFA-$workspace/tfa}"
-			if assert_can_git_clone "TFA"; then
-				echo "Cloning TF-A..."
-				clone_url="$tf_src_repo_url" where="$TFA" clone_repo
-			fi
-			show_head "$TFA"
-			poetry -C "$TFA" install --without docs
-
-			rfa_build_root="$tf_root/target"
-			echo "Building Rusted Firmware ($mode) ..." |& log_separator
-
-			if not_upon "$local_ci"; then
-				# In the CI Dockerfile, rustup is installed by the root user in the
-				# non-default location /usr/local/rustup, so $RUSTUP_HOME is required to
-				# access rust config e.g. default toolchains and run cargo
-				#
-				# Leave $CARGO_HOME blank so when this script is run in CI by the buildslave
-				# user, it uses the default /home/buildslave/.cargo directory which it has
-				# write permissions for - that allows it to download new crates during
-				# compilation
-				#
-				# The buildslave user does not have write permissions to the default
-				# $CARGO_HOME=/usr/local/cargo dir and so will error when trying to download
-				# new crates otherwise
-				#
-				# note: $PATH still contains /usr/local/cargo/bin at this point so cargo is
-				# still run via the root installation
-				#
-				# see https://github.com/rust-lang/rustup/issues/1085
-				set_hook_var "RUSTUP_HOME" "/usr/local/rustup"
-			fi
-		fi
+		tf_build_root="$tf_root/build"
+		echo "Building Trusted Firmware ($mode) ..." |& log_separator
 
 		# Call pre-build hook
 		call_hook pre_tf_build
 
-		if upon "$(get_tf_opt RUST)"; then
-			build_rfa
-		else
-			build_tf
-		fi
+		build_tf
 
 		# Call post-build hook
 		call_hook post_tf_build
@@ -1795,11 +1869,7 @@
 		# Pre-archive hook
 		call_hook pre_tf_archive
 
-		if not_upon "$(get_tf_opt RUST)"; then
-			from="$tf_build_root" to="$archive" collect_build_artefacts
-		else
-			from="$rfa_build_root" to="$archive" collect_rfa_artefacts
-		fi
+		from="$tf_build_root" to="$archive" collect_build_artefacts
 
 		# Post-archive hook
 		call_hook post_tf_archive