blob: f3ad92cad5014b850b66674efd650e8b7124fdae [file] [log] [blame]
Justin Yundde502b2023-06-28 21:53:34 +09001#!/bin/bash -e
Justin Yunb931ab12023-06-07 17:45:21 +09002#
3# Copyright (C) 2023 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
17usage() {
Justin Yunf44122f2023-08-23 10:52:28 +090018 echo "usage: ${0} -t TARGET -v VARIANT [-d DIST_OUT] [[-a ALTER_TARGET] ...] [-c] [-o] [-r] [GOALS ...]"
Justin Yunb931ab12023-06-07 17:45:21 +090019 echo " -t TARGET : Primay target to build"
20 echo " -v VARIANT : Build variant (ex. user, userdebug)"
21 echo " -d DIST_OUT : Path for dist out"
Justin Yunf44122f2023-08-23 10:52:28 +090022 echo " -a ALTER_TARGET: Alternative targets that share the build artifacts with the primary target"
Justin Yune2d794c2023-06-13 15:20:17 +090023 echo " -c : Run the target build again after installclean for reference"
Justin Yunddcda652023-09-26 16:13:47 +090024 echo ' -o : Write build time ("build_time_results.txt") and disk usage results (disk_size_results.txt") to "${OUT_DIR}" or "${DIST_OUT}/logs" if -d defined'
Justin Yune2d794c2023-06-13 15:20:17 +090025 echo " -r : Dryrun to see the commands without actually building the targets"
Justin Yunb931ab12023-06-07 17:45:21 +090026}
27
Justin Yune2d794c2023-06-13 15:20:17 +090028while getopts ha:cd:ort:v: opt; do
Justin Yunb931ab12023-06-07 17:45:21 +090029 case "${opt}" in
30 h)
31 usage
32 ;;
33 a)
Justin Yunf44122f2023-08-23 10:52:28 +090034 alter_targets+=("${OPTARG}")
Justin Yunb931ab12023-06-07 17:45:21 +090035 ;;
36 c)
Justin Yune2d794c2023-06-13 15:20:17 +090037 installclean="true"
Justin Yunb931ab12023-06-07 17:45:21 +090038 ;;
39 d)
40 dist_dir="${OPTARG}"
41 ;;
Justin Yune2d794c2023-06-13 15:20:17 +090042 o)
43 result_out="build_time_results.txt"
Justin Yunddcda652023-09-26 16:13:47 +090044 result_out_size="disk_size_results.txt"
Justin Yune2d794c2023-06-13 15:20:17 +090045 ;;
46 r)
47 dry_run="true"
48 ;;
Justin Yunb931ab12023-06-07 17:45:21 +090049 t)
50 target="${OPTARG}"
51 ;;
52 v)
53 variant="${OPTARG}"
54 ;;
55 *)
56 usage
57 ;;
58 esac
59done
60
Justin Yune2d794c2023-06-13 15:20:17 +090061if [[ -z "${target}" ]]; then
Justin Yunb931ab12023-06-07 17:45:21 +090062 echo "-t must set for the primary target"
Justin Yune2d794c2023-06-13 15:20:17 +090063 usage
Justin Yunb931ab12023-06-07 17:45:21 +090064 exit 1
65fi
Justin Yune2d794c2023-06-13 15:20:17 +090066if [[ -z "${variant}" ]]; then
Justin Yunb931ab12023-06-07 17:45:21 +090067 echo "-v must set for build variant"
Justin Yune2d794c2023-06-13 15:20:17 +090068 usage
Justin Yunb931ab12023-06-07 17:45:21 +090069 exit 1
70fi
Justin Yunf44122f2023-08-23 10:52:28 +090071if [ "${#alter_targets[@]}" -eq 1 ]; then
72 # test for a-b-a builds
73 alter_targets+=("${target}")
74fi
Justin Yunb931ab12023-06-07 17:45:21 +090075
Justin Yunb931ab12023-06-07 17:45:21 +090076goals="${@:OPTIND}"
77
Justin Yune2d794c2023-06-13 15:20:17 +090078readonly ANDROID_TOP="$(cd $(dirname $0)/../..; pwd)"
79cd "${ANDROID_TOP}"
80
81out_dir="${OUT_DIR:-out}"
82if [[ -n "${dist_dir}" ]]; then
83 out_dir="${dist_dir}/logs"
84fi
85
Justin Yundb037192023-08-01 11:39:27 +090086base_command="build/soong/soong_ui.bash --make-mode TARGET_RELEASE=trunk_staging"
Justin Yune2d794c2023-06-13 15:20:17 +090087if [[ -n "${dist_dir}" ]]; then
Justin Yunb931ab12023-06-07 17:45:21 +090088 base_command="${base_command} DIST_DIR=${dist_dir} dist"
89fi
90
Justin Yune2d794c2023-06-13 15:20:17 +090091run_command() {
Justin Yunf44122f2023-08-23 10:52:28 +090092 echo "**Running: ${1}"
Justin Yune2d794c2023-06-13 15:20:17 +090093 if [[ -z "${dry_run}" ]]; then
Justin Yun884015b2023-06-29 21:55:12 +090094 eval "${1}"
Justin Yune2d794c2023-06-13 15:20:17 +090095 fi
96}
97
Justin Yunddcda652023-09-26 16:13:47 +090098read_df() {
99 # read the available disk size
Justin Yun338ec9a2023-10-13 10:44:40 +0900100 df -k . | awk '{print $4}' | sed -n '2p'
Justin Yunddcda652023-09-26 16:13:47 +0900101}
102
Justin Yune2d794c2023-06-13 15:20:17 +0900103write_output() {
Justin Yunddcda652023-09-26 16:13:47 +0900104 if [[ -z "$2" || -n "${dry_run}" ]]; then
Justin Yune2d794c2023-06-13 15:20:17 +0900105 echo "Output: $1"
106 else
Justin Yunddcda652023-09-26 16:13:47 +0900107 echo "$1" >> "${out_dir}/$2"
Justin Yune2d794c2023-06-13 15:20:17 +0900108 fi
109}
110
111get_build_trace() {
112 run_command "cp -f ${out_dir}/build.trace.gz ${out_dir}/${1}"
113 if [[ -n "${result_out}" ]]; then
Justin Yunddcda652023-09-26 16:13:47 +0900114 write_output "$(python3 development/treble/read_build_trace_gz.py ${out_dir}/${1})" "${result_out}"
Justin Yune2d794c2023-06-13 15:20:17 +0900115 fi
116}
117
118if [[ -n "${result_out}" ]]; then
119 run_command "rm -f ${out_dir}/${result_out}"
Justin Yunddcda652023-09-26 16:13:47 +0900120 write_output "target, soong, kati, ninja, total" "${result_out}"
121fi
122
123if [[ -n "${result_out_size}" ]]; then
124 run_command "rm -f ${out_dir}/${result_out_size}"
125 write_output "target, size, size_after_clean" "${result_out_size}"
Justin Yune2d794c2023-06-13 15:20:17 +0900126fi
127
Justin Yunb931ab12023-06-07 17:45:21 +0900128# Build the target first.
Justin Yunddcda652023-09-26 16:13:47 +0900129disk_space_source=$(read_df)
Justin Yunf44122f2023-08-23 10:52:28 +0900130echo; echo "Initial build..."
Justin Yune2d794c2023-06-13 15:20:17 +0900131run_command "${base_command} TARGET_PRODUCT=${target} TARGET_BUILD_VARIANT=${variant} ${goals}"
Justin Yunddcda652023-09-26 16:13:47 +0900132size_primary=$((${disk_space_source}-$(read_df)))
Justin Yunb931ab12023-06-07 17:45:21 +0900133
Justin Yune2d794c2023-06-13 15:20:17 +0900134if [[ -n "${installclean}" ]]; then
135 # Run the same build after installclean
Justin Yunf44122f2023-08-23 10:52:28 +0900136 echo; echo "Installclean for incremental build..."
Justin Yune2d794c2023-06-13 15:20:17 +0900137 run_command "${base_command} TARGET_PRODUCT=${target} TARGET_BUILD_VARIANT=${variant} installclean"
Justin Yunddcda652023-09-26 16:13:47 +0900138 size_primary_clean=$((${disk_space_source}-$(read_df)))
139 write_output "${target}, ${size_primary}, ${size_primary_clean}" "${result_out_size}"
140
Justin Yune2d794c2023-06-13 15:20:17 +0900141 echo "Build the same initial build..."
Justin Yun523397c2023-06-28 18:07:35 +0900142 run_command "${base_command} TARGET_PRODUCT=${target} TARGET_BUILD_VARIANT=${variant} NINJA_ARGS=\"-d explain\" ${goals}"
Justin Yune2d794c2023-06-13 15:20:17 +0900143 get_build_trace "build_${target}_installclean.trace.gz"
Justin Yunf44122f2023-08-23 10:52:28 +0900144 echo "Installclean to prepare for the next build..."
145 run_command "${base_command} TARGET_PRODUCT=${target} TARGET_BUILD_VARIANT=${variant} installclean"
Justin Yunb931ab12023-06-07 17:45:21 +0900146fi
147
Justin Yunf44122f2023-08-23 10:52:28 +0900148count=0
149# Building the next targets in sequence
150for alter_target in "${alter_targets[@]}"; do
151 count=$((${count}+1))
152 echo; echo "Build ${alter_target}...(${count})"
Justin Yun523397c2023-06-28 18:07:35 +0900153 run_command "${base_command} TARGET_PRODUCT=${alter_target} TARGET_BUILD_VARIANT=${variant} NINJA_ARGS=\"-d explain\" ${goals}"
Justin Yunddcda652023-09-26 16:13:47 +0900154 size_alter=$((${disk_space_source}-$(read_df)))
Justin Yunf44122f2023-08-23 10:52:28 +0900155 get_build_trace "build_${alter_target}_ab${count}.trace.gz"
Justin Yune2d794c2023-06-13 15:20:17 +0900156
Justin Yunf44122f2023-08-23 10:52:28 +0900157 echo "Installclean for ${alter_target}..."
158 run_command "${base_command} TARGET_PRODUCT=${alter_target} TARGET_BUILD_VARIANT=${variant} installclean"
Justin Yunddcda652023-09-26 16:13:47 +0900159 size_alter_clean=$((${disk_space_source}-$(read_df)))
160 write_output "${alter_target}, ${size_alter}, ${size_alter_clean}" "${result_out_size}"
Justin Yunf44122f2023-08-23 10:52:28 +0900161
Justin Yunb178c4c2023-06-21 15:18:47 +0900162 if [[ -n "${dist_dir}" ]]; then
Justin Yunf44122f2023-08-23 10:52:28 +0900163 # Remove target-specific dist artifacts
Justin Yunb178c4c2023-06-21 15:18:47 +0900164 run_command "rm -f ${dist_dir}/${alter_target}*"
165 fi
Justin Yunf44122f2023-08-23 10:52:28 +0900166done
Justin Yunb178c4c2023-06-21 15:18:47 +0900167
168if [[ -n "${dist_dir}" ]]; then
169 # Remove some dist artifacts to save disk space
170 run_command "rm -f ${dist_dir}/${target}*"
171 run_command "rm -f ${dist_dir}/device-tests*"
172 run_command "rm -f ${dist_dir}/cvd-host_package.tar.gz"
173 run_command "rm -f ${dist_dir}/dexpreopt_tools.zip"
174 run_command "rm -f ${dist_dir}/otatools.zip"
175fi