From e4e3decfc0601d4e7e96c48216f3f638eae6ed8b Mon Sep 17 00:00:00 2001 From: "J. Richard Barnette" Date: Mon, 8 Aug 2011 16:35:02 -0700 Subject: [PATCH] Extract some common code for sharing with build_image, et al. BUG=None TEST=build regular, test, and recovery images Change-Id: I2d7d073c27d14fb88be6a63953dcc77fc76a0807 Reviewed-on: http://gerrit.chromium.org/gerrit/5512 Tested-by: Richard Barnette Reviewed-by: Chris Sosa Reviewed-by: David James --- build_image | 81 +++++++++++----------------------- build_library/board_options.sh | 32 ++++++++++++++ mod_image_for_recovery.sh | 56 +++++++---------------- mod_image_for_test.sh | 62 +++++++++----------------- 4 files changed, 97 insertions(+), 134 deletions(-) create mode 100644 build_library/board_options.sh diff --git a/build_image b/build_image index ccc580ac63..710a4fce08 100755 --- a/build_image +++ b/build_image @@ -12,8 +12,6 @@ SCRIPT_ROOT=$(dirname "$0") . "${SCRIPT_ROOT}/build_library/build_common.sh" || exit 1 -. "${SCRIPT_ROOT}/build_library/build_gpt.sh" || exit 1 - DEFINE_string board "${DEFAULT_BOARD}" \ "The board to build an image for." @@ -83,14 +81,28 @@ eval set -- "${FLAGS_ARGV}" # so will die prematurely if 'set -e' is specified before now. set -e -if [ -z "${FLAGS_board}" ] ; then - error "--board is required." - exit 1 +. "${SCRIPT_ROOT}/build_library/board_options.sh" || exit 1 + +# Determine build version. +OVERLAY_CHROMEOS_DIR="${SRC_ROOT}/third_party/chromiumos-overlay/chromeos" +. "${OVERLAY_CHROMEOS_DIR}/config/chromeos_version.sh" || exit 1 + +. "${SCRIPT_ROOT}/build_library/build_gpt.sh" || exit 1 + + +EMERGE_BOARD_CMD="emerge-$BOARD" +if [ $FLAGS_fast -eq $FLAGS_TRUE ]; then + echo "Using alternate emerge" + EMERGE_BOARD_CMD="$GCLIENT_ROOT/chromite/bin/parallel_emerge" + EMERGE_BOARD_CMD="$EMERGE_BOARD_CMD --board=$BOARD" +fi +if [ $FLAGS_jobs -ne -1 ]; then + EMERGE_JOBS="--jobs=$FLAGS_jobs" fi # Perform an eclean to remove packages which are not installed -if [[ ${FLAGS_eclean} -eq ${FLAGS_TRUE} ]]; then - eclean-${FLAGS_board} -d packages +if [ $FLAGS_eclean -eq $FLAGS_TRUE ]; then + eclean-$BOARD -d packages fi check_blacklist() { @@ -102,7 +114,7 @@ check_blacklist() { return fi local blacklisted_packages=$(${SCRIPTS_DIR}/get_package_list \ - --board="${FLAGS_board}" chromeos-base/chromeos \ + --board="${BOARD}" chromeos-base/chromeos \ | grep -x -f "${package_blacklist_file}") if [ -n "${blacklisted_packages}" ]; then die "Blacklisted packages found: ${blacklisted_packages}." @@ -171,43 +183,6 @@ if [ $((FLAGS_rootfs_size + FLAGS_rootfs_hash_pad)) -gt \ bigger than partition (${FLAGS_rootfs_partition_size} MiB)." fi -EMERGE_BOARD_CMD="emerge-${FLAGS_board}" -if [ ${FLAGS_fast} -eq ${FLAGS_TRUE} ]; then - echo "Using alternate emerge" - EMERGE_CMD="${GCLIENT_ROOT}/chromite/bin/parallel_emerge" - EMERGE_BOARD_CMD="${EMERGE_CMD} --board=${FLAGS_board}" -fi - -OVERLAY_CHROMEOS_DIR="${SRC_ROOT}/third_party/chromiumos-overlay/chromeos/" - -# Determine build version. -. "${OVERLAY_CHROMEOS_DIR}/config/chromeos_version.sh" - -BOARD="${FLAGS_board}" -BOARD_ROOT="${FLAGS_build_root}/${BOARD}" - -# What cross-build are we targeting? -. "${BOARD_ROOT}/etc/make.conf.board_setup" -LIBC_VERSION=${LIBC_VERSION} - -# Figure out ARCH from the given toolchain. -# TODO: Move to common.sh as a function after scripts are switched over. -TC_ARCH=$(echo "${CHOST}" | awk -F'-' '{ print $1 }') -case "${TC_ARCH}" in - arm*) - ARCH="arm" - ;; - *86) - ARCH="x86" - ;; - *x86_64) - ARCH="amd64" - ;; - *) - error "Unable to determine ARCH from toolchain: ${CHOST}" - exit 1 -esac - # Configure extra USE or packages for this type of build. EXTRA_PACKAGES="" if [ ${FLAGS_factory_install} -eq ${FLAGS_TRUE} ] ; then @@ -238,9 +213,9 @@ emerge_to_image() { # Use canonical path since some tools (e.g. mount) do not like symlinks. # Append build attempt to output directory. IMAGE_SUBDIR="${CHROMEOS_VERSION_STRING}-a${FLAGS_build_attempt}" -OUTPUT_DIR="${FLAGS_output_root}/${FLAGS_board}/${IMAGE_SUBDIR}" +OUTPUT_DIR="${FLAGS_output_root}/${BOARD}/${IMAGE_SUBDIR}" -OUTSIDE_OUTPUT_DIR="../build/images/${FLAGS_board}/${IMAGE_SUBDIR}" +OUTSIDE_OUTPUT_DIR="../build/images/${BOARD}/${IMAGE_SUBDIR}" # If we are creating a developer image, also create a pristine image with a # different name. @@ -280,10 +255,6 @@ STATEFUL_LOOP_DEV= # translate to /usr/local. DEV_IMAGE_ROOT="${STATEFUL_FS_DIR}/dev_image" -if [ ${FLAGS_jobs} -ne -1 ]; then - EMERGE_JOBS="--jobs=${FLAGS_jobs}" -fi - if [[ ${FLAGS_enable_rootfs_verification} -eq ${FLAGS_TRUE} ]]; then enable_rootfs_verification_flag="--enable_rootfs_verification" fi @@ -688,7 +659,7 @@ emerge_chromeos_test() { } install_autotest_for_factory() { - local autotest_src="${FLAGS_build_root}/${FLAGS_board}/usr/local/autotest" + local autotest_src="${BOARD_ROOT}/usr/local/autotest" local stateful_root="${ROOT_FS_DIR}/usr/local" local autotest_client="${stateful_root}/autotest" @@ -769,7 +740,7 @@ mod_image_for_test () { mod_factory_script="${SCRIPTS_DIR}/mod_for_factory_scripts/factory_setup.sh" # Run factory setup script to modify the image sudo -E GCLIENT_ROOT="${GCLIENT_ROOT}" ROOT_FS_DIR="${ROOT_FS_DIR}" \ - BOARD="${FLAGS_board}" "${mod_factory_script}" + BOARD="${BOARD}" "${mod_factory_script}" fi # Re-run ldconfig to fix /etc/ldconfig.so.cache. @@ -841,8 +812,8 @@ rmdir "${ROOT_FS_DIR}" "${STATEFUL_FS_DIR}" "${ESP_FS_DIR}" # Generating AU generator zip file to run outside chroot generate_au_zip || echo "Failed generating AU zip file - ignoring Error..." # Create a 'latest' link -rm -f ${FLAGS_output_root}/${FLAGS_board}/latest -ln -s $(basename ${OUTPUT_DIR}) ${FLAGS_output_root}/${FLAGS_board}/latest +rm -f ${FLAGS_output_root}/${BOARD}/latest +ln -s $(basename ${OUTPUT_DIR}) ${FLAGS_output_root}/${BOARD}/latest echo "Done. Image created in ${OUTPUT_DIR}" echo "Chromium OS image created as ${PRISTINE_IMAGE_NAME}" diff --git a/build_library/board_options.sh b/build_library/board_options.sh new file mode 100644 index 0000000000..b108d3fee2 --- /dev/null +++ b/build_library/board_options.sh @@ -0,0 +1,32 @@ +# Copyright (c) 2011 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. + +if [ -z "${FLAGS_board}" ]; then + error "--board is required." + exit 1 +fi + +BOARD="${FLAGS_board}" +BOARD_ROOT="${FLAGS_build_root}/${BOARD}" + +# What cross-build are we targeting? +. "${BOARD_ROOT}/etc/make.conf.board_setup" + +# Figure out ARCH from the given toolchain. +# TODO(jrbarnette): There's a copy of this code in setup_board; +# it should be shared. +case "$(echo "${CHOST}" | awk -F'-' '{ print $1 }')" in + arm*) + ARCH="arm" + ;; + *86) + ARCH="x86" + ;; + *x86_64) + ARCH="amd64" + ;; + *) + error "Unable to determine ARCH from toolchain: ${CHOST}" + exit 1 +esac diff --git a/mod_image_for_recovery.sh b/mod_image_for_recovery.sh index d1d35d3436..a118a4bce0 100755 --- a/mod_image_for_recovery.sh +++ b/mod_image_for_recovery.sh @@ -55,6 +55,10 @@ DEFINE_string keys_dir "/usr/share/vboot/devkeys" \ 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 'set -e' is specified before now. +set -e + if [ $FLAGS_verbose -eq $FLAGS_FALSE ]; then exec 2>/dev/null # Redirecting to stdout instead of stderr since @@ -66,52 +70,28 @@ if [ $FLAGS_verbose -eq $FLAGS_FALSE ]; then fi set -x # Make debugging with -v easy. -EMERGE_CMD="emerge" -EMERGE_BOARD_CMD="emerge-${FLAGS_board}" -# No board, no default and no image set then we can't find the image -if [ -z $FLAGS_image ] && [ -z $FLAGS_board ] ; then - setup_board_warning - die "mod_image_for_recovery failed. No board set and no image set" -fi +. "${SCRIPT_ROOT}/build_library/board_options.sh" || exit 1 + + +EMERGE_BOARD_CMD="emerge-$BOARD" # We have a board name but no image set. Use image at default location -if [ -z $FLAGS_image ] ; then - IMAGES_DIR="${DEFAULT_BUILD_ROOT}/images/${FLAGS_board}" - FILENAME="chromiumos_image.bin" - FLAGS_image="${IMAGES_DIR}/$(ls -t $IMAGES_DIR 2>&-| head -1)/${FILENAME}" +if [ -z "$FLAGS_image" ]; then + IMAGES_DIR="$DEFAULT_BUILD_ROOT/images/$BOARD" + FILENAME="$CHROMEOS_IMAGE_NAME" + FLAGS_image="$IMAGES_DIR/$(ls -t $IMAGES_DIR 2>&-| head -1)/$FILENAME" fi # Turn path into an absolute path. -FLAGS_image=`eval readlink -f ${FLAGS_image}` +FLAGS_image=$(eval readlink -f "$FLAGS_image") # Abort early if we can't find the image -if [ ! -f $FLAGS_image ] ; then +if [ ! -f "$FLAGS_image" ]; then echo "No image found at $FLAGS_image" exit 1 fi -# What cross-build are we targeting? -. "${FLAGS_build_root}/${FLAGS_board}/etc/make.conf.board_setup" -# Figure out ARCH from the given toolchain. -# TODO: Move to common.sh as a function after scripts are switched over. -TC_ARCH=$(echo "${CHOST}" | awk -F'-' '{ print $1 }') -case "${TC_ARCH}" in - arm*) - ARCH="arm" - error "ARM recovery mode is still in the works. Use a normal image for now." - ;; - *86) - ARCH="x86" - ;; - *x86_64) - ARCH="amd64" - ;; - *) - error "Unable to determine ARCH from toolchain: ${CHOST}" - exit 1 -esac - get_install_vblock() { # If it exists, we need to copy the vblock over to stateful # This is the real vblock and not the recovery vblock. @@ -390,8 +370,6 @@ cleanup() { } # main process begins here. - -set -e set -u IMAGE_DIR="$(dirname "$FLAGS_image")" @@ -431,9 +409,9 @@ if [ -z "$INSTALL_VBLOCK" ]; then fi # Build the recovery kernel. -FACTORY_ROOT="${FLAGS_build_root}/${FLAGS_board}/factory-root" -USE="fbconsole initramfs" emerge_custom_kernel "$FACTORY_ROOT" \ - || failboat "Cannot emerge custom kernel" +FACTORY_ROOT="${BOARD_ROOT}/factory-root" +USE="fbconsole initramfs" emerge_custom_kernel "$FACTORY_ROOT" || + failboat "Cannot emerge custom kernel" if [ -z "$FLAGS_kernel_image" ]; then create_recovery_kernel_image diff --git a/mod_image_for_test.sh b/mod_image_for_test.sh index 7222db59f8..bd8e832e58 100755 --- a/mod_image_for_test.sh +++ b/mod_image_for_test.sh @@ -49,49 +49,38 @@ DEFINE_boolean standard_backdoor ${FLAGS_TRUE} \ FLAGS "$@" || exit 1 eval set -- "$FLAGS_ARGV" -EMERGE_CMD="emerge" -EMERGE_BOARD_CMD="emerge-$FLAGS_board" +# Only now can we die on error. shflags functions leak non-zero error codes, +# so will die prematurely if 'set -e' is specified before now. +set -e + +. "${SCRIPT_ROOT}/build_library/board_options.sh" || exit 1 + + +EMERGE_BOARD_CMD="emerge-$BOARD" if [ $FLAGS_fast -eq $FLAGS_TRUE ]; then echo "Using alternate emerge" - EMERGE_CMD="$GCLIENT_ROOT/chromite/bin/parallel_emerge" - EMERGE_BOARD_CMD="$EMERGE_CMD --board=$FLAGS_board" + EMERGE_BOARD_CMD="$GCLIENT_ROOT/chromite/bin/parallel_emerge" + EMERGE_BOARD_CMD="$EMERGE_BOARD_CMD --board=$BOARD" fi - -# No board, no default and no image set then we can't find the image -if [ -z "$FLAGS_image" -a -z "$FLAGS_board" ] ; then - setup_board_warning - die "mod_image_for_test failed. No board set and no image set" +if [ $FLAGS_jobs -ne -1 ]; then + EMERGE_JOBS="--jobs=$FLAGS_jobs" fi # We have a board name but no image set. Use image at default location -if [ -z "$FLAGS_image" ] ; then - IMAGES_DIR="$DEFAULT_BUILD_ROOT/images/$FLAGS_board" +if [ -z "$FLAGS_image" ]; then + IMAGES_DIR="$DEFAULT_BUILD_ROOT/images/$BOARD" FILENAME="$CHROMEOS_IMAGE_NAME" FLAGS_image="$IMAGES_DIR/$(ls -t $IMAGES_DIR 2>&-| head -1)/$FILENAME" fi # Turn path into an absolute path. -FLAGS_image=$(eval readlink -f $FLAGS_image) +FLAGS_image=$(eval readlink -f "$FLAGS_image") -# What cross-build are we targeting? -. "$FLAGS_build_root/$FLAGS_board/etc/make.conf.board_setup" -# Figure out ARCH from the given toolchain. -# TODO: Move to common.sh as a function after scripts are switched over. -TC_ARCH=$(echo "$CHOST" | awk -F'-' '{ print $1 }') -case "$TC_ARCH" in - arm*) - ARCH="arm" - ;; - *86) - ARCH="x86" - ;; - *x86_64) - ARCH="amd64" - ;; - *) - error "Unable to determine ARCH from toolchain: $CHOST" - exit 1 -esac +# Abort early if we can't find the image +if [ ! -f "$FLAGS_image" ]; then + echo "No image found at $FLAGS_image" + exit 1 +fi # Make sure anything mounted in the rootfs/stateful is cleaned up ok on exit. cleanup_mounts() { @@ -127,7 +116,7 @@ emerge_chromeos_test() { install_autotest() { - local autotest_src="$FLAGS_build_root/$FLAGS_board/usr/local/autotest" + local autotest_src="${BOARD_ROOT}/usr/local/autotest" local stateful_root="$ROOT_FS_DIR/usr/local" local autotest_client="$stateful_root/autotest" @@ -196,13 +185,6 @@ else echo "Modifying image $FLAGS_image for test..." fi -# Abort early if we can't find the image -if [ ! -f $FLAGS_image -a $FLAGS_inplace -eq $FLAGS_TRUE ] ; then - die "No image found at $FLAGS_image to modify" -fi - -set -e - IMAGE_DIR=$(dirname "$FLAGS_image") IMAGE_NAME=$(basename "$FLAGS_image") ROOT_FS_DIR="$IMAGE_DIR/rootfs" @@ -236,7 +218,7 @@ if [ $FLAGS_factory -eq $FLAGS_TRUE ]; then MOD_FACTORY_SCRIPT="$SCRIPTS_DIR/mod_for_factory_scripts/factory_setup.sh" # Run factory setup script to modify the image sudo GCLIENT_ROOT="$GCLIENT_ROOT" ROOT_FS_DIR="$ROOT_FS_DIR" \ - BOARD=$FLAGS_board "$MOD_FACTORY_SCRIPT" + BOARD="$BOARD" "$MOD_FACTORY_SCRIPT" fi # Re-run ldconfig to fix /etc/ldconfig.so.cache.