#!/bin/bash # Build script for ptprnt - simplifies common build configurations # Usage: ./scripts/build.sh [release|debug|clean] [options] set -e SCRIPT_PATH="$( cd -- "$(dirname "$0")" >/dev/null 2>&1 ; pwd -P )" PROJECT_ROOT="${SCRIPT_PATH}/.." # Color output RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[1;33m' BLUE='\033[0;34m' NC='\033[0m' # No Color print_usage() { echo "Usage: $0 [build-type] [options]" echo "" echo "Build Types:" echo " release Build optimized release version (default)" echo " debug Build debug version with symbols" echo " clean Clean build directories" echo "" echo "Options:" echo " --coverage Enable coverage reporting (debug builds only)" echo " --reconfigure Force reconfiguration" echo " --test Run tests after building" echo " -j N Use N parallel jobs (default: auto)" echo "" echo "Examples:" echo " $0 # Build release" echo " $0 debug --test # Build debug and run tests" echo " $0 debug --coverage # Build debug with coverage" echo " $0 clean # Clean all build directories" } # Default values BUILD_TYPE="release" BUILDDIR="builddir" COVERAGE=false RECONFIGURE=false RUN_TESTS=false JOBS="" # Parse arguments while [[ $# -gt 0 ]]; do case $1 in release|debug|clean) BUILD_TYPE="$1" shift ;; --coverage) COVERAGE=true shift ;; --reconfigure) RECONFIGURE=true shift ;; --test) RUN_TESTS=true shift ;; -j) JOBS="-j $2" shift 2 ;; -h|--help) print_usage exit 0 ;; *) echo -e "${RED}Error: Unknown option: $1${NC}" print_usage exit 1 ;; esac done cd "${PROJECT_ROOT}" # Handle clean if [[ "${BUILD_TYPE}" == "clean" ]]; then echo -e "${YELLOW}Cleaning build directories...${NC}" rm -rf builddir builddir-debug echo -e "${GREEN}Clean complete!${NC}" exit 0 fi # Set build directory and options based on build type if [[ "${BUILD_TYPE}" == "debug" ]]; then BUILDDIR="builddir-debug" MESON_OPTS="--buildtype=debug" if [[ "${COVERAGE}" == true ]]; then MESON_OPTS="${MESON_OPTS} -Db_coverage=true" echo -e "${BLUE}Building debug with coverage enabled${NC}" else echo -e "${BLUE}Building debug version${NC}" fi else BUILDDIR="builddir" MESON_OPTS="--buildtype=release" if [[ "${COVERAGE}" == true ]]; then echo -e "${YELLOW}Warning: Coverage is only supported for debug builds, ignoring --coverage${NC}" fi echo -e "${BLUE}Building release version${NC}" fi # Setup or reconfigure build directory if [[ ! -d "${BUILDDIR}" ]] || [[ "${RECONFIGURE}" == true ]]; then if [[ "${RECONFIGURE}" == true ]]; then echo -e "${YELLOW}Reconfiguring build...${NC}" meson setup "${BUILDDIR}" ${MESON_OPTS} --wipe --reconfigure else echo -e "${YELLOW}Setting up build directory...${NC}" meson setup "${BUILDDIR}" ${MESON_OPTS} fi fi # Build echo -e "${YELLOW}Building...${NC}" ninja -C "${BUILDDIR}" ${JOBS} if [[ $? -eq 0 ]]; then echo -e "${GREEN}Build successful!${NC}" echo -e "Binary: ${BUILDDIR}/ptprnt" else echo -e "${RED}Build failed!${NC}" exit 1 fi # Run tests if requested if [[ "${RUN_TESTS}" == true ]]; then echo -e "${YELLOW}Running tests...${NC}" ninja -C "${BUILDDIR}" test if [[ $? -eq 0 ]]; then echo -e "${GREEN}All tests passed!${NC}" else echo -e "${RED}Tests failed!${NC}" exit 1 fi fi # Show binary info echo "" echo -e "${BLUE}Build Information:${NC}" echo " Build type: ${BUILD_TYPE}" echo " Build dir: ${BUILDDIR}" echo " Binary: $(ls -lh ${BUILDDIR}/ptprnt | awk '{print $5, $9}')" echo "" echo -e "${GREEN}Done!${NC}"