blob: 22b51585074f6a025b7aaabadf085f47debfd755 [file] [log] [blame]
Alex Klein29986332018-12-10 12:08:46 -07001#!/bin/bash
2
3# Copyright 2018 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# The host (chroot) specific "setup_board" process. This separates the chroot
8# specific setup from the generic board setup.
9
10. "$(dirname "$0")/common.sh" || exit 1
11
12# Script must run inside the chroot
13restart_in_chroot_if_needed "$@"
14
15assert_not_root_user
16
17# Developer-visible flags.
18DEFINE_string board "amd64-host" \
19 "The name of the board to set up."
20DEFINE_boolean force $FLAGS_FALSE \
21 "Force re-creating board root."
22
23FLAGS_HELP="usage: $(basename $0) [flags]
24
25setup_host_board builds the chroot for the amd64-host (chroot) board.
26This should not need to be called except by the SDK Builder.
27"
Alex Klein29986332018-12-10 12:08:46 -070028
29# Parse command line flags
30FLAGS "$@" || exit 1
31eval set -- "${FLAGS_ARGV}"
32
33# Only now can we die on error. shflags functions leak non-zero error codes,
34# so will die prematurely if 'switch_to_strict_mode' is specified before now.
35switch_to_strict_mode
36
37BOARD=${FLAGS_board}
38
39# Locations we will need
40BOARD_ROOT="/build/${BOARD}"
41CROSSDEV_OVERLAY="/usr/local/portage/crossdev"
42CHROMIUMOS_OVERLAY="/usr/local/portage/chromiumos"
43CHROMIUMOS_CONFIG="${CHROMIUMOS_OVERLAY}/chromeos/config"
44CHROMIUMOS_PROFILES="${CHROMIUMOS_OVERLAY}/profiles"
45BOARD_ETC="${BOARD_ROOT}/etc"
46BOARD_SETUP="${BOARD_ETC}/make.conf.board_setup"
47BOARD_PROFILE="${BOARD_ETC}/portage/profile"
48
49eval $(portageq envvar -v CHOST PKGDIR)
50
51SYSROOT_EXISTS=false
52if [ -d "${BOARD_ROOT}" ]; then
53 if [[ ${FLAGS_force} -eq ${FLAGS_TRUE} ]]; then
54 echo "--force set. Re-creating ${BOARD_ROOT}..."
55 # Removal takes long. Make it asynchronous.
56 TEMP_DIR=`mktemp -d`
57 sudo mv "${BOARD_ROOT}" "${TEMP_DIR}"
58 sudo rm -rf --one-file-system "${TEMP_DIR}" &
59 else
60 # The sysroot exists. Take note so that we can exit early once the
61 # configuration has been updated.
62 SYSROOT_EXISTS=true
63 fi
64fi
65
66# Setup the make.confs. We use the following:
67# make.conf <- Overall target make.conf [arm, x86, etc. version]
68# make.conf.board_setup <- Declares CHOST, ROOT, etc.
69# make.conf.board <- Optional board-supplied make.conf.
70# make.conf.user <- User specified parameters.
71cmds=(
72 "mkdir -p '${BOARD_ROOT}' '${BOARD_ETC}' '${BOARD_PROFILE}' /usr/local/bin"
73 "ln -sf /etc/make.conf.user '${BOARD_ROOT}/etc/make.conf.user'"
74 "mkdir -p '${BOARD_ROOT}/etc/portage/hooks'"
75)
76for d in "${SCRIPTS_DIR}"/hooks/*; do
77 cmds+=( "ln -sfT '${d}' '${BOARD_ROOT}/etc/portage/hooks/${d##*/}'" )
78done
79sudo_multi "${cmds[@]}"
80
81# Generating the standard configuration file (make.conf.board_setup) for the
82# sysroot.
83cros_sysroot_utils generate-config --sysroot="${BOARD_ROOT}" \
84 --board="${BOARD}" --out-file="${BOARD_SETUP}"
85
86# Generate wrappers for portage helpers (equery, portageq, emerge, etc...).
87# Those are used to generate make.conf.board.
88cros_sysroot_utils create-wrappers --sysroot="${BOARD_ROOT}" \
89 --friendlyname="${BOARD}"
90
91# Choose the default profile.
92if ! cros_choose_profile --profile "" \
93 --board-root "${BOARD_ROOT}" --board "${BOARD}"; then
94 sudo rm -rf --one-file-system "${BOARD_ROOT}"
95 die "Selecting profile failed, removing incomplete board directory!"
96fi
97
98cmds=(
99 "ln -sf '${CHROMIUMOS_CONFIG}/make.conf.${BOARD}' \
100 '${BOARD_ETC}/make.conf'"
101 "cp -f '/etc/make.conf.host_setup' '${BOARD_ETC}/'"
102
103 # Setting up symlinks for bootstrapping multilib.
104 # See http://crosbug.com/14498
105 "mkdir -p '${BOARD_ROOT}'{/usr,}/lib64"
106 "ln -sfT lib64 '${BOARD_ROOT}/lib'"
107 "rm -r '${BOARD_ROOT}/usr/lib'"
108 "ln -sfT lib64 '${BOARD_ROOT}/usr/lib'"
109
110 # Copying some files for bootstrapping empty chroot.
111 # See http://crosbug.com/14499
112 "mkdir -p '${BOARD_ETC}'/{init.d,xml}"
113 "cp /etc/xml/catalog '${BOARD_ETC}'/xml/"
114 "cp /etc/init.d/functions.sh '${BOARD_ETC}'/init.d/"
115)
116sudo_multi "${cmds[@]}"
117
118EMERGE_CMD="${CHROMITE_BIN}/parallel_emerge"
119TOOLCHAIN_PACKAGES=(
120 $("${CHROMITE_BIN}/cros_setup_toolchains" --show-packages host)
121)
122# Sanity check we got some valid results.
123if [[ ${#TOOLCHAIN_PACKAGES[@]} -eq 0 ]]; then
124 die_notrace "cros_setup_toolchains failed"
125fi
126PACKAGES=( system virtual/target-sdk world )
127
Mike Frysinger9c7529d2020-05-21 19:53:58 -0400128run_emerge() {
129 local cmd=( ${EMERGE_CMD} "$@" )
130 info "Running: ${cmd[*]}"
131 sudo -E "${cmd[@]}"
132}
133
Alex Klein29986332018-12-10 12:08:46 -0700134# First, rebuild all packages from scratch. This is needed to make sure
135# we rebuild all chroot packages.
Mike Frysingerd9190572018-12-20 18:04:51 -0500136
137# We build the toolchain by hand to avoid race conditions where the toolchain
138# is used by other packages that we're building. See https://crbug.com/906289.
Mike Frysinger9c7529d2020-05-21 19:53:58 -0400139run_emerge "${TOOLCHAIN_PACKAGES[@]}"
Mike Frysingerd9190572018-12-20 18:04:51 -0500140
141# Then build everything else.
Mike Frysinger9c7529d2020-05-21 19:53:58 -0400142run_emerge --emptytree --with-bdeps=y \
Mike Frysingerd9190572018-12-20 18:04:51 -0500143 --exclude "${TOOLCHAIN_PACKAGES[*]}" \
144 "${PACKAGES[@]}" virtual/target-sdk-nobdeps
Alex Klein29986332018-12-10 12:08:46 -0700145sudo eclean -d packages
146
147# Next, install our rebuilt packages into our separate root.
148HOST_FLAGS="--root=$BOARD_ROOT --update --verbose --deep --root-deps"
149HOST_FLAGS+=" --newuse --usepkgonly"
Mike Frysinger9c7529d2020-05-21 19:53:58 -0400150run_emerge $HOST_FLAGS --with-bdeps=y "${PACKAGES[@]}"
Alex Klein29986332018-12-10 12:08:46 -0700151# Install our rebuilt packages from the nobdeps target into our separate root
152# without their build-time deps. We also avoid adding this target to the
153# world set so that subsequent update_chroot commands won't re-import the
154# build deps.
Mike Frysinger9c7529d2020-05-21 19:53:58 -0400155run_emerge $HOST_FLAGS --with-bdeps=n --oneshot \
Alex Klein29986332018-12-10 12:08:46 -0700156 virtual/target-sdk-nobdeps
157sudo cp -a "${PKGDIR}" $BOARD_ROOT/packages
158
159# Copy our chroot version into the newly packaged chroot.
160sudo cp -a "${CHROOT_VERSION_FILE}" "${BOARD_ROOT}${CHROOT_VERSION_FILE}"
161
162# Now cleanup paths referencing the ROOT from the *.la files.
Mike Frysinger874c24b2019-02-22 00:46:33 -0500163sudo find "${BOARD_ROOT}" -type f -name '*.la' -exec \
164 sed -i -e "s|${BOARD_ROOT}/|/|g" {} +
Alex Klein29986332018-12-10 12:08:46 -0700165
166command_completed
167echo "Done!"
168echo "The SYSROOT is: ${BOARD_ROOT}"