1#! /bin/bash
2#
3# Copyright (C) 2015 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
17set -e
18
19if [ ! -d art ]; then
20  echo "Script needs to be run at the root of the android tree"
21  exit 1
22fi
23
24source build/envsetup.sh >&/dev/null # for get_build_var
25
26# Logic for setting out_dir from build/make/core/envsetup.mk:
27if [[ -z $OUT_DIR ]]; then
28  if [[ -z $OUT_DIR_COMMON_BASE ]]; then
29    out_dir=out
30  else
31    out_dir=${OUT_DIR_COMMON_BASE}/${PWD##*/}
32  fi
33else
34  out_dir=${OUT_DIR}
35fi
36
37java_libraries_dir=${out_dir}/target/common/obj/JAVA_LIBRARIES
38common_targets="vogar core-tests apache-harmony-jdwp-tests-hostdex jsr166-tests libartpalette-system mockito-target"
39mode="target"
40j_arg="-j$(nproc)"
41showcommands=
42make_command=
43
44while true; do
45  if [[ "$1" == "--host" ]]; then
46    mode="host"
47    shift
48  elif [[ "$1" == "--target" ]]; then
49    mode="target"
50    shift
51  elif [[ "$1" == -j* ]]; then
52    j_arg=$1
53    shift
54  elif [[ "$1" == "--showcommands" ]]; then
55    showcommands="showcommands"
56    shift
57  elif [[ "$1" == "" ]]; then
58    break
59  else
60    echo "Unknown options $@"
61    exit 1
62  fi
63done
64
65# Allow to build successfully in master-art.
66extra_args="SOONG_ALLOW_MISSING_DEPENDENCIES=true"
67
68# Switch the build system to unbundled mode in the reduced manifest branch.
69# TODO(b/159109002): Clean this up.
70if [ ! -d frameworks/base ]; then
71  extra_args="$extra_args TARGET_BUILD_UNBUNDLED=true"
72fi
73
74apexes=(
75  "com.android.art.testing"
76  "com.android.conscrypt"
77  "com.android.i18n"
78  "com.android.runtime"
79  "com.android.tzdata"
80)
81
82if [[ $mode == "host" ]]; then
83  make_command="build/soong/soong_ui.bash --make-mode $j_arg $extra_args $showcommands build-art-host-tests $common_targets"
84  make_command+=" dx-tests junit-host"
85  mode_suffix="-host"
86elif [[ $mode == "target" ]]; then
87  if [[ -z "${ANDROID_PRODUCT_OUT}" ]]; then
88    echo 'ANDROID_PRODUCT_OUT environment variable is empty; did you forget to run `lunch`?'
89    exit 1
90  fi
91  make_command="build/soong/soong_ui.bash --make-mode $j_arg $extra_args $showcommands build-art-target-tests $common_targets"
92  make_command+=" libnetd_client-target toybox sh"
93  make_command+=" debuggerd su gdbserver"
94  # vogar requires the class files for conscrypt and ICU.
95  make_command+=" conscrypt core-icu4j"
96  make_command+=" ${ANDROID_PRODUCT_OUT#"${ANDROID_BUILD_TOP}/"}/system/etc/public.libraries.txt"
97  # Targets required to generate a linker configuration for device within the
98  # chroot environment. The *.libraries.txt targets are required by
99  # linkerconfig but not included in host_linkerconfig_all_targets. We cannot
100  # use linkerconfig, because building the device binary statically might not
101  # work in an unbundled tree.
102  make_command+=" host_linkerconfig_all_targets sanitizer.libraries.txt vndkcorevariant.libraries.txt"
103  # Additional targets needed for the chroot environment.
104  make_command+=" event-log-tags"
105  # Needed to extract prebuilt APEXes.
106  make_command+=" deapexer"
107  # Build/install the required APEXes.
108  make_command+=" ${apexes[*]}"
109fi
110
111mode_specific_libraries="libjavacoretests libjdwp libwrapagentproperties libwrapagentpropertiesd"
112for LIB in ${mode_specific_libraries} ; do
113  make_command+=" $LIB${mode_suffix}"
114done
115
116
117echo "Executing $make_command"
118# Disable path restrictions to enable luci builds using vpython.
119eval "$make_command"
120
121if [[ $mode == "target" ]]; then
122  if [[ -z "${ANDROID_HOST_OUT}" ]]; then
123    echo "ANDROID_HOST_OUT environment variable is empty; using $out_dir/host/linux-x86"
124    ANDROID_HOST_OUT=$out_dir/host/linux-x86
125  fi
126
127  # Extract prebuilt APEXes.
128  debugfs=$ANDROID_HOST_OUT/bin/debugfs_static
129  for apex in ${apexes[@]}; do
130    dir="$ANDROID_PRODUCT_OUT/system/apex/${apex}"
131    file="$ANDROID_PRODUCT_OUT/system/apex/${apex}.apex"
132    if [ -f "${file}" ]; then
133      echo "Extracting APEX file: ${apex}"
134      rm -rf $dir
135      mkdir -p $dir
136      $ANDROID_HOST_OUT/bin/deapexer --debugfs_path $debugfs extract $file $dir
137    fi
138  done
139
140  # Create canonical name -> file name symlink in the symbol directory for the
141  # Testing ART APEX.
142  #
143  # This mimics the logic from `art/Android.mk`. We made the choice not to
144  # implement this in `art/Android.mk`, as the Testing ART APEX is a test artifact
145  # that should never ship with an actual product, and we try to keep it out of
146  # standard build recipes
147  #
148  # TODO(b/141004137, b/129534335): Remove this, expose the Testing ART APEX in
149  # the `art/Android.mk` build logic, and add absence checks (e.g. in
150  # `build/make/core/main.mk`) to prevent the Testing ART APEX from ending up in a
151  # system image.
152  target_out_unstripped="$ANDROID_PRODUCT_OUT/symbols"
153  link_name="$target_out_unstripped/apex/com.android.art"
154  link_command="mkdir -p $(dirname "$link_name") && ln -sf com.android.art.testing \"$link_name\""
155  echo "Executing $link_command"
156  eval "$link_command"
157  # Also provide access to symbols of binaries from the Runtime (Bionic) APEX,
158  # e.g. to support debugging in GDB.
159  find "$target_out_unstripped/apex/com.android.runtime/bin" -type f | while read target; do
160    cmd="ln -sf $target $target_out_unstripped/system/bin/$(basename $target)"
161    echo "Executing $cmd"
162    eval "$cmd"
163  done
164
165  # Temporary fix for libjavacrypto.so dependencies in libcore and jvmti tests (b/147124225).
166  conscrypt_dir="$ANDROID_PRODUCT_OUT/system/apex/com.android.conscrypt"
167  conscrypt_libs="libjavacrypto.so libcrypto.so libssl.so"
168  if [ ! -d "${conscrypt_dir}" ]; then
169    echo -e "Missing conscrypt APEX in build output: ${conscrypt_dir}"
170    exit 1
171  fi
172  if [ ! -f "${conscrypt_dir}/javalib/conscrypt.jar" ]; then
173    echo -e "Missing conscrypt jar in build output: ${conscrypt_dir}"
174    exit 1
175  fi
176  for l in lib lib64; do
177    if [ ! -d "$ANDROID_PRODUCT_OUT/system/$l" ]; then
178      continue
179    fi
180    for so in $conscrypt_libs; do
181      src="${conscrypt_dir}/${l}/${so}"
182      dst="$ANDROID_PRODUCT_OUT/system/${l}/${so}"
183      if [ "${src}" -nt "${dst}" ]; then
184        cmd="cp -p \"${src}\" \"${dst}\""
185        echo "Executing $cmd"
186        eval "$cmd"
187      fi
188    done
189  done
190
191  # TODO(b/159355595): Ensure there is a tzdata in system to avoid warnings on
192  # stderr from Bionic.
193  if [ ! -f $ANDROID_PRODUCT_OUT/system/usr/share/zoneinfo/tzdata ]; then
194    mkdir -p $ANDROID_PRODUCT_OUT/system/usr/share/zoneinfo
195    cp $ANDROID_PRODUCT_OUT/system/apex/com.android.tzdata/etc/tz/tzdata \
196      $ANDROID_PRODUCT_OUT/system/usr/share/zoneinfo/tzdata
197  fi
198
199  # Create system symlinks for the Runtime APEX. Normally handled by
200  # installSymlinkToRuntimeApex in soong/cc/binary.go, but we have to replicate
201  # it here since we don't run the install rules for the Runtime APEX.
202  for b in linker{,_asan}{,64}; do
203    echo "Symlinking /apex/com.android.runtime/bin/$b to /system/bin"
204    ln -sf /apex/com.android.runtime/bin/$b $ANDROID_PRODUCT_OUT/system/bin/$b
205  done
206  for p in $ANDROID_PRODUCT_OUT/system/apex/com.android.runtime/lib{,64}/bionic/*; do
207    lib_dir=$(expr $p : '.*/\(lib[0-9]*\)/.*')
208    lib_file=$(basename $p)
209    src=/apex/com.android.runtime/${lib_dir}/bionic/${lib_file}
210    dst=$ANDROID_PRODUCT_OUT/system/${lib_dir}/${lib_file}
211    echo "Symlinking $src into /system/${lib_dir}"
212    mkdir -p $(dirname $dst)
213    ln -sf $src $dst
214  done
215
216  # Create linker config files. We run linkerconfig on host to avoid problems
217  # building it statically for device in an unbundled tree.
218
219  # temporary root for linkerconfig
220  linkerconfig_root=$ANDROID_PRODUCT_OUT/art_linkerconfig_root
221
222  rm -rf $linkerconfig_root
223
224  # Linkerconfig reads files from /system/etc
225  mkdir -p $linkerconfig_root/system
226  cp -r $ANDROID_PRODUCT_OUT/system/etc $linkerconfig_root/system
227
228  # For linkerconfig to pick up the APEXes correctly we need to make them
229  # available in $linkerconfig_root/apex.
230  mkdir -p $linkerconfig_root/apex
231  for apex in ${apexes[@]}; do
232    src="$ANDROID_PRODUCT_OUT/system/apex/${apex}"
233    if [[ $apex == com.android.art.* ]]; then
234      dst="$linkerconfig_root/apex/com.android.art"
235    else
236      dst="$linkerconfig_root/apex/${apex}"
237    fi
238    echo "Copying APEX directory from $src to $dst"
239    rm -rf $dst
240    cp -r $src $dst
241  done
242
243  # To avoid warnings from linkerconfig when it checks following two partitions
244  mkdir -p $linkerconfig_root/product
245  mkdir -p $linkerconfig_root/system_ext
246
247  platform_version=$(build/soong/soong_ui.bash --dumpvar-mode PLATFORM_VERSION)
248  linkerconfig_out=$ANDROID_PRODUCT_OUT/linkerconfig
249  echo "Generating linkerconfig in $linkerconfig_out"
250  rm -rf $linkerconfig_out
251  mkdir -p $linkerconfig_out
252  $ANDROID_HOST_OUT/bin/linkerconfig --target $linkerconfig_out --root $linkerconfig_root --vndk $platform_version
253fi
254