blob: d55ac9270168d7cf5285ab0920ac987700e3e20b [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."
25
26FLAGS_HELP="usage: $(basename $0) [flags]
27
28build_packages updates the set of binary packages needed by Chrome OS. It will
29cross compile all packages that have been updated into the given target's root
30and build binary packages as a side-effect. The output packages will be picked
31up by the build_image script to put together a bootable Chrome OS image.
32
33For the fastest builds, use --nowithautotest --noworkon.
34"
35show_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.
40DEFINE_boolean fast "${DEFAULT_FAST}" \
41 "Call many emerges in parallel."
42DEFINE_integer jobs -1 \
43 "How many packages to build in parallel at maximum."
44DEFINE_boolean norebuild "${FLAGS_FALSE}" \
45 "Don't automatically rebuild dependencies."
Mike Frysinger839e82a2012-03-01 14:22:10 -050046DEFINE_boolean skip_chroot_upgrade "${FLAGS_FALSE}" \
47 "Don't run the chroot upgrade automatically; use with care."
David James855afb72012-03-14 20:04:59 -070048DEFINE_boolean skip_toolchain_update "${FLAGS_FALSE}" \
49 "Don't update toolchain automatically."
50DEFINE_boolean withdev "${FLAGS_TRUE}" \
51 "Build useful developer friendly utilities."
52DEFINE_boolean withdebug "${FLAGS_TRUE}" \
53 "Build debug versions of Chromium-OS-specific packages."
54DEFINE_boolean withfactory "${FLAGS_TRUE}" \
55 "Build factory installer."
56DEFINE_boolean withtest "${FLAGS_TRUE}" \
57 "Build packages required for testing."
Brian Harringcb782242011-12-13 19:48:44 -080058
David James17c622a2012-03-07 09:34:08 -080059# 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.
64DEFINE_boolean reuse_pkgs_from_local_boards $FLAGS_FALSE \
65 "Bootstrap from local packages instead of remote packages."
66
Brian Harringcb782242011-12-13 19:48:44 -080067# Parse command line
Brian Harringcb782242011-12-13 19:48:44 -080068FLAGS "$@" || exit 1
69eval set -- "${FLAGS_ARGV}"
70check_flags_only_and_allow_null_arg "$@" && set --
71
72# Die on any errors.
Brian Harring7f175a52012-03-02 05:37:00 -080073switch_to_strict_mode
Brian Harringcb782242011-12-13 19:48:44 -080074
75# Right now build_packages has to be run from scripts/
76. ${SRC_ROOT}/third_party/chromiumos-overlay/chromeos/config/chromeos_version.sh
77
78if [[ -z "${FLAGS_board}" ]]; then
79 echo "Error: --board is required."
80 exit 1
81fi
82
Mike Frysinger650bf872012-02-27 11:05:26 -050083EMERGE_FLAGS="--backtrack=30 --select"
Brian Harringcb782242011-12-13 19:48:44 -080084
85EMERGE_CMD="emerge"
86EMERGE_BOARD_CMD="emerge-${FLAGS_board}"
David James4c29c242012-06-06 20:45:18 -070087CHROMITE_BIN="${GCLIENT_ROOT}/chromite/bin"
Brian Harringcb782242011-12-13 19:48:44 -080088if [[ "${FLAGS_fast}" -eq "${FLAGS_TRUE}" ]]; then
David James4c29c242012-06-06 20:45:18 -070089 EMERGE_CMD="${CHROMITE_BIN}/parallel_emerge"
Brian Harringcb782242011-12-13 19:48:44 -080090 EMERGE_BOARD_CMD="${EMERGE_CMD} --board=${FLAGS_board}"
91fi
92if [[ -n "${EXTRA_BOARD_FLAGS}" ]]; then
93 EMERGE_BOARD_CMD="${EMERGE_BOARD_CMD} ${EXTRA_BOARD_FLAGS}"
94fi
95
David James17c622a2012-03-07 09:34:08 -080096if [[ "${FLAGS_usepkg}" -eq "${FLAGS_TRUE}" ||
97 "${FLAGS_reuse_pkgs_from_local_boards}" -eq "${FLAGS_TRUE}" ]]; then
Brian Harringcb782242011-12-13 19:48:44 -080098 # 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"
102fi
103
104if [[ "${FLAGS_jobs}" -ne -1 ]]; then
David James184e3902012-02-23 20:19:28 -0800105 EMERGE_FLAGS+=" --jobs=${FLAGS_jobs}"
Brian Harringcb782242011-12-13 19:48:44 -0800106fi
107
108if [[ "${FLAGS_withdebug}" -eq "${FLAGS_FALSE}" ]]; then
109 export USE="${USE} -cros-debug"
110fi
111
Brian Harringcb782242011-12-13 19:48:44 -0800112# Before we can run any tools, we need to update chroot or setup_board.
113UPDATE_ARGS=""
114if [ "${FLAGS_fast}" -eq "${FLAGS_TRUE}" ]; then
115 UPDATE_ARGS+=" --fast"
116else
117 UPDATE_ARGS+=" --nofast"
118fi
119if [ "${FLAGS_usepkg}" -eq "${FLAGS_TRUE}" ]; then
120 UPDATE_ARGS+=" --usepkg"
121else
122 UPDATE_ARGS+=" --nousepkg"
123fi
David James184e3902012-02-23 20:19:28 -0800124if [[ "${FLAGS_jobs}" -ne -1 ]]; then
125 UPDATE_ARGS+=" --jobs=${FLAGS_jobs}"
126fi
David James17c622a2012-03-07 09:34:08 -0800127if [ "${FLAGS_reuse_pkgs_from_local_boards}" -eq "${FLAGS_TRUE}" ]; then
128 UPDATE_ARGS+=" --reuse_pkgs_from_local_boards"
129fi
Brian Harringcb782242011-12-13 19:48:44 -0800130if [ "${FLAGS_skip_toolchain_update}" -eq "${FLAGS_TRUE}" ]; then
131 UPDATE_ARGS+=" --skip_toolchain_update"
132fi
Mike Frysinger839e82a2012-03-01 14:22:10 -0500133if [ "${FLAGS_skip_chroot_upgrade}" -eq "${FLAGS_TRUE}" ]; then
134 UPDATE_ARGS+=" --skip_chroot_upgrade"
135fi
136
Brian Harringcb782242011-12-13 19:48:44 -0800137${SCRIPTS_DIR}/setup_board --quiet --board=${FLAGS_board} ${UPDATE_ARGS}
138
139if [ "${FLAGS_noworkon}" -eq "${FLAGS_FALSE}" ]; then
David James4c29c242012-06-06 20:45:18 -0700140 # 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 Harringcb782242011-12-13 19:48:44 -0800143fi
144
145# TODO(anush): Make chrome a fake cros-workon package.
146if [[ -n "${CHROME_ORIGIN}" ]]; then
147 CROS_WORKON_PKGS="${CROS_WORKON_PKGS} chromeos-base/chromeos-chrome"
148fi
149
150PACKAGES="chromeos-base/chromeos"
151if [[ "${FLAGS_withdev}" -eq "${FLAGS_TRUE}" ]]; then
152 PACKAGES="${PACKAGES} chromeos-base/chromeos-dev"
153fi
154if [[ "${FLAGS_withfactory}" -eq "${FLAGS_TRUE}" ]]; then
Liam McLoughlineb7ccc32012-08-22 14:41:23 -0700155 PACKAGES="${PACKAGES} chromeos-base/chromeos-factory"
Brian Harringcb782242011-12-13 19:48:44 -0800156 PACKAGES="${PACKAGES} chromeos-base/chromeos-factoryinstall"
157 PACKAGES="${PACKAGES} chromeos-base/factorytest-init"
158 PACKAGES="${PACKAGES} chromeos-base/chromeos-hwid"
159fi
160if [[ "${FLAGS_withtest}" -eq "${FLAGS_TRUE}" ]]; then
161 PACKAGES="${PACKAGES} chromeos-base/chromeos-test"
162fi
163if [[ "${FLAGS_withautotest}" -eq "${FLAGS_TRUE}" ]]; then
164 PACKAGES="${PACKAGES} chromeos-base/autotest-all"
165fi
166
167# Verify that all packages can be emerged from scratch, without any
168# backtracking. Only print the output if this step fails.
169if ! OUTPUT=$(emerge-${FLAGS_board} -pe --backtrack=0 ${PACKAGES} 2>&1); then
170 printf "%s\n" "${OUTPUT}"
Brian Harring7f175a52012-03-02 05:37:00 -0800171 die_notrace "emerge detected broken ebuilds. See error message above."
Brian Harringcb782242011-12-13 19:48:44 -0800172fi
173
David James710a7d12011-12-21 15:57:02 -0800174for pkg in ${CROS_WORKON_PKGS}; do
175 EMERGE_FLAGS+=" --reinstall-atoms=${pkg}"
176 EMERGE_FLAGS+=" --usepkg-exclude=${pkg}"
177done
178if [[ "${FLAGS_norebuild}" -eq "${FLAGS_FALSE}" ]]; then
179 EMERGE_FLAGS+=" --rebuild-if-unbuilt"
Brian Harringcb782242011-12-13 19:48:44 -0800180fi
David James710a7d12011-12-21 15:57:02 -0800181if [[ "${FLAGS_showoutput}" -eq "${FLAGS_TRUE}" && \
182 "${FLAGS_fast}" -eq "${FLAGS_TRUE}" ]]; then
183 # Only parallel_emerge supports --show-output.
184 EMERGE_FLAGS+=" --show-output"
185fi
Matt Tennant298f61a2012-06-25 21:54:33 -0700186
187# Prepare tmp file to capture emerge output from tee.
188tmpfile=$(mktemp -t tmp.build_packages-emerge.XXXXXX)
189trap "rm -f '${tmpfile}'" EXIT
190
Mike Frysinger5011c7f2012-03-08 14:41:31 -0500191info "Merging board packages ${PACKAGES}"
Matt Tennant298f61a2012-06-25 21:54:33 -0700192(
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.
199package_count=$(awk '$0 ~ /^Total: [0-9]+ packages/ { print $2 }' "${tmpfile}")
200rm "${tmpfile}"
201trap - EXIT
Brian Harringcb782242011-12-13 19:48:44 -0800202
203echo "Builds complete"
Matt Tennant298f61a2012-06-25 21:54:33 -0700204EXTRA_COMMAND_STATS[package_count]=${package_count}
205command_completed
Brian Harringcb782242011-12-13 19:48:44 -0800206echo "Done"