Justin Yun | dde502b | 2023-06-28 21:53:34 +0900 | [diff] [blame] | 1 | #!/bin/bash -e |
Justin Yun | b931ab1 | 2023-06-07 17:45:21 +0900 | [diff] [blame] | 2 | # |
| 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 | |
| 17 | usage() { |
Justin Yun | f44122f | 2023-08-23 10:52:28 +0900 | [diff] [blame] | 18 | echo "usage: ${0} -t TARGET -v VARIANT [-d DIST_OUT] [[-a ALTER_TARGET] ...] [-c] [-o] [-r] [GOALS ...]" |
Justin Yun | b931ab1 | 2023-06-07 17:45:21 +0900 | [diff] [blame] | 19 | 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 Yun | f44122f | 2023-08-23 10:52:28 +0900 | [diff] [blame] | 22 | echo " -a ALTER_TARGET: Alternative targets that share the build artifacts with the primary target" |
Justin Yun | e2d794c | 2023-06-13 15:20:17 +0900 | [diff] [blame] | 23 | echo " -c : Run the target build again after installclean for reference" |
Justin Yun | ddcda65 | 2023-09-26 16:13:47 +0900 | [diff] [blame] | 24 | 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 Yun | e2d794c | 2023-06-13 15:20:17 +0900 | [diff] [blame] | 25 | echo " -r : Dryrun to see the commands without actually building the targets" |
Justin Yun | b931ab1 | 2023-06-07 17:45:21 +0900 | [diff] [blame] | 26 | } |
| 27 | |
Justin Yun | e2d794c | 2023-06-13 15:20:17 +0900 | [diff] [blame] | 28 | while getopts ha:cd:ort:v: opt; do |
Justin Yun | b931ab1 | 2023-06-07 17:45:21 +0900 | [diff] [blame] | 29 | case "${opt}" in |
| 30 | h) |
| 31 | usage |
| 32 | ;; |
| 33 | a) |
Justin Yun | f44122f | 2023-08-23 10:52:28 +0900 | [diff] [blame] | 34 | alter_targets+=("${OPTARG}") |
Justin Yun | b931ab1 | 2023-06-07 17:45:21 +0900 | [diff] [blame] | 35 | ;; |
| 36 | c) |
Justin Yun | e2d794c | 2023-06-13 15:20:17 +0900 | [diff] [blame] | 37 | installclean="true" |
Justin Yun | b931ab1 | 2023-06-07 17:45:21 +0900 | [diff] [blame] | 38 | ;; |
| 39 | d) |
| 40 | dist_dir="${OPTARG}" |
| 41 | ;; |
Justin Yun | e2d794c | 2023-06-13 15:20:17 +0900 | [diff] [blame] | 42 | o) |
| 43 | result_out="build_time_results.txt" |
Justin Yun | ddcda65 | 2023-09-26 16:13:47 +0900 | [diff] [blame] | 44 | result_out_size="disk_size_results.txt" |
Justin Yun | e2d794c | 2023-06-13 15:20:17 +0900 | [diff] [blame] | 45 | ;; |
| 46 | r) |
| 47 | dry_run="true" |
| 48 | ;; |
Justin Yun | b931ab1 | 2023-06-07 17:45:21 +0900 | [diff] [blame] | 49 | t) |
| 50 | target="${OPTARG}" |
| 51 | ;; |
| 52 | v) |
| 53 | variant="${OPTARG}" |
| 54 | ;; |
| 55 | *) |
| 56 | usage |
| 57 | ;; |
| 58 | esac |
| 59 | done |
| 60 | |
Justin Yun | e2d794c | 2023-06-13 15:20:17 +0900 | [diff] [blame] | 61 | if [[ -z "${target}" ]]; then |
Justin Yun | b931ab1 | 2023-06-07 17:45:21 +0900 | [diff] [blame] | 62 | echo "-t must set for the primary target" |
Justin Yun | e2d794c | 2023-06-13 15:20:17 +0900 | [diff] [blame] | 63 | usage |
Justin Yun | b931ab1 | 2023-06-07 17:45:21 +0900 | [diff] [blame] | 64 | exit 1 |
| 65 | fi |
Justin Yun | e2d794c | 2023-06-13 15:20:17 +0900 | [diff] [blame] | 66 | if [[ -z "${variant}" ]]; then |
Justin Yun | b931ab1 | 2023-06-07 17:45:21 +0900 | [diff] [blame] | 67 | echo "-v must set for build variant" |
Justin Yun | e2d794c | 2023-06-13 15:20:17 +0900 | [diff] [blame] | 68 | usage |
Justin Yun | b931ab1 | 2023-06-07 17:45:21 +0900 | [diff] [blame] | 69 | exit 1 |
| 70 | fi |
Justin Yun | f44122f | 2023-08-23 10:52:28 +0900 | [diff] [blame] | 71 | if [ "${#alter_targets[@]}" -eq 1 ]; then |
| 72 | # test for a-b-a builds |
| 73 | alter_targets+=("${target}") |
| 74 | fi |
Justin Yun | b931ab1 | 2023-06-07 17:45:21 +0900 | [diff] [blame] | 75 | |
Justin Yun | b931ab1 | 2023-06-07 17:45:21 +0900 | [diff] [blame] | 76 | goals="${@:OPTIND}" |
| 77 | |
Justin Yun | e2d794c | 2023-06-13 15:20:17 +0900 | [diff] [blame] | 78 | readonly ANDROID_TOP="$(cd $(dirname $0)/../..; pwd)" |
| 79 | cd "${ANDROID_TOP}" |
| 80 | |
| 81 | out_dir="${OUT_DIR:-out}" |
| 82 | if [[ -n "${dist_dir}" ]]; then |
| 83 | out_dir="${dist_dir}/logs" |
| 84 | fi |
| 85 | |
Justin Yun | db03719 | 2023-08-01 11:39:27 +0900 | [diff] [blame] | 86 | base_command="build/soong/soong_ui.bash --make-mode TARGET_RELEASE=trunk_staging" |
Justin Yun | e2d794c | 2023-06-13 15:20:17 +0900 | [diff] [blame] | 87 | if [[ -n "${dist_dir}" ]]; then |
Justin Yun | b931ab1 | 2023-06-07 17:45:21 +0900 | [diff] [blame] | 88 | base_command="${base_command} DIST_DIR=${dist_dir} dist" |
| 89 | fi |
| 90 | |
Justin Yun | e2d794c | 2023-06-13 15:20:17 +0900 | [diff] [blame] | 91 | run_command() { |
Justin Yun | f44122f | 2023-08-23 10:52:28 +0900 | [diff] [blame] | 92 | echo "**Running: ${1}" |
Justin Yun | e2d794c | 2023-06-13 15:20:17 +0900 | [diff] [blame] | 93 | if [[ -z "${dry_run}" ]]; then |
Justin Yun | 884015b | 2023-06-29 21:55:12 +0900 | [diff] [blame] | 94 | eval "${1}" |
Justin Yun | e2d794c | 2023-06-13 15:20:17 +0900 | [diff] [blame] | 95 | fi |
| 96 | } |
| 97 | |
Justin Yun | ddcda65 | 2023-09-26 16:13:47 +0900 | [diff] [blame] | 98 | read_df() { |
| 99 | # read the available disk size |
Justin Yun | 338ec9a | 2023-10-13 10:44:40 +0900 | [diff] [blame] | 100 | df -k . | awk '{print $4}' | sed -n '2p' |
Justin Yun | ddcda65 | 2023-09-26 16:13:47 +0900 | [diff] [blame] | 101 | } |
| 102 | |
Justin Yun | e2d794c | 2023-06-13 15:20:17 +0900 | [diff] [blame] | 103 | write_output() { |
Justin Yun | ddcda65 | 2023-09-26 16:13:47 +0900 | [diff] [blame] | 104 | if [[ -z "$2" || -n "${dry_run}" ]]; then |
Justin Yun | e2d794c | 2023-06-13 15:20:17 +0900 | [diff] [blame] | 105 | echo "Output: $1" |
| 106 | else |
Justin Yun | ddcda65 | 2023-09-26 16:13:47 +0900 | [diff] [blame] | 107 | echo "$1" >> "${out_dir}/$2" |
Justin Yun | e2d794c | 2023-06-13 15:20:17 +0900 | [diff] [blame] | 108 | fi |
| 109 | } |
| 110 | |
| 111 | get_build_trace() { |
| 112 | run_command "cp -f ${out_dir}/build.trace.gz ${out_dir}/${1}" |
| 113 | if [[ -n "${result_out}" ]]; then |
Justin Yun | ddcda65 | 2023-09-26 16:13:47 +0900 | [diff] [blame] | 114 | write_output "$(python3 development/treble/read_build_trace_gz.py ${out_dir}/${1})" "${result_out}" |
Justin Yun | e2d794c | 2023-06-13 15:20:17 +0900 | [diff] [blame] | 115 | fi |
| 116 | } |
| 117 | |
| 118 | if [[ -n "${result_out}" ]]; then |
| 119 | run_command "rm -f ${out_dir}/${result_out}" |
Justin Yun | ddcda65 | 2023-09-26 16:13:47 +0900 | [diff] [blame] | 120 | write_output "target, soong, kati, ninja, total" "${result_out}" |
| 121 | fi |
| 122 | |
| 123 | if [[ -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 Yun | e2d794c | 2023-06-13 15:20:17 +0900 | [diff] [blame] | 126 | fi |
| 127 | |
Justin Yun | b931ab1 | 2023-06-07 17:45:21 +0900 | [diff] [blame] | 128 | # Build the target first. |
Justin Yun | ddcda65 | 2023-09-26 16:13:47 +0900 | [diff] [blame] | 129 | disk_space_source=$(read_df) |
Justin Yun | f44122f | 2023-08-23 10:52:28 +0900 | [diff] [blame] | 130 | echo; echo "Initial build..." |
Justin Yun | e2d794c | 2023-06-13 15:20:17 +0900 | [diff] [blame] | 131 | run_command "${base_command} TARGET_PRODUCT=${target} TARGET_BUILD_VARIANT=${variant} ${goals}" |
Justin Yun | ddcda65 | 2023-09-26 16:13:47 +0900 | [diff] [blame] | 132 | size_primary=$((${disk_space_source}-$(read_df))) |
Justin Yun | b931ab1 | 2023-06-07 17:45:21 +0900 | [diff] [blame] | 133 | |
Justin Yun | e2d794c | 2023-06-13 15:20:17 +0900 | [diff] [blame] | 134 | if [[ -n "${installclean}" ]]; then |
| 135 | # Run the same build after installclean |
Justin Yun | f44122f | 2023-08-23 10:52:28 +0900 | [diff] [blame] | 136 | echo; echo "Installclean for incremental build..." |
Justin Yun | e2d794c | 2023-06-13 15:20:17 +0900 | [diff] [blame] | 137 | run_command "${base_command} TARGET_PRODUCT=${target} TARGET_BUILD_VARIANT=${variant} installclean" |
Justin Yun | ddcda65 | 2023-09-26 16:13:47 +0900 | [diff] [blame] | 138 | size_primary_clean=$((${disk_space_source}-$(read_df))) |
| 139 | write_output "${target}, ${size_primary}, ${size_primary_clean}" "${result_out_size}" |
| 140 | |
Justin Yun | e2d794c | 2023-06-13 15:20:17 +0900 | [diff] [blame] | 141 | echo "Build the same initial build..." |
Justin Yun | 523397c | 2023-06-28 18:07:35 +0900 | [diff] [blame] | 142 | run_command "${base_command} TARGET_PRODUCT=${target} TARGET_BUILD_VARIANT=${variant} NINJA_ARGS=\"-d explain\" ${goals}" |
Justin Yun | e2d794c | 2023-06-13 15:20:17 +0900 | [diff] [blame] | 143 | get_build_trace "build_${target}_installclean.trace.gz" |
Justin Yun | f44122f | 2023-08-23 10:52:28 +0900 | [diff] [blame] | 144 | echo "Installclean to prepare for the next build..." |
| 145 | run_command "${base_command} TARGET_PRODUCT=${target} TARGET_BUILD_VARIANT=${variant} installclean" |
Justin Yun | b931ab1 | 2023-06-07 17:45:21 +0900 | [diff] [blame] | 146 | fi |
| 147 | |
Justin Yun | f44122f | 2023-08-23 10:52:28 +0900 | [diff] [blame] | 148 | count=0 |
| 149 | # Building the next targets in sequence |
| 150 | for alter_target in "${alter_targets[@]}"; do |
| 151 | count=$((${count}+1)) |
| 152 | echo; echo "Build ${alter_target}...(${count})" |
Justin Yun | 523397c | 2023-06-28 18:07:35 +0900 | [diff] [blame] | 153 | run_command "${base_command} TARGET_PRODUCT=${alter_target} TARGET_BUILD_VARIANT=${variant} NINJA_ARGS=\"-d explain\" ${goals}" |
Justin Yun | ddcda65 | 2023-09-26 16:13:47 +0900 | [diff] [blame] | 154 | size_alter=$((${disk_space_source}-$(read_df))) |
Justin Yun | f44122f | 2023-08-23 10:52:28 +0900 | [diff] [blame] | 155 | get_build_trace "build_${alter_target}_ab${count}.trace.gz" |
Justin Yun | e2d794c | 2023-06-13 15:20:17 +0900 | [diff] [blame] | 156 | |
Justin Yun | f44122f | 2023-08-23 10:52:28 +0900 | [diff] [blame] | 157 | echo "Installclean for ${alter_target}..." |
| 158 | run_command "${base_command} TARGET_PRODUCT=${alter_target} TARGET_BUILD_VARIANT=${variant} installclean" |
Justin Yun | ddcda65 | 2023-09-26 16:13:47 +0900 | [diff] [blame] | 159 | size_alter_clean=$((${disk_space_source}-$(read_df))) |
| 160 | write_output "${alter_target}, ${size_alter}, ${size_alter_clean}" "${result_out_size}" |
Justin Yun | f44122f | 2023-08-23 10:52:28 +0900 | [diff] [blame] | 161 | |
Justin Yun | b178c4c | 2023-06-21 15:18:47 +0900 | [diff] [blame] | 162 | if [[ -n "${dist_dir}" ]]; then |
Justin Yun | f44122f | 2023-08-23 10:52:28 +0900 | [diff] [blame] | 163 | # Remove target-specific dist artifacts |
Justin Yun | b178c4c | 2023-06-21 15:18:47 +0900 | [diff] [blame] | 164 | run_command "rm -f ${dist_dir}/${alter_target}*" |
| 165 | fi |
Justin Yun | f44122f | 2023-08-23 10:52:28 +0900 | [diff] [blame] | 166 | done |
Justin Yun | b178c4c | 2023-06-21 15:18:47 +0900 | [diff] [blame] | 167 | |
| 168 | if [[ -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" |
| 175 | fi |