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 TEMPORARY_DISABLE_PATH_RESTRICTIONS=true"
67
68if [[ $mode == "host" ]]; then
69  make_command="build/soong/soong_ui.bash --make-mode $j_arg $extra_args $showcommands build-art-host-tests $common_targets"
70  make_command+=" dx-tests junit-host"
71  mode_suffix="-host"
72elif [[ $mode == "target" ]]; then
73  if [[ -z "${ANDROID_PRODUCT_OUT}" ]]; then
74    echo 'ANDROID_PRODUCT_OUT environment variable is empty; did you forget to run `lunch`?'
75    exit 1
76  fi
77  make_command="build/soong/soong_ui.bash --make-mode $j_arg $extra_args $showcommands build-art-target-tests $common_targets"
78  make_command+=" libnetd_client-target toybox toolbox sh"
79  make_command+=" debuggerd su gdbserver"
80  make_command+=" libstdc++ "
81  make_command+=" ${ANDROID_PRODUCT_OUT#"${ANDROID_BUILD_TOP}/"}/system/etc/public.libraries.txt"
82  if [[ -n "$ART_TEST_CHROOT" ]]; then
83    # Targets required to generate a linker configuration on device within the
84    # chroot environment.
85    make_command+=" linkerconfig"
86    # Additional targets needed for the chroot environment.
87    make_command+=" crash_dump event-log-tags"
88  fi
89  # Build the Runtime (Bionic) APEX.
90  make_command+=" com.android.runtime"
91  # Build the Testing ART APEX (which is a superset of the Release and Debug ART APEXes).
92  make_command+=" com.android.art.testing"
93  # Build the bootstrap Bionic artifacts links (linker, libc, libdl, libm).
94  # These targets create these symlinks:
95  # - from /system/bin/linker(64) to /apex/com.android.runtime/bin/linker(64); and
96  # - from /system/lib(64)/$lib to /apex/com.android.runtime/lib(64)/$lib.
97  make_command+=" linker libc.bootstrap libdl.bootstrap libdl_android.bootstrap libm.bootstrap"
98  # Build the Conscrypt APEX.
99  make_command+=" com.android.conscrypt"
100  # Build the i18n APEX.
101  make_command+=" com.android.i18n"
102  # Build the Time Zone Data APEX.
103  make_command+=" com.android.tzdata"
104fi
105
106mode_specific_libraries="libjavacoretests libjdwp libwrapagentproperties libwrapagentpropertiesd"
107for LIB in ${mode_specific_libraries} ; do
108  make_command+=" $LIB${mode_suffix}"
109done
110
111
112echo "Executing $make_command"
113# Disable path restrictions to enable luci builds using vpython.
114eval "$make_command"
115
116if [[ $mode == "target" ]]; then
117  # Create canonical name -> file name symlink in the symbol directory for the
118  # Testing ART APEX.
119  #
120  # This mimics the logic from `art/Android.mk`. We made the choice not to
121  # implement this in `art/Android.mk`, as the Testing ART APEX is a test artifact
122  # that should never ship with an actual product, and we try to keep it out of
123  # standard build recipes
124  #
125  # TODO(b/141004137, b/129534335): Remove this, expose the Testing ART APEX in
126  # the `art/Android.mk` build logic, and add absence checks (e.g. in
127  # `build/make/core/main.mk`) to prevent the Testing ART APEX from ending up in a
128  # system image.
129  target_out_unstripped="$ANDROID_PRODUCT_OUT/symbols"
130  link_name="$target_out_unstripped/apex/com.android.art"
131  link_command="mkdir -p $(dirname "$link_name") && ln -sf com.android.art.testing \"$link_name\""
132  echo "Executing $link_command"
133  eval "$link_command"
134  # Also provide access to symbols of binaries from the Runtime (Bionic) APEX,
135  # e.g. to support debugging in GDB.
136  find "$target_out_unstripped/apex/com.android.runtime/bin" -type f | while read target; do
137    cmd="ln -sf $target $target_out_unstripped/system/bin/$(basename $target)"
138    echo "Executing $cmd"
139    eval "$cmd"
140  done
141
142  # Temporary fix for libjavacrypto.so dependencies in libcore and jvmti tests (b/147124225).
143  conscrypt_apex="$ANDROID_PRODUCT_OUT/system/apex/com.android.conscrypt"
144  conscrypt_libs="libjavacrypto.so libcrypto.so libssl.so"
145  if [ ! -d "${conscrypt_apex}" ]; then
146    echo -e "Missing conscrypt APEX in build output: ${conscrypt_apex}"
147    exit 1
148  fi
149  for l in lib lib64; do
150    if [ ! -d "${conscrypt_apex}/$l" ]; then
151      continue
152    fi
153    for so in $conscrypt_libs; do
154      src="${conscrypt_apex}/${l}/${so}"
155      dst="$ANDROID_PRODUCT_OUT/system/${l}/${so}"
156      if [ "${src}" -nt "${dst}" ]; then
157        cmd="cp -p \"${src}\" \"${dst}\""
158        echo "Executing $cmd"
159        eval "$cmd"
160      fi
161    done
162  done
163fi
164