Alex Klein | 2998633 | 2018-12-10 12:08:46 -0700 | [diff] [blame] | 1 | #!/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 |
| 13 | restart_in_chroot_if_needed "$@" |
| 14 | |
| 15 | assert_not_root_user |
| 16 | |
| 17 | # Developer-visible flags. |
| 18 | DEFINE_string board "amd64-host" \ |
| 19 | "The name of the board to set up." |
| 20 | DEFINE_boolean force $FLAGS_FALSE \ |
| 21 | "Force re-creating board root." |
| 22 | |
| 23 | FLAGS_HELP="usage: $(basename $0) [flags] |
| 24 | |
| 25 | setup_host_board builds the chroot for the amd64-host (chroot) board. |
| 26 | This should not need to be called except by the SDK Builder. |
| 27 | " |
| 28 | show_help_if_requested "$@" |
| 29 | |
| 30 | # Parse command line flags |
| 31 | FLAGS "$@" || exit 1 |
| 32 | eval 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. |
| 36 | switch_to_strict_mode |
| 37 | |
| 38 | BOARD=${FLAGS_board} |
| 39 | |
| 40 | # Locations we will need |
| 41 | BOARD_ROOT="/build/${BOARD}" |
| 42 | CROSSDEV_OVERLAY="/usr/local/portage/crossdev" |
| 43 | CHROMIUMOS_OVERLAY="/usr/local/portage/chromiumos" |
| 44 | CHROMIUMOS_CONFIG="${CHROMIUMOS_OVERLAY}/chromeos/config" |
| 45 | CHROMIUMOS_PROFILES="${CHROMIUMOS_OVERLAY}/profiles" |
| 46 | BOARD_ETC="${BOARD_ROOT}/etc" |
| 47 | BOARD_SETUP="${BOARD_ETC}/make.conf.board_setup" |
| 48 | BOARD_PROFILE="${BOARD_ETC}/portage/profile" |
| 49 | |
| 50 | eval $(portageq envvar -v CHOST PKGDIR) |
| 51 | |
| 52 | SYSROOT_EXISTS=false |
| 53 | if [ -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 |
| 65 | fi |
| 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. |
| 72 | cmds=( |
| 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 | ) |
| 77 | for d in "${SCRIPTS_DIR}"/hooks/*; do |
| 78 | cmds+=( "ln -sfT '${d}' '${BOARD_ROOT}/etc/portage/hooks/${d##*/}'" ) |
| 79 | done |
| 80 | sudo_multi "${cmds[@]}" |
| 81 | |
| 82 | # Generating the standard configuration file (make.conf.board_setup) for the |
| 83 | # sysroot. |
| 84 | cros_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. |
| 89 | cros_sysroot_utils create-wrappers --sysroot="${BOARD_ROOT}" \ |
| 90 | --friendlyname="${BOARD}" |
| 91 | |
| 92 | # Choose the default profile. |
| 93 | if ! 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!" |
| 97 | fi |
| 98 | |
| 99 | cmds=( |
| 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 | ) |
| 117 | sudo_multi "${cmds[@]}" |
| 118 | |
| 119 | EMERGE_CMD="${CHROMITE_BIN}/parallel_emerge" |
| 120 | TOOLCHAIN_PACKAGES=( |
| 121 | $("${CHROMITE_BIN}/cros_setup_toolchains" --show-packages host) |
| 122 | ) |
| 123 | # Sanity check we got some valid results. |
| 124 | if [[ ${#TOOLCHAIN_PACKAGES[@]} -eq 0 ]]; then |
| 125 | die_notrace "cros_setup_toolchains failed" |
| 126 | fi |
| 127 | PACKAGES=( 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. |
| 131 | sudo -E $EMERGE_CMD --emptytree --with-bdeps=y \ |
| 132 | "${TOOLCHAIN_PACKAGES[@]}" "${PACKAGES[@]}" \ |
| 133 | virtual/target-sdk-nobdeps |
| 134 | sudo eclean -d packages |
| 135 | |
| 136 | # Next, install our rebuilt packages into our separate root. |
| 137 | HOST_FLAGS="--root=$BOARD_ROOT --update --verbose --deep --root-deps" |
| 138 | HOST_FLAGS+=" --newuse --usepkgonly" |
| 139 | sudo -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. |
| 144 | sudo -E $EMERGE_CMD $HOST_FLAGS --with-bdeps=n --oneshot \ |
| 145 | virtual/target-sdk-nobdeps |
| 146 | sudo cp -a "${PKGDIR}" $BOARD_ROOT/packages |
| 147 | |
| 148 | # Copy our chroot version into the newly packaged chroot. |
| 149 | sudo cp -a "${CHROOT_VERSION_FILE}" "${BOARD_ROOT}${CHROOT_VERSION_FILE}" |
| 150 | |
| 151 | # Now cleanup paths referencing the ROOT from the *.la files. |
| 152 | sudo find $BOARD_ROOT -type f -name '*.la' | xargs sudo \ |
| 153 | sed -i -e "s|$BOARD_ROOT/|/|g" |
| 154 | |
| 155 | command_completed |
| 156 | echo "Done!" |
| 157 | echo "The SYSROOT is: ${BOARD_ROOT}" |