blob: 247d365f3d1b4288886ab2835d06a4722ed82a21 [file] [log] [blame]
Brian Harringcb782242011-12-13 19:48:44 -08001#!/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 James0b1baf62012-03-15 09:26:23 -07007. "$(dirname "$0")/common.sh" || exit 1
Brian Harringcb782242011-12-13 19:48:44 -08008
9# Script must run inside the chroot
10restart_in_chroot_if_needed "$@"
11
Zdenek Behan05780782012-05-18 03:07:28 +020012assert_not_root_user
13
David James855afb72012-03-14 20:04:59 -070014# Developer-visible flags.
Brian Harringcb782242011-12-13 19:48:44 -080015DEFINE_string board "${DEFAULT_BOARD}" \
16 "The board to build packages for."
Brian Harringcb782242011-12-13 19:48:44 -080017DEFINE_boolean usepkg "${FLAGS_TRUE}" \
18 "Use binary packages to bootstrap when possible."
Brian Harringcb782242011-12-13 19:48:44 -080019DEFINE_boolean noworkon "${FLAGS_FALSE}" \
20 "Don't force-build workon packages."
David James855afb72012-03-14 20:04:59 -070021DEFINE_boolean showoutput "${FLAGS_FALSE}" \
22 "Show all output from parallel_emerge."
23DEFINE_boolean withautotest "${FLAGS_TRUE}" \
24 "Build autotest client code."
Brian Harring17c1fe82012-12-05 21:12:31 -080025DEFINE_boolean fetchonly "${FLAGS_FALSE}" \
26 "Don't build anything, instead only fetch what is needed."
David James855afb72012-03-14 20:04:59 -070027
Mike Frysinger76452ba2012-09-13 22:45:34 -040028FLAGS_HELP="usage: $(basename $0) [flags] [packages]
David James855afb72012-03-14 20:04:59 -070029
30build_packages updates the set of binary packages needed by Chrome OS. It will
31cross compile all packages that have been updated into the given target's root
32and build binary packages as a side-effect. The output packages will be picked
33up by the build_image script to put together a bootable Chrome OS image.
34
Mike Frysinger76452ba2012-09-13 22:45:34 -040035If [packages] are specified, only build those specific packages (and any
36dependencies they might need).
37
David James855afb72012-03-14 20:04:59 -070038For the fastest builds, use --nowithautotest --noworkon.
39"
40show_help_if_requested "$@"
41
42# The following options are advanced options, only available to those willing
43# to read the source code. They are not shown in help output, since they are
44# not needed for the typical developer workflow.
45DEFINE_boolean fast "${DEFAULT_FAST}" \
46 "Call many emerges in parallel."
47DEFINE_integer jobs -1 \
48 "How many packages to build in parallel at maximum."
49DEFINE_boolean norebuild "${FLAGS_FALSE}" \
50 "Don't automatically rebuild dependencies."
Mike Frysinger839e82a2012-03-01 14:22:10 -050051DEFINE_boolean skip_chroot_upgrade "${FLAGS_FALSE}" \
52 "Don't run the chroot upgrade automatically; use with care."
David James855afb72012-03-14 20:04:59 -070053DEFINE_boolean skip_toolchain_update "${FLAGS_FALSE}" \
54 "Don't update toolchain automatically."
55DEFINE_boolean withdev "${FLAGS_TRUE}" \
56 "Build useful developer friendly utilities."
57DEFINE_boolean withdebug "${FLAGS_TRUE}" \
58 "Build debug versions of Chromium-OS-specific packages."
59DEFINE_boolean withfactory "${FLAGS_TRUE}" \
60 "Build factory installer."
61DEFINE_boolean withtest "${FLAGS_TRUE}" \
62 "Build packages required for testing."
Brian Harringcb782242011-12-13 19:48:44 -080063
David James17c622a2012-03-07 09:34:08 -080064# The --reuse_pkgs_from_local_boards flag tells Portage to share binary
65# packages between boards that are built locally, so that the total time
66# required to build several boards is reduced. This flag is only useful
67# when you are not able to use remote binary packages, since remote binary
68# packages are usually more up to date than anything you have locally.
69DEFINE_boolean reuse_pkgs_from_local_boards $FLAGS_FALSE \
70 "Bootstrap from local packages instead of remote packages."
71
Brian Harringcb782242011-12-13 19:48:44 -080072# Parse command line
Brian Harringcb782242011-12-13 19:48:44 -080073FLAGS "$@" || exit 1
74eval set -- "${FLAGS_ARGV}"
Brian Harringcb782242011-12-13 19:48:44 -080075
76# Die on any errors.
Brian Harring7f175a52012-03-02 05:37:00 -080077switch_to_strict_mode
Brian Harringcb782242011-12-13 19:48:44 -080078
79# Right now build_packages has to be run from scripts/
80. ${SRC_ROOT}/third_party/chromiumos-overlay/chromeos/config/chromeos_version.sh
81
82if [[ -z "${FLAGS_board}" ]]; then
83 echo "Error: --board is required."
84 exit 1
85fi
86
David James4c29c242012-06-06 20:45:18 -070087CHROMITE_BIN="${GCLIENT_ROOT}/chromite/bin"
Mike Frysinger4114c792012-09-13 22:33:12 -040088
89# Before we can run any tools, we need to update chroot or setup_board.
90UPDATE_ARGS=()
91if [ "${FLAGS_fast}" -eq "${FLAGS_TRUE}" ]; then
92 UPDATE_ARGS+=( --fast )
93else
94 UPDATE_ARGS+=( --nofast )
95fi
96if [ "${FLAGS_usepkg}" -eq "${FLAGS_TRUE}" ]; then
97 UPDATE_ARGS+=( --usepkg )
98else
99 UPDATE_ARGS+=( --nousepkg )
100fi
101if [[ "${FLAGS_jobs}" -ne -1 ]]; then
102 UPDATE_ARGS+=( --jobs=${FLAGS_jobs} )
103fi
104if [ "${FLAGS_reuse_pkgs_from_local_boards}" -eq "${FLAGS_TRUE}" ]; then
105 UPDATE_ARGS+=( --reuse_pkgs_from_local_boards )
106fi
107if [ "${FLAGS_skip_toolchain_update}" -eq "${FLAGS_TRUE}" ]; then
108 UPDATE_ARGS+=( --skip_toolchain_update )
109fi
110if [ "${FLAGS_skip_chroot_upgrade}" -eq "${FLAGS_TRUE}" ]; then
111 UPDATE_ARGS+=( --skip_chroot_upgrade )
112fi
113
114"${SCRIPTS_DIR}"/setup_board --quiet --board=${FLAGS_board} "${UPDATE_ARGS[@]}"
115
116# Setup all the emerge command/flags.
117EMERGE_FLAGS=( -uDNv --backtrack=30 --select )
118
Brian Harringcb782242011-12-13 19:48:44 -0800119if [[ "${FLAGS_fast}" -eq "${FLAGS_TRUE}" ]]; then
Mike Frysinger4114c792012-09-13 22:33:12 -0400120 EMERGE_CMD=(
121 "${CHROMITE_BIN}/parallel_emerge"
122 --board=${FLAGS_board}
123 )
124else
125 EMERGE_CMD=( "emerge-${FLAGS_board}" )
Brian Harringcb782242011-12-13 19:48:44 -0800126fi
Brian Harring17c1fe82012-12-05 21:12:31 -0800127if [ "${FLAGS_fetchonly}" -eq "${FLAGS_TRUE}" ]; then
128 EMERGE_CMD+=( --fetchonly )
129fi
130
Mike Frysinger4114c792012-09-13 22:33:12 -0400131EMERGE_CMD+=( ${EXTRA_BOARD_FLAGS} )
Brian Harringcb782242011-12-13 19:48:44 -0800132
David James17c622a2012-03-07 09:34:08 -0800133if [[ "${FLAGS_usepkg}" -eq "${FLAGS_TRUE}" ||
134 "${FLAGS_reuse_pkgs_from_local_boards}" -eq "${FLAGS_TRUE}" ]]; then
Brian Harringcb782242011-12-13 19:48:44 -0800135 # Use binary packages. Include all build-time dependencies,
136 # so as to avoid unnecessary differences between source
137 # and binary builds.
Mike Frysinger4114c792012-09-13 22:33:12 -0400138 EMERGE_FLAGS+=( --getbinpkg --usepkg --with-bdeps y )
Brian Harringcb782242011-12-13 19:48:44 -0800139fi
140
141if [[ "${FLAGS_jobs}" -ne -1 ]]; then
Mike Frysinger4114c792012-09-13 22:33:12 -0400142 EMERGE_FLAGS+=( --jobs=${FLAGS_jobs} )
143fi
144
145if [[ "${FLAGS_norebuild}" -eq "${FLAGS_FALSE}" ]]; then
146 EMERGE_FLAGS+=( --rebuild-if-unbuilt )
147fi
148if [[ "${FLAGS_showoutput}" -eq "${FLAGS_TRUE}" && \
149 "${FLAGS_fast}" -eq "${FLAGS_TRUE}" ]]; then
150 # Only parallel_emerge supports --show-output.
151 EMERGE_FLAGS+=( --show-output )
Brian Harringcb782242011-12-13 19:48:44 -0800152fi
153
154if [[ "${FLAGS_withdebug}" -eq "${FLAGS_FALSE}" ]]; then
155 export USE="${USE} -cros-debug"
156fi
157
Mike Frysinger4114c792012-09-13 22:33:12 -0400158# Figure out which packages we should be building.
Mike Frysinger76452ba2012-09-13 22:45:34 -0400159PACKAGES=( "$@" )
160if [[ $# -eq 0 ]]; then
161 PACKAGES=( chromeos-base/chromeos )
162 if [[ "${FLAGS_withdev}" -eq "${FLAGS_TRUE}" ]]; then
163 PACKAGES+=( chromeos-base/chromeos-dev )
Chris Sosa05385712012-09-11 15:04:41 -0700164 PACKAGES+=( chromeos-base/chromeos-dev-init )
Mike Frysinger76452ba2012-09-13 22:45:34 -0400165 fi
166 if [[ "${FLAGS_withfactory}" -eq "${FLAGS_TRUE}" ]]; then
167 PACKAGES+=( chromeos-base/chromeos-factory )
168 PACKAGES+=( chromeos-base/chromeos-factoryinstall )
169 PACKAGES+=( chromeos-base/factorytest-init )
170 PACKAGES+=( chromeos-base/chromeos-hwid )
171 fi
172 if [[ "${FLAGS_withtest}" -eq "${FLAGS_TRUE}" ]]; then
173 PACKAGES+=( chromeos-base/chromeos-test )
Chris Sosa05385712012-09-11 15:04:41 -0700174 PACKAGES+=( chromeos-base/chromeos-test-init )
Mike Frysinger76452ba2012-09-13 22:45:34 -0400175 fi
176 if [[ "${FLAGS_withautotest}" -eq "${FLAGS_TRUE}" ]]; then
177 PACKAGES+=( chromeos-base/autotest-all )
178 fi
Brian Harringcb782242011-12-13 19:48:44 -0800179fi
180
181# Verify that all packages can be emerged from scratch, without any
182# backtracking. Only print the output if this step fails.
Mike Frysinger76452ba2012-09-13 22:45:34 -0400183info "Checking package dependencies are correct: ${PACKAGES[*]}"
David Jamesab9ca212012-11-06 11:06:07 -0800184if ! OUTPUT=$(emerge-${FLAGS_board} -pe --backtrack=0 \
185 "${PACKAGES[@]}" 2>&1); then
Brian Harringcb782242011-12-13 19:48:44 -0800186 printf "%s\n" "${OUTPUT}"
Brian Harring7f175a52012-03-02 05:37:00 -0800187 die_notrace "emerge detected broken ebuilds. See error message above."
Brian Harringcb782242011-12-13 19:48:44 -0800188fi
189
Mike Frysinger4114c792012-09-13 22:33:12 -0400190# Build cros_workon packages when they are changed.
191CROS_WORKON_PKGS=()
192if [ "${FLAGS_noworkon}" -eq "${FLAGS_FALSE}" ]; then
193 LIST_MODIFIED_PACKAGES="${CHROMITE_BIN}/cros_list_modified_packages"
194 CROS_WORKON_PKGS+=( $("${LIST_MODIFIED_PACKAGES}" --board=${FLAGS_board}) )
Brian Harringcb782242011-12-13 19:48:44 -0800195fi
Mike Frysinger4114c792012-09-13 22:33:12 -0400196
197# TODO(anush): Make chrome a fake cros-workon package.
198if [[ -n "${CHROME_ORIGIN}" ]]; then
199 CROS_WORKON_PKGS+=( chromeos-base/chromeos-chrome )
200fi
201
202if [[ ${#CROS_WORKON_PKGS[@]} -gt 0 ]]; then
203 EMERGE_FLAGS+=(
204 --reinstall-atoms="${CROS_WORKON_PKGS[*]}"
205 --usepkg-exclude="${CROS_WORKON_PKGS[*]}"
206 )
David James710a7d12011-12-21 15:57:02 -0800207fi
Matt Tennant298f61a2012-06-25 21:54:33 -0700208
209# Prepare tmp file to capture emerge output from tee.
210tmpfile=$(mktemp -t tmp.build_packages-emerge.XXXXXX)
211trap "rm -f '${tmpfile}'" EXIT
212
Mike Frysinger76452ba2012-09-13 22:45:34 -0400213info "Merging board packages now"
Matt Tennant298f61a2012-06-25 21:54:33 -0700214(
215 set -o pipefail
Mike Frysinger4114c792012-09-13 22:33:12 -0400216 sudo -E "${EMERGE_CMD[@]}" "${EMERGE_FLAGS[@]}" "${PACKAGES[@]}" | \
Matt Tennant298f61a2012-06-25 21:54:33 -0700217 tee "${tmpfile}"
218)
219
220# Extract total package count from emerge output.
221package_count=$(awk '$0 ~ /^Total: [0-9]+ packages/ { print $2 }' "${tmpfile}")
222rm "${tmpfile}"
223trap - EXIT
Brian Harringcb782242011-12-13 19:48:44 -0800224
225echo "Builds complete"
Matt Tennant298f61a2012-06-25 21:54:33 -0700226EXTRA_COMMAND_STATS[package_count]=${package_count}
227command_completed
Brian Harringcb782242011-12-13 19:48:44 -0800228echo "Done"