You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

91 lines
2.3 KiB

#!/bin/bash
set -e
TPUT=$(command -v tput 2>/dev/null || echo true)
DIR=$(cd "$(dirname "$0")"; pwd -P)
TOOLCHAIN_IMAGE="uas-ugv-build:latest"
: ${BUILD_DIR:="$DIR/cmake-build"}
DOCKERFILE="$DIR/Dockerfile"
echo_status() {
echo "$($TPUT setaf 2)==> $*$($TPUT sgr0)"
}
get_docker_run_flags() {
if [ -z "$DOCKER_RUN_FLAGS" ]; then
DOCKER_RUN_FLAGS=(--interactive --rm)
# if stdin is a terminal, tell docker to allocate a tty
if [ -t 0 ]; then
DOCKER_RUN_FLAGS+=(--tty)
fi
USB_SERIAL_DEVICES=$(find /dev -name 'ttyUSB0' -o -name 'ttyACM*')
for device in $USB_SERIAL_DEVICES; do
DOCKER_RUN_FLAGS+=(--device "$device")
echo_status "Adding device $device to container"
done
fi
}
build_docker_container() {
echo_status "Building docker container image"
docker build -f "$DOCKERFILE" -t "$TOOLCHAIN_IMAGE" "$DIR"
echo_status "Built docker container image and tagged as $TOOLCHAIN_IMAGE"
}
build_docker() {
mkdir -p "$BUILD_DIR"
command -v setfacl 1>/dev/null && setfacl -m default:u:$USER:rwx "$BUILD_DIR"
build_docker_container
get_docker_run_flags
local targets=$*
if [ -z "$targets" ]; then
targets="all"
fi
echo_status "Building target(s) $targets in container"
docker run \
--volume="$DIR:/esp/uas-ugv" \
"${DOCKER_RUN_FLAGS[@]}" \
"$TOOLCHAIN_IMAGE" \
/esp/uas-ugv/build.sh cmake "$targets"
}
docker_idf() {
mkdir -p "$BUILD_DIR"
command -v setfacl 1>/dev/null && setfacl -m default:u:$USER:rwx "$BUILD_DIR"
build_docker_container
get_docker_run_flags
echo_status "Running idf.py $*"
docker run \
--volume="$DIR:/esp/uas-ugv" \
"${DOCKER_RUN_FLAGS[@]}" \
"$TOOLCHAIN_IMAGE" \
'/bin/bash' -c "cd /esp/uas-ugv && \$IDF_PATH/tools/idf.py $*"
}
build_cmake() {
mkdir -p "$BUILD_DIR"
cd "$BUILD_DIR"
echo_status "CMake configure"
cmake -G Ninja "$DIR"
echo_status "CMake build $*"
cmake --build "$BUILD_DIR" -- "$@"
}
build_clean() {
rm -rf "$BUILD_DIR"
}
CMD="$1"; shift
case $CMD in
docker_container) build_docker_container ;;
docker) build_docker "$@" ;;
docker_idf) docker_idf "$@" ;;
cmake) build_cmake "$@" ;;
clean) build_clean ;;
*) echo "Usage: $0 <docker_container|docker|cmake>" ;;
esac