0
0
Fork 0
mirror of https://github.com/MarlinFirmware/Marlin.git synced 2025-02-02 07:00:42 +00:00
MarlinFirmware/buildroot/bin/build_example

168 lines
4.7 KiB
Text
Raw Normal View History

#!/usr/bin/env bash
#
# Usage:
#
# build_example -b|--base=<path> - Configurations root folder (e.g., ./.pio/build-BRANCH)
# -c|--config=<rel> - Sub-path of the configs to build (within config/examples)
# [-e|--export=N] - Set CONFIG_EXPORT before build and export into the config folder
# [-n|--nofail] - Don't stop on a failed build
# [-r|--reveal] - Reveal the config folder after the build
# [-h|--help] - Print usage and exit
# [-a|--archive] - Archive the build (to the export location)
# [--allow] - Allow this script to run standalone
#
2024-01-23 05:31:04 +00:00
HERE=`dirname $0`
source "$HERE/mfutil"
annc() { echo -e "\033[0;32m$1\033[0m" ; }
# Get arguments
2024-08-26 17:58:04 +00:00
BUILD=./.pio/build
CLEANER=
ALLOW=
ARCHIVE=
BASE=
CONFIG=
REVEAL=
EXPNUM=
NOFAIL=
while getopts 'ab:c:e:hinr-:' OFLAG; do
case "${OFLAG}" in
a) ARCHIVE=1 ;;
2024-08-26 17:58:04 +00:00
b) BASE="${OPTARG%/}" ;;
c) CONFIG="${OPTARG%/}" ;;
e) EXPNUM="$OPTARG" ;;
h) EXIT_USAGE=1 ; break ;;
n) NOFAIL=1 ;;
r) REVEAL=1 ;;
-) IFS="=" read -r ONAM OVAL <<< "$OPTARG"
case "$ONAM" in
archive) ARCHIVE=1 ;;
allow) ALLOW=1 ;;
2024-08-26 17:58:04 +00:00
base) BASE="${OVAL%/}" ;;
config) CONFIG="${OVAL%/}" ;;
export) EXPNUM="$OVAL" ;;
help) EXIT_USAGE=1 ; break ;;
nofail) NOFAIL=1 ;;
reveal) REVEAL=1 ;;
*) EXIT_USAGE=2 ; echo "$SELF: unrecognized option \`--$ONAM'" ; break ;;
esac
;;
esac
done
2024-08-26 17:58:04 +00:00
# Must be called from another script (or with --allow)
[[ $ALLOW || $SHLVL -gt 2 ]] || { echo "Don't call this script directly, use build_all_examples instead." ; exit 1 ; }
2024-08-26 17:58:04 +00:00
# -b|--base and -c|--config are required
[[ -z $BASE ]] && { echo "-b|--base is required" ; exit 1 ; }
[[ -z $CONFIG ]] && { echo "-c|--config is required" ; exit 1 ; }
# Make sure the examples exist
SUB1="$BASE/config/examples"
2024-08-26 17:58:04 +00:00
[[ -d "$SUB1" ]] || { echo "-b|--base $BASE doesn't contain config/examples" ; exit 1 ; }
# Make sure the specific config folder exists
SUB="$SUB1/$CONFIG"
2024-08-26 17:58:04 +00:00
[[ -d "$SUB" ]] || { echo "-c|--config $CONFIG doesn't exist" ; exit 1 ; }
2024-08-26 17:58:04 +00:00
# ...and contains Configuration.h or Configuration_adv.h
[[ -n $(compgen -G "$SUB/Configuration*.h") ]] || { echo "No configuration files found in $SUB" ; exit 1 ; }
2024-08-26 17:58:04 +00:00
# Delete any config files from previous builds
rm -f Marlin/_Bootscreen.h Marlin/_Statusscreen.h
2024-08-26 17:58:04 +00:00
# Copy configurations into the Marlin folder
echo "Getting configuration files from $SUB"
2024-08-26 17:58:04 +00:00
cp "$BASE/config/default"/*.h "$SUB"/*.h Marlin/ 2>/dev/null
rm -f Marlin/Config.h Marlin/Config-export.h
set -e
# Strip #error lines from Configuration.h
IFS=$'\n'; set -f
$SED -i~ -e "20,30{/#error/d}" Marlin/Configuration.h
rm Marlin/Configuration.h~
unset IFS; set +f
# Suppress fatal warnings
if ((CLEANER)); then
opt_add NO_CONTROLLER_CUSTOM_WIRING_WARNING
opt_add NO_AUTO_ASSIGN_WARNING
opt_add NO_CREALITY_DRIVER_WARNING
opt_add DIAG_JUMPERS_REMOVED
opt_add DIAG_PINS_REMOVED
opt_add NO_MK3_FAN_PINS_WARNING
opt_add NO_USER_FEEDBACK_WARNING
opt_add NO_Z_SAFE_HOMING_WARNING
opt_add NO_LCD_CONTRAST_WARNING
opt_add NO_MICROPROBE_WARNING
opt_add NO_CONFIGURATION_EMBEDDING_WARNING
opt_add NO_HOMING_CURRENT_WARNING
fi
2024-08-26 17:58:04 +00:00
ENAME=("-name" "marlin_config.json" \
"-o" "-name" "config.ini" \
"-o" "-name" "schema.json" \
"-o" "-name" "schema.yml")
2024-08-26 17:58:04 +00:00
BNAME=("-type" "f" \
"-name" 'firmware*.hex' \
"-o" "-name" "firmware*.bin" \
"-o" "-name" "project*.bin" \
"-o" "-name" "Robin*.bin" \
"-o" "-name" "main_*.bin")
mkdir -p "$BUILD"
# If EXPNUM is set then apply to the config before build
if [[ $EXPNUM ]]; then
opt_set CONFIG_EXPORT $EXPNUM
# Clean up old exports
2024-08-26 17:58:04 +00:00
find "$BUILD" \( "${ENAME[@]}" \) -exec rm "{}" \;
fi
2024-08-26 17:58:04 +00:00
((ARCHIVE)) && find "$BUILD" \( "${BNAME[@]}" \) -exec rm "{}" \;
set +e
echo "Building example $CONFIG ..."
"$HERE/mftest" -s -a -n1 ; ERR=$?
2024-08-26 17:58:04 +00:00
((ERR)) && echo "Failed" || echo "Success"
set -e
# Copy exports back to the configs
if [[ -n $EXPNUM ]]; then
annc "Exporting $EXPNUM"
[[ -f Marlin/Config-export.h ]] && { cp Marlin/Config-export.h "$SUB"/Config.h ; }
2024-08-26 17:58:04 +00:00
find "$BUILD" "${ENAME[@]}" -exec cp "{}" "$SUB" \;
fi
2024-08-26 17:58:04 +00:00
if ((ARCHIVE)); then
annc "Archiving"
find "$BUILD" \( "${BNAME[@]}" \) -exec sh -c '
SUB="$1"
shift 1
for file in "$@"; do
cd "$(dirname "$file")"
base=$(basename "$file")
tar -czf "$SUB/$base.tar.gz" "$base"
rm "$base"
cd - >/dev/null
done
' sh "$SUB" {} +
fi
# Exit with error unless --nofail is set
[[ $ERR -gt 0 && -z $NOFAIL ]] && exit $ERR
# Reveal the configs after the build, if requested
((REVEAL)) && { annc "Revealing $SUB" ; open "$SUB" ; }
exit 0