blob: edde767010bd5766de5b82abecbd65cbac64048b [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
76
77# Parse command line
78FLAGS_HELP="usage: $0 [flags]"
79FLAGS "$@" || exit 1
80eval set -- "${FLAGS_ARGV}"
81check_flags_only_and_allow_null_arg "$@" && set --
82
83# Die on any errors.
84set -e
85
86# Right now build_packages has to be run from scripts/
87. ${SRC_ROOT}/third_party/chromiumos-overlay/chromeos/config/chromeos_version.sh
88
89if [[ -z "${FLAGS_board}" ]]; then
90 echo "Error: --board is required."
91 exit 1
92fi
93
94EMERGE_FLAGS="--backtrack=30"
95
96EMERGE_CMD="emerge"
97EMERGE_BOARD_CMD="emerge-${FLAGS_board}"
98if [[ "${FLAGS_fast}" -eq "${FLAGS_TRUE}" ]]; then
99 EMERGE_CMD="${GCLIENT_ROOT}/chromite/bin/parallel_emerge"
100 EMERGE_BOARD_CMD="${EMERGE_CMD} --board=${FLAGS_board}"
101fi
102if [[ -n "${EXTRA_BOARD_FLAGS}" ]]; then
103 EMERGE_BOARD_CMD="${EMERGE_BOARD_CMD} ${EXTRA_BOARD_FLAGS}"
104fi
105
106if [[ "${FLAGS_usepkg}" -eq "${FLAGS_TRUE}" ]]; then
107 # Use binary packages. Include all build-time dependencies,
108 # so as to avoid unnecessary differences between source
109 # and binary builds.
110 EMERGE_FLAGS="${EMERGE_FLAGS} --getbinpkg --usepkg --with-bdeps y"
111fi
112
113if [[ "${FLAGS_jobs}" -ne -1 ]]; then
114 EMERGE_JOBS="--jobs=${FLAGS_jobs}"
115 if [[ "${FLAGS_retries}" -eq -1 ]]; then
116 # The jobs flag can be flaky. Retry once by default,
117 # without the jobs flag.
118 FLAGS_retries=1
119 fi
120fi
121
122if [[ "${FLAGS_withdebug}" -eq "${FLAGS_FALSE}" ]]; then
123 export USE="${USE} -cros-debug"
124fi
125
126${EMERGE_CMD} --info
127
128# Before we can run any tools, we need to update chroot or setup_board.
129UPDATE_ARGS=""
130if [ "${FLAGS_fast}" -eq "${FLAGS_TRUE}" ]; then
131 UPDATE_ARGS+=" --fast"
132else
133 UPDATE_ARGS+=" --nofast"
134fi
135if [ "${FLAGS_usepkg}" -eq "${FLAGS_TRUE}" ]; then
136 UPDATE_ARGS+=" --usepkg"
137else
138 UPDATE_ARGS+=" --nousepkg"
139fi
140if [ "${FLAGS_skip_toolchain_update}" -eq "${FLAGS_TRUE}" ]; then
141 UPDATE_ARGS+=" --skip_toolchain_update"
142fi
143${SCRIPTS_DIR}/setup_board --quiet --board=${FLAGS_board} ${UPDATE_ARGS}
144
145if [ "${FLAGS_noworkon}" -eq "${FLAGS_FALSE}" ]; then
146 # Always build cros-workon packages
147 CROS_WORKON_PKGS=$(cros_workon --board="${FLAGS_board}" list)
148fi
149
150# TODO(anush): Make chrome a fake cros-workon package.
151if [[ -n "${CHROME_ORIGIN}" ]]; then
152 CROS_WORKON_PKGS="${CROS_WORKON_PKGS} chromeos-base/chromeos-chrome"
153fi
154
155PACKAGES="chromeos-base/chromeos"
156if [[ "${FLAGS_withdev}" -eq "${FLAGS_TRUE}" ]]; then
157 PACKAGES="${PACKAGES} chromeos-base/chromeos-dev"
158fi
159if [[ "${FLAGS_withfactory}" -eq "${FLAGS_TRUE}" ]]; then
160 PACKAGES="${PACKAGES} chromeos-base/chromeos-factoryinstall"
161 PACKAGES="${PACKAGES} chromeos-base/factorytest-init"
162 PACKAGES="${PACKAGES} chromeos-base/chromeos-hwid"
163fi
164if [[ "${FLAGS_withtest}" -eq "${FLAGS_TRUE}" ]]; then
165 PACKAGES="${PACKAGES} chromeos-base/chromeos-test"
166fi
167if [[ "${FLAGS_withautotest}" -eq "${FLAGS_TRUE}" ]]; then
168 PACKAGES="${PACKAGES} chromeos-base/autotest-all"
169fi
170
171# Verify that all packages can be emerged from scratch, without any
172# backtracking. Only print the output if this step fails.
173if ! OUTPUT=$(emerge-${FLAGS_board} -pe --backtrack=0 ${PACKAGES} 2>&1); then
174 printf "%s\n" "${OUTPUT}"
175 die "emerge detected broken ebuilds. See error message above."
176fi
177
178if [[ "${FLAGS_fast}" -eq "${FLAGS_TRUE}" ]]; then
179 # These flags are specific to parallel_emerge
180 for pkg in ${CROS_WORKON_PKGS}; do
181 EMERGE_FLAGS="${EMERGE_FLAGS} --workon=${pkg}"
182 done
183 if [[ "${FLAGS_norebuild}" -eq "${FLAGS_FALSE}" ]]; then
184 EMERGE_FLAGS="${EMERGE_FLAGS} --rebuild"
185 fi
186 if [[ "${FLAGS_showoutput}" -eq "${FLAGS_TRUE}" ]]; then
187 EMERGE_FLAGS="${EMERGE_FLAGS} --show-output"
188 fi
189 if [[ "${FLAGS_oldchromebinary}" -eq "${FLAGS_TRUE}" ]]; then
190 EMERGE_FLAGS="${EMERGE_FLAGS} --force-remote-binary=chromeos-chrome"
191 EMERGE_FLAGS="${EMERGE_FLAGS} --force-remote-binary=libcros"
192 fi
193 eretry sudo -E ${EMERGE_BOARD_CMD} -uDNv ${EMERGE_FLAGS} ${PACKAGES}
194else
195 echo "Building ${PACKAGES} ${CROS_WORKON_PKGS}"
196 if [[ "${FLAGS_usepkg}" -eq "${FLAGS_TRUE}" ]]; then
197 # Merge all packages, including binary versions of packages.
198 eretry ${EMERGE_BOARD_CMD} -uDNv ${EMERGE_FLAGS} ${PACKAGES}
199 if [[ -n "${CROS_WORKON_PKGS}" ]]; then
200 # Re-merge cros-workon packages, this time from source. This is
201 # inefficient, but regular emerge doesn't support any way of specifying
202 # that you want specific packages to be merged from source only.
203 eretry ${EMERGE_BOARD_CMD} --oneshot ${CROS_WORKON_PKGS}
204 fi
205 else
206 # Re-merge cros_workon packages, updating dependencies if necessary.
207 # --selective=n: Always re-merge cros_workon packages.
208 # -1: Don't add cros_workon packages to world file.
209 if [[ -n "${CROS_WORKON_PKGS}" ]]; then
210 eretry ${EMERGE_BOARD_CMD} -uDNv1 --selective=n ${CROS_WORKON_PKGS}
211 fi
212
213 # Update the remainder of the packages.
214 eretry ${EMERGE_BOARD_CMD} -uDNv ${EMERGE_FLAGS} ${PACKAGES}
215 fi
216fi
217
218
219echo "Builds complete"
220print_time_elapsed
221echo "Done"