blob: d21e8bac58e959026f0a3f56f71c35a7d9166bb1 [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_integer jobs -1 \
64 "How many packages to build in parallel at maximum."
65DEFINE_boolean norebuild "${FLAGS_FALSE}" \
66 "Don't automatically rebuild dependencies."
Mike Frysinger839e82a2012-03-01 14:22:10 -050067DEFINE_boolean skip_chroot_upgrade "${FLAGS_FALSE}" \
68 "Don't run the chroot upgrade automatically; use with care."
David James855afb72012-03-14 20:04:59 -070069DEFINE_boolean skip_toolchain_update "${FLAGS_FALSE}" \
70 "Don't update toolchain automatically."
71DEFINE_boolean withdev "${FLAGS_TRUE}" \
72 "Build useful developer friendly utilities."
73DEFINE_boolean withdebug "${FLAGS_TRUE}" \
74 "Build debug versions of Chromium-OS-specific packages."
75DEFINE_boolean withfactory "${FLAGS_TRUE}" \
76 "Build factory installer."
77DEFINE_boolean withtest "${FLAGS_TRUE}" \
78 "Build packages required for testing."
David James0ae653d2013-07-03 15:59:29 -070079DEFINE_boolean chrome_binhost_only $FLAGS_FALSE \
80 "Only fetch packages from the Chrome binhost."
Brian Harringcb782242011-12-13 19:48:44 -080081
David James17c622a2012-03-07 09:34:08 -080082# The --reuse_pkgs_from_local_boards flag tells Portage to share binary
83# packages between boards that are built locally, so that the total time
84# required to build several boards is reduced. This flag is only useful
85# when you are not able to use remote binary packages, since remote binary
86# packages are usually more up to date than anything you have locally.
87DEFINE_boolean reuse_pkgs_from_local_boards $FLAGS_FALSE \
88 "Bootstrap from local packages instead of remote packages."
89
Brian Harringcb782242011-12-13 19:48:44 -080090# Parse command line
Brian Harringcb782242011-12-13 19:48:44 -080091FLAGS "$@" || exit 1
92eval set -- "${FLAGS_ARGV}"
Brian Harringcb782242011-12-13 19:48:44 -080093
94# Die on any errors.
Brian Harring7f175a52012-03-02 05:37:00 -080095switch_to_strict_mode
Brian Harringcb782242011-12-13 19:48:44 -080096
97# Right now build_packages has to be run from scripts/
98. ${SRC_ROOT}/third_party/chromiumos-overlay/chromeos/config/chromeos_version.sh
99
100if [[ -z "${FLAGS_board}" ]]; then
101 echo "Error: --board is required."
102 exit 1
103fi
104
David James4c29c242012-06-06 20:45:18 -0700105CHROMITE_BIN="${GCLIENT_ROOT}/chromite/bin"
Mike Frysinger4114c792012-09-13 22:33:12 -0400106
107# Before we can run any tools, we need to update chroot or setup_board.
108UPDATE_ARGS=()
Ryan Cuieb4595f2012-12-17 14:28:42 -0800109if [[ -n ${FLAGS_accept_licenses} ]]; then
110 UPDATE_ARGS+=( --accept_licenses "${FLAGS_accept_licenses}" )
111fi
Mike Frysinger4114c792012-09-13 22:33:12 -0400112if [ "${FLAGS_usepkg}" -eq "${FLAGS_TRUE}" ]; then
113 UPDATE_ARGS+=( --usepkg )
114else
115 UPDATE_ARGS+=( --nousepkg )
116fi
117if [[ "${FLAGS_jobs}" -ne -1 ]]; then
118 UPDATE_ARGS+=( --jobs=${FLAGS_jobs} )
119fi
David James0ae653d2013-07-03 15:59:29 -0700120if [ "${FLAGS_chrome_binhost_only}" -eq "${FLAGS_TRUE}" ]; then
121 UPDATE_ARGS+=( --chrome_binhost_only )
122fi
Mike Frysinger4114c792012-09-13 22:33:12 -0400123if [ "${FLAGS_reuse_pkgs_from_local_boards}" -eq "${FLAGS_TRUE}" ]; then
124 UPDATE_ARGS+=( --reuse_pkgs_from_local_boards )
125fi
126if [ "${FLAGS_skip_toolchain_update}" -eq "${FLAGS_TRUE}" ]; then
127 UPDATE_ARGS+=( --skip_toolchain_update )
128fi
129if [ "${FLAGS_skip_chroot_upgrade}" -eq "${FLAGS_TRUE}" ]; then
130 UPDATE_ARGS+=( --skip_chroot_upgrade )
131fi
Thiago Goncales5ad4ca02013-07-12 10:55:11 -0700132if [[ ${FLAGS_unpackonly} -eq ${FLAGS_TRUE} ]]; then
133 UPDATE_ARGS+=( --skip_board_pkg_init )
134fi
Bertrand SIMONNET10ede912014-10-09 16:21:13 -0700135if [[ -n ${FLAGS_board_root} ]]; then
136 UPDATE_ARGS+=( --board_root "${FLAGS_board_root}" )
137fi
Mike Frysinger4114c792012-09-13 22:33:12 -0400138
139"${SCRIPTS_DIR}"/setup_board --quiet --board=${FLAGS_board} "${UPDATE_ARGS[@]}"
140
Chris Masonebbccc242014-02-08 16:23:53 -0800141sudo_clear_shadow_locks "/build/${FLAGS_board}"
142
Mike Frysinger4114c792012-09-13 22:33:12 -0400143# Setup all the emerge command/flags.
David James886f2c32014-02-26 20:38:04 -0800144EMERGE_FLAGS=( -uDNv --backtrack=30 --select --newrepo )
Mike Frysinger4114c792012-09-13 22:33:12 -0400145
Bertrand SIMONNET4dda4f52015-03-19 13:40:58 -0700146EMERGE_CMD=(
147 "${CHROMITE_BIN}/parallel_emerge"
148 --board=${FLAGS_board}
149)
150
Brian Harring3d01db22012-12-15 21:09:03 -0800151if [[ "${FLAGS_fetchonly}" -eq "${FLAGS_TRUE}" ]]; then
Brian Harring17c1fe82012-12-05 21:12:31 -0800152 EMERGE_CMD+=( --fetchonly )
153fi
Thiago Goncales5ad4ca02013-07-12 10:55:11 -0700154if [[ ${FLAGS_unpackonly} -eq ${FLAGS_TRUE} ]]; then
155 EMERGE_CMD+=( --unpackonly )
156fi
Brian Harring17c1fe82012-12-05 21:12:31 -0800157
Mike Frysinger4114c792012-09-13 22:33:12 -0400158EMERGE_CMD+=( ${EXTRA_BOARD_FLAGS} )
Brian Harringcb782242011-12-13 19:48:44 -0800159
David James17c622a2012-03-07 09:34:08 -0800160if [[ "${FLAGS_usepkg}" -eq "${FLAGS_TRUE}" ||
Thiago Goncales5ad4ca02013-07-12 10:55:11 -0700161 "${FLAGS_reuse_pkgs_from_local_boards}" -eq "${FLAGS_TRUE}" ||
162 "${FLAGS_usepkgonly}" -eq "${FLAGS_TRUE}" ]]; then
Brian Harringcb782242011-12-13 19:48:44 -0800163 # Use binary packages. Include all build-time dependencies,
164 # so as to avoid unnecessary differences between source
165 # and binary builds.
Thiago Goncales5ad4ca02013-07-12 10:55:11 -0700166 EMERGE_FLAGS+=( --getbinpkg --with-bdeps y )
167 if [[ ${FLAGS_usepkgonly} -eq ${FLAGS_TRUE} ]]; then
168 EMERGE_FLAGS+=( --usepkgonly )
169 else
170 EMERGE_FLAGS+=( --usepkg )
171 fi
Brian Harringcb782242011-12-13 19:48:44 -0800172fi
173
174if [[ "${FLAGS_jobs}" -ne -1 ]]; then
Mike Frysinger4114c792012-09-13 22:33:12 -0400175 EMERGE_FLAGS+=( --jobs=${FLAGS_jobs} )
176fi
177
178if [[ "${FLAGS_norebuild}" -eq "${FLAGS_FALSE}" ]]; then
179 EMERGE_FLAGS+=( --rebuild-if-unbuilt )
180fi
Bertrand SIMONNET4dda4f52015-03-19 13:40:58 -0700181if [[ "${FLAGS_showoutput}" -eq "${FLAGS_TRUE}" ]]; then
Mike Frysinger4114c792012-09-13 22:33:12 -0400182 EMERGE_FLAGS+=( --show-output )
Brian Harringcb782242011-12-13 19:48:44 -0800183fi
184
185if [[ "${FLAGS_withdebug}" -eq "${FLAGS_FALSE}" ]]; then
186 export USE="${USE} -cros-debug"
187fi
188
Mike Frysinger4114c792012-09-13 22:33:12 -0400189# Figure out which packages we should be building.
Mike Frysinger76452ba2012-09-13 22:45:34 -0400190PACKAGES=( "$@" )
Daniel Wangaf4d0012015-07-17 11:52:05 -0700191FORCE_LOCAL_BUILD_PKGS=()
Mike Frysinger76452ba2012-09-13 22:45:34 -0400192if [[ $# -eq 0 ]]; then
Mike Frysinger22621952014-04-01 17:41:20 -0400193 PACKAGES=( virtual/target-os )
Mike Frysinger76452ba2012-09-13 22:45:34 -0400194 if [[ "${FLAGS_withdev}" -eq "${FLAGS_TRUE}" ]]; then
Mike Frysingerde399282014-04-15 18:26:04 -0400195 PACKAGES+=( virtual/target-os-dev )
Mike Frysinger76452ba2012-09-13 22:45:34 -0400196 fi
197 if [[ "${FLAGS_withfactory}" -eq "${FLAGS_TRUE}" ]]; then
Hung-Te Lin9b43ced2016-01-14 20:29:47 +0800198 PACKAGES+=( virtual/target-os-factory )
199 PACKAGES+=( virtual/target-os-factory-shim )
Mike Frysinger76452ba2012-09-13 22:45:34 -0400200 fi
201 if [[ "${FLAGS_withtest}" -eq "${FLAGS_TRUE}" ]]; then
Mike Frysinger52669432014-05-05 14:34:09 -0400202 PACKAGES+=( virtual/target-os-test )
Daniel Wangaf4d0012015-07-17 11:52:05 -0700203 # chromeos-ssh-testkeys may generate ssh keys if the right USE flag is set.
204 # We force rebuilding this package from source every time, so that
205 # consecutive builds don't share ssh keys.
206 FORCE_LOCAL_BUILD_PKGS+=( chromeos-base/chromeos-ssh-testkeys )
Mike Frysinger76452ba2012-09-13 22:45:34 -0400207 fi
208 if [[ "${FLAGS_withautotest}" -eq "${FLAGS_TRUE}" ]]; then
209 PACKAGES+=( chromeos-base/autotest-all )
210 fi
Brian Harringcb782242011-12-13 19:48:44 -0800211fi
212
213# Verify that all packages can be emerged from scratch, without any
214# backtracking. Only print the output if this step fails.
Mike Frysinger76452ba2012-09-13 22:45:34 -0400215info "Checking package dependencies are correct: ${PACKAGES[*]}"
David Jamesab9ca212012-11-06 11:06:07 -0800216if ! OUTPUT=$(emerge-${FLAGS_board} -pe --backtrack=0 \
217 "${PACKAGES[@]}" 2>&1); then
Brian Harringcb782242011-12-13 19:48:44 -0800218 printf "%s\n" "${OUTPUT}"
Brian Harring7f175a52012-03-02 05:37:00 -0800219 die_notrace "emerge detected broken ebuilds. See error message above."
Brian Harringcb782242011-12-13 19:48:44 -0800220fi
221
Mike Frysinger4114c792012-09-13 22:33:12 -0400222# Build cros_workon packages when they are changed.
223CROS_WORKON_PKGS=()
224if [ "${FLAGS_noworkon}" -eq "${FLAGS_FALSE}" ]; then
225 LIST_MODIFIED_PACKAGES="${CHROMITE_BIN}/cros_list_modified_packages"
226 CROS_WORKON_PKGS+=( $("${LIST_MODIFIED_PACKAGES}" --board=${FLAGS_board}) )
Mike Frysinger4114c792012-09-13 22:33:12 -0400227
David James4941c6b2014-02-03 16:24:59 -0800228 # TODO(anush): Make chrome a fake cros-workon package.
229 if [[ -n "${CHROME_ORIGIN}" ]]; then
230 CROS_WORKON_PKGS+=( chromeos-base/chromeos-chrome )
231 fi
Mike Frysinger4114c792012-09-13 22:33:12 -0400232fi
233
Daniel Wangaf4d0012015-07-17 11:52:05 -0700234# cros_workon packages always have to be rebuilt.
Daniel Wang491f42a2015-08-20 12:29:59 -0700235FORCE_LOCAL_BUILD_PKGS+=( "${CROS_WORKON_PKGS[@]}" )
Daniel Wangaf4d0012015-07-17 11:52:05 -0700236
237if [[ ${#FORCE_LOCAL_BUILD_PKGS[@]} -gt 0 ]]; then
Mike Frysinger4114c792012-09-13 22:33:12 -0400238 EMERGE_FLAGS+=(
Daniel Wangaf4d0012015-07-17 11:52:05 -0700239 --reinstall-atoms="${FORCE_LOCAL_BUILD_PKGS[*]}"
240 --usepkg-exclude="${FORCE_LOCAL_BUILD_PKGS[*]}"
Mike Frysinger4114c792012-09-13 22:33:12 -0400241 )
David James710a7d12011-12-21 15:57:02 -0800242fi
Matt Tennant298f61a2012-06-25 21:54:33 -0700243
244# Prepare tmp file to capture emerge output from tee.
245tmpfile=$(mktemp -t tmp.build_packages-emerge.XXXXXX)
246trap "rm -f '${tmpfile}'" EXIT
247
Thiago Goncales5ad4ca02013-07-12 10:55:11 -0700248if [[ -n "${FLAGS_board_root}" ]]; then
249 export ROOT="${FLAGS_board_root}"
Bertrand SIMONNET10ede912014-10-09 16:21:13 -0700250 export PORTAGE_CONFIGROOT="${ROOT}"
251 export SYSROOT="${ROOT}"
Thiago Goncales5ad4ca02013-07-12 10:55:11 -0700252 export PKGDIR="${ROOT}"/packages
253fi
254
Mike Frysinger76452ba2012-09-13 22:45:34 -0400255info "Merging board packages now"
Matt Tennant298f61a2012-06-25 21:54:33 -0700256(
257 set -o pipefail
Mike Frysinger4114c792012-09-13 22:33:12 -0400258 sudo -E "${EMERGE_CMD[@]}" "${EMERGE_FLAGS[@]}" "${PACKAGES[@]}" | \
Matt Tennant298f61a2012-06-25 21:54:33 -0700259 tee "${tmpfile}"
260)
261
262# Extract total package count from emerge output.
263package_count=$(awk '$0 ~ /^Total: [0-9]+ packages/ { print $2 }' "${tmpfile}")
264rm "${tmpfile}"
265trap - EXIT
Brian Harringcb782242011-12-13 19:48:44 -0800266
267echo "Builds complete"
Bertrand SIMONNETc7a46e22014-12-11 15:58:11 -0800268
269if [[ ${FLAGS_withdebugsymbols} -eq ${FLAGS_TRUE} ]]; then
270 info "fetching the debug symbols"
271 sudo -E "${GCLIENT_ROOT}/chromite/bin/cros_install_debug_syms" \
272 "--board=${FLAGS_board}" "--all"
273fi
274
Matt Tennant298f61a2012-06-25 21:54:33 -0700275EXTRA_COMMAND_STATS[package_count]=${package_count}
276command_completed
Brian Harringcb782242011-12-13 19:48:44 -0800277echo "Done"