blob: b025f6182f624a786d8b174c99eeb8e911247e8a [file] [log] [blame]
# Copyright 2020 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 CB_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 "${CB_SRC_DIR}" ]]; then
echo "CB_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} hatch hatch kohaku b:140261109"
echo "e.g., ${SCRIPT} zork trembyle dalboz"
echo "* Adds a new variant of the baseboard to Kconfig and"
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.
# TODO(b/149391804): fix the baseboard/reference board/variant terminology
REFERENCE="$(to_lower "$2")"
# This is the name of the variant that is being cloned.
VARIANT="$(to_lower "$3")"
VARIANT_UPPER="$(to_upper "${VARIANT}")"
# 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
# Get the directory where this script is located; it is also where
# is located.
pushd "${BASH_SOURCE%/*}"
# The template files are in ${CB_SRC_DIR}/util/mainboard/google/${REFERENCE}/template
# We need to create files in ${CB_SRC_DIR}/src/mainboard/google/${BASE}/variants/${VARIANT}
if [[ ! -e "${CB_SRC_DIR}/src/mainboard/google/${BASE}" ]]; then
echo "The baseboard directory for ${BASE} does not exist."
exit 1
pushd "${CB_SRC_DIR}/src/mainboard/google/${BASE}"
# Make sure the variant doesn't already exist.
if [[ -e "variants/${VARIANT}" ]]; then
echo "variants/${VARIANT} 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}" . "${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.
cd "${CB_SRC_DIR}/src/mainboard/google/${BASE}"
rm -Rf "variants/${VARIANT}"
repo abandon "${BRANCH}" .
trap 'abandon' ERR
# Copy the template tree to the target.
mkdir -p "variants/${VARIANT}/"
cp -pr "${TEMPLATE}/." "variants/${VARIANT}/"
if [[ -e "variants/${VARIANT}/Kconfig" ]]; then
git add "variants/${VARIANT}/"
restore_git() {
# After adding changes to git, now to recover from an error we need to
# remove the variant from the git commit, restore Kconfig and,
# and delete the .new files if they exist.
cd "${CB_SRC_DIR}/src/mainboard/google/${BASE}"
git restore --staged "variants/${VARIANT}"
git restore --staged Kconfig
git restore Kconfig
rm -f
# And call the previous trap function.
trap 'restore_git' ERR
# Now add the new variant to Kconfig and
# These files are in the current directory, e.g. src/mainboard/google/hatch
"${SRC}/" --board "${REFERENCE}" --variant "${VARIANT}"
mv Kconfig
git add Kconfig
# Now commit the files. Use fmt to word-wrap the main commit message.
MSG=$(echo "Create the ${VARIANT} variant of the ${REFERENCE} reference
board by copying the template files to a new directory named for the
variant." | fmt -w 70)
git commit -sm "mb/google/${BASE}: Create ${VARIANT} variant
(Auto-Generated by ${SCRIPT} version ${VERSION}).
TEST=util/abuild/abuild -p none -t google/${BASE} -x -a
make sure the build includes GOOGLE_${VARIANT_UPPER}"
# TODO(b/149702214): verify that it builds correctly