Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Base output name on parameters from caller. #2

Merged
merged 2 commits into from
Mar 20, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 49 additions & 16 deletions build-with-cli-lib.sh
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ set -e
INVOKEDIR=$(realpath .)
readonly INVOKEDIR

# we only want tthe first word.
# shellcheck disable=SC2128
SCRIPTPATH="$(realpath "$BASH_SOURCE")"
SCRIPTNAME=$(basename "$SCRIPTPATH")
readonly SCRIPTNAME
Expand Down Expand Up @@ -69,16 +71,28 @@ function _setDefaults {
readonly MCCI_ARDUINO_BOOTLOADER_LIST
}

##############################################################################
# Override this function with one of your own
##############################################################################
function _setProject {
_error "You must provide your own _setProject function"
}

##############################################################################
# Check that project settings are complete
##############################################################################
function _checkProject {
#---- project settings -----
declare -r -g OPTKEYFILE_DEFAULT="$INVOKEDIR/keys/project.pem"
declare -r -g OPTREGION_DEFAULT=us915
declare -r -g OPTNETWORK_DEFAULT=ttn
declare -r -g OPTSUBBAND_DEFAULT=default
declare -r -g OPTCLOCK_DEFAULT=32
declare -r -g OPTXSERIAL_DEFAULT=usb
declare -r -g OPTARDUINO_BOARD_DEFAULT=4610
declare -r -g OPTARDUINO_SOURCE_DEFAULT=libraries/mcci-catena-4430/examples/Catena4430_Sensor/Catena4430_Sensor.ino
[[ -n "$OPTKEYFILE_DEFAULT" ]] || _error "OPTKEYFILE_DEFAULT must be set to a suitable default keyfile location (.pem)"
[[ -n "$OPTREGION_DEFAULT" ]] || _error "OPTREGION_DEFAULT must be set to a value from MCCI_ARDUINO_BOOTLOADER_LIST"
[[ -n "$OPTNETWORK_DEFAULT" ]] || _error "OPTNETWORK_DEFAULT must be set to a target network"
[[ -n "$OPTSUBBAND_DEFAULT" ]] || _error "OPTSUBBAND_DEFAULT must be set"
[[ -n "$OPTCLOCK_DEFAULT" ]] || _error "OPTCLOCK_DEFAULT must be set"
[[ -n "$OPTXSERIAL_DEFAULT" ]] || _error "OPTXSERIAL_DEFAULT must be set"
[[ -n "$OPTARDUINO_BOARD_DEFAULT" ]] || _error "OPTARDUINO_BOARD_DEFAULT must be set"
[[ -n "$OPTARDUINO_SOURCE_DEFAULT" ]] || _error "OPTARDUINO_SOURCE_DEFAULT must be set"
[[ -n "$OPTOUTPUTNAME_DEFAULT" ]] || _error "OPTOUTPUTNAME_DEFAULT must be set"
true
}

##############################################################################
Expand Down Expand Up @@ -121,9 +135,10 @@ function _fatal {
##############################################################################

function _help {
# shellcheck disable=SC2086
less <<.
${PNAME} calls ${SCRIPTNAME} in order to build the target
firmware $(basename ${OPTARDUINO_SOURCE_DEFAULT}) using the arduino-cli tool.
firmware $(basename "${OPTARDUINO_SOURCE_DEFAULT}") using the arduino-cli tool.

All the work is in the script ${SCRIPTNAME}, which is invoked from a
top-level collection, not directly.
Expand Down Expand Up @@ -157,6 +172,10 @@ Options:

--sketch={file} gives the path to the sketch to be built.

--outputname={name} gives the base pattern for the output name. The
default is ${OPTOUTPUTNAME_DEFAULT}, unless --sketch is given, in
which case the default is the name of the sketch.

--debug turns on more debug output.

--help prints this message.
Expand Down Expand Up @@ -187,6 +206,7 @@ function _parseOptions {
#typeset -i OPTVERBOSE=0 -- during init
#typeset -i OPTDEBUG=0
declare -g -i OPTCLEAN=0
declare -g -i OPTSKETCH=0

OPTKEYFILE="${OPTKEYFILE_DEFAULT}"
OPTREGION="${OPTREGION_DEFAULT}"
Expand All @@ -196,6 +216,7 @@ function _parseOptions {
OPTCLOCK="${OPTCLOCK_DEFAULT}"
OPTARDUINO_SOURCE="${OPTARDUINO_SOURCE_DEFAULT}"
OPTARDUINO_BOARD="${OPTARDUINO_BOARD_DEFAULT}"
OPTOUTPUTNAME=

# make sure everything is clean
for opt in "$@"; do
Expand Down Expand Up @@ -238,6 +259,7 @@ function _parseOptions {
;;
"--sketch="* )
OPTARDUINO_SOURCE="${opt#--sketch=}"
OPTSKETCH=1
;;
"--debug" )
OPTDEBUG=1
Expand All @@ -257,7 +279,7 @@ function _setfqbn {
_debug "Set up FTQBN and bootloader"
[[ -z "$OPTARDUINO_BOARD" ]] && _fatal "Arduino board must not be null"
ARDUINO_FQBN="${MCCI_ARDUINO_BOARD_LIST[$OPTARDUINO_BOARD]}"
[[ -z "ARDUINO_FQBN" ]] && _fatal "Arduino board not recognized: $OPTARDUINO_BOARD"
[[ -z "$ARDUINO_FQBN" ]] && _fatal "Arduino board not recognized: $OPTARDUINO_BOARD"

readonly BOOTLOADER_NAME=McciBootloader_"${MCCI_ARDUINO_BOOTLOADER_LIST[$OPTARDUINO_BOARD]}"
}
Expand Down Expand Up @@ -350,9 +372,17 @@ function _setBspVars {
}

function _setupOutput {
_verbose "Setup output tree"
OUTPUT_SIG="v${SKETCHVERSION}-${OPTNETWORK}-${OPTREGION}-${OPTSUBBAND}-clk${OPTCLOCK}-ser${OPTXSERIAL}-${BUILDKEYSIG}"
OUTPUT_ROOT="$(realpath "$INVOKEDIR/build/$(basename "$ARDUINO_SOURCE")-${OUTPUT_SIG}")"
if [[ -n "$OPTOUTPUTNAME" ]]; then
OUTPUT_SLUG="${OPTOUTPUTNAME}-${OUTPUT_SIG}"
elif [[ $OPTSKETCH -ne 0 ]]; then
OUTPUT_SLUG="$(basename "$ARDUINO_SOURCE")-${OUTPUT_SIG}"
else
OUTPUT_SLUG="${OPTOUTPUTNAME_DEFAULT}-${OUTPUT_SIG}"
fi
[[ -n "$OUTPUT_SLUG" ]] || _error "Internal error: empty OUTPUT_SLUG"
OUTPUT_ROOT="$(realpath "$INVOKEDIR/build")/$OUTPUT_SLUG"
_verbose "output tree:" "$OUTPUT_ROOT"
OUTPUT="${OUTPUT_ROOT}/ide"
OUTPUT_BOOTLOADER="${OUTPUT_ROOT}/boot"

Expand Down Expand Up @@ -466,12 +496,14 @@ function _removeOldBuilds {

# do a build
function _buildSketchWithCli {
# shellcheck disable=SC2086
_verbose arduino-cli compile $ARDUINO_CLI_FLAGS \
-b "${ARDUINO_FQBN}":"${ARDUINO_OPTIONS//[[:space:]]/,}" \
--build-path "$OUTPUT" \
--libraries libraries \
"${ARDUINO_SOURCE}"

# shellcheck disable=SC2086
arduino-cli compile $ARDUINO_CLI_FLAGS \
-b "${ARDUINO_FQBN}":"${ARDUINO_OPTIONS//[[:space:]]/,}" \
--build-path "$OUTPUT" \
Expand Down Expand Up @@ -500,21 +532,21 @@ function _buildBootloader {

# copy bootloader images to output dir
_verbose "Save bootloader"
cp -p "$OUTPUT_BOOTLOADER"/arm-none-eabi/release/${BOOTLOADER_NAME}.* "$OUTPUT"
cp -p "$OUTPUT_BOOTLOADER"/arm-none-eabi/release/"${BOOTLOADER_NAME}".* "$OUTPUT"
}

# combine hex images to simplify download
function _combineImages {
_verbose "Combine bootloader and app"

# all lines but the last, then append the main app
ARDUINO_SOURCE_BASE="$(basename ${ARDUINO_SOURCE} .ino)"
head -n-1 "$OUTPUT"/${BOOTLOADER_NAME}.hex | cat - "$OUTPUT"/"${ARDUINO_SOURCE_BASE}".ino.hex > "$OUTPUT"/"${ARDUINO_SOURCE_BASE}"-bootloader.hex
ARDUINO_SOURCE_BASE="$(basename "${ARDUINO_SOURCE}" .ino)"
head -n-1 "$OUTPUT"/"${BOOTLOADER_NAME}".hex | cat - "$OUTPUT"/"${ARDUINO_SOURCE_BASE}".ino.hex > "$OUTPUT"/"${ARDUINO_SOURCE_BASE}"-bootloader.hex

# make a packed DFU variant
_verbose "Make a packed DFU variant"
python3 -m pip --disable-pip-version-check -q install IntelHex
python3 extra/dfu-util/dfuse-pack.py -i "$OUTPUT"/${BOOTLOADER_NAME}.hex -i "$OUTPUT"/"${ARDUINO_SOURCE_BASE}".ino.hex -D 0x040e:0x00a1 "$OUTPUT"/"${ARDUINO_SOURCE_BASE}"-bootloader.dfu
python3 extra/dfu-util/dfuse-pack.py -i "$OUTPUT"/"${BOOTLOADER_NAME}".hex -i "$OUTPUT"/"${ARDUINO_SOURCE_BASE}".ino.hex -D 0x040e:0x00a1 "$OUTPUT"/"${ARDUINO_SOURCE_BASE}"-bootloader.dfu
}

# rename everything
Expand All @@ -539,6 +571,7 @@ function _doBuild {
_setDefaults
_checkPreconditions
_setProject
_checkProject
_parseOptions "$@"
cd "$PDIR"
_checkargs
Expand Down