Extract the modules bootclasspath handling.

Extracts the modules bootclasspath handling into
a separate utils file, with minor refactoring
(no semantical changes to the behaviour). These utils
could be very useful when using ART chroot mode outside
of Art testsuite.

Test: test-art-target, test-art-host.

Change-Id: I5c03605ac234d15c1f3d9bac342f351abe043368
diff --git a/test/etc/apex-bootclasspath-utils.sh b/test/etc/apex-bootclasspath-utils.sh
new file mode 100755
index 0000000..5a0873d
--- /dev/null
+++ b/test/etc/apex-bootclasspath-utils.sh
@@ -0,0 +1,86 @@
+#!/bin/bash
+#
+# Copyright (C) 2022 The Android Open Source Project
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+# This file contains utils for constructing -Xbootclasspath and -Xbootclasspath-location
+# for dex2oat and dalvikvm from apex modules list.
+#
+# Those utils could be used outside of art/test/ to run ART in chroot setup.
+
+# Note: This must start with the CORE_IMG_JARS in Android.common_path.mk
+# because that's what we use for compiling the boot.art image.
+# It may contain additional modules from TEST_CORE_JARS.
+readonly bpath_modules="core-oj core-libart okhttp bouncycastle apache-xml core-icu4j conscrypt"
+
+# Helper function to construct paths for apex modules (for both -Xbootclasspath and
+# -Xbootclasspath-location).
+#
+#  Arguments.
+#   ${1}: path prefix.
+get_apex_bootclasspath_impl() {
+  local -r bpath_prefix="$1"
+  local bpath_separator=""
+  local bpath=""
+  local bpath_jar=""
+  for bpath_module in ${bpath_modules}; do
+    local apex_module="com.android.art"
+    case "$bpath_module" in
+      (conscrypt)  apex_module="com.android.conscrypt";;
+      (core-icu4j) apex_module="com.android.i18n";;
+      (*)          apex_module="com.android.art";;
+    esac
+    bpath_jar="/apex/${apex_module}/javalib/${bpath_module}.jar"
+    bpath+="${bpath_separator}${bpath_prefix}${bpath_jar}"
+    bpath_separator=":"
+  done
+  echo "${bpath}"
+}
+
+# Gets a -Xbootclasspath paths with the apex modules.
+#
+#  Arguments.
+#   ${1}: host (y|n).
+get_apex_bootclasspath() {
+  local -r host="${1}"
+  local bpath_prefix=""
+
+  if [[ "${host}" == "y" ]]; then
+    bpath_prefix="${ANDROID_HOST_OUT}"
+  fi
+
+  get_apex_bootclasspath_impl "${bpath_prefix}"
+}
+
+# Gets a -Xbootclasspath-location paths with the apex modules.
+#
+#  Arguments.
+#   ${1}: host (y|n).
+get_apex_bootclasspath_locations() {
+  local -r host="${1}"
+  local bpath_location_prefix=""
+
+  if [[ "${host}" == "y" ]]; then
+    if [[ "${ANDROID_HOST_OUT:0:${#ANDROID_BUILD_TOP}+1}" == "${ANDROID_BUILD_TOP}/" ]]; then
+      bpath_location_prefix="${ANDROID_HOST_OUT:${#ANDROID_BUILD_TOP}+1}"
+    else
+      error_msg "ANDROID_BUILD_TOP/ is not a prefix of ANDROID_HOST_OUT"\
+                "\nANDROID_BUILD_TOP=${ANDROID_BUILD_TOP}"\
+                "\nANDROID_HOST_OUT=${ANDROID_HOST_OUT}"
+      exit
+    fi
+  fi
+
+  get_apex_bootclasspath_impl "${bpath_location_prefix}"
+}
diff --git a/test/etc/run-test-jar b/test/etc/run-test-jar
index cfac63b..e67fa42 100755
--- a/test/etc/run-test-jar
+++ b/test/etc/run-test-jar
@@ -2,6 +2,9 @@
 #
 # Runner for an individual run-test.
 
+readonly local_path=$(dirname "$0")
+source "${local_path}/apex-bootclasspath-utils.sh"
+
 # Check how many colors the terminal can display.
 ncolors=$(tput colors 2>/dev/null)
 
@@ -735,43 +738,14 @@
   exit
 fi
 
-# Note: This must start with the CORE_IMG_JARS in Android.common_path.mk
-# because that's what we use for compiling the boot.art image.
-# It may contain additional modules from TEST_CORE_JARS.
-bpath_modules="core-oj core-libart okhttp bouncycastle apache-xml core-icu4j conscrypt"
-bpath=""
-bpath_locations=""
-bpath_separator=""
-bpath_prefix=""
-bpath_location_prefix=""
-if [ "${HOST}" = "y" ]; then
-  bpath_prefix="${ANDROID_HOST_OUT}"
-  if [ "${ANDROID_HOST_OUT:0:${#ANDROID_BUILD_TOP}+1}" = "${ANDROID_BUILD_TOP}/" ]; then
-    bpath_location_prefix="${ANDROID_HOST_OUT:${#ANDROID_BUILD_TOP}+1}"
-  else
-    error_msg "ANDROID_BUILD_TOP/ is not a prefix of ANDROID_HOST_OUT"\
-              "\nANDROID_BUILD_TOP=${ANDROID_BUILD_TOP}"\
-              "\nANDROID_HOST_OUT=${ANDROID_HOST_OUT}"
-    exit
-  fi
-fi
-for bpath_module in ${bpath_modules}; do
-  apex_module="com.android.art"
-  case "$bpath_module" in
-    (conscrypt)  apex_module="com.android.conscrypt";;
-    (core-icu4j) apex_module="com.android.i18n";;
-    (*)          apex_module="com.android.art";;
-  esac
-  bpath_jar="/apex/${apex_module}/javalib/${bpath_module}.jar"
-  bpath+="${bpath_separator}${bpath_prefix}${bpath_jar}"
-  bpath_locations+="${bpath_separator}${bpath_location_prefix}${bpath_jar}"
-  bpath_separator=":"
-done
+readonly b_path=$(get_apex_bootclasspath ${HOST})
+readonly b_path_locations=$(get_apex_bootclasspath_locations ${HOST})
+
 # Pass down the bootclasspath
-FLAGS="${FLAGS} -Xbootclasspath:${bpath}"
-FLAGS="${FLAGS} -Xbootclasspath-locations:${bpath_locations}"
-COMPILE_FLAGS="${COMPILE_FLAGS} --runtime-arg -Xbootclasspath:${bpath}"
-COMPILE_FLAGS="${COMPILE_FLAGS} --runtime-arg -Xbootclasspath-locations:${bpath_locations}"
+FLAGS="${FLAGS} -Xbootclasspath:${b_path}"
+FLAGS="${FLAGS} -Xbootclasspath-locations:${b_path_locations}"
+COMPILE_FLAGS="${COMPILE_FLAGS} --runtime-arg -Xbootclasspath:${b_path}"
+COMPILE_FLAGS="${COMPILE_FLAGS} --runtime-arg -Xbootclasspath-locations:${b_path_locations}"
 
 if [ "$HAVE_IMAGE" = "n" ]; then
     # Disable image dex2oat - this will forbid the runtime to patch or compile an image.