blob: ebc821a872cf1d1d9328b06493ef9e51cda0cd35 [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."
Thiago Goncales5ad4ca02013-07-12 10:55:11 -070019DEFINE_boolean usepkgonly "${FLAGS_FALSE}" \
20 "Only use binary packages to bootstrap; abort if any are missing."
Brian Harringcb782242011-12-13 19:48:44 -080021DEFINE_boolean noworkon "${FLAGS_FALSE}" \
22 "Don't force-build workon packages."
David James855afb72012-03-14 20:04:59 -070023DEFINE_boolean showoutput "${FLAGS_FALSE}" \
24 "Show all output from parallel_emerge."
25DEFINE_boolean withautotest "${FLAGS_TRUE}" \
26 "Build autotest client code."
Brian Harring17c1fe82012-12-05 21:12:31 -080027DEFINE_boolean fetchonly "${FLAGS_FALSE}" \
28 "Don't build anything, instead only fetch what is needed."
Thiago Goncales5ad4ca02013-07-12 10:55:11 -070029DEFINE_boolean unpackonly "${FLAGS_FALSE}" \
30 "Don't build anything; instead only fetch and unpack what is needed."
Bertrand SIMONNET2f6aaf22015-02-09 15:49:05 -080031DEFINE_boolean withdebugsymbols "${FLAGS_FALSE}" \
Bertrand SIMONNETc7a46e22014-12-11 15:58:11 -080032 "Install the debug symbols for all packages"
Thiago Goncales5ad4ca02013-07-12 10:55:11 -070033
34# The --board_root flag specifies the environment variables ROOT and PKGDIR.
35# This allows fetching and emerging of all packages to specified board_root.
36# Note that --board_root will setup the board normally in /build/$BOARD, if it's
37# not setup yet. It also expects the toolchain to already be installed in the
38# board_root. --usepkgonly and --norebuild are required, because building is not
39# supported when board_root is set.
40# enforce this)."
41DEFINE_string board_root "" \
42 "Emerge packages to board_root."
David James855afb72012-03-14 20:04:59 -070043
Mike Frysinger76452ba2012-09-13 22:45:34 -040044FLAGS_HELP="usage: $(basename $0) [flags] [packages]
David James855afb72012-03-14 20:04:59 -070045
46build_packages updates the set of binary packages needed by Chrome OS. It will
47cross compile all packages that have been updated into the given target's root
48and build binary packages as a side-effect. The output packages will be picked
49up by the build_image script to put together a bootable Chrome OS image.
50
Mike Frysinger76452ba2012-09-13 22:45:34 -040051If [packages] are specified, only build those specific packages (and any
52dependencies they might need).
53
David James855afb72012-03-14 20:04:59 -070054For the fastest builds, use --nowithautotest --noworkon.
55"
56show_help_if_requested "$@"
57
58# The following options are advanced options, only available to those willing
59# to read the source code. They are not shown in help output, since they are
60# not needed for the typical developer workflow.
Ryan Cuieb4595f2012-12-17 14:28:42 -080061DEFINE_string accept_licenses "" \
62 "Licenses to append to the accept list."
David James855afb72012-03-14 20:04:59 -070063DEFINE_boolean fast "${DEFAULT_FAST}" \
64 "Call many emerges in parallel."
65DEFINE_integer jobs -1 \
66 "How many packages to build in parallel at maximum."
67DEFINE_boolean norebuild "${FLAGS_FALSE}" \
68 "Don't automatically rebuild dependencies."
Mike Frysinger839e82a2012-03-01 14:22:10 -050069DEFINE_boolean skip_chroot_upgrade "${FLAGS_FALSE}" \
70 "Don't run the chroot upgrade automatically; use with care."
David James855afb72012-03-14 20:04:59 -070071DEFINE_boolean skip_toolchain_update "${FLAGS_FALSE}" \
72 "Don't update toolchain automatically."
73DEFINE_boolean withdev "${FLAGS_TRUE}" \
74 "Build useful developer friendly utilities."
75DEFINE_boolean withdebug "${FLAGS_TRUE}" \
76 "Build debug versions of Chromium-OS-specific packages."
77DEFINE_boolean withfactory "${FLAGS_TRUE}" \
78 "Build factory installer."
79DEFINE_boolean withtest "${FLAGS_TRUE}" \
80 "Build packages required for testing."
David James0ae653d2013-07-03 15:59:29 -070081DEFINE_boolean chrome_binhost_only $FLAGS_FALSE \
82 "Only fetch packages from the Chrome binhost."
Brian Harringcb782242011-12-13 19:48:44 -080083
David James17c622a2012-03-07 09:34:08 -080084# The --reuse_pkgs_from_local_boards flag tells Portage to share binary
85# packages between boards that are built locally, so that the total time
86# required to build several boards is reduced. This flag is only useful
87# when you are not able to use remote binary packages, since remote binary
88# packages are usually more up to date than anything you have locally.
89DEFINE_boolean reuse_pkgs_from_local_boards $FLAGS_FALSE \
90 "Bootstrap from local packages instead of remote packages."
91
Brian Harringcb782242011-12-13 19:48:44 -080092# Parse command line
Brian Harringcb782242011-12-13 19:48:44 -080093FLAGS "$@" || exit 1
94eval set -- "${FLAGS_ARGV}"
Brian Harringcb782242011-12-13 19:48:44 -080095
96# Die on any errors.
Brian Harring7f175a52012-03-02 05:37:00 -080097switch_to_strict_mode
Brian Harringcb782242011-12-13 19:48:44 -080098
99# Right now build_packages has to be run from scripts/
100. ${SRC_ROOT}/third_party/chromiumos-overlay/chromeos/config/chromeos_version.sh
101
102if [[ -z "${FLAGS_board}" ]]; then
103 echo "Error: --board is required."
104 exit 1
105fi
106
David James4c29c242012-06-06 20:45:18 -0700107CHROMITE_BIN="${GCLIENT_ROOT}/chromite/bin"
Mike Frysinger4114c792012-09-13 22:33:12 -0400108
109# Before we can run any tools, we need to update chroot or setup_board.
110UPDATE_ARGS=()
Ryan Cuieb4595f2012-12-17 14:28:42 -0800111if [[ -n ${FLAGS_accept_licenses} ]]; then
112 UPDATE_ARGS+=( --accept_licenses "${FLAGS_accept_licenses}" )
113fi
Mike Frysinger4114c792012-09-13 22:33:12 -0400114if [ "${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
124if [[ "${FLAGS_jobs}" -ne -1 ]]; then
125 UPDATE_ARGS+=( --jobs=${FLAGS_jobs} )
126fi
David James0ae653d2013-07-03 15:59:29 -0700127if [ "${FLAGS_chrome_binhost_only}" -eq "${FLAGS_TRUE}" ]; then
128 UPDATE_ARGS+=( --chrome_binhost_only )
129fi
Mike Frysinger4114c792012-09-13 22:33:12 -0400130if [ "${FLAGS_reuse_pkgs_from_local_boards}" -eq "${FLAGS_TRUE}" ]; then
131 UPDATE_ARGS+=( --reuse_pkgs_from_local_boards )
132fi
133if [ "${FLAGS_skip_toolchain_update}" -eq "${FLAGS_TRUE}" ]; then
134 UPDATE_ARGS+=( --skip_toolchain_update )
135fi
136if [ "${FLAGS_skip_chroot_upgrade}" -eq "${FLAGS_TRUE}" ]; then
137 UPDATE_ARGS+=( --skip_chroot_upgrade )
138fi
Thiago Goncales5ad4ca02013-07-12 10:55:11 -0700139if [[ ${FLAGS_unpackonly} -eq ${FLAGS_TRUE} ]]; then
140 UPDATE_ARGS+=( --skip_board_pkg_init )
141fi
Bertrand SIMONNET10ede912014-10-09 16:21:13 -0700142if [[ -n ${FLAGS_board_root} ]]; then
143 UPDATE_ARGS+=( --board_root "${FLAGS_board_root}" )
144fi
Mike Frysinger4114c792012-09-13 22:33:12 -0400145
146"${SCRIPTS_DIR}"/setup_board --quiet --board=${FLAGS_board} "${UPDATE_ARGS[@]}"
147
Chris Masonebbccc242014-02-08 16:23:53 -0800148sudo_clear_shadow_locks "/build/${FLAGS_board}"
149
Mike Frysinger4114c792012-09-13 22:33:12 -0400150# Setup all the emerge command/flags.
David James886f2c32014-02-26 20:38:04 -0800151EMERGE_FLAGS=( -uDNv --backtrack=30 --select --newrepo )
Mike Frysinger4114c792012-09-13 22:33:12 -0400152
Brian Harringcb782242011-12-13 19:48:44 -0800153if [[ "${FLAGS_fast}" -eq "${FLAGS_TRUE}" ]]; then
Mike Frysinger4114c792012-09-13 22:33:12 -0400154 EMERGE_CMD=(
155 "${CHROMITE_BIN}/parallel_emerge"
156 --board=${FLAGS_board}
157 )
158else
159 EMERGE_CMD=( "emerge-${FLAGS_board}" )
Brian Harringcb782242011-12-13 19:48:44 -0800160fi
Brian Harring3d01db22012-12-15 21:09:03 -0800161if [[ "${FLAGS_fetchonly}" -eq "${FLAGS_TRUE}" ]]; then
Brian Harring17c1fe82012-12-05 21:12:31 -0800162 EMERGE_CMD+=( --fetchonly )
163fi
Thiago Goncales5ad4ca02013-07-12 10:55:11 -0700164if [[ ${FLAGS_unpackonly} -eq ${FLAGS_TRUE} ]]; then
165 EMERGE_CMD+=( --unpackonly )
166fi
Brian Harring17c1fe82012-12-05 21:12:31 -0800167
Mike Frysinger4114c792012-09-13 22:33:12 -0400168EMERGE_CMD+=( ${EXTRA_BOARD_FLAGS} )
Brian Harringcb782242011-12-13 19:48:44 -0800169
David James17c622a2012-03-07 09:34:08 -0800170if [[ "${FLAGS_usepkg}" -eq "${FLAGS_TRUE}" ||
Thiago Goncales5ad4ca02013-07-12 10:55:11 -0700171 "${FLAGS_reuse_pkgs_from_local_boards}" -eq "${FLAGS_TRUE}" ||
172 "${FLAGS_usepkgonly}" -eq "${FLAGS_TRUE}" ]]; then
Brian Harringcb782242011-12-13 19:48:44 -0800173 # Use binary packages. Include all build-time dependencies,
174 # so as to avoid unnecessary differences between source
175 # and binary builds.
Thiago Goncales5ad4ca02013-07-12 10:55:11 -0700176 EMERGE_FLAGS+=( --getbinpkg --with-bdeps y )
177 if [[ ${FLAGS_usepkgonly} -eq ${FLAGS_TRUE} ]]; then
178 EMERGE_FLAGS+=( --usepkgonly )
179 else
180 EMERGE_FLAGS+=( --usepkg )
181 fi
Brian Harringcb782242011-12-13 19:48:44 -0800182fi
183
184if [[ "${FLAGS_jobs}" -ne -1 ]]; then
Mike Frysinger4114c792012-09-13 22:33:12 -0400185 EMERGE_FLAGS+=( --jobs=${FLAGS_jobs} )
186fi
187
188if [[ "${FLAGS_norebuild}" -eq "${FLAGS_FALSE}" ]]; then
189 EMERGE_FLAGS+=( --rebuild-if-unbuilt )
190fi
191if [[ "${FLAGS_showoutput}" -eq "${FLAGS_TRUE}" && \
192 "${FLAGS_fast}" -eq "${FLAGS_TRUE}" ]]; then
193 # Only parallel_emerge supports --show-output.
194 EMERGE_FLAGS+=( --show-output )
Brian Harringcb782242011-12-13 19:48:44 -0800195fi
196
197if [[ "${FLAGS_withdebug}" -eq "${FLAGS_FALSE}" ]]; then
198 export USE="${USE} -cros-debug"
199fi
200
Mike Frysinger4114c792012-09-13 22:33:12 -0400201# Figure out which packages we should be building.
Mike Frysinger76452ba2012-09-13 22:45:34 -0400202PACKAGES=( "$@" )
203if [[ $# -eq 0 ]]; then
Mike Frysinger22621952014-04-01 17:41:20 -0400204 PACKAGES=( virtual/target-os )
Mike Frysinger76452ba2012-09-13 22:45:34 -0400205 if [[ "${FLAGS_withdev}" -eq "${FLAGS_TRUE}" ]]; then
Mike Frysingerde399282014-04-15 18:26:04 -0400206 PACKAGES+=( virtual/target-os-dev )
Mike Frysinger76452ba2012-09-13 22:45:34 -0400207 fi
208 if [[ "${FLAGS_withfactory}" -eq "${FLAGS_TRUE}" ]]; then
Vic Yang8e0cd072012-12-18 22:31:46 +0800209 PACKAGES+=( chromeos-base/chromeos-installshim )
Mike Frysinger76452ba2012-09-13 22:45:34 -0400210 PACKAGES+=( chromeos-base/chromeos-factory )
Mike Frysinger76452ba2012-09-13 22:45:34 -0400211 PACKAGES+=( chromeos-base/chromeos-hwid )
Christopher Wileyca943ac2013-11-13 11:22:54 -0800212 PACKAGES+=( chromeos-base/autotest-factory-install )
Mike Frysinger76452ba2012-09-13 22:45:34 -0400213 fi
214 if [[ "${FLAGS_withtest}" -eq "${FLAGS_TRUE}" ]]; then
Mike Frysinger52669432014-05-05 14:34:09 -0400215 PACKAGES+=( virtual/target-os-test )
Mike Frysinger76452ba2012-09-13 22:45:34 -0400216 fi
217 if [[ "${FLAGS_withautotest}" -eq "${FLAGS_TRUE}" ]]; then
218 PACKAGES+=( chromeos-base/autotest-all )
219 fi
Brian Harringcb782242011-12-13 19:48:44 -0800220fi
221
222# Verify that all packages can be emerged from scratch, without any
223# backtracking. Only print the output if this step fails.
Mike Frysinger76452ba2012-09-13 22:45:34 -0400224info "Checking package dependencies are correct: ${PACKAGES[*]}"
David Jamesab9ca212012-11-06 11:06:07 -0800225if ! OUTPUT=$(emerge-${FLAGS_board} -pe --backtrack=0 \
226 "${PACKAGES[@]}" 2>&1); then
Brian Harringcb782242011-12-13 19:48:44 -0800227 printf "%s\n" "${OUTPUT}"
Brian Harring7f175a52012-03-02 05:37:00 -0800228 die_notrace "emerge detected broken ebuilds. See error message above."
Brian Harringcb782242011-12-13 19:48:44 -0800229fi
230
Mike Frysinger4114c792012-09-13 22:33:12 -0400231# Build cros_workon packages when they are changed.
232CROS_WORKON_PKGS=()
233if [ "${FLAGS_noworkon}" -eq "${FLAGS_FALSE}" ]; then
234 LIST_MODIFIED_PACKAGES="${CHROMITE_BIN}/cros_list_modified_packages"
235 CROS_WORKON_PKGS+=( $("${LIST_MODIFIED_PACKAGES}" --board=${FLAGS_board}) )
Mike Frysinger4114c792012-09-13 22:33:12 -0400236
David James4941c6b2014-02-03 16:24:59 -0800237 # TODO(anush): Make chrome a fake cros-workon package.
238 if [[ -n "${CHROME_ORIGIN}" ]]; then
239 CROS_WORKON_PKGS+=( chromeos-base/chromeos-chrome )
240 fi
Mike Frysinger4114c792012-09-13 22:33:12 -0400241fi
242
243if [[ ${#CROS_WORKON_PKGS[@]} -gt 0 ]]; then
244 EMERGE_FLAGS+=(
245 --reinstall-atoms="${CROS_WORKON_PKGS[*]}"
246 --usepkg-exclude="${CROS_WORKON_PKGS[*]}"
247 )
David James710a7d12011-12-21 15:57:02 -0800248fi
Matt Tennant298f61a2012-06-25 21:54:33 -0700249
250# Prepare tmp file to capture emerge output from tee.
251tmpfile=$(mktemp -t tmp.build_packages-emerge.XXXXXX)
252trap "rm -f '${tmpfile}'" EXIT
253
Thiago Goncales5ad4ca02013-07-12 10:55:11 -0700254if [[ -n "${FLAGS_board_root}" ]]; then
255 export ROOT="${FLAGS_board_root}"
Bertrand SIMONNET10ede912014-10-09 16:21:13 -0700256 export PORTAGE_CONFIGROOT="${ROOT}"
257 export SYSROOT="${ROOT}"
Thiago Goncales5ad4ca02013-07-12 10:55:11 -0700258 export PKGDIR="${ROOT}"/packages
259fi
260
Mike Frysinger76452ba2012-09-13 22:45:34 -0400261info "Merging board packages now"
Matt Tennant298f61a2012-06-25 21:54:33 -0700262(
263 set -o pipefail
Mike Frysinger4114c792012-09-13 22:33:12 -0400264 sudo -E "${EMERGE_CMD[@]}" "${EMERGE_FLAGS[@]}" "${PACKAGES[@]}" | \
Matt Tennant298f61a2012-06-25 21:54:33 -0700265 tee "${tmpfile}"
266)
267
268# Extract total package count from emerge output.
269package_count=$(awk '$0 ~ /^Total: [0-9]+ packages/ { print $2 }' "${tmpfile}")
270rm "${tmpfile}"
271trap - EXIT
Brian Harringcb782242011-12-13 19:48:44 -0800272
273echo "Builds complete"
Bertrand SIMONNETc7a46e22014-12-11 15:58:11 -0800274
275if [[ ${FLAGS_withdebugsymbols} -eq ${FLAGS_TRUE} ]]; then
276 info "fetching the debug symbols"
277 sudo -E "${GCLIENT_ROOT}/chromite/bin/cros_install_debug_syms" \
278 "--board=${FLAGS_board}" "--all"
279fi
280
Matt Tennant298f61a2012-06-25 21:54:33 -0700281EXTRA_COMMAND_STATS[package_count]=${package_count}
282command_completed
Brian Harringcb782242011-12-13 19:48:44 -0800283echo "Done"