#!/bin/bash /** * @file build_target.sh * @brief Build specific target */ set -e # Script directory SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" PROJECT_ROOT="$(cd "${SCRIPT_DIR}/../.." && pwd)" # Default values TARGET="" BUILD_TYPE="debug" CLEAN=false VERBOSE=false JOBS=$(nproc) # Color codes RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[1;33m' BLUE='\033[0;34m' NC='\033[0m' # Function to print usage print_usage() { echo "Usage: $0 [OPTIONS]" echo "" echo "Options:" echo " -t, --target Target board (required)" echo " Available: stm32f407_discovery" echo " nxp_s32k144_evb" echo " custom_ecu" echo " -b, --build-type Build type (default: debug)" echo " Available: debug, release" echo " -c, --clean Clean build" echo " -v, --verbose Verbose output" echo " -j, --jobs Number of parallel jobs" echo " -h, --help Show this help" echo "" } # Parse arguments while [[ $# -gt 0 ]]; do case $1 in -t|--target) TARGET="$2" shift 2 ;; -b|--build-type) BUILD_TYPE="$2" shift 2 ;; -c|--clean) CLEAN=true shift ;; -v|--verbose) VERBOSE=true shift ;; -j|--jobs) JOBS="$2" shift 2 ;; -h|--help) print_usage exit 0 ;; *) echo "Unknown option: $1" print_usage exit 1 ;; esac done # Validate target if [ -z "${TARGET}" ]; then echo -e "${RED}Error: Target not specified${NC}" print_usage exit 1 fi # Validate build type case "${BUILD_TYPE}" in debug|release) ;; *) echo -e "${RED}Error: Invalid build type: ${BUILD_TYPE}${NC}" exit 1 ;; esac # Build directories BUILD_DIR="${PROJECT_ROOT}/build/${TARGET}/${BUILD_TYPE}" LOG_DIR="${PROJECT_ROOT}/build/logs" # Create directories mkdir -p "${BUILD_DIR}" mkdir -p "${LOG_DIR}" # Print build info echo -e "${BLUE}========================================${NC}" echo -e "${BLUE} Building Automotive RTOS${NC}" echo -e "${BLUE}========================================${NC}" echo -e " Target: ${YELLOW}${TARGET}${NC}" echo -e " Build Type: ${YELLOW}${BUILD_TYPE}${NC}" echo -e " Jobs: ${YELLOW}${JOBS}${NC}" echo "" # Clean if requested if [ "${CLEAN}" = true ]; then echo -e "${YELLOW}Cleaning build directory...${NC}" rm -rf "${BUILD_DIR}"/* echo -e "${GREEN}✓ Clean complete${NC}" echo "" fi # Configure echo -e "${YELLOW}Configuring build...${NC}" # Set CMake arguments CMAKE_ARGS=( "${PROJECT_ROOT}" "-DTARGET_BOARD=${TARGET}" "-DCMAKE_BUILD_TYPE=${BUILD_TYPE}" "-DCMAKE_TOOLCHAIN_FILE=${PROJECT_ROOT}/cmake/toolchain-${TARGET}.cmake" "-DCMAKE_EXPORT_COMPILE_COMMANDS=ON" ) if [ "${VERBOSE}" = true ]; then CMAKE_ARGS+=("-DCMAKE_VERBOSE_MAKEFILE=ON") fi # Run CMake cd "${BUILD_DIR}" cmake "${CMAKE_ARGS[@]}" 2>&1 | tee "${LOG_DIR}/${TARGET}_${BUILD_TYPE}_configure.log" if [ $? -ne 0 ]; then echo -e "${RED}✗ Configuration failed${NC}" exit 1 fi echo -e "${GREEN}✓ Configuration complete${NC}" echo "" # Build echo -e "${YELLOW}Building...${NC}" make -j${JOBS} 2>&1 | tee "${LOG_DIR}/${TARGET}_${BUILD_TYPE}_build.log" if [ $? -ne 0 ]; then echo -e "${RED}✗ Build failed${NC}" exit 1 fi echo -e "${GREEN}✓ Build complete${NC}" echo "" # Generate artifacts echo -e "${YELLOW}Generating artifacts...${NC}" ELF_FILE="${BUILD_DIR}/automotive_rtos.elf" HEX_FILE="${BUILD_DIR}/automotive_rtos.hex" BIN_FILE="${BUILD_DIR}/automotive_rtos.bin" MAP_FILE="${BUILD_DIR}/automotive_rtos.map" SIZE_FILE="${BUILD_DIR}/size_report.txt" # Generate hex file arm-none-eabi-objcopy -O ihex "${ELF_FILE}" "${HEX_FILE}" # Generate binary file arm-none-eabi-objcopy -O binary "${ELF_FILE}" "${BIN_FILE}" # Generate map file arm-none-eabi-nm -n "${ELF_FILE}" > "${MAP_FILE}" # Generate size report arm-none-eabi-size "${ELF_FILE}" > "${SIZE_FILE}" echo -e "${GREEN}✓ Artifacts generated:${NC}" echo -e " ELF: ${BLUE}${ELF_FILE}${NC}" echo -e " HEX: ${BLUE}${HEX_FILE}${NC}" echo -e " BIN: ${BLUE}${BIN_FILE}${NC}" echo -e " MAP: ${BLUE}${MAP_FILE}${NC}" echo "" # Print size report echo -e "${YELLOW}Size Report:${NC}" cat "${SIZE_FILE}" echo "" # Run static analysis if available if command -v cppcheck >/dev/null 2>&1; then echo -e "${YELLOW}Running static analysis...${NC}" cppcheck \ --enable=all \ --inconclusive \ --std=c11 \ --platform=arm32 \ -I"${PROJECT_ROOT}/kernel/include" \ -I"${PROJECT_ROOT}/drivers/include" \ -I"${PROJECT_ROOT}/middleware/include" \ "${PROJECT_ROOT}/kernel/src" \ "${PROJECT_ROOT}/drivers/src" \ "${PROJECT_ROOT}/middleware/src" \ 2>&1 | tee "${LOG_DIR}/${TARGET}_${BUILD_TYPE}_cppcheck.log" echo -e "${GREEN}✓ Static analysis complete${NC}" echo "" fi echo -e "${GREEN}========================================${NC}" echo -e "${GREEN} Build Successful${NC}" echo -e "${GREEN}========================================${NC}"