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