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
19shopt -s failglob
20
21if [ ! -d art ]; then
22  echo "Script needs to be run at the root of the android tree"
23  exit 1
24fi
25
26TARGET_ARCH=$(source build/envsetup.sh > /dev/null; get_build_var TARGET_ARCH)
27
28# Logic for setting out_dir from build/make/core/envsetup.mk:
29if [[ -z $OUT_DIR ]]; then
30  if [[ -z $OUT_DIR_COMMON_BASE ]]; then
31    out_dir=out
32  else
33    out_dir=${OUT_DIR_COMMON_BASE}/${PWD##*/}
34  fi
35else
36  out_dir=${OUT_DIR}
37fi
38
39java_libraries_dir=${out_dir}/target/common/obj/JAVA_LIBRARIES
40common_targets="vogar core-tests apache-harmony-jdwp-tests-hostdex jsr166-tests libartpalette-system mockito-target"
41# These build targets have different names on device and host.
42specific_targets="libjavacoretests libjdwp libwrapagentproperties libwrapagentpropertiesd"
43build_host="no"
44build_target="no"
45installclean="no"
46j_arg="-j$(nproc)"
47showcommands=
48make_command=
49
50while true; do
51  if [[ "$1" == "--host" ]]; then
52    build_host="yes"
53    shift
54  elif [[ "$1" == "--target" ]]; then
55    build_target="yes"
56    shift
57  elif [[ "$1" == "--installclean" ]]; then
58    installclean="yes"
59    shift
60  elif [[ "$1" == -j* ]]; then
61    j_arg=$1
62    shift
63  elif [[ "$1" == "--showcommands" ]]; then
64    showcommands="showcommands"
65    shift
66  elif [[ "$1" == "" ]]; then
67    break
68  else
69    echo "Unknown options $@"
70    exit 1
71  fi
72done
73
74# If neither was selected, build both by default.
75if [[ $build_host == "no" ]] && [[ $build_target == "no" ]]; then
76  build_host="yes"
77  build_target="yes"
78fi
79
80# Allow to build successfully in master-art.
81extra_args="SOONG_ALLOW_MISSING_DEPENDENCIES=true"
82
83# Switch the build system to unbundled mode in the reduced manifest branch.
84if [ ! -d frameworks/base ]; then
85  extra_args="$extra_args TARGET_BUILD_UNBUNDLED=true"
86fi
87
88apexes=(
89  "com.android.art.testing"
90  "com.android.conscrypt"
91  "com.android.i18n"
92  "com.android.runtime"
93  "com.android.tzdata"
94  "com.android.os.statsd"
95)
96
97make_command="build/soong/soong_ui.bash --make-mode $j_arg $extra_args $showcommands $common_targets"
98if [[ $build_host == "yes" ]]; then
99  make_command+=" build-art-host-tests"
100  make_command+=" dx-tests junit-host"
101  for LIB in ${specific_targets} ; do
102    make_command+=" $LIB-host"
103  done
104fi
105if [[ $build_target == "yes" ]]; then
106  if [[ -z "${ANDROID_PRODUCT_OUT}" ]]; then
107    echo 'ANDROID_PRODUCT_OUT environment variable is empty; did you forget to run `lunch`?'
108    exit 1
109  fi
110  make_command+=" build-art-target-tests"
111  make_command+=" libnetd_client-target toybox sh libtombstoned_client"
112  make_command+=" debuggerd su gdbserver"
113  # vogar requires the class files for conscrypt and ICU.
114  make_command+=" conscrypt core-icu4j"
115  make_command+=" ${ANDROID_PRODUCT_OUT#"${ANDROID_BUILD_TOP}/"}/system/etc/public.libraries.txt"
116  # Targets required to generate a linker configuration for device within the
117  # chroot environment. The *.libraries.txt targets are required by
118  # the source linkerconfig but not included in the prebuilt one.
119  make_command+=" linkerconfig conv_linker_config sanitizer.libraries.txt vndkcorevariant.libraries.txt"
120  # Additional targets needed for the chroot environment.
121  make_command+=" event-log-tags"
122  # Needed to extract prebuilt APEXes.
123  make_command+=" deapexer"
124  # Build/install the required APEXes.
125  make_command+=" ${apexes[*]}"
126  make_command+=" ${specific_targets}"
127fi
128
129if [[ $installclean == "yes" ]]; then
130  echo "Perform installclean"
131  ANDROID_QUIET_BUILD=true build/soong/soong_ui.bash --make-mode $extra_args installclean
132else
133  echo "WARNING: Missing --installclean argument to buildbot-build.sh"
134  echo "WARNING: This is usually ok, but may cause rare odd failures."
135  echo ""
136fi
137
138echo "Executing $make_command"
139# Disable path restrictions to enable luci builds using vpython.
140eval "$make_command"
141
142if [[ $build_target == "yes" ]]; then
143  if [[ -z "${ANDROID_HOST_OUT}" ]]; then
144    echo "ANDROID_HOST_OUT environment variable is empty; using $out_dir/host/linux-x86"
145    ANDROID_HOST_OUT=$out_dir/host/linux-x86
146  fi
147
148  # Extract prebuilt APEXes.
149  debugfs=$ANDROID_HOST_OUT/bin/debugfs_static
150  for apex in ${apexes[@]}; do
151    dir="$ANDROID_PRODUCT_OUT/system/apex/${apex}"
152    file="$ANDROID_PRODUCT_OUT/system/apex/${apex}.apex"
153    if [ -f "${file}" ]; then
154      echo "Extracting APEX file: ${apex}"
155      rm -rf $dir
156      mkdir -p $dir
157      $ANDROID_HOST_OUT/bin/deapexer --debugfs_path $debugfs extract $file $dir
158    fi
159  done
160
161  # Replace stub libraries with implemenation libraries: because we do chroot
162  # testing, we need to install an implementation of the libraries (and cannot
163  # rely on the one already installed on the device, if the device is post R and
164  # has it).
165  implementation_libs=(
166    "heapprofd_client_api.so"
167    "libartpalette-system.so"
168    "liblog.so"
169  )
170  if [ -d prebuilts/runtime/mainline/platform/impl ]; then
171    if [[ $TARGET_ARCH = arm* ]]; then
172      arch32=arm
173      arch64=arm64
174    else
175      arch32=x86
176      arch64=x86_64
177    fi
178    for so in ${implementation_libs[@]}; do
179      if [ -d "$ANDROID_PRODUCT_OUT/system/lib" ]; then
180        cmd="cp -p prebuilts/runtime/mainline/platform/impl/$arch32/$so $ANDROID_PRODUCT_OUT/system/lib/$so"
181        echo "Executing $cmd"
182        eval "$cmd"
183      fi
184      if [ -d "$ANDROID_PRODUCT_OUT/system/lib64" ]; then
185        cmd="cp -p prebuilts/runtime/mainline/platform/impl/$arch64/$so $ANDROID_PRODUCT_OUT/system/lib64/$so"
186        echo "Executing $cmd"
187        eval "$cmd"
188      fi
189   done
190  fi
191
192  # Create canonical name -> file name symlink in the symbol directory for the
193  # Testing ART APEX.
194  #
195  # This mimics the logic from `art/Android.mk`. We made the choice not to
196  # implement this in `art/Android.mk`, as the Testing ART APEX is a test artifact
197  # that should never ship with an actual product, and we try to keep it out of
198  # standard build recipes
199  #
200  # TODO(b/141004137, b/129534335): Remove this, expose the Testing ART APEX in
201  # the `art/Android.mk` build logic, and add absence checks (e.g. in
202  # `build/make/core/main.mk`) to prevent the Testing ART APEX from ending up in a
203  # system image.
204  target_out_unstripped="$ANDROID_PRODUCT_OUT/symbols"
205  link_name="$target_out_unstripped/apex/com.android.art"
206  link_command="mkdir -p $(dirname "$link_name") && ln -sf com.android.art.testing \"$link_name\""
207  echo "Executing $link_command"
208  eval "$link_command"
209
210  # Temporary fix for libjavacrypto.so dependencies in libcore and jvmti tests (b/147124225).
211  conscrypt_dir="$ANDROID_PRODUCT_OUT/system/apex/com.android.conscrypt"
212  conscrypt_libs="libjavacrypto.so libcrypto.so libssl.so"
213  if [ ! -d "${conscrypt_dir}" ]; then
214    echo -e "Missing conscrypt APEX in build output: ${conscrypt_dir}"
215    exit 1
216  fi
217  if [ ! -f "${conscrypt_dir}/javalib/conscrypt.jar" ]; then
218    echo -e "Missing conscrypt jar in build output: ${conscrypt_dir}"
219    exit 1
220  fi
221  for l in lib lib64; do
222    if [ ! -d "$ANDROID_PRODUCT_OUT/system/$l" ]; then
223      continue
224    fi
225    for so in $conscrypt_libs; do
226      src="${conscrypt_dir}/${l}/${so}"
227      dst="$ANDROID_PRODUCT_OUT/system/${l}/${so}"
228      if [ "${src}" -nt "${dst}" ]; then
229        cmd="cp -p \"${src}\" \"${dst}\""
230        echo "Executing $cmd"
231        eval "$cmd"
232      fi
233    done
234  done
235
236  # TODO(b/159355595): Ensure there is a tzdata in system to avoid warnings on
237  # stderr from Bionic.
238  if [ ! -f $ANDROID_PRODUCT_OUT/system/usr/share/zoneinfo/tzdata ]; then
239    mkdir -p $ANDROID_PRODUCT_OUT/system/usr/share/zoneinfo
240    cp $ANDROID_PRODUCT_OUT/system/apex/com.android.tzdata/etc/tz/tzdata \
241      $ANDROID_PRODUCT_OUT/system/usr/share/zoneinfo/tzdata
242  fi
243
244  # Create system symlinks for the Runtime APEX. Normally handled by
245  # installSymlinkToRuntimeApex in soong/cc/binary.go, but we have to replicate
246  # it here since we don't run the install rules for the Runtime APEX.
247  for b in linker{,_asan}{,64}; do
248    echo "Symlinking /apex/com.android.runtime/bin/$b to /system/bin"
249    ln -sf /apex/com.android.runtime/bin/$b $ANDROID_PRODUCT_OUT/system/bin/$b
250  done
251  for d in $ANDROID_PRODUCT_OUT/system/apex/com.android.runtime/lib{,64}/bionic; do
252    if [ -d $d ]; then
253      for p in $d/*; do
254        lib_dir=$(expr $p : '.*/\(lib[0-9]*\)/.*')
255        lib_file=$(basename $p)
256        src=/apex/com.android.runtime/${lib_dir}/bionic/${lib_file}
257        dst=$ANDROID_PRODUCT_OUT/system/${lib_dir}/${lib_file}
258        echo "Symlinking $src into /system/${lib_dir}"
259        mkdir -p $(dirname $dst)
260        ln -sf $src $dst
261      done
262    fi
263  done
264
265  # Create linker config files. We run linkerconfig on host to avoid problems
266  # building it statically for device in an unbundled tree.
267
268  # temporary root for linkerconfig
269  linkerconfig_root=$ANDROID_PRODUCT_OUT/art_linkerconfig_root
270
271  rm -rf $linkerconfig_root
272
273  # Linkerconfig reads files from /system/etc
274  mkdir -p $linkerconfig_root/system
275  cp -r $ANDROID_PRODUCT_OUT/system/etc $linkerconfig_root/system
276
277  # For linkerconfig to pick up the APEXes correctly we need to make them
278  # available in $linkerconfig_root/apex.
279  mkdir -p $linkerconfig_root/apex
280  for apex in ${apexes[@]}; do
281    src="$ANDROID_PRODUCT_OUT/system/apex/${apex}"
282    if [[ $apex == com.android.art.* ]]; then
283      dst="$linkerconfig_root/apex/com.android.art"
284    else
285      dst="$linkerconfig_root/apex/${apex}"
286    fi
287    echo "Copying APEX directory from $src to $dst"
288    rm -rf $dst
289    cp -r $src $dst
290  done
291
292  # Linkerconfig also looks at /apex/apex-info-list.xml to check for system APEXes.
293  apex_xml_file=$linkerconfig_root/apex/apex-info-list.xml
294  echo "Creating $apex_xml_file"
295  cat <<EOF > $apex_xml_file
296<?xml version="1.0" encoding="utf-8"?>
297<apex-info-list>
298EOF
299  for apex in ${apexes[@]}; do
300    [[ $apex == com.android.art.* ]] && apex=com.android.art
301    cat <<EOF >> $apex_xml_file
302    <apex-info moduleName="${apex}" modulePath="/system/apex/${apex}.apex" preinstalledModulePath="/system/apex/${apex}.apex" versionCode="1" versionName="" isFactory="true" isActive="true">
303    </apex-info>
304EOF
305  done
306  cat <<EOF >> $apex_xml_file
307</apex-info-list>
308EOF
309
310  system_linker_config_pb=$linkerconfig_root/system/etc/linker.config.pb
311  # This list needs to be synced with provideLibs in system/etc/linker.config.pb
312  # in the targeted platform image.
313  # TODO(b/186649223): Create a prebuilt for it in platform-mainline-sdk.
314  system_provide_libs=(
315    heapprofd_client_api.so
316    libEGL.so
317    libGLESv1_CM.so
318    libGLESv2.so
319    libGLESv3.so
320    libOpenMAXAL.so
321    libOpenSLES.so
322    libRS.so
323    libaaudio.so
324    libadbd_auth.so
325    libadbd_fs.so
326    libamidi.so
327    libandroid.so
328    libandroid_net.so
329    libartpalette-system.so
330    libbinder_ndk.so
331    libc.so
332    libcamera2ndk.so
333    libcgrouprc.so
334    libclang_rt.asan-i686-android.so
335    libclang_rt.asan-x86_64-android.so
336    libdl.so
337    libdl_android.so
338    libft2.so
339    libincident.so
340    libjnigraphics.so
341    liblog.so
342    libm.so
343    libmediametrics.so
344    libmediandk.so
345    libnativewindow.so
346    libneuralnetworks_packageinfo.so
347    libselinux.so
348    libstdc++.so
349    libsync.so
350    libvndksupport.so
351    libvulkan.so
352    libz.so
353  )
354
355  echo "Encoding linker.config.json to $system_linker_config_pb"
356  $ANDROID_HOST_OUT/bin/conv_linker_config proto -s $ANDROID_BUILD_TOP/system/core/rootdir/etc/linker.config.json -o $system_linker_config_pb
357  $ANDROID_HOST_OUT/bin/conv_linker_config append -s $system_linker_config_pb -o $system_linker_config_pb --key "provideLibs" --value "${system_provide_libs[*]}"
358
359  # To avoid warnings from linkerconfig when it checks following two partitions
360  mkdir -p $linkerconfig_root/product
361  mkdir -p $linkerconfig_root/system_ext
362
363  platform_version=$(build/soong/soong_ui.bash --dumpvar-mode PLATFORM_VERSION)
364  linkerconfig_out=$ANDROID_PRODUCT_OUT/linkerconfig
365  echo "Generating linkerconfig in $linkerconfig_out"
366  rm -rf $linkerconfig_out
367  mkdir -p $linkerconfig_out
368  $ANDROID_HOST_OUT/bin/linkerconfig --target $linkerconfig_out --root $linkerconfig_root --vndk $platform_version
369fi
370