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
|
# generate objects from sources
# args 1: target.SRCS
# returns: target.OBJS
objects = $(foreach f,$($1),$(addsuffix _${ARCH}.o,$(basename $f)))
# generate depends from .S .c .cpp sources
# args 1: target.SRCS
# returns: target.DEPS
depends = $(foreach f,$(filter %.S %.c %.cpp,$($1)),$(addsuffix .d,$(basename $f)))
# default target
all: targets
@echo " -> Built all in $(shell pwd | xargs basename)"
# for each target.SRCS
# - generate target.OBJS
# - generate target.DEPS and include them
# - generate target.a that depends on target.OBJS
# - add target.a to TARGETS
$(foreach V,$(filter %.SRCS, ${.VARIABLES}),\
$(eval $(V:%.SRCS=%.OBJS) := $(call objects,$V)) \
$(eval TARGET.OBJS += $(V:%.SRCS=%.OBJS)) \
$(eval $(V:%.SRCS=%.DEPS) := $(call depends,$V)) \
$(eval include $($(V:%.SRCS=%.DEPS))) \
$(eval TARGET.DEPS += $(V:%.SRCS=%.DEPS)) \
$(eval $(V:%.SRCS=%.a): $($(V:%.SRCS=%.OBJS))) \
$(eval TARGETS += $(V:%.SRCS=%.a)) \
)
TESTS.DEPS = $(foreach F,${TESTS},$(addsuffix .d,$F))
include ${TESTS.DEPS}
debug:
@echo "targets: ${TARGETS}"
@echo " OBJS: ${TARGET.OBJS}"
@echo " DEPS: ${TARGET.DEPS}"
@echo "tests : ${TESTS}"
@echo " DEPS: ${TESTS.DEPS}"
%.info:
@echo "Target: $(basename $@)"
@echo " SRCS: ${$(basename $@).SRCS}"
@echo " OBJS: ${$(basename $@).OBJS}"
@echo " DEPS: ${$(basename $@).DEPS}"
targets: ${TARGETS}
# extra flags
CFLAGS += -I../lib \
-Werror=implicit-function-declaration
CXXFLAGS += -I../lib -Drestrict=__restrict__
# Depndency rules
%.d: %.S
@${CC} ${CFLAGS} -M -MT $(<:.S=_${ARCH}.o) $< -MF $@
%.d: %.c
@${CC} ${CFLAGS} -M -MT $(<:.c=_${ARCH}.o) $< -MF $@
%.d: %.cpp
@${CXX} ${CXXFLAGS} -M -MT $(<:.cpp=_${ARCH}.o) $< -MF $@
%.d: %.cc
@${TEST_CXX} ${TEST_CXXFLAGS} -M -MT $(<:.cc=_${ARCH}.o) $< -MF $@
# Suffix rules
%.a:
@echo ' AR $@'
@${AR} ${ARFLAGS} $@ $(filter %.o,$^)
%_${ARCH}.o: %.s
@echo ' AS $<'
@$(AS) $(ASFLAGS) -c -o $@ $<
%_${ARCH}.o: %.S
@echo ' CC $<'
@$(CC) $(CFLAGS) -c -o $@ $<
%_${ARCH}.o: %.c
@echo ' CC $<'
@$(CC) $(CFLAGS) -c -o $@ $<
%_${ARCH}.o: %.cpp
@echo ' CXX $<'
@$(CXX) $(CXXFLAGS) -c -o $@ $<
%.elf:
@echo ' LD $@'
@${LD} ${LDFLAGS} -o $@ $^
@echo -n ' B2b '
@b2sum $@ | cut -d' ' -f1
# test rules
tst/%: tst/%.cc
@echo ' CXX TEST $@'
@$(TEST_CXX) $(TEST_CXXFLAGS) $< -o $@
.PHONY: test.base valgrind.base clean.base FORCE
test.base: ${TESTS}
@echo " -> Running tests in $(shell pwd | xargs basename)"
@$(foreach f,$^,echo " -> $f"; ./$f &&) echo "Done"
valgrind.base: ${TESTS}
@echo " -> Running valgrind on tests in $(shell pwd | xargs basename)"
@$(foreach f,$^,echo " -> $f"; valgrind --leak-check=full ./$f;)
clean.base: FORCE
@echo " -> Cleaning $(shell pwd | xargs basename)"
@$(foreach V,$(filter %.OBJS, ${.VARIABLES}), rm -rf $($(V)))
@$(foreach V,$(filter %.DEPS, ${.VARIABLES}), rm -rf $($(V)))
@rm -rf *.a
@rm -rf ${TESTS}
%: %.base ;
FORCE: ;
|