-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMakefile
More file actions
210 lines (178 loc) · 6.93 KB
/
Makefile
File metadata and controls
210 lines (178 loc) · 6.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
# RTOS selects the kernel.
# TARGET selects the platform.
#
# make # threadx, posix-host (default)
# make RTOS=freertos # freertos, posix-host
# make RTOS=threadx TARGET=cortex-m-qemu # threadx, QEMU Cortex-M3
RTOS ?= threadx
TARGET ?= posix-host
CC ?= cc
CFLAGS ?= -O2 -Wall
LDFLAGS =
# Reporting interval in seconds. POSIX timer overhead may roughly double
# the actual wall-clock time (e.g. 30 -> ~60 s on macOS).
TM_TEST_DURATION ?= 30
# Number of reporting cycles before exit(0). 0 = infinite (default).
# Set to 1 for CI / QEMU semihosting runs so the program terminates
# cleanly via _exit() -> SYS_EXIT.
TM_TEST_CYCLES ?= 0
TM_INC = -Iinclude
TM_COMMON_SRC = src/tm_report.c
TM_CFLAGS = -DTM_TEST_DURATION=$(TM_TEST_DURATION)
ifneq ($(TM_TEST_CYCLES),0)
TM_CFLAGS += -DTM_TEST_CYCLES=$(TM_TEST_CYCLES)
endif
# Non-interrupt tests (build on any platform).
TESTS = \
basic_processing \
cooperative_scheduling \
preemptive_scheduling \
message_processing \
synchronization_processing \
memory_allocation
# Interrupt tests require tm_cause_interrupt() and wired ISR handlers in the
# porting layer. Each supported target provides its own dispatch mechanism:
# POSIX host uses TM_ISR_VIA_THREAD; Cortex-M uses RTOS-specific dispatch
# (ThreadX: SVC #0, FreeRTOS: NVIC IRQ 31 -- SVC is reserved by FreeRTOS).
BINS = $(addprefix tm_, $(TESTS))
# Build directory for intermediate objects (keeps project root clean).
BUILD = build
# Target-specific toolchain
CM_SRCS =
ifeq ($(TARGET),posix-host)
LDFLAGS += -lpthread
else ifeq ($(TARGET),cortex-m-qemu)
CROSS_COMPILE ?= arm-none-eabi-
CC := $(CROSS_COMPILE)gcc
override AR := $(CROSS_COMPILE)ar
CFLAGS += -mcpu=cortex-m3 -mthumb -mfloat-abi=soft
LDFLAGS += -T ports/common/cortex-m/mps2_an385.ld -nostartfiles
CM_SRCS += ports/common/cortex-m/startup.S \
ports/common/cortex-m/vector_table.c \
ports/common/cortex-m/tm_putchar.c
LDFLAGS += --specs=rdimon.specs
TM_CFLAGS += -DTM_SEMIHOSTING
# Semihosting target mode. "native" lets QEMU handle SYS_OPEN /
# SYS_WRITE directly on the host filesystem -- convenient for local
# development but a security risk on shared CI runners. Override
# with QEMU_SEMIHOSTING_TARGET=gdb for sandboxed environments.
QEMU_SEMIHOSTING_TARGET ?= native
QEMU_FLAGS = -semihosting-config enable=on,target=$(QEMU_SEMIHOSTING_TARGET)
endif
# RTOS: ThreadX
ifeq ($(RTOS),threadx)
THREADX_DIR = threadx
TM_CFLAGS += -DTX_DISABLE_ERROR_CHECKING
TM_PORT_SRC = ports/threadx/tm_port.c
TM_MAIN_SRC = ports/threadx/main.c
RTOS_DIR = $(THREADX_DIR)
ifeq ($(TARGET),posix-host)
POSIX_PORT = ports/threadx/posix-host
RTOS_INC = -I$(THREADX_DIR)/common/inc -I$(POSIX_PORT)
RTOS_SRCS = $(wildcard $(THREADX_DIR)/common/src/*.c) \
$(wildcard $(POSIX_PORT)/tx_*.c)
TM_CFLAGS += -DTM_ISR_VIA_THREAD
else ifeq ($(TARGET),cortex-m-qemu)
CM3_PORT = $(THREADX_DIR)/ports/cortex_m3/gnu
RTOS_INC = -I$(THREADX_DIR)/common/inc -I$(CM3_PORT)/inc
RTOS_SRCS = $(wildcard $(THREADX_DIR)/common/src/*.c) \
$(wildcard $(CM3_PORT)/src/*.S)
CM_SRCS += ports/threadx/cortex-m/tm_isr_dispatch.c \
ports/threadx/cortex-m/tx_initialize_low_level.S
endif
TESTS += interrupt_processing interrupt_preemption_processing
CLONE_STAMP = $(THREADX_DIR)/.cloned
RTOS_LIB = $(BUILD)/libtx.a
CLONE_URL = https://github.com/eclipse-threadx/threadx
# RTOS: FreeRTOS
else ifeq ($(RTOS),freertos)
FREERTOS_DIR = freertos-kernel
TM_PORT_SRC = ports/freertos/tm_port.c
TM_MAIN_SRC = ports/freertos/main.c
RTOS_DIR = $(FREERTOS_DIR)
# Kernel sources common to all targets (no timers, no event groups).
FREERTOS_SRCS = $(FREERTOS_DIR)/tasks.c \
$(FREERTOS_DIR)/queue.c \
$(FREERTOS_DIR)/list.c \
$(FREERTOS_DIR)/portable/MemMang/heap_4.c
ifeq ($(TARGET),posix-host)
FREERTOS_PORT = $(FREERTOS_DIR)/portable/ThirdParty/GCC/Posix
RTOS_INC = -I$(FREERTOS_DIR)/include \
-I$(FREERTOS_PORT) \
-Iports/freertos/posix-host
RTOS_SRCS = $(FREERTOS_SRCS) \
$(wildcard $(FREERTOS_PORT)/*.c) \
$(wildcard $(FREERTOS_PORT)/utils/*.c)
RTOS_INC += -I$(FREERTOS_PORT)/utils
TM_CFLAGS += -DTM_ISR_VIA_THREAD
else ifeq ($(TARGET),cortex-m-qemu)
FREERTOS_PORT = $(FREERTOS_DIR)/portable/GCC/ARM_CM3
RTOS_INC = -I$(FREERTOS_DIR)/include \
-I$(FREERTOS_PORT) \
-Iports/freertos/cortex-m
RTOS_SRCS = $(FREERTOS_SRCS) \
$(FREERTOS_PORT)/port.c
CM_SRCS += ports/freertos/cortex-m/tm_isr_dispatch.c
endif
TESTS += interrupt_processing interrupt_preemption_processing
CLONE_STAMP = $(FREERTOS_DIR)/.cloned
RTOS_LIB = $(BUILD)/libfreertos.a
CLONE_URL = https://github.com/FreeRTOS/FreeRTOS-Kernel
endif
.PHONY: all clean distclean check run
.DELETE_ON_ERROR:
all: $(BINS)
# Clone RTOS source tree on first build.
$(CLONE_STAMP):
git clone $(CLONE_URL) $(RTOS_DIR) --depth=1
@touch $@
# Collect RTOS kernel + port objects into a static library.
$(RTOS_LIB): $(CLONE_STAMP) | $(BUILD)
$(CC) $(CFLAGS) $(TM_CFLAGS) $(RTOS_INC) -c $(RTOS_SRCS)
mv *.o $(BUILD)/
$(AR) rcs $@ $(BUILD)/*.o
$(BUILD):
mkdir -p $@
tm_%: src/%.c $(TM_PORT_SRC) $(TM_MAIN_SRC) $(RTOS_LIB)
$(CC) $(CFLAGS) $(TM_CFLAGS) $(RTOS_INC) $(TM_INC) \
-o $@ $< $(TM_COMMON_SRC) $(TM_PORT_SRC) $(TM_MAIN_SRC) $(CM_SRCS) \
$(RTOS_LIB) $(LDFLAGS)
ifeq ($(TARGET),cortex-m-qemu)
# Force TM_TEST_CYCLES=1 so the test self-terminates via semihosting
# SYS_EXIT. The rm forces a rebuild since Make tracks source timestamps,
# not compiler flags. Override with: make run TM_TEST_CYCLES=0 QEMU_TIMEOUT=60
run:
rm -f $(firstword $(BINS))
$(MAKE) TM_TEST_CYCLES=1 $(firstword $(BINS))
scripts/qemu-run.sh $(firstword $(BINS)) $(QEMU_FLAGS)
endif
clean:
rm -f $(BINS)
rm -rf $(BUILD)
distclean: clean
rm -rf threadx freertos-kernel rt-thread
# Quick smoke test: build with 3-second intervals and 1 reporting cycle,
# run each test under a timeout. TM_TEST_CYCLES=1 ensures tests
# self-terminate even when timeout/gtimeout is unavailable (macOS).
check:
$(MAKE) clean
$(MAKE) TM_TEST_DURATION=3 TM_TEST_CYCLES=1
@TCMD=""; \
if command -v timeout >/dev/null 2>&1; then TCMD="timeout"; \
elif command -v gtimeout >/dev/null 2>&1; then TCMD="gtimeout"; fi; \
passed=0; failed=0; \
for t in $(BINS); do \
printf " %-35s" "$$t ..."; \
if [ -n "$$TCMD" ]; then \
out=$$($$TCMD 10 ./$$t 2>&1 | grep 'Time Period Total'); \
else \
out=$$(./$$t 2>&1 | grep 'Time Period Total'); \
fi; \
if [ -n "$$out" ]; then \
printf "OK\n"; passed=$$((passed + 1)); \
else \
printf "FAIL\n"; failed=$$((failed + 1)); \
fi; \
done; \
printf "\n%d passed, %d failed\n" "$$passed" "$$failed"; \
[ "$$failed" -eq 0 ]