blob: e8a8ccb71a5f1b99048124b216ed815d1f5c05fe [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
7# Script to build the set of binary packages needed by Chrome OS. It will
8# cross compile all of the packages into the given targets root and build
9# binary packages as a side-effect. The output packages will be picked up
10# by the build_image script to put together a bootable Chrome OS image.
11
12# Load common CrOS utilities. Inside the chroot this file is installed in
13# /usr/lib/crosutils. Outside the chroot we find it relative to the script's
14# location.
15find_common_sh() {
16 local common_paths=(/usr/lib/crosutils $(dirname "$0"))
17 local path
18
19 SCRIPT_ROOT=
20 for path in "${common_paths[@]}"; do
21 local common="${path}/common.sh"
22 if ([ -r "${common}" ] && . "${common}" && [ -d "${SCRIPTS_DIR}" ]); then
23 SCRIPT_ROOT=${path}
24 break
25 fi
26 done
27}
28
29find_common_sh
30. "${SCRIPT_ROOT}/common.sh" || ! echo "Unable to load common.sh" || exit 1
31
32# Script must run inside the chroot
33restart_in_chroot_if_needed "$@"
34
35get_default_board
36
37# Flags
38DEFINE_string board "${DEFAULT_BOARD}" \
39 "The board to build packages for."
40# Deprecate chrome* options below once we have cbuild not passing these options
41DEFINE_boolean chromefromsource "${FLAGS_FALSE}" \
42 "Deprecated"
43DEFINE_string chromebuild "" \
44 "Deprecated"
45DEFINE_string chromebase "" \
46 "Deprecated"
47DEFINE_boolean usepkg "${FLAGS_TRUE}" \
48 "Use binary packages to bootstrap when possible."
49DEFINE_boolean withdev "${FLAGS_TRUE}" \
50 "Build useful developer friendly utilities."
51DEFINE_boolean withautotest "${FLAGS_TRUE}" \
52 "Build autotest client code."
53DEFINE_integer jobs -1 \
54 "How many packages to build in parallel at maximum."
55DEFINE_integer retries -1 \
56 "On build failure, the number of times to retry."
57DEFINE_boolean withtest "${FLAGS_TRUE}" \
58 "Build packages required for testing."
59DEFINE_boolean withfactory "${FLAGS_TRUE}" \
60 "Build factory installer."
61DEFINE_boolean fast "${DEFAULT_FAST}" \
62 "Call many emerges in parallel."
63DEFINE_boolean norebuild "${FLAGS_FALSE}" \
64 "Don't automatically rebuild dependencies."
65DEFINE_boolean showoutput "${FLAGS_FALSE}" \
66 "Show all output from parallel_emerge."
67DEFINE_boolean noworkon "${FLAGS_FALSE}" \
68 "Don't force-build workon packages."
69DEFINE_boolean withdebug "${FLAGS_TRUE}" \
70 "Build debug versions of Chromium-OS-specific packages."
71DEFINE_boolean oldchromebinary "${FLAGS_FALSE}" \
72 "Use the last prebuilt binary for Chrome produced by the buildbot."
73DEFINE_boolean skip_toolchain_update "${FLAGS_FALSE}" \
74 "Don't update toolchain automatically."
75
David James17c622a2012-03-07 09:34:08 -080076# The --reuse_pkgs_from_local_boards flag tells Portage to share binary
77# packages between boards that are built locally, so that the total time
78# required to build several boards is reduced. This flag is only useful
79# when you are not able to use remote binary packages, since remote binary
80# packages are usually more up to date than anything you have locally.
81DEFINE_boolean reuse_pkgs_from_local_boards $FLAGS_FALSE \
82 "Bootstrap from local packages instead of remote packages."
83
Brian Harringcb782242011-12-13 19:48:44 -080084
85# Parse command line
86FLAGS_HELP="usage: $0 [flags]"
87FLAGS "$@" || exit 1
88eval set -- "${FLAGS_ARGV}"
89check_flags_only_and_allow_null_arg "$@" && set --
90
91# Die on any errors.
92set -e
93
94# Right now build_packages has to be run from scripts/
95. ${SRC_ROOT}/third_party/chromiumos-overlay/chromeos/config/chromeos_version.sh
96
97if [[ -z "${FLAGS_board}" ]]; then
98 echo "Error: --board is required."
99 exit 1
100fi
101
Mike Frysinger650bf872012-02-27 11:05:26 -0500102EMERGE_FLAGS="--backtrack=30 --select"
Brian Harringcb782242011-12-13 19:48:44 -0800103
104EMERGE_CMD="emerge"
105EMERGE_BOARD_CMD="emerge-${FLAGS_board}"
106if [[ "${FLAGS_fast}" -eq "${FLAGS_TRUE}" ]]; then
107 EMERGE_CMD="${GCLIENT_ROOT}/chromite/bin/parallel_emerge"
108 EMERGE_BOARD_CMD="${EMERGE_CMD} --board=${FLAGS_board}"
109fi
110if [[ -n "${EXTRA_BOARD_FLAGS}" ]]; then
111 EMERGE_BOARD_CMD="${EMERGE_BOARD_CMD} ${EXTRA_BOARD_FLAGS}"
112fi
113
David James17c622a2012-03-07 09:34:08 -0800114if [[ "${FLAGS_usepkg}" -eq "${FLAGS_TRUE}" ||
115 "${FLAGS_reuse_pkgs_from_local_boards}" -eq "${FLAGS_TRUE}" ]]; then
Brian Harringcb782242011-12-13 19:48:44 -0800116 # Use binary packages. Include all build-time dependencies,
117 # so as to avoid unnecessary differences between source
118 # and binary builds.
119 EMERGE_FLAGS="${EMERGE_FLAGS} --getbinpkg --usepkg --with-bdeps y"
120fi
121
122if [[ "${FLAGS_jobs}" -ne -1 ]]; then
David James184e3902012-02-23 20:19:28 -0800123 EMERGE_FLAGS+=" --jobs=${FLAGS_jobs}"
Brian Harringcb782242011-12-13 19:48:44 -0800124fi
125
126if [[ "${FLAGS_withdebug}" -eq "${FLAGS_FALSE}" ]]; then
127 export USE="${USE} -cros-debug"
128fi
129
130${EMERGE_CMD} --info
131
132# Before we can run any tools, we need to update chroot or setup_board.
133UPDATE_ARGS=""
134if [ "${FLAGS_fast}" -eq "${FLAGS_TRUE}" ]; then
135 UPDATE_ARGS+=" --fast"
136else
137 UPDATE_ARGS+=" --nofast"
138fi
139if [ "${FLAGS_usepkg}" -eq "${FLAGS_TRUE}" ]; then
140 UPDATE_ARGS+=" --usepkg"
141else
142 UPDATE_ARGS+=" --nousepkg"
143fi
David James184e3902012-02-23 20:19:28 -0800144if [[ "${FLAGS_jobs}" -ne -1 ]]; then
145 UPDATE_ARGS+=" --jobs=${FLAGS_jobs}"
146fi
David James17c622a2012-03-07 09:34:08 -0800147if [ "${FLAGS_reuse_pkgs_from_local_boards}" -eq "${FLAGS_TRUE}" ]; then
148 UPDATE_ARGS+=" --reuse_pkgs_from_local_boards"
149fi
Brian Harringcb782242011-12-13 19:48:44 -0800150if [ "${FLAGS_skip_toolchain_update}" -eq "${FLAGS_TRUE}" ]; then
151 UPDATE_ARGS+=" --skip_toolchain_update"
152fi
153${SCRIPTS_DIR}/setup_board --quiet --board=${FLAGS_board} ${UPDATE_ARGS}
154
155if [ "${FLAGS_noworkon}" -eq "${FLAGS_FALSE}" ]; then
156 # Always build cros-workon packages
157 CROS_WORKON_PKGS=$(cros_workon --board="${FLAGS_board}" list)
158fi
159
160# TODO(anush): Make chrome a fake cros-workon package.
161if [[ -n "${CHROME_ORIGIN}" ]]; then
162 CROS_WORKON_PKGS="${CROS_WORKON_PKGS} chromeos-base/chromeos-chrome"
163fi
164
165PACKAGES="chromeos-base/chromeos"
166if [[ "${FLAGS_withdev}" -eq "${FLAGS_TRUE}" ]]; then
167 PACKAGES="${PACKAGES} chromeos-base/chromeos-dev"
168fi
169if [[ "${FLAGS_withfactory}" -eq "${FLAGS_TRUE}" ]]; then
170 PACKAGES="${PACKAGES} chromeos-base/chromeos-factoryinstall"
171 PACKAGES="${PACKAGES} chromeos-base/factorytest-init"
172 PACKAGES="${PACKAGES} chromeos-base/chromeos-hwid"
173fi
174if [[ "${FLAGS_withtest}" -eq "${FLAGS_TRUE}" ]]; then
175 PACKAGES="${PACKAGES} chromeos-base/chromeos-test"
176fi
177if [[ "${FLAGS_withautotest}" -eq "${FLAGS_TRUE}" ]]; then
178 PACKAGES="${PACKAGES} chromeos-base/autotest-all"
179fi
180
181# Verify that all packages can be emerged from scratch, without any
182# backtracking. Only print the output if this step fails.
183if ! OUTPUT=$(emerge-${FLAGS_board} -pe --backtrack=0 ${PACKAGES} 2>&1); then
184 printf "%s\n" "${OUTPUT}"
185 die "emerge detected broken ebuilds. See error message above."
186fi
187
David James710a7d12011-12-21 15:57:02 -0800188for pkg in ${CROS_WORKON_PKGS}; do
189 EMERGE_FLAGS+=" --reinstall-atoms=${pkg}"
190 EMERGE_FLAGS+=" --usepkg-exclude=${pkg}"
191done
192if [[ "${FLAGS_norebuild}" -eq "${FLAGS_FALSE}" ]]; then
193 EMERGE_FLAGS+=" --rebuild-if-unbuilt"
Brian Harringcb782242011-12-13 19:48:44 -0800194fi
David James710a7d12011-12-21 15:57:02 -0800195if [[ "${FLAGS_oldchromebinary}" -eq "${FLAGS_TRUE}" ]]; then
196 EMERGE_FLAGS+=" --useoldpkg-atoms=chromeos-chrome"
197 EMERGE_FLAGS+=" --useoldpkg-atoms=libcros"
198fi
199if [[ "${FLAGS_showoutput}" -eq "${FLAGS_TRUE}" && \
200 "${FLAGS_fast}" -eq "${FLAGS_TRUE}" ]]; then
201 # Only parallel_emerge supports --show-output.
202 EMERGE_FLAGS+=" --show-output"
203fi
204eretry sudo -E ${EMERGE_BOARD_CMD} -uDNv ${EMERGE_FLAGS} ${PACKAGES}
Brian Harringcb782242011-12-13 19:48:44 -0800205
206echo "Builds complete"
207print_time_elapsed
208echo "Done"