blob: cda8b60e5210a423125520aef5f9db5216e49a8c [file] [log] [blame]
#!/bin/bash
# Copyright 2022 The ChromiumOS Authors
# 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
export LC_ALL=C.UTF-8
export LANG=C.UTF-8
if [[ ! -e /etc/cros_chroot_version ]]; then
echo "This script must be run inside the chroot."
exit 1
fi
if [[ "$#" -lt 3 ]]; then
echo "Usage: ${SCRIPT} base reference_name variant_name [trunk_name] [bug_number]"
echo "e.g. ${SCRIPT} nissa nereid destiny chromiumos b:229550821"
echo "Creates the initial EC image as a copy of the reference board's EC."
echo "trunk_path is an optional parameter, script uses 'chromiumos' as default path"
exit 1
fi
# 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.
# ${var,,} converts to all lowercase.
BASE="${1,,}"
# This is the name of the reference board that we copying to make the variant.
# ${var,,} converts to all lowercase.
REF="${2,,}"
# This is the name of the variant that is being cloned.
VARIANT="${3,,}"
# This is the trunk path for the source. Default is "chromiumos" if not specified.
TRUNK=${4:-"chromiumos"}
# Assign BUG= text, or "None" if that parameter wasn't specified.
BUG=${5:-None}
# Assign the value for BRANCH= in the commit message, or use None if unspecified
COMMIT_MSG_BRANCH="${NEW_VARIANT_BRANCH:-None}"
# All of the necessary files are in platform/ec/zephyr/program
WORKDIR="${HOME}/${TRUNK}/src/platform/ec/zephyr/program"
cd "${WORKDIR}"
# Make sure that the reference board exists.
BUILD_PY="${BASE}/BUILD.py"
if [[ ! -e "${BUILD_PY}" ]]; then
echo "${BASE} does not exist or is not a Zephyr EC build."
echo "Please specify a valid reference board."
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="create_${VARIANT}_${DATE}"
# Store current branch information to restore in case of failure
CURRENT_BRANCH=`git describe --all | cut -d '/' -f 2-3`
# Checkout new branch to work on.
# In case script has already been running, skip new branch creation.
if [ -z "${NEW_VARIANT_WIP}" ]; then
git checkout -b "${BRANCH}" -t "${CURRENT_BRANCH}"
fi
cleanup() {
# If there is an error after the `repo start`, then remove the added files
# and `repo abandon` the new branch.
cd "${WORKDIR}"
git restore "${BUILD_PY}" || true
git checkout "${CURRENT_BRANCH}"
git branch -D "${BRANCH}"
}
trap 'cleanup' ERR
# Copy the reference board EC files into a new directory named for the variant.
mkdir -p "${BASE}"/"${VARIANT}"
cp -r "${BASE}"/"${REF}"/* "${BASE}"/"${VARIANT}"
# Add the new variant to BUILD.py
echo "${VARIANT} = register_${BASE}_project(project_name=\"${VARIANT}\",)" >> "${BUILD_PY}"
# Update Cmakelist.txt to include new variant
CMAKE="elseif(DEFINED CONFIG_BOARD_${VARIANT^^})\n project(${VARIANT})\n add_subdirectory(${VARIANT})\nendif()"
sed -i 's/.*endif.*/'"${CMAKE}"'/' "${BASE}/CMakeLists.txt"
# Update Kconfig for new variant
KCONF_PATH="${BASE}/Kconfig"
CONFIG="config BOARD_${BASE^^}" # CONFIG string to be searched
KCONFIG=`awk "/\<${CONFIG}\>/ {print}" RS= ${KCONF_PATH}`
KCONFIG_NEW=`echo "${KCONFIG}" | sed -e "s/${BASE}/${VARIANT^^}/gI"`
echo "${KCONFIG_NEW}" >> "${BASE}/Kconfig"
# Add RW_FWID addr assertion line for build
NEW_LINE=`grep -rsn "assert_rw_fwid_DO_NOT_EDIT(project_name=\"${REF}\"" | cut -d ':' -f3`
UPDATED_FWID=${NEW_LINE//$REF/$VARIANT}
echo "${UPDATED_FWID}" >> "${BUILD_PY}"
# Format the code
black "${BUILD_PY}"
# Build the code; exit if it fails.
zmake build "${VARIANT}"
# Add the changed file
git add "${BUILD_PY}"
git add "${BASE}"/"${VARIANT}"
# Now commit the files. Use fmt to word-wrap the main commit message.
MSG=$(echo "Create the initial Zephyr EC image for the ${VARIANT} variant
based on the ${REF} reference board." | fmt -w 70)
git commit -sm "${VARIANT}: Initial Zephyr EC image
${MSG}
(Auto-Generated by ${SCRIPT} version ${VERSION}).
BUG=${BUG}
BRANCH=${COMMIT_MSG_BRANCH}
TEST=`zmake build ${VARIANT} && echo PASS`"