Brian Harring | cb78224 | 2011-12-13 19:48:44 -0800 | [diff] [blame] | 1 | #!/bin/bash |
| 2 | |
| 3 | # Copyright (c) 2011 The Chromium OS Authors. All rights reserved. |
| 4 | # Use of this source code is governed by a BSD-style license that can be |
| 5 | # found in the LICENSE file. |
| 6 | |
David James | 0b1baf6 | 2012-03-15 09:26:23 -0700 | [diff] [blame] | 7 | . "$(dirname "$0")/common.sh" || exit 1 |
Brian Harring | cb78224 | 2011-12-13 19:48:44 -0800 | [diff] [blame] | 8 | |
| 9 | # Script must run inside the chroot |
| 10 | restart_in_chroot_if_needed "$@" |
| 11 | |
Zdenek Behan | 0578078 | 2012-05-18 03:07:28 +0200 | [diff] [blame] | 12 | assert_not_root_user |
| 13 | |
David James | 855afb7 | 2012-03-14 20:04:59 -0700 | [diff] [blame] | 14 | # Developer-visible flags. |
Brian Harring | cb78224 | 2011-12-13 19:48:44 -0800 | [diff] [blame] | 15 | DEFINE_string board "${DEFAULT_BOARD}" \ |
| 16 | "The board to build packages for." |
Brian Harring | cb78224 | 2011-12-13 19:48:44 -0800 | [diff] [blame] | 17 | DEFINE_boolean usepkg "${FLAGS_TRUE}" \ |
| 18 | "Use binary packages to bootstrap when possible." |
Brian Harring | cb78224 | 2011-12-13 19:48:44 -0800 | [diff] [blame] | 19 | DEFINE_boolean noworkon "${FLAGS_FALSE}" \ |
| 20 | "Don't force-build workon packages." |
David James | 855afb7 | 2012-03-14 20:04:59 -0700 | [diff] [blame] | 21 | DEFINE_boolean showoutput "${FLAGS_FALSE}" \ |
| 22 | "Show all output from parallel_emerge." |
| 23 | DEFINE_boolean withautotest "${FLAGS_TRUE}" \ |
| 24 | "Build autotest client code." |
| 25 | |
Mike Frysinger | 76452ba | 2012-09-13 22:45:34 -0400 | [diff] [blame] | 26 | FLAGS_HELP="usage: $(basename $0) [flags] [packages] |
David James | 855afb7 | 2012-03-14 20:04:59 -0700 | [diff] [blame] | 27 | |
| 28 | build_packages updates the set of binary packages needed by Chrome OS. It will |
| 29 | cross compile all packages that have been updated into the given target's root |
| 30 | and build binary packages as a side-effect. The output packages will be picked |
| 31 | up by the build_image script to put together a bootable Chrome OS image. |
| 32 | |
Mike Frysinger | 76452ba | 2012-09-13 22:45:34 -0400 | [diff] [blame] | 33 | If [packages] are specified, only build those specific packages (and any |
| 34 | dependencies they might need). |
| 35 | |
David James | 855afb7 | 2012-03-14 20:04:59 -0700 | [diff] [blame] | 36 | For the fastest builds, use --nowithautotest --noworkon. |
| 37 | " |
| 38 | show_help_if_requested "$@" |
| 39 | |
| 40 | # The following options are advanced options, only available to those willing |
| 41 | # to read the source code. They are not shown in help output, since they are |
| 42 | # not needed for the typical developer workflow. |
| 43 | DEFINE_boolean fast "${DEFAULT_FAST}" \ |
| 44 | "Call many emerges in parallel." |
| 45 | DEFINE_integer jobs -1 \ |
| 46 | "How many packages to build in parallel at maximum." |
| 47 | DEFINE_boolean norebuild "${FLAGS_FALSE}" \ |
| 48 | "Don't automatically rebuild dependencies." |
Mike Frysinger | 839e82a | 2012-03-01 14:22:10 -0500 | [diff] [blame] | 49 | DEFINE_boolean skip_chroot_upgrade "${FLAGS_FALSE}" \ |
| 50 | "Don't run the chroot upgrade automatically; use with care." |
David James | 855afb7 | 2012-03-14 20:04:59 -0700 | [diff] [blame] | 51 | DEFINE_boolean skip_toolchain_update "${FLAGS_FALSE}" \ |
| 52 | "Don't update toolchain automatically." |
| 53 | DEFINE_boolean withdev "${FLAGS_TRUE}" \ |
| 54 | "Build useful developer friendly utilities." |
| 55 | DEFINE_boolean withdebug "${FLAGS_TRUE}" \ |
| 56 | "Build debug versions of Chromium-OS-specific packages." |
| 57 | DEFINE_boolean withfactory "${FLAGS_TRUE}" \ |
| 58 | "Build factory installer." |
| 59 | DEFINE_boolean withtest "${FLAGS_TRUE}" \ |
| 60 | "Build packages required for testing." |
Brian Harring | cb78224 | 2011-12-13 19:48:44 -0800 | [diff] [blame] | 61 | |
David James | 17c622a | 2012-03-07 09:34:08 -0800 | [diff] [blame] | 62 | # The --reuse_pkgs_from_local_boards flag tells Portage to share binary |
| 63 | # packages between boards that are built locally, so that the total time |
| 64 | # required to build several boards is reduced. This flag is only useful |
| 65 | # when you are not able to use remote binary packages, since remote binary |
| 66 | # packages are usually more up to date than anything you have locally. |
| 67 | DEFINE_boolean reuse_pkgs_from_local_boards $FLAGS_FALSE \ |
| 68 | "Bootstrap from local packages instead of remote packages." |
| 69 | |
Brian Harring | cb78224 | 2011-12-13 19:48:44 -0800 | [diff] [blame] | 70 | # Parse command line |
Brian Harring | cb78224 | 2011-12-13 19:48:44 -0800 | [diff] [blame] | 71 | FLAGS "$@" || exit 1 |
| 72 | eval set -- "${FLAGS_ARGV}" |
Brian Harring | cb78224 | 2011-12-13 19:48:44 -0800 | [diff] [blame] | 73 | |
| 74 | # Die on any errors. |
Brian Harring | 7f175a5 | 2012-03-02 05:37:00 -0800 | [diff] [blame] | 75 | switch_to_strict_mode |
Brian Harring | cb78224 | 2011-12-13 19:48:44 -0800 | [diff] [blame] | 76 | |
| 77 | # Right now build_packages has to be run from scripts/ |
| 78 | . ${SRC_ROOT}/third_party/chromiumos-overlay/chromeos/config/chromeos_version.sh |
| 79 | |
| 80 | if [[ -z "${FLAGS_board}" ]]; then |
| 81 | echo "Error: --board is required." |
| 82 | exit 1 |
| 83 | fi |
| 84 | |
David James | 4c29c24 | 2012-06-06 20:45:18 -0700 | [diff] [blame] | 85 | CHROMITE_BIN="${GCLIENT_ROOT}/chromite/bin" |
Mike Frysinger | 4114c79 | 2012-09-13 22:33:12 -0400 | [diff] [blame] | 86 | |
| 87 | # Before we can run any tools, we need to update chroot or setup_board. |
| 88 | UPDATE_ARGS=() |
| 89 | if [ "${FLAGS_fast}" -eq "${FLAGS_TRUE}" ]; then |
| 90 | UPDATE_ARGS+=( --fast ) |
| 91 | else |
| 92 | UPDATE_ARGS+=( --nofast ) |
| 93 | fi |
| 94 | if [ "${FLAGS_usepkg}" -eq "${FLAGS_TRUE}" ]; then |
| 95 | UPDATE_ARGS+=( --usepkg ) |
| 96 | else |
| 97 | UPDATE_ARGS+=( --nousepkg ) |
| 98 | fi |
| 99 | if [[ "${FLAGS_jobs}" -ne -1 ]]; then |
| 100 | UPDATE_ARGS+=( --jobs=${FLAGS_jobs} ) |
| 101 | fi |
| 102 | if [ "${FLAGS_reuse_pkgs_from_local_boards}" -eq "${FLAGS_TRUE}" ]; then |
| 103 | UPDATE_ARGS+=( --reuse_pkgs_from_local_boards ) |
| 104 | fi |
| 105 | if [ "${FLAGS_skip_toolchain_update}" -eq "${FLAGS_TRUE}" ]; then |
| 106 | UPDATE_ARGS+=( --skip_toolchain_update ) |
| 107 | fi |
| 108 | if [ "${FLAGS_skip_chroot_upgrade}" -eq "${FLAGS_TRUE}" ]; then |
| 109 | UPDATE_ARGS+=( --skip_chroot_upgrade ) |
| 110 | fi |
| 111 | |
| 112 | "${SCRIPTS_DIR}"/setup_board --quiet --board=${FLAGS_board} "${UPDATE_ARGS[@]}" |
| 113 | |
| 114 | # Setup all the emerge command/flags. |
| 115 | EMERGE_FLAGS=( -uDNv --backtrack=30 --select ) |
| 116 | |
Brian Harring | cb78224 | 2011-12-13 19:48:44 -0800 | [diff] [blame] | 117 | if [[ "${FLAGS_fast}" -eq "${FLAGS_TRUE}" ]]; then |
Mike Frysinger | 4114c79 | 2012-09-13 22:33:12 -0400 | [diff] [blame] | 118 | EMERGE_CMD=( |
| 119 | "${CHROMITE_BIN}/parallel_emerge" |
| 120 | --board=${FLAGS_board} |
| 121 | ) |
| 122 | else |
| 123 | EMERGE_CMD=( "emerge-${FLAGS_board}" ) |
Brian Harring | cb78224 | 2011-12-13 19:48:44 -0800 | [diff] [blame] | 124 | fi |
Mike Frysinger | 4114c79 | 2012-09-13 22:33:12 -0400 | [diff] [blame] | 125 | EMERGE_CMD+=( ${EXTRA_BOARD_FLAGS} ) |
Brian Harring | cb78224 | 2011-12-13 19:48:44 -0800 | [diff] [blame] | 126 | |
David James | 17c622a | 2012-03-07 09:34:08 -0800 | [diff] [blame] | 127 | if [[ "${FLAGS_usepkg}" -eq "${FLAGS_TRUE}" || |
| 128 | "${FLAGS_reuse_pkgs_from_local_boards}" -eq "${FLAGS_TRUE}" ]]; then |
Brian Harring | cb78224 | 2011-12-13 19:48:44 -0800 | [diff] [blame] | 129 | # Use binary packages. Include all build-time dependencies, |
| 130 | # so as to avoid unnecessary differences between source |
| 131 | # and binary builds. |
Mike Frysinger | 4114c79 | 2012-09-13 22:33:12 -0400 | [diff] [blame] | 132 | EMERGE_FLAGS+=( --getbinpkg --usepkg --with-bdeps y ) |
Brian Harring | cb78224 | 2011-12-13 19:48:44 -0800 | [diff] [blame] | 133 | fi |
| 134 | |
| 135 | if [[ "${FLAGS_jobs}" -ne -1 ]]; then |
Mike Frysinger | 4114c79 | 2012-09-13 22:33:12 -0400 | [diff] [blame] | 136 | EMERGE_FLAGS+=( --jobs=${FLAGS_jobs} ) |
| 137 | fi |
| 138 | |
| 139 | if [[ "${FLAGS_norebuild}" -eq "${FLAGS_FALSE}" ]]; then |
| 140 | EMERGE_FLAGS+=( --rebuild-if-unbuilt ) |
| 141 | fi |
| 142 | if [[ "${FLAGS_showoutput}" -eq "${FLAGS_TRUE}" && \ |
| 143 | "${FLAGS_fast}" -eq "${FLAGS_TRUE}" ]]; then |
| 144 | # Only parallel_emerge supports --show-output. |
| 145 | EMERGE_FLAGS+=( --show-output ) |
Brian Harring | cb78224 | 2011-12-13 19:48:44 -0800 | [diff] [blame] | 146 | fi |
| 147 | |
| 148 | if [[ "${FLAGS_withdebug}" -eq "${FLAGS_FALSE}" ]]; then |
| 149 | export USE="${USE} -cros-debug" |
| 150 | fi |
| 151 | |
Mike Frysinger | 4114c79 | 2012-09-13 22:33:12 -0400 | [diff] [blame] | 152 | # Figure out which packages we should be building. |
Mike Frysinger | 76452ba | 2012-09-13 22:45:34 -0400 | [diff] [blame] | 153 | PACKAGES=( "$@" ) |
| 154 | if [[ $# -eq 0 ]]; then |
| 155 | PACKAGES=( chromeos-base/chromeos ) |
| 156 | if [[ "${FLAGS_withdev}" -eq "${FLAGS_TRUE}" ]]; then |
| 157 | PACKAGES+=( chromeos-base/chromeos-dev ) |
Chris Sosa | 0538571 | 2012-09-11 15:04:41 -0700 | [diff] [blame] | 158 | PACKAGES+=( chromeos-base/chromeos-dev-init ) |
Mike Frysinger | 76452ba | 2012-09-13 22:45:34 -0400 | [diff] [blame] | 159 | fi |
| 160 | if [[ "${FLAGS_withfactory}" -eq "${FLAGS_TRUE}" ]]; then |
| 161 | PACKAGES+=( chromeos-base/chromeos-factory ) |
| 162 | PACKAGES+=( chromeos-base/chromeos-factoryinstall ) |
| 163 | PACKAGES+=( chromeos-base/factorytest-init ) |
| 164 | PACKAGES+=( chromeos-base/chromeos-hwid ) |
| 165 | fi |
| 166 | if [[ "${FLAGS_withtest}" -eq "${FLAGS_TRUE}" ]]; then |
| 167 | PACKAGES+=( chromeos-base/chromeos-test ) |
Chris Sosa | 0538571 | 2012-09-11 15:04:41 -0700 | [diff] [blame] | 168 | PACKAGES+=( chromeos-base/chromeos-test-init ) |
Mike Frysinger | 76452ba | 2012-09-13 22:45:34 -0400 | [diff] [blame] | 169 | fi |
| 170 | if [[ "${FLAGS_withautotest}" -eq "${FLAGS_TRUE}" ]]; then |
| 171 | PACKAGES+=( chromeos-base/autotest-all ) |
| 172 | fi |
Brian Harring | cb78224 | 2011-12-13 19:48:44 -0800 | [diff] [blame] | 173 | fi |
| 174 | |
| 175 | # Verify that all packages can be emerged from scratch, without any |
| 176 | # backtracking. Only print the output if this step fails. |
Mike Frysinger | 76452ba | 2012-09-13 22:45:34 -0400 | [diff] [blame] | 177 | info "Checking package dependencies are correct: ${PACKAGES[*]}" |
Brian Harring | cb78224 | 2011-12-13 19:48:44 -0800 | [diff] [blame] | 178 | if ! OUTPUT=$(emerge-${FLAGS_board} -pe --backtrack=0 ${PACKAGES} 2>&1); then |
| 179 | printf "%s\n" "${OUTPUT}" |
Brian Harring | 7f175a5 | 2012-03-02 05:37:00 -0800 | [diff] [blame] | 180 | die_notrace "emerge detected broken ebuilds. See error message above." |
Brian Harring | cb78224 | 2011-12-13 19:48:44 -0800 | [diff] [blame] | 181 | fi |
| 182 | |
Mike Frysinger | 4114c79 | 2012-09-13 22:33:12 -0400 | [diff] [blame] | 183 | # Build cros_workon packages when they are changed. |
| 184 | CROS_WORKON_PKGS=() |
| 185 | if [ "${FLAGS_noworkon}" -eq "${FLAGS_FALSE}" ]; then |
| 186 | LIST_MODIFIED_PACKAGES="${CHROMITE_BIN}/cros_list_modified_packages" |
| 187 | CROS_WORKON_PKGS+=( $("${LIST_MODIFIED_PACKAGES}" --board=${FLAGS_board}) ) |
Brian Harring | cb78224 | 2011-12-13 19:48:44 -0800 | [diff] [blame] | 188 | fi |
Mike Frysinger | 4114c79 | 2012-09-13 22:33:12 -0400 | [diff] [blame] | 189 | |
| 190 | # TODO(anush): Make chrome a fake cros-workon package. |
| 191 | if [[ -n "${CHROME_ORIGIN}" ]]; then |
| 192 | CROS_WORKON_PKGS+=( chromeos-base/chromeos-chrome ) |
| 193 | fi |
| 194 | |
| 195 | if [[ ${#CROS_WORKON_PKGS[@]} -gt 0 ]]; then |
| 196 | EMERGE_FLAGS+=( |
| 197 | --reinstall-atoms="${CROS_WORKON_PKGS[*]}" |
| 198 | --usepkg-exclude="${CROS_WORKON_PKGS[*]}" |
| 199 | ) |
David James | 710a7d1 | 2011-12-21 15:57:02 -0800 | [diff] [blame] | 200 | fi |
Matt Tennant | 298f61a | 2012-06-25 21:54:33 -0700 | [diff] [blame] | 201 | |
| 202 | # Prepare tmp file to capture emerge output from tee. |
| 203 | tmpfile=$(mktemp -t tmp.build_packages-emerge.XXXXXX) |
| 204 | trap "rm -f '${tmpfile}'" EXIT |
| 205 | |
Mike Frysinger | 76452ba | 2012-09-13 22:45:34 -0400 | [diff] [blame] | 206 | info "Merging board packages now" |
Matt Tennant | 298f61a | 2012-06-25 21:54:33 -0700 | [diff] [blame] | 207 | ( |
| 208 | set -o pipefail |
Mike Frysinger | 4114c79 | 2012-09-13 22:33:12 -0400 | [diff] [blame] | 209 | sudo -E "${EMERGE_CMD[@]}" "${EMERGE_FLAGS[@]}" "${PACKAGES[@]}" | \ |
Matt Tennant | 298f61a | 2012-06-25 21:54:33 -0700 | [diff] [blame] | 210 | tee "${tmpfile}" |
| 211 | ) |
| 212 | |
| 213 | # Extract total package count from emerge output. |
| 214 | package_count=$(awk '$0 ~ /^Total: [0-9]+ packages/ { print $2 }' "${tmpfile}") |
| 215 | rm "${tmpfile}" |
| 216 | trap - EXIT |
Brian Harring | cb78224 | 2011-12-13 19:48:44 -0800 | [diff] [blame] | 217 | |
| 218 | echo "Builds complete" |
Matt Tennant | 298f61a | 2012-06-25 21:54:33 -0700 | [diff] [blame] | 219 | EXTRA_COMMAND_STATS[package_count]=${package_count} |
| 220 | command_completed |
Brian Harring | cb78224 | 2011-12-13 19:48:44 -0800 | [diff] [blame] | 221 | echo "Done" |