blob: d8aa7f161f12fb962ccb9a14645d9b6806977918 [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.
Ryan Cuieb4595f2012-12-17 14:28:42 -080045DEFINE_string accept_licenses "" \
46 "Licenses to append to the accept list."
David James855afb72012-03-14 20:04:59 -070047DEFINE_boolean fast "${DEFAULT_FAST}" \
48 "Call many emerges in parallel."
49DEFINE_integer jobs -1 \
50 "How many packages to build in parallel at maximum."
51DEFINE_boolean norebuild "${FLAGS_FALSE}" \
52 "Don't automatically rebuild dependencies."
Mike Frysinger839e82a2012-03-01 14:22:10 -050053DEFINE_boolean skip_chroot_upgrade "${FLAGS_FALSE}" \
54 "Don't run the chroot upgrade automatically; use with care."
David James855afb72012-03-14 20:04:59 -070055DEFINE_boolean skip_toolchain_update "${FLAGS_FALSE}" \
56 "Don't update toolchain automatically."
57DEFINE_boolean withdev "${FLAGS_TRUE}" \
58 "Build useful developer friendly utilities."
59DEFINE_boolean withdebug "${FLAGS_TRUE}" \
60 "Build debug versions of Chromium-OS-specific packages."
61DEFINE_boolean withfactory "${FLAGS_TRUE}" \
62 "Build factory installer."
63DEFINE_boolean withtest "${FLAGS_TRUE}" \
64 "Build packages required for testing."
David James0ae653d2013-07-03 15:59:29 -070065DEFINE_boolean chrome_binhost_only $FLAGS_FALSE \
66 "Only fetch packages from the Chrome binhost."
Brian Harringcb782242011-12-13 19:48:44 -080067
David James17c622a2012-03-07 09:34:08 -080068# The --reuse_pkgs_from_local_boards flag tells Portage to share binary
69# packages between boards that are built locally, so that the total time
70# required to build several boards is reduced. This flag is only useful
71# when you are not able to use remote binary packages, since remote binary
72# packages are usually more up to date than anything you have locally.
73DEFINE_boolean reuse_pkgs_from_local_boards $FLAGS_FALSE \
74 "Bootstrap from local packages instead of remote packages."
75
Brian Harringcb782242011-12-13 19:48:44 -080076# Parse command line
Brian Harringcb782242011-12-13 19:48:44 -080077FLAGS "$@" || exit 1
78eval set -- "${FLAGS_ARGV}"
Brian Harringcb782242011-12-13 19:48:44 -080079
80# Die on any errors.
Brian Harring7f175a52012-03-02 05:37:00 -080081switch_to_strict_mode
Brian Harringcb782242011-12-13 19:48:44 -080082
83# Right now build_packages has to be run from scripts/
84. ${SRC_ROOT}/third_party/chromiumos-overlay/chromeos/config/chromeos_version.sh
85
86if [[ -z "${FLAGS_board}" ]]; then
87 echo "Error: --board is required."
88 exit 1
89fi
90
David James4c29c242012-06-06 20:45:18 -070091CHROMITE_BIN="${GCLIENT_ROOT}/chromite/bin"
Mike Frysinger4114c792012-09-13 22:33:12 -040092
93# Before we can run any tools, we need to update chroot or setup_board.
94UPDATE_ARGS=()
Ryan Cuieb4595f2012-12-17 14:28:42 -080095if [[ -n ${FLAGS_accept_licenses} ]]; then
96 UPDATE_ARGS+=( --accept_licenses "${FLAGS_accept_licenses}" )
97fi
Mike Frysinger4114c792012-09-13 22:33:12 -040098if [ "${FLAGS_fast}" -eq "${FLAGS_TRUE}" ]; then
99 UPDATE_ARGS+=( --fast )
100else
101 UPDATE_ARGS+=( --nofast )
102fi
103if [ "${FLAGS_usepkg}" -eq "${FLAGS_TRUE}" ]; then
104 UPDATE_ARGS+=( --usepkg )
105else
106 UPDATE_ARGS+=( --nousepkg )
107fi
108if [[ "${FLAGS_jobs}" -ne -1 ]]; then
109 UPDATE_ARGS+=( --jobs=${FLAGS_jobs} )
110fi
David James0ae653d2013-07-03 15:59:29 -0700111if [ "${FLAGS_chrome_binhost_only}" -eq "${FLAGS_TRUE}" ]; then
112 UPDATE_ARGS+=( --chrome_binhost_only )
113fi
Mike Frysinger4114c792012-09-13 22:33:12 -0400114if [ "${FLAGS_reuse_pkgs_from_local_boards}" -eq "${FLAGS_TRUE}" ]; then
115 UPDATE_ARGS+=( --reuse_pkgs_from_local_boards )
116fi
117if [ "${FLAGS_skip_toolchain_update}" -eq "${FLAGS_TRUE}" ]; then
118 UPDATE_ARGS+=( --skip_toolchain_update )
119fi
120if [ "${FLAGS_skip_chroot_upgrade}" -eq "${FLAGS_TRUE}" ]; then
121 UPDATE_ARGS+=( --skip_chroot_upgrade )
122fi
123
124"${SCRIPTS_DIR}"/setup_board --quiet --board=${FLAGS_board} "${UPDATE_ARGS[@]}"
125
126# Setup all the emerge command/flags.
127EMERGE_FLAGS=( -uDNv --backtrack=30 --select )
128
Brian Harringcb782242011-12-13 19:48:44 -0800129if [[ "${FLAGS_fast}" -eq "${FLAGS_TRUE}" ]]; then
Mike Frysinger4114c792012-09-13 22:33:12 -0400130 EMERGE_CMD=(
131 "${CHROMITE_BIN}/parallel_emerge"
132 --board=${FLAGS_board}
133 )
134else
135 EMERGE_CMD=( "emerge-${FLAGS_board}" )
Brian Harringcb782242011-12-13 19:48:44 -0800136fi
Brian Harring3d01db22012-12-15 21:09:03 -0800137if [[ "${FLAGS_fetchonly}" -eq "${FLAGS_TRUE}" ]]; then
Brian Harring17c1fe82012-12-05 21:12:31 -0800138 EMERGE_CMD+=( --fetchonly )
139fi
140
Mike Frysinger4114c792012-09-13 22:33:12 -0400141EMERGE_CMD+=( ${EXTRA_BOARD_FLAGS} )
Brian Harringcb782242011-12-13 19:48:44 -0800142
David James17c622a2012-03-07 09:34:08 -0800143if [[ "${FLAGS_usepkg}" -eq "${FLAGS_TRUE}" ||
144 "${FLAGS_reuse_pkgs_from_local_boards}" -eq "${FLAGS_TRUE}" ]]; then
Brian Harringcb782242011-12-13 19:48:44 -0800145 # Use binary packages. Include all build-time dependencies,
146 # so as to avoid unnecessary differences between source
147 # and binary builds.
Mike Frysinger4114c792012-09-13 22:33:12 -0400148 EMERGE_FLAGS+=( --getbinpkg --usepkg --with-bdeps y )
Brian Harringcb782242011-12-13 19:48:44 -0800149fi
150
151if [[ "${FLAGS_jobs}" -ne -1 ]]; then
Mike Frysinger4114c792012-09-13 22:33:12 -0400152 EMERGE_FLAGS+=( --jobs=${FLAGS_jobs} )
153fi
154
155if [[ "${FLAGS_norebuild}" -eq "${FLAGS_FALSE}" ]]; then
156 EMERGE_FLAGS+=( --rebuild-if-unbuilt )
157fi
158if [[ "${FLAGS_showoutput}" -eq "${FLAGS_TRUE}" && \
159 "${FLAGS_fast}" -eq "${FLAGS_TRUE}" ]]; then
160 # Only parallel_emerge supports --show-output.
161 EMERGE_FLAGS+=( --show-output )
Brian Harringcb782242011-12-13 19:48:44 -0800162fi
163
164if [[ "${FLAGS_withdebug}" -eq "${FLAGS_FALSE}" ]]; then
165 export USE="${USE} -cros-debug"
166fi
167
Mike Frysinger4114c792012-09-13 22:33:12 -0400168# Figure out which packages we should be building.
Mike Frysinger76452ba2012-09-13 22:45:34 -0400169PACKAGES=( "$@" )
170if [[ $# -eq 0 ]]; then
171 PACKAGES=( chromeos-base/chromeos )
172 if [[ "${FLAGS_withdev}" -eq "${FLAGS_TRUE}" ]]; then
173 PACKAGES+=( chromeos-base/chromeos-dev )
174 fi
175 if [[ "${FLAGS_withfactory}" -eq "${FLAGS_TRUE}" ]]; then
Vic Yang8e0cd072012-12-18 22:31:46 +0800176 PACKAGES+=( chromeos-base/chromeos-installshim )
Mike Frysinger76452ba2012-09-13 22:45:34 -0400177 PACKAGES+=( chromeos-base/chromeos-factory )
Mike Frysinger76452ba2012-09-13 22:45:34 -0400178 PACKAGES+=( chromeos-base/factorytest-init )
179 PACKAGES+=( chromeos-base/chromeos-hwid )
180 fi
181 if [[ "${FLAGS_withtest}" -eq "${FLAGS_TRUE}" ]]; then
182 PACKAGES+=( chromeos-base/chromeos-test )
183 fi
184 if [[ "${FLAGS_withautotest}" -eq "${FLAGS_TRUE}" ]]; then
185 PACKAGES+=( chromeos-base/autotest-all )
186 fi
Brian Harringcb782242011-12-13 19:48:44 -0800187fi
188
189# Verify that all packages can be emerged from scratch, without any
190# backtracking. Only print the output if this step fails.
Mike Frysinger76452ba2012-09-13 22:45:34 -0400191info "Checking package dependencies are correct: ${PACKAGES[*]}"
David Jamesab9ca212012-11-06 11:06:07 -0800192if ! OUTPUT=$(emerge-${FLAGS_board} -pe --backtrack=0 \
193 "${PACKAGES[@]}" 2>&1); then
Brian Harringcb782242011-12-13 19:48:44 -0800194 printf "%s\n" "${OUTPUT}"
Brian Harring7f175a52012-03-02 05:37:00 -0800195 die_notrace "emerge detected broken ebuilds. See error message above."
Brian Harringcb782242011-12-13 19:48:44 -0800196fi
197
Mike Frysinger4114c792012-09-13 22:33:12 -0400198# Build cros_workon packages when they are changed.
199CROS_WORKON_PKGS=()
200if [ "${FLAGS_noworkon}" -eq "${FLAGS_FALSE}" ]; then
201 LIST_MODIFIED_PACKAGES="${CHROMITE_BIN}/cros_list_modified_packages"
202 CROS_WORKON_PKGS+=( $("${LIST_MODIFIED_PACKAGES}" --board=${FLAGS_board}) )
Brian Harringcb782242011-12-13 19:48:44 -0800203fi
Mike Frysinger4114c792012-09-13 22:33:12 -0400204
205# TODO(anush): Make chrome a fake cros-workon package.
206if [[ -n "${CHROME_ORIGIN}" ]]; then
207 CROS_WORKON_PKGS+=( chromeos-base/chromeos-chrome )
208fi
209
210if [[ ${#CROS_WORKON_PKGS[@]} -gt 0 ]]; then
211 EMERGE_FLAGS+=(
212 --reinstall-atoms="${CROS_WORKON_PKGS[*]}"
213 --usepkg-exclude="${CROS_WORKON_PKGS[*]}"
214 )
David James710a7d12011-12-21 15:57:02 -0800215fi
Matt Tennant298f61a2012-06-25 21:54:33 -0700216
217# Prepare tmp file to capture emerge output from tee.
218tmpfile=$(mktemp -t tmp.build_packages-emerge.XXXXXX)
219trap "rm -f '${tmpfile}'" EXIT
220
Mike Frysinger76452ba2012-09-13 22:45:34 -0400221info "Merging board packages now"
Matt Tennant298f61a2012-06-25 21:54:33 -0700222(
223 set -o pipefail
Mike Frysinger4114c792012-09-13 22:33:12 -0400224 sudo -E "${EMERGE_CMD[@]}" "${EMERGE_FLAGS[@]}" "${PACKAGES[@]}" | \
Matt Tennant298f61a2012-06-25 21:54:33 -0700225 tee "${tmpfile}"
226)
227
228# Extract total package count from emerge output.
229package_count=$(awk '$0 ~ /^Total: [0-9]+ packages/ { print $2 }' "${tmpfile}")
230rm "${tmpfile}"
231trap - EXIT
Brian Harringcb782242011-12-13 19:48:44 -0800232
233echo "Builds complete"
Matt Tennant298f61a2012-06-25 21:54:33 -0700234EXTRA_COMMAND_STATS[package_count]=${package_count}
235command_completed
Brian Harringcb782242011-12-13 19:48:44 -0800236echo "Done"