1#!/usr/bin/env bash
2
3# Copyright (C) 2016 The Android Open Source Project
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9#     http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16
17
18
19###
20### Change the BOOTCLASSPATH to pick up bootlib classes.
21### Export ANDROID_DATA=something else (it should have dalvik-cache dir in it)
22### Point the image to something else that's not existing.
23###
24### Actually run dalvikvm now...
25###
26
27if [[ -z $ANDROID_BUILD_TOP ]]; then
28  echo "Run source build/envsetup.sh first" >& 2
29  exit 1
30fi
31
32invoke_with=
33DALVIKVM=dalvikvm
34LIBART=libart.so
35
36function follow_links() {
37  if [ z"$BASH_SOURCE" != z ]; then
38    file="$BASH_SOURCE"
39  else
40    file="$0"
41  fi
42  while [ -h "$file" ]; do
43    # On Mac OS, readlink -f doesn't work.
44    file="$(readlink "$file")"
45  done
46  echo "$file"
47}
48
49function find_libdir() {
50  # Use realpath instead of readlink because Android does not have a readlink.
51  if [ "$(realpath "$ANDROID_ROOT/bin/$DALVIKVM")" = "$(realpath "$ANDROID_ROOT/bin/dalvikvm64")" ]; then
52    echo "lib64"
53  else
54    echo "lib"
55  fi
56}
57
58function join { local IFS="$1"; shift; echo "$*"; }
59
60PROG_NAME="$(follow_links)"
61PROG_DIR="$(cd "${PROG_NAME%/*}" ; pwd -P)"
62
63if [[ -z $ANDROID_ROOT ]]; then
64  # Already set to /system for actual android devices
65  ANDROID_ROOT="$ANDROID_HOST_OUT"
66fi
67LIBDIR="$(find_libdir)"
68LD_LIBRARY_PATH="$ANDROID_ROOT/$LIBDIR"
69DEBUG_OPTION=""
70
71DELETE_ANDROID_DATA=false
72# If ANDROID_DATA is the system ANDROID_DATA or is not set, use our own,
73# and ensure we delete it at the end.
74if [ "$ANDROID_DATA" = "/data" ] || [ "$ANDROID_DATA" = "" ]; then
75  # TODO: use /data/tmp/... for android, and mktemp for host
76  #ANDROID_DATA=$PWD/android-data$$
77  ANDROID_DATA="$(mktemp -q -d -t "$(basename "$0").XXXXXX")"
78  IMAGE_DIRECTORY="$ANDROID_DATA/image"
79  mkdir -p $ANDROID_DATA/dalvik-cache/{arm,arm64,x86,x86_64}
80  mkdir -p $IMAGE_DIRECTORY
81  DELETE_ANDROID_DATA=true
82fi
83
84# Clean up the temporary files we made earlier on.
85function finish {
86  if $DELETE_ANDROID_DATA; then
87    [[ -d $ANDROID_DATA ]] && rm -rf "$ANDROID_DATA"
88  fi
89}
90
91trap finish EXIT
92
93# Dummy image location name. Art ignores the bootclasspath setting if a boot image
94# already exists, so we force it to create a new boot image with our correct bootclasspath.
95IMAGE_LOCATION="$IMAGE_DIRECTORY/core-extrabootclasspath.art"
96
97# TODO: Get this list from somewhere else, a makefile perhaps?
98BOOT_DEXJARS=(
99bouncycastle-hostdex.jar
100apache-xml-hostdex.jar
101core-tests-hostdex.jar
102core-libart-hostdex.jar
103conscrypt-hostdex.jar
104core-ojtests-hostdex.jar  # This is the *one* addition that makes our OJ tests actually run. The rest of these are standard jars on the bootclasspath.
105core-oj-hostdex.jar
106okhttp-hostdex.jar)
107
108BOOT_DEXJAR_PREFIX="$ANDROID_ROOT/framework"
109
110BOOT_DEXJARS_ABS=()
111for dexjar in ${BOOT_DEXJARS[@]}; do
112  BOOT_DEXJARS_ABS=(${BOOT_DEXJARS_ABS[@]} $BOOT_DEXJAR_PREFIX/$dexjar)
113done
114
115export BOOTCLASSPATH=$(join ":" "${BOOT_DEXJARS_ABS[@]}") # a,b,c
116
117echo "BOOTCLASSPATH=$BOOTCLASSPATH"
118echo "PROG_NAME=$PROG_NAME"
119echo "PROG_DIR=$PROG_DIR"
120echo "ANDROID_ROOT=$ANDROID_ROOT"
121echo "LIBDIR=$LIBDIR"
122echo "LD_LIBRARY_PATH=$LD_LIBRARY_PATH"
123echo "DEBUG_OPTION=$DEBUG_OPTION"
124
125echo "export BOOTCLASSPATH=$BOOTCLASSPATH"
126echo export ANDROID_ROOT="$ANDROID_ROOT"
127ANDROID_DATA=$ANDROID_DATA \
128  ANDROID_ROOT=$ANDROID_ROOT \
129  LD_LIBRARY_PATH=$LD_LIBRARY_PATH \
130  PATH=$ANDROID_ROOT/bin:$PATH \
131  LD_USE_LOAD_BIAS=1 \
132  $invoke_with $ANDROID_ROOT/bin/$DALVIKVM $lib \
133    -XXlib:$LIBART \
134    -Xnorelocate \
135    -Ximage:$IMAGE_LOCATION \
136    $DEBUG_OPTION \
137    "$@"
138
139