blob: fe051acab54881beb9fb2e627651e1490cea4c87 [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"
28show_help_if_requested "$@"
29
30# Parse command line flags
31FLAGS "$@" || exit 1
32eval set -- "${FLAGS_ARGV}"
33
34# Only now can we die on error. shflags functions leak non-zero error codes,
35# so will die prematurely if 'switch_to_strict_mode' is specified before now.
36switch_to_strict_mode
37
38BOARD=${FLAGS_board}
39
40# Locations we will need
41BOARD_ROOT="/build/${BOARD}"
42CROSSDEV_OVERLAY="/usr/local/portage/crossdev"
43CHROMIUMOS_OVERLAY="/usr/local/portage/chromiumos"
44CHROMIUMOS_CONFIG="${CHROMIUMOS_OVERLAY}/chromeos/config"
45CHROMIUMOS_PROFILES="${CHROMIUMOS_OVERLAY}/profiles"
46BOARD_ETC="${BOARD_ROOT}/etc"
47BOARD_SETUP="${BOARD_ETC}/make.conf.board_setup"
48BOARD_PROFILE="${BOARD_ETC}/portage/profile"
49
50eval $(portageq envvar -v CHOST PKGDIR)
51
52SYSROOT_EXISTS=false
53if [ -d "${BOARD_ROOT}" ]; then
54 if [[ ${FLAGS_force} -eq ${FLAGS_TRUE} ]]; then
55 echo "--force set. Re-creating ${BOARD_ROOT}..."
56 # Removal takes long. Make it asynchronous.
57 TEMP_DIR=`mktemp -d`
58 sudo mv "${BOARD_ROOT}" "${TEMP_DIR}"
59 sudo rm -rf --one-file-system "${TEMP_DIR}" &
60 else
61 # The sysroot exists. Take note so that we can exit early once the
62 # configuration has been updated.
63 SYSROOT_EXISTS=true
64 fi
65fi
66
67# Setup the make.confs. We use the following:
68# make.conf <- Overall target make.conf [arm, x86, etc. version]
69# make.conf.board_setup <- Declares CHOST, ROOT, etc.
70# make.conf.board <- Optional board-supplied make.conf.
71# make.conf.user <- User specified parameters.
72cmds=(
73 "mkdir -p '${BOARD_ROOT}' '${BOARD_ETC}' '${BOARD_PROFILE}' /usr/local/bin"
74 "ln -sf /etc/make.conf.user '${BOARD_ROOT}/etc/make.conf.user'"
75 "mkdir -p '${BOARD_ROOT}/etc/portage/hooks'"
76)
77for d in "${SCRIPTS_DIR}"/hooks/*; do
78 cmds+=( "ln -sfT '${d}' '${BOARD_ROOT}/etc/portage/hooks/${d##*/}'" )
79done
80sudo_multi "${cmds[@]}"
81
82# Generating the standard configuration file (make.conf.board_setup) for the
83# sysroot.
84cros_sysroot_utils generate-config --sysroot="${BOARD_ROOT}" \
85 --board="${BOARD}" --out-file="${BOARD_SETUP}"
86
87# Generate wrappers for portage helpers (equery, portageq, emerge, etc...).
88# Those are used to generate make.conf.board.
89cros_sysroot_utils create-wrappers --sysroot="${BOARD_ROOT}" \
90 --friendlyname="${BOARD}"
91
92# Choose the default profile.
93if ! cros_choose_profile --profile "" \
94 --board-root "${BOARD_ROOT}" --board "${BOARD}"; then
95 sudo rm -rf --one-file-system "${BOARD_ROOT}"
96 die "Selecting profile failed, removing incomplete board directory!"
97fi
98
99cmds=(
100 "ln -sf '${CHROMIUMOS_CONFIG}/make.conf.${BOARD}' \
101 '${BOARD_ETC}/make.conf'"
102 "cp -f '/etc/make.conf.host_setup' '${BOARD_ETC}/'"
103
104 # Setting up symlinks for bootstrapping multilib.
105 # See http://crosbug.com/14498
106 "mkdir -p '${BOARD_ROOT}'{/usr,}/lib64"
107 "ln -sfT lib64 '${BOARD_ROOT}/lib'"
108 "rm -r '${BOARD_ROOT}/usr/lib'"
109 "ln -sfT lib64 '${BOARD_ROOT}/usr/lib'"
110
111 # Copying some files for bootstrapping empty chroot.
112 # See http://crosbug.com/14499
113 "mkdir -p '${BOARD_ETC}'/{init.d,xml}"
114 "cp /etc/xml/catalog '${BOARD_ETC}'/xml/"
115 "cp /etc/init.d/functions.sh '${BOARD_ETC}'/init.d/"
116)
117sudo_multi "${cmds[@]}"
118
119EMERGE_CMD="${CHROMITE_BIN}/parallel_emerge"
120TOOLCHAIN_PACKAGES=(
121 $("${CHROMITE_BIN}/cros_setup_toolchains" --show-packages host)
122)
123# Sanity check we got some valid results.
124if [[ ${#TOOLCHAIN_PACKAGES[@]} -eq 0 ]]; then
125 die_notrace "cros_setup_toolchains failed"
126fi
127PACKAGES=( system virtual/target-sdk world )
128
129# First, rebuild all packages from scratch. This is needed to make sure
130# we rebuild all chroot packages.
131sudo -E $EMERGE_CMD --emptytree --with-bdeps=y \
132 "${TOOLCHAIN_PACKAGES[@]}" "${PACKAGES[@]}" \
133 virtual/target-sdk-nobdeps
134sudo eclean -d packages
135
136# Next, install our rebuilt packages into our separate root.
137HOST_FLAGS="--root=$BOARD_ROOT --update --verbose --deep --root-deps"
138HOST_FLAGS+=" --newuse --usepkgonly"
139sudo -E $EMERGE_CMD $HOST_FLAGS --with-bdeps=y --select=y "${PACKAGES[@]}"
140# Install our rebuilt packages from the nobdeps target into our separate root
141# without their build-time deps. We also avoid adding this target to the
142# world set so that subsequent update_chroot commands won't re-import the
143# build deps.
144sudo -E $EMERGE_CMD $HOST_FLAGS --with-bdeps=n --oneshot \
145 virtual/target-sdk-nobdeps
146sudo cp -a "${PKGDIR}" $BOARD_ROOT/packages
147
148# Copy our chroot version into the newly packaged chroot.
149sudo cp -a "${CHROOT_VERSION_FILE}" "${BOARD_ROOT}${CHROOT_VERSION_FILE}"
150
151# Now cleanup paths referencing the ROOT from the *.la files.
152sudo find $BOARD_ROOT -type f -name '*.la' | xargs sudo \
153 sed -i -e "s|$BOARD_ROOT/|/|g"
154
155command_completed
156echo "Done!"
157echo "The SYSROOT is: ${BOARD_ROOT}"