Skip to content

Commit 1044aaa

Browse files
committed
Full and independent Linux example
1 parent 97c2c57 commit 1044aaa

12 files changed

Lines changed: 308 additions & 0 deletions

File tree

.gitignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,9 @@
33

44
**/sdkconfig*
55
**/CMakeLists.txt
6+
**/*.o
7+
**/*.bin
8+
**/*.elf
9+
**/*.hex
10+
**/memory_ram.txt
11+
**/memory_program.txt
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Ignore everything in this directory
2+
*
3+
4+
# Except this file
5+
!.gitignore
Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
2+
# Specify project name and default compilers
3+
PRJ = minbasecli_basicusage
4+
5+
# UART Driver path
6+
MINBASECLIPATH = $(WORKSPACE)/../../../src
7+
8+
# Project root WorkSpace
9+
WORKSPACE = ..
10+
11+
# Build and Binary Release directories
12+
BUILDDIR = $(WORKSPACE)/build
13+
BINDIR = $(WORKSPACE)/bin
14+
15+
###############################################################################
16+
17+
# Get actual date and setup output binary directory name
18+
#DATE = $(shell date '+%Y_%m_%d_%H_%M_%S')
19+
DATE = $(shell date '+%Y_%m_%d')
20+
RELEASEDIR = $(WORKSPACE)/bin/$(DATE)
21+
22+
# Specify Sources files (Automatic search in specific directories)
23+
SRCS = $(shell find $(WORKSPACE)/src -type f -name *.c)
24+
SRCS += $(shell find $(WORKSPACE)/src -type f -name *.cpp)
25+
SRCS += $(shell find $(WORKSPACE)/lib -type f -name *.c)
26+
SRCS += $(shell find $(WORKSPACE)/lib -type f -name *.cpp)
27+
28+
# MinBaseCLI Sources
29+
SRCS += $(MINBASECLIPATH)/minbasecli.cpp
30+
SRCS += $(MINBASECLIPATH)/hal/linux/minbasecli_linux.cpp
31+
32+
# Specify Headers files (Automatic search in specific directories)
33+
HEADS = $(shell find $(WORKSPACE)/inc -type f -name *.h)
34+
HEADS += $(shell find $(WORKSPACE)/inc -type f -name *.hpp)
35+
HEADS += $(shell find $(WORKSPACE)/src -type f -name *.h)
36+
HEADS += $(shell find $(WORKSPACE)/src -type f -name *.hpp)
37+
HEADS += $(shell find $(WORKSPACE)/lib -type f -name *.h)
38+
HEADS += $(shell find $(WORKSPACE)/lib -type f -name *.hpp)
39+
40+
# MinBaseCLI Headers
41+
HEADS += $(MINBASECLIPATH)/minbasecli.h
42+
HEADS += $(MINBASECLIPATH)/hal/linux/minbasecli_linux.h
43+
44+
# Get Headers directories from headers paths
45+
HEADERSDIR = $(shell echo $(HEADS) | xargs -n1 dirname | sort -u | xargs -n1 -i echo "-I{}")
46+
HEADERSDIR += $(shell echo $(HEADS) | xargs -n1 dirname | sort -u | xargs -n1 -i echo "-L{}")
47+
48+
# Get objects files from sources and output object
49+
_OBJS = $(SRCS:.c=.o)
50+
OBJS = $(_OBJS:.cpp=.o)
51+
BUILDOBJS = $(shell echo $(OBJS) | xargs -n1 basename | xargs -n1 -i echo "$(BUILDDIR)/{}")
52+
53+
###############################################################################
54+
55+
# Specify default compilers and tools
56+
CC = gcc
57+
CXX = g++
58+
OBJCOPY = objcopy
59+
OBJDUMP = objdump
60+
NM = nm --line-numbers --print-size --size-sort --radix=d
61+
SIZE = size
62+
63+
# Setup compilation flags
64+
CFLAGS = -Os -Wall -g -fstack-usage -flto -pthread
65+
ifeq ($(TEST), 1)
66+
CFLAGS += -DTEST
67+
endif
68+
CFLAGS += $(HEADERSDIR)
69+
CXXFLAGS = $(CFLAGS)
70+
71+
# Linker flags
72+
LDFLAGS = -lpthread
73+
74+
# Compile instruction
75+
COMPILE_C = $(CC) $(CFLAGS) $(LDFLAGS)
76+
COMPILE_CXX = $(CXX) $(CXXFLAGS) $(LDFLAGS)
77+
78+
###############################################################################
79+
80+
help:
81+
@cat README.md
82+
83+
# Target: make all (build project generating output directory)
84+
build: $(PRJ).bin
85+
rm -f ${RELEASEDIR}/*
86+
mkdir -p ${RELEASEDIR}
87+
cp -a $(PRJ).elf $(RELEASEDIR)
88+
cp -a $(PRJ).hex $(RELEASEDIR)
89+
cp -a $(PRJ).bin $(RELEASEDIR)
90+
@echo "ADDRESS SIZE FUNCTION FILE:LINE\n" > memory_program.txt
91+
@$(NM) $(PRJ).elf >> memory_program.txt
92+
@../tools/memusageram $(BUILDDIR)/memory_ram.txt >/dev/null 2>&1
93+
@rm -f $(BUILDDIR)/*.su
94+
@mkdir -p $(BUILDDIR)/obj
95+
@mv $(BUILDDIR)/*.o $(BUILDDIR)/obj/
96+
$(SIZE) $(PRJ).elf
97+
@../tools/binary_size $(PRJ).bin
98+
@echo ""
99+
100+
# Target: make clean (remove all previously builds)
101+
clean:
102+
rm -rf $(BUILDDIR)/obj
103+
rm -f $(BUILDDIR)/*.su
104+
rm -f $(BUILDDIR)/$(PRJ).elf
105+
rm -f $(BUILDDIR)/$(PRJ).hex
106+
rm -f $(BUILDDIR)/$(PRJ).bin
107+
rm -f $(BUILDDIR)/memory_program.txt
108+
rm -f $(BUILDDIR)/memory_ram.txt
109+
110+
# Target: make rebuild (clean previously builds and build again)
111+
rebuild: clean build
112+
113+
# Target: check (custom target to check build variables)
114+
check:
115+
@echo ""
116+
@echo "HEADERS:"
117+
@echo " $(HEADS)"
118+
@echo ""
119+
@echo "SRCS:"
120+
@echo " $(SRCS)"
121+
@echo ""
122+
@echo "BUILD FLAGS:"
123+
@echo " $(CFLAGS)"
124+
@echo ""
125+
@echo "LINK FLAGS:"
126+
@echo " $(LDFLAGS)"
127+
@echo ""
128+
@echo "OBJS:"
129+
@echo " $(BUILDOBJS)"
130+
@echo ""
131+
@echo "BINDIR:"
132+
@echo " $(BINDIR)"
133+
@echo ""
134+
135+
###############################################################################
136+
137+
# Target for generate BIN file from .elf file
138+
$(PRJ).bin: $(PRJ).hex
139+
rm -f $(PRJ).bin
140+
$(OBJCOPY) -j .text -j .data -O binary $(PRJ).elf $(PRJ).bin
141+
142+
# Target for generate HEX file from .elf file
143+
$(PRJ).hex: $(PRJ).elf
144+
rm -f $(PRJ).hex
145+
$(OBJCOPY) -j .text -j .data -O ihex $(PRJ).elf $(PRJ).hex
146+
147+
# Target for generate ELF file linking all .o files
148+
$(PRJ).elf: $(OBJS)
149+
$(COMPILE_C) -o $(PRJ).elf $(BUILDOBJS)
150+
151+
# Target for generate object file of each .c file
152+
%.o: %.c
153+
$(COMPILE_C) -c $<
154+
155+
# Target for generate object file of each .cpp file
156+
%.o: %.cpp
157+
$(COMPILE_CXX) -c $<
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Build Help
2+
3+
```bash
4+
Usage: make <TARGET> [OPTION=]...
5+
Targets:
6+
help: Show build usage information (current file).
7+
check: Show build parameters.
8+
clean: Clean last built files.
9+
build: Build the project.
10+
rebuild: Force clean last built files and build again.
11+
Options:
12+
TEST: Build a Test Application (add TEST global Define to code).
13+
```
14+
15+
# Build Examples
16+
17+
```bash
18+
make help
19+
make check
20+
make clean
21+
make build
22+
make build TEST=1
23+
make rebuild
24+
```
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# About
2+
3+
Place in this directory project documentation files.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# About
2+
3+
Place in this directory project headers files.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# About
2+
3+
Place in this directory external libraries used in the project.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# About
2+
3+
Place in this directory project tests.
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#!/usr/bin/env bash
2+
3+
# Current script directory path
4+
DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" >/dev/null 2>&1 && pwd)"
5+
6+
# Check if argument was provided
7+
if [ $# -lt 1 ]; then
8+
echo " This script shows binary file size in Bytes and KB."
9+
echo " You need to provide a binary file to analyze."
10+
echo " Example:"
11+
echo " ${0} firmware.bin"
12+
echo ""
13+
exit 1
14+
fi
15+
16+
# Get and show File Size
17+
BIN_SIZE_B=`ls -l "${1}" | awk '{print $5}'`
18+
BIN_SIZE_KB=$((BIN_SIZE_B / 1024))
19+
echo "Binary size: ${BIN_SIZE_KB}KB (${BIN_SIZE_B} Bytes)"
20+
21+
exit 0

0 commit comments

Comments
 (0)