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 | " |
Alex Klein | 2998633 | 2018-12-10 12:08:46 -0700 | [diff] [blame] | 28 | |
| 29 | # Parse command line flags |
| 30 | FLAGS "$@" || exit 1 |
| 31 | eval 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. |
| 35 | switch_to_strict_mode |
| 36 | |
| 37 | BOARD=${FLAGS_board} |
| 38 | |
| 39 | # Locations we will need |
| 40 | BOARD_ROOT="/build/${BOARD}" |
| 41 | CROSSDEV_OVERLAY="/usr/local/portage/crossdev" |
| 42 | CHROMIUMOS_OVERLAY="/usr/local/portage/chromiumos" |
| 43 | CHROMIUMOS_CONFIG="${CHROMIUMOS_OVERLAY}/chromeos/config" |
| 44 | CHROMIUMOS_PROFILES="${CHROMIUMOS_OVERLAY}/profiles" |
| 45 | BOARD_ETC="${BOARD_ROOT}/etc" |
| 46 | BOARD_SETUP="${BOARD_ETC}/make.conf.board_setup" |
| 47 | BOARD_PROFILE="${BOARD_ETC}/portage/profile" |
| 48 | |
| 49 | eval $(portageq envvar -v CHOST PKGDIR) |
| 50 | |
| 51 | SYSROOT_EXISTS=false |
| 52 | if [ -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 |
| 64 | fi |
| 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. |
| 71 | cmds=( |
| 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 | ) |
| 76 | for d in "${SCRIPTS_DIR}"/hooks/*; do |
| 77 | cmds+=( "ln -sfT '${d}' '${BOARD_ROOT}/etc/portage/hooks/${d##*/}'" ) |
| 78 | done |
| 79 | sudo_multi "${cmds[@]}" |
| 80 | |
| 81 | # Generating the standard configuration file (make.conf.board_setup) for the |
| 82 | # sysroot. |
| 83 | cros_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. |
| 88 | cros_sysroot_utils create-wrappers --sysroot="${BOARD_ROOT}" \ |
| 89 | --friendlyname="${BOARD}" |
| 90 | |
| 91 | # Choose the default profile. |
| 92 | if ! 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!" |
| 96 | fi |
| 97 | |
| 98 | cmds=( |
| 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 | ) |
| 116 | sudo_multi "${cmds[@]}" |
| 117 | |
| 118 | EMERGE_CMD="${CHROMITE_BIN}/parallel_emerge" |
| 119 | TOOLCHAIN_PACKAGES=( |
| 120 | $("${CHROMITE_BIN}/cros_setup_toolchains" --show-packages host) |
| 121 | ) |
| 122 | # Sanity check we got some valid results. |
| 123 | if [[ ${#TOOLCHAIN_PACKAGES[@]} -eq 0 ]]; then |
| 124 | die_notrace "cros_setup_toolchains failed" |
| 125 | fi |
| 126 | PACKAGES=( system virtual/target-sdk world ) |
| 127 | |
Mike Frysinger | 9c7529d | 2020-05-21 19:53:58 -0400 | [diff] [blame] | 128 | run_emerge() { |
Mike Frysinger | 5cf035f | 2021-01-26 00:57:46 -0500 | [diff] [blame] | 129 | info_run sudo -E ${EMERGE_CMD} "$@" |
Mike Frysinger | 9c7529d | 2020-05-21 19:53:58 -0400 | [diff] [blame] | 130 | } |
| 131 | |
Alex Klein | 2998633 | 2018-12-10 12:08:46 -0700 | [diff] [blame] | 132 | # First, rebuild all packages from scratch. This is needed to make sure |
| 133 | # we rebuild all chroot packages. |
Mike Frysinger | d919057 | 2018-12-20 18:04:51 -0500 | [diff] [blame] | 134 | |
| 135 | # We build the toolchain by hand to avoid race conditions where the toolchain |
| 136 | # is used by other packages that we're building. See https://crbug.com/906289. |
Mike Frysinger | 9c7529d | 2020-05-21 19:53:58 -0400 | [diff] [blame] | 137 | run_emerge "${TOOLCHAIN_PACKAGES[@]}" |
Mike Frysinger | d919057 | 2018-12-20 18:04:51 -0500 | [diff] [blame] | 138 | |
| 139 | # Then build everything else. |
Mike Frysinger | 9c7529d | 2020-05-21 19:53:58 -0400 | [diff] [blame] | 140 | run_emerge --emptytree --with-bdeps=y \ |
Mike Frysinger | 5d66158 | 2021-01-21 23:35:29 -0500 | [diff] [blame] | 141 | --exclude --verbose "${TOOLCHAIN_PACKAGES[*]}" \ |
Mike Frysinger | d919057 | 2018-12-20 18:04:51 -0500 | [diff] [blame] | 142 | "${PACKAGES[@]}" virtual/target-sdk-nobdeps |
Alex Klein | 2998633 | 2018-12-10 12:08:46 -0700 | [diff] [blame] | 143 | sudo eclean -d packages |
| 144 | |
| 145 | # Next, install our rebuilt packages into our separate root. |
| 146 | HOST_FLAGS="--root=$BOARD_ROOT --update --verbose --deep --root-deps" |
| 147 | HOST_FLAGS+=" --newuse --usepkgonly" |
Mike Frysinger | 9c7529d | 2020-05-21 19:53:58 -0400 | [diff] [blame] | 148 | run_emerge $HOST_FLAGS --with-bdeps=y "${PACKAGES[@]}" |
Alex Klein | 2998633 | 2018-12-10 12:08:46 -0700 | [diff] [blame] | 149 | # Install our rebuilt packages from the nobdeps target into our separate root |
| 150 | # without their build-time deps. We also avoid adding this target to the |
| 151 | # world set so that subsequent update_chroot commands won't re-import the |
| 152 | # build deps. |
Mike Frysinger | 9c7529d | 2020-05-21 19:53:58 -0400 | [diff] [blame] | 153 | run_emerge $HOST_FLAGS --with-bdeps=n --oneshot \ |
Alex Klein | 2998633 | 2018-12-10 12:08:46 -0700 | [diff] [blame] | 154 | virtual/target-sdk-nobdeps |
| 155 | sudo cp -a "${PKGDIR}" $BOARD_ROOT/packages |
| 156 | |
| 157 | # Copy our chroot version into the newly packaged chroot. |
| 158 | sudo cp -a "${CHROOT_VERSION_FILE}" "${BOARD_ROOT}${CHROOT_VERSION_FILE}" |
| 159 | |
| 160 | # Now cleanup paths referencing the ROOT from the *.la files. |
Mike Frysinger | 874c24b | 2019-02-22 00:46:33 -0500 | [diff] [blame] | 161 | sudo find "${BOARD_ROOT}" -type f -name '*.la' -exec \ |
| 162 | sed -i -e "s|${BOARD_ROOT}/|/|g" {} + |
Alex Klein | 2998633 | 2018-12-10 12:08:46 -0700 | [diff] [blame] | 163 | |
| 164 | command_completed |
| 165 | echo "Done!" |
| 166 | echo "The SYSROOT is: ${BOARD_ROOT}" |