|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +# Configuration |
| 4 | +CC=arm-none-eabi-gcc |
| 5 | +SIZE=arm-none-eabi-size |
| 6 | +NM=arm-none-eabi-nm |
| 7 | + |
| 8 | +# Use -ffreestanding and provide minimal headers to avoid toolchain dependencies |
| 9 | +CFLAGS="-mthumb -mcpu=cortex-m3 -Os -I. -std=c99 -ffreestanding" |
| 10 | + |
| 11 | +# Create a mock include directory |
| 12 | +mkdir -p mock_incl |
| 13 | + |
| 14 | +cat <<EOF > mock_incl/string.h |
| 15 | +#include <stddef.h> |
| 16 | +void *memset(void *s, int c, size_t n); |
| 17 | +char *strncpy(char *dest, const char *src, size_t n); |
| 18 | +size_t strlen(const char *s); |
| 19 | +int strcmp(const char *s1, const char *s2); |
| 20 | +void *memcpy(void *dest, const void *src, size_t n); |
| 21 | +void *memmove(void *dest, const void *src, size_t n); |
| 22 | +char *strstr(const char *haystack, const char *needle); |
| 23 | +EOF |
| 24 | + |
| 25 | +# Compile embedded_cli.c to object file |
| 26 | +$CC $CFLAGS -Imock_incl -c embedded_cli.c -o embedded_cli.o |
| 27 | + |
| 28 | +if [ $? -ne 0 ]; then |
| 29 | + echo "Error: Compilation of embedded_cli.c failed" |
| 30 | + rm -rf mock_incl |
| 31 | + exit 1 |
| 32 | +fi |
| 33 | + |
| 34 | +echo "Binary sizes (ARM Thumb-2, -Os):" |
| 35 | +$SIZE embedded_cli.o |
| 36 | + |
| 37 | +# Calculate size of the structure |
| 38 | +cat <<EOF > struct_size.c |
| 39 | +#include "embedded_cli.h" |
| 40 | +struct embedded_cli cli; |
| 41 | +EOF |
| 42 | + |
| 43 | +$CC $CFLAGS -Imock_incl -c struct_size.c -o struct_size.o |
| 44 | +if [ $? -ne 0 ]; then |
| 45 | + echo "Error: Compilation of struct_size.c failed" |
| 46 | + rm -rf mock_incl embedded_cli.o struct_size.c |
| 47 | + exit 1 |
| 48 | +fi |
| 49 | + |
| 50 | +# Get size using nm |
| 51 | +STRUCT_SIZE_HEX=$($NM -S struct_size.o | grep " cli" | awk '{print $2}') |
| 52 | + |
| 53 | +if [ -z "$STRUCT_SIZE_HEX" ]; then |
| 54 | + echo "Error: Could not find 'cli' symbol in object file" |
| 55 | +else |
| 56 | + # Convert hex size from nm to decimal |
| 57 | + STRUCT_SIZE_DECIMAL=$((16#$STRUCT_SIZE_HEX)) |
| 58 | + echo "" |
| 59 | + echo "Size of struct embedded_cli: $STRUCT_SIZE_DECIMAL bytes" |
| 60 | +fi |
| 61 | + |
| 62 | +# Cleanup |
| 63 | +rm -rf embedded_cli.o struct_size.c struct_size.o mock_incl |
0 commit comments