blob: 10749219a6bd6c5fba09eaab02e27b4f966d96a6 [file] [log] [blame]
#!/bin/bash
# Copyright 2018 The Chromium OS Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
# The host (chroot) specific "setup_board" process. This separates the chroot
# specific setup from the generic board setup.
. "$(dirname "$0")/common.sh" || exit 1
# Script must run inside the chroot
restart_in_chroot_if_needed "$@"
assert_not_root_user
# Developer-visible flags.
DEFINE_string board "amd64-host" \
"The name of the board to set up."
DEFINE_boolean force $FLAGS_FALSE \
"Force re-creating board root."
FLAGS_HELP="usage: $(basename $0) [flags]
setup_host_board builds the chroot for the amd64-host (chroot) board.
This should not need to be called except by the SDK Builder.
"
# Parse command line flags
FLAGS "$@" || exit 1
eval set -- "${FLAGS_ARGV}"
# Only now can we die on error. shflags functions leak non-zero error codes,
# so will die prematurely if 'switch_to_strict_mode' is specified before now.
switch_to_strict_mode
BOARD=${FLAGS_board}
# Locations we will need
BOARD_ROOT="/build/${BOARD}"
CROSSDEV_OVERLAY="/usr/local/portage/crossdev"
CHROMIUMOS_OVERLAY="/usr/local/portage/chromiumos"
CHROMIUMOS_CONFIG="${CHROMIUMOS_OVERLAY}/chromeos/config"
CHROMIUMOS_PROFILES="${CHROMIUMOS_OVERLAY}/profiles"
BOARD_ETC="${BOARD_ROOT}/etc"
BOARD_SETUP="${BOARD_ETC}/make.conf.board_setup"
BOARD_PROFILE="${BOARD_ETC}/portage/profile"
eval $(portageq envvar -v CHOST PKGDIR)
SYSROOT_EXISTS=false
if [ -d "${BOARD_ROOT}" ]; then
if [[ ${FLAGS_force} -eq ${FLAGS_TRUE} ]]; then
echo "--force set. Re-creating ${BOARD_ROOT}..."
# Removal takes long. Make it asynchronous.
TEMP_DIR=`mktemp -d`
sudo mv "${BOARD_ROOT}" "${TEMP_DIR}"
sudo rm -rf --one-file-system "${TEMP_DIR}" &
else
# The sysroot exists. Take note so that we can exit early once the
# configuration has been updated.
SYSROOT_EXISTS=true
fi
fi
# Setup the make.confs. We use the following:
# make.conf <- Overall target make.conf [arm, x86, etc. version]
# make.conf.board_setup <- Declares CHOST, ROOT, etc.
# make.conf.board <- Optional board-supplied make.conf.
# make.conf.user <- User specified parameters.
cmds=(
"mkdir -p '${BOARD_ROOT}' '${BOARD_ETC}' '${BOARD_PROFILE}' /usr/local/bin"
"ln -sf /etc/make.conf.user '${BOARD_ROOT}/etc/make.conf.user'"
"mkdir -p '${BOARD_ROOT}/etc/portage/hooks'"
)
for d in "${SCRIPTS_DIR}"/hooks/*; do
cmds+=( "ln -sfT '${d}' '${BOARD_ROOT}/etc/portage/hooks/${d##*/}'" )
done
cmds+=(
"ln -sf '${CHROMIUMOS_CONFIG}/make.conf.${BOARD}' \
'${BOARD_ETC}/make.conf'"
"cp -f '/etc/make.conf.host_setup' '${BOARD_ETC}/'"
# Setting up symlinks for bootstrapping multilib.
# See http://crosbug.com/14498
"mkdir -p '${BOARD_ROOT}'{/usr,}/lib64"
"ln -sfT lib64 '${BOARD_ROOT}/lib'"
"rm -rf '${BOARD_ROOT}/usr/lib'"
"ln -sfT lib64 '${BOARD_ROOT}/usr/lib'"
# Copying some files for bootstrapping empty chroot.
# See http://crosbug.com/14499
"mkdir -p '${BOARD_ETC}'/{init.d,xml}"
"cp /etc/xml/catalog '${BOARD_ETC}'/xml/"
"cp /etc/init.d/functions.sh '${BOARD_ETC}'/init.d/"
)
sudo_multi "${cmds[@]}"
# Generating the standard configuration file (make.conf.board_setup) for the
# sysroot.
info_run cros_sysroot_utils generate-config --sysroot="${BOARD_ROOT}" \
--board="${BOARD}" --out-file="${BOARD_SETUP}"
# Generate wrappers for portage helpers (equery, portageq, emerge, etc...).
# Those are used to generate make.conf.board.
info_run cros_sysroot_utils create-wrappers --sysroot="${BOARD_ROOT}" \
--friendlyname="${BOARD}"
# Choose the default profile.
if ! info_run cros_choose_profile --profile "" \
--board-root "${BOARD_ROOT}" --board "${BOARD}"; then
sudo rm -rf --one-file-system "${BOARD_ROOT}"
die "Selecting profile failed, removing incomplete board directory!"
fi
EMERGE_CMD="${CHROMITE_BIN}/parallel_emerge"
TOOLCHAIN_PACKAGES=(
$("${CHROMITE_BIN}/cros_setup_toolchains" --show-packages host)
)
# Sanity check we got some valid results.
if [[ ${#TOOLCHAIN_PACKAGES[@]} -eq 0 ]]; then
die_notrace "cros_setup_toolchains failed"
fi
PACKAGES=( system virtual/target-sdk world )
run_emerge() {
info_run sudo -E ${EMERGE_CMD} "$@"
}
# First, rebuild all packages from scratch. This is needed to make sure
# we rebuild all chroot packages.
# We build the toolchain by hand to avoid race conditions where the toolchain
# is used by other packages that we're building. See https://crbug.com/906289.
run_emerge "${TOOLCHAIN_PACKAGES[@]}"
# Then build everything else.
run_emerge --emptytree --with-bdeps=y \
--exclude --verbose "${TOOLCHAIN_PACKAGES[*]}" \
"${PACKAGES[@]}" virtual/target-sdk-nobdeps
info_run sudo eclean -d packages
# Next, install our rebuilt packages into our separate root.
HOST_FLAGS="--root=$BOARD_ROOT --update --verbose --deep --root-deps"
HOST_FLAGS+=" --newuse --usepkgonly"
run_emerge $HOST_FLAGS --with-bdeps=y "${PACKAGES[@]}"
# Install our rebuilt packages from the nobdeps target into our separate root
# without their build-time deps. We also avoid adding this target to the
# world set so that subsequent update_chroot commands won't re-import the
# build deps.
run_emerge $HOST_FLAGS --with-bdeps=n --oneshot \
virtual/target-sdk-nobdeps
info_run sudo cp -a "${PKGDIR}" $BOARD_ROOT/packages
# Copy our chroot version into the newly packaged chroot.
sudo cp -a "${CHROOT_VERSION_FILE}" "${BOARD_ROOT}${CHROOT_VERSION_FILE}"
# Now cleanup paths referencing the ROOT from the *.la files.
sudo find "${BOARD_ROOT}" -type f -name '*.la' -exec \
sed -i -e "s|${BOARD_ROOT}/|/|g" {} +
command_completed
echo "Done!"
echo "The SYSROOT is: ${BOARD_ROOT}"