blob: 2b59602fc47dacde99eb6179912b101ea1ba0705 [file] [log] [blame]
# 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.
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
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
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
# shellcheck disable=SC1091
source "${BASH_SOURCE%/*}/"
# shellcheck
# shellcheck disable=SC1091
source "${BASH_SOURCE%/*}/"
# 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.
# Assign the value for BRANCH= in the commit message, or use None if unspecified
# The template files are in ${DC_SRC_DIR}/util/template
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
# 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)"
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"
# 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 .
# And call the previous trap function.
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
(Auto-Generated by ${SCRIPT} version ${VERSION}).
TEST=FW_NAME=${VARIANT} emerge-${BASE} depthcharge
# TODO(b/149702214): verify that it builds correctly