blob: 2b59602fc47dacde99eb6179912b101ea1ba0705 [file] [log] [blame]
#!/bin/bash
# Copyright 2021 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.
VERSION="1.0.0"
SCRIPT="$(basename -- "$0")"
set -e
# Disable a warning about DC_SRC_DIR not being defined; we look for it in
# the environment, and if it isn't found, then we'll exit.
# shellcheck disable=SC2154
if [[ -z "${DC_SRC_DIR}" ]]; then
echo "DC_SRC_DIR must be set in the environment"
exit 1
fi
if [[ "$#" -lt 3 ]]; then
echo "Usage: ${SCRIPT} base_name reference_name variant_name [bug_number]"
echo "e.g., ${SCRIPT} brya brya0 primus"
echo "e.g., ${SCRIPT} brya brask brask"
echo "* Copies the template files for the baseboard to the new variant"
exit 1
fi
to_lower() {
# Convert a string to lower case using ASCII translation rules.
LC_ALL=C LOWER="${1,,}"
echo "${LOWER}"
}
to_upper() {
# Convert a string to upper case using ASCII translation rules.
LC_ALL=C UPPER="${1^^}"
echo "${UPPER}"
}
# shellcheck source=check_standalone.sh
# shellcheck disable=SC1091
source "${BASH_SOURCE%/*}/check_standalone.sh"
check_standalone
# shellcheck source=check_pending_changes.sh
# shellcheck disable=SC1091
source "${BASH_SOURCE%/*}/check_pending_changes.sh"
# This is the name of the base board
BASE="$(to_lower "$1")"
# This is the name of the reference board that we're using to make the variant.
REFERENCE="$(to_lower "$2")"
# This is the name of the variant that is being cloned.
VARIANT="$(to_lower "$3")"
# Assign BUG= text, or "None" if that parameter wasn't specified.
BUG="${4:-None}"
# Assign the value for BRANCH= in the commit message, or use None if unspecified
COMMIT_MSG_BRANCH="${NEW_VARIANT_BRANCH:-None}"
# The template files are in ${DC_SRC_DIR}/util/template
DEFCONFIG="${DC_SRC_DIR}/util/template/board/${BASE}/defconfig"
VARIANTC="${DC_SRC_DIR}/util/template/src/board/${BASE}/variant.c"
pushd "${DC_SRC_DIR}"
# Make sure the variant doesn't already exist.
if [[ -e "${DC_SRC_DIR}/src/board/${VARIANT}/board.c" ]]; then
echo "${VARIANT}/board.c already exists."
echo "Have you already created this variant?"
exit 1
fi
# If there are pending changes, exit the script (unless overridden)
check_pending_changes "$(pwd)"
# Start a branch. Use YMD timestamp to avoid collisions.
DATE="$(date +%Y%m%d)"
BRANCH="depthcharge_${VARIANT}_${DATE}"
repo start "${BRANCH}" . --head #"${NEW_VARIANT_WIP:+--head}"
# ${parameter:+word}" substitutes "word" if $parameter is set to a non-null
# value, or substitutes null if $parameter is null or unset.
abandon() {
# If there is an error after the `repo start` and before we start adding
# changes to git, then delete the new variant directory and `repo abandon`
# the new branch.
rm -rRf "${DC_SRC_DIR}/src/board/${BASE}/${VARIANT}"
rm -Rf "${DC_SRC_DIR}/board/${VARIANT}/defconfig"
repo abandon "${BRANCH}" .
}
trap 'abandon' ERR
# Copy template defconfig
pushd "${DC_SRC_DIR}"
mkdir -p "board/${VARIANT}/"
cp "${DEFCONFIG}" "board/${VARIANT}/defconfig"
# Fixup CONFIG_VARIANT_NAME
sed -i -e "s/CONFIG_VARIANT_NAME.*/CONFIG_VARIANT_NAME=\"${VARIANT}\"/" \
"board/${VARIANT}/defconfig"
# Copy variant.c
cp "${VARIANTC}" "src/board/${BASE}/${VARIANT}.c"
git add "src/board/${BASE}/${VARIANT}.c"
git add "board/${VARIANT}/defconfig"
restore_git() {
# After adding changes to git, now to recover from an error we need to
# remove the variant from the git commit
pushd "${DC_SRC_DIR}/"
git restore --staged .
git restore .
popd
# And call the previous trap function.
abandon
}
trap 'restore_git' ERR
# Now commit the files. Use fmt to word-wrap the main commit message.
MSG=$(echo "Create a depthcharge target ${VARIANT} by copying the template
files from the reference ${REFERENCE} to the appropriate
places in the source tree." | fmt -w 70)
git commit -sm "Create ${VARIANT} variant
${MSG}
(Auto-Generated by ${SCRIPT} version ${VERSION}).
BUG=${BUG}
BRANCH=${COMMIT_MSG_BRANCH}
TEST=FW_NAME=${VARIANT} emerge-${BASE} depthcharge
"
# TODO(b/149702214): verify that it builds correctly
popd