blob: 662380df7193ce577bb154357697b271d53b83d4 [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
David James4c29c242012-06-06 20:45:18 -070083CHROMITE_BIN="${GCLIENT_ROOT}/chromite/bin"
Mike Frysinger4114c792012-09-13 22:33:12 -040084
85# Before we can run any tools, we need to update chroot or setup_board.
86UPDATE_ARGS=()
87if [ "${FLAGS_fast}" -eq "${FLAGS_TRUE}" ]; then
88 UPDATE_ARGS+=( --fast )
89else
90 UPDATE_ARGS+=( --nofast )
91fi
92if [ "${FLAGS_usepkg}" -eq "${FLAGS_TRUE}" ]; then
93 UPDATE_ARGS+=( --usepkg )
94else
95 UPDATE_ARGS+=( --nousepkg )
96fi
97if [[ "${FLAGS_jobs}" -ne -1 ]]; then
98 UPDATE_ARGS+=( --jobs=${FLAGS_jobs} )
99fi
100if [ "${FLAGS_reuse_pkgs_from_local_boards}" -eq "${FLAGS_TRUE}" ]; then
101 UPDATE_ARGS+=( --reuse_pkgs_from_local_boards )
102fi
103if [ "${FLAGS_skip_toolchain_update}" -eq "${FLAGS_TRUE}" ]; then
104 UPDATE_ARGS+=( --skip_toolchain_update )
105fi
106if [ "${FLAGS_skip_chroot_upgrade}" -eq "${FLAGS_TRUE}" ]; then
107 UPDATE_ARGS+=( --skip_chroot_upgrade )
108fi
109
110"${SCRIPTS_DIR}"/setup_board --quiet --board=${FLAGS_board} "${UPDATE_ARGS[@]}"
111
112# Setup all the emerge command/flags.
113EMERGE_FLAGS=( -uDNv --backtrack=30 --select )
114
Brian Harringcb782242011-12-13 19:48:44 -0800115if [[ "${FLAGS_fast}" -eq "${FLAGS_TRUE}" ]]; then
Mike Frysinger4114c792012-09-13 22:33:12 -0400116 EMERGE_CMD=(
117 "${CHROMITE_BIN}/parallel_emerge"
118 --board=${FLAGS_board}
119 )
120else
121 EMERGE_CMD=( "emerge-${FLAGS_board}" )
Brian Harringcb782242011-12-13 19:48:44 -0800122fi
Mike Frysinger4114c792012-09-13 22:33:12 -0400123EMERGE_CMD+=( ${EXTRA_BOARD_FLAGS} )
Brian Harringcb782242011-12-13 19:48:44 -0800124
David James17c622a2012-03-07 09:34:08 -0800125if [[ "${FLAGS_usepkg}" -eq "${FLAGS_TRUE}" ||
126 "${FLAGS_reuse_pkgs_from_local_boards}" -eq "${FLAGS_TRUE}" ]]; then
Brian Harringcb782242011-12-13 19:48:44 -0800127 # Use binary packages. Include all build-time dependencies,
128 # so as to avoid unnecessary differences between source
129 # and binary builds.
Mike Frysinger4114c792012-09-13 22:33:12 -0400130 EMERGE_FLAGS+=( --getbinpkg --usepkg --with-bdeps y )
Brian Harringcb782242011-12-13 19:48:44 -0800131fi
132
133if [[ "${FLAGS_jobs}" -ne -1 ]]; then
Mike Frysinger4114c792012-09-13 22:33:12 -0400134 EMERGE_FLAGS+=( --jobs=${FLAGS_jobs} )
135fi
136
137if [[ "${FLAGS_norebuild}" -eq "${FLAGS_FALSE}" ]]; then
138 EMERGE_FLAGS+=( --rebuild-if-unbuilt )
139fi
140if [[ "${FLAGS_showoutput}" -eq "${FLAGS_TRUE}" && \
141 "${FLAGS_fast}" -eq "${FLAGS_TRUE}" ]]; then
142 # Only parallel_emerge supports --show-output.
143 EMERGE_FLAGS+=( --show-output )
Brian Harringcb782242011-12-13 19:48:44 -0800144fi
145
146if [[ "${FLAGS_withdebug}" -eq "${FLAGS_FALSE}" ]]; then
147 export USE="${USE} -cros-debug"
148fi
149
Mike Frysinger4114c792012-09-13 22:33:12 -0400150# Figure out which packages we should be building.
151PACKAGES=( chromeos-base/chromeos )
Brian Harringcb782242011-12-13 19:48:44 -0800152if [[ "${FLAGS_withdev}" -eq "${FLAGS_TRUE}" ]]; then
Mike Frysinger4114c792012-09-13 22:33:12 -0400153 PACKAGES+=( chromeos-base/chromeos-dev )
Brian Harringcb782242011-12-13 19:48:44 -0800154fi
155if [[ "${FLAGS_withfactory}" -eq "${FLAGS_TRUE}" ]]; then
Mike Frysinger4114c792012-09-13 22:33:12 -0400156 PACKAGES+=( chromeos-base/chromeos-factory )
157 PACKAGES+=( chromeos-base/chromeos-factoryinstall )
158 PACKAGES+=( chromeos-base/factorytest-init )
159 PACKAGES+=( chromeos-base/chromeos-hwid )
Brian Harringcb782242011-12-13 19:48:44 -0800160fi
161if [[ "${FLAGS_withtest}" -eq "${FLAGS_TRUE}" ]]; then
Mike Frysinger4114c792012-09-13 22:33:12 -0400162 PACKAGES+=( chromeos-base/chromeos-test )
Brian Harringcb782242011-12-13 19:48:44 -0800163fi
164if [[ "${FLAGS_withautotest}" -eq "${FLAGS_TRUE}" ]]; then
Mike Frysinger4114c792012-09-13 22:33:12 -0400165 PACKAGES+=( chromeos-base/autotest-all )
Brian Harringcb782242011-12-13 19:48:44 -0800166fi
167
168# Verify that all packages can be emerged from scratch, without any
169# backtracking. Only print the output if this step fails.
170if ! OUTPUT=$(emerge-${FLAGS_board} -pe --backtrack=0 ${PACKAGES} 2>&1); then
171 printf "%s\n" "${OUTPUT}"
Brian Harring7f175a52012-03-02 05:37:00 -0800172 die_notrace "emerge detected broken ebuilds. See error message above."
Brian Harringcb782242011-12-13 19:48:44 -0800173fi
174
Mike Frysinger4114c792012-09-13 22:33:12 -0400175# Build cros_workon packages when they are changed.
176CROS_WORKON_PKGS=()
177if [ "${FLAGS_noworkon}" -eq "${FLAGS_FALSE}" ]; then
178 LIST_MODIFIED_PACKAGES="${CHROMITE_BIN}/cros_list_modified_packages"
179 CROS_WORKON_PKGS+=( $("${LIST_MODIFIED_PACKAGES}" --board=${FLAGS_board}) )
Brian Harringcb782242011-12-13 19:48:44 -0800180fi
Mike Frysinger4114c792012-09-13 22:33:12 -0400181
182# TODO(anush): Make chrome a fake cros-workon package.
183if [[ -n "${CHROME_ORIGIN}" ]]; then
184 CROS_WORKON_PKGS+=( chromeos-base/chromeos-chrome )
185fi
186
187if [[ ${#CROS_WORKON_PKGS[@]} -gt 0 ]]; then
188 EMERGE_FLAGS+=(
189 --reinstall-atoms="${CROS_WORKON_PKGS[*]}"
190 --usepkg-exclude="${CROS_WORKON_PKGS[*]}"
191 )
David James710a7d12011-12-21 15:57:02 -0800192fi
Matt Tennant298f61a2012-06-25 21:54:33 -0700193
194# Prepare tmp file to capture emerge output from tee.
195tmpfile=$(mktemp -t tmp.build_packages-emerge.XXXXXX)
196trap "rm -f '${tmpfile}'" EXIT
197
Mike Frysinger4114c792012-09-13 22:33:12 -0400198info "Merging board packages ${PACKAGES[*]}"
Matt Tennant298f61a2012-06-25 21:54:33 -0700199(
200 set -o pipefail
Mike Frysinger4114c792012-09-13 22:33:12 -0400201 sudo -E "${EMERGE_CMD[@]}" "${EMERGE_FLAGS[@]}" "${PACKAGES[@]}" | \
Matt Tennant298f61a2012-06-25 21:54:33 -0700202 tee "${tmpfile}"
203)
204
205# Extract total package count from emerge output.
206package_count=$(awk '$0 ~ /^Total: [0-9]+ packages/ { print $2 }' "${tmpfile}")
207rm "${tmpfile}"
208trap - EXIT
Brian Harringcb782242011-12-13 19:48:44 -0800209
210echo "Builds complete"
Matt Tennant298f61a2012-06-25 21:54:33 -0700211EXTRA_COMMAND_STATS[package_count]=${package_count}
212command_completed
Brian Harringcb782242011-12-13 19:48:44 -0800213echo "Done"