Skip to content

Commit 50dfe3b

Browse files
authored
Fix up headers to just be what we actually use (#39)
* Fix up headers to just be what we actually use Added a helper script to determine binary sizes
1 parent 52edbb2 commit 50dfe3b

4 files changed

Lines changed: 65 additions & 2 deletions

File tree

embedded_cli.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
* https://en.wikipedia.org/wiki/ANSI_escape_code
55
*/
66

7-
#include <stdio.h>
87
#include <string.h>
98

109
#include "embedded_cli.h"

embedded_cli.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#define EMBEDDED_CLI
33

44
#include <stdbool.h>
5+
#include <stddef.h>
56

67
#ifndef EMBEDDED_CLI_MAX_LINE
78
/**

examples/posix_demo.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ int main(void)
9595
cli_argc = embedded_cli_argc(&cli, &cli_argv);
9696
printf("Got %d args\n", cli_argc);
9797
for (int i = 0; i < cli_argc; i++) {
98-
printf("Arg %d/%d: [%lu bytes] '%s'\n", i, cli_argc,
98+
printf("Arg %d/%d: [%zu bytes] '%s'\n", i, cli_argc,
9999
strlen(cli_argv[i]), cli_argv[i]);
100100
}
101101
done = cli_argc >= 1 && (strcmp(cli_argv[0], "quit") == 0);

tests/sizes.sh

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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

Comments
 (0)