aboutsummaryrefslogtreecommitdiff
path: root/rules.mk
blob: 5d7305142c3929f4f47efc43557ce74315172209 (plain)
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
# generate objects from sources
# arg 1:  target.SRCS
# arg 2:  object suffix prefix
# returns: target.OBJS
objects = $(foreach f,$($1),$(addsuffix $2.o,$(basename $f)))

# generate depends from .S .c .cpp sources
# arg 1:  target.SRCS
# arg 2:  dependency suffix prefix
# returns: target.DEPS
depends = $(foreach f,$(filter %.S %.c %.cpp,$($1)),$(addsuffix $2.d,$(basename $f)))

# default target
all: targets


# TARGETBIN: binaries using the target architecture
$(foreach T,${TARGETBIN},\
	$(eval  $T.OBJS += $(call objects,$T.SRCS,_${ARCH})   ) \
	$(eval  $T.DEPS += $(call depends,$T.SRCS,_${ARCH})   ) \
	$(eval  include $($T.DEPS)                            ) \
	$(eval  $T.elf: ${$T.OBJS}                            ) \
	$(eval  DEFAULT_TARGETS += $T.elf) \
)

# TARGETLIB: static libraries using the target architecture
$(foreach T,${TARGETLIB},\
	$(eval  $T.OBJS += $(call objects,$T.SRCS,_${ARCH})   ) \
	$(eval  $T.DEPS += $(call depends,$T.SRCS,_${ARCH})   ) \
	$(eval  include $($T.DEPS)                            ) \
	$(eval  $T.a: ${$T.OBJS}                              ) \
	$(eval  DEFAULT_TARGETS += $T.a) \
)

# HOSTTARGETBIN: binaries using the host compiler
$(foreach T,${HOSTTARGETBIN},\
	$(eval  $T.OBJS += $(call objects,$T.SRCS,)  ) \
	$(eval  $T.DEPS += $(call depends,$T.SRCS,)  ) \
	$(eval  include $($T.DEPS)                   ) \
	$(eval  $T: ${$T.OBJS}; @echo '  HOST LD    $T'; ${HOST_CXX} ${HOST_LDFLAGS} -o $T ${$T.OBJS}   ) \
	$(eval  DEFAULT_TARGETS += $T) \
)

TESTS.DEPS = $(foreach F,${TESTS},$(addsuffix .d,$F))
include ${TESTS.DEPS}

debug:
	@echo "TARGETBIN        ${TARGETBIN}"
	@echo "TARGETLIB        ${TARGETLIB}"
	@echo "HOSTTARGETBIN    ${HOSTTARGETBIN}"
	@echo "HOSTTARGETLIB    ${HOSTTARGETLIB}"
	@echo "DEFAULT_TARGETS  ${DEFAULT_TARGETS}"
	@echo "TESTS            ${TESTS}"
	@echo "   DEPS          ${TESTS.DEPS}"

%.info:
	@echo "Target: $(basename $@)"
	@echo "  SRCS: ${$(basename $@).SRCS}"
	@echo "  OBJS: ${$(basename $@).OBJS}"
	@echo "  DEPS: ${$(basename $@).DEPS}"

targets: ${DEFAULT_TARGETS}
	@echo " -> Built ${DEFAULT_TARGETS}"

# extra flags
${ARCH}_CFLAGS   += -I../lib/libk \
		    -Werror=implicit-function-declaration
${ARCH}_CXXFLAGS += -I../lib/libk -Drestrict=__restrict__

# Target depndency rules
%_${ARCH}.d: %.S
	@${${ARCH}_CC} ${${ARCH}_CFLAGS} -M -MT $(<:.S=_${ARCH}.o) $< -MF $@

%_${ARCH}.d: %.c
	@${${ARCH}_CC} ${${ARCH}_CFLAGS} -M -MT $(<:.c=_${ARCH}.o) $< -MF $@

%_${ARCH}.d: %.cpp
	@${${ARCH}_CXX} ${${ARCH}_CXXFLAGS} -M -MT $(<:.cpp=_${ARCH}.o) $< -MF $@

# Host dependency rules
%.d: %.c
	@${HOST_CC} ${HOST_CFLAGS} -M -MT $(<:.c=.o) $< -MF $@

%.d: %.cc
	@${HOST_CXX} ${HOST_CXXFLAGS} -M -MT $(<:.cc=) $< -MF $@

# Target suffix rules
%.a:
	@echo '  AR   $@'
	@${${ARCH}_AR} ${${ARCH}_ARFLAGS} $@ $(filter %.o,$^)

%_${ARCH}.o: %.s
	@echo '  AS   $<'
	@${${ARCH}_AS} ${${ARCH}_ASFLAGS} -c -o $@ $<

%_${ARCH}.o: %.S
	@echo '  CC   $<'
	@${${ARCH}_CC} ${${ARCH}_CFLAGS} -c -o $@ $<

%_${ARCH}.o: %.c
	@echo '  CC   $<'
	@${${ARCH}_CC} ${${ARCH}_CFLAGS} -c -o $@ $<

%_${ARCH}.o: %.cpp
	@echo '  CXX  $<'
	@${${ARCH}_CXX} ${${ARCH}_CXXFLAGS} -c -o $@ $<

%.elf:
	@echo '  LD   $@'
	@${${ARCH}_LD} ${${ARCH}_LDFLAGS} -o $@ $^
	@echo -n '  B2b  '
	@b2sum $@ | cut -d' ' -f1

# Host suffix rules
%.o: %.c
	@echo '  HOST CC    $<'
	@${HOST_CC} ${HOST_CFLAGS} -c -o $@ $<

%.o: %.cc
	@echo '  HOST CXX   $<'
	@${HOST_CXX} ${HOST_CXXFLAGS} -c -o $@ $<

# test rules
test_%: test_%.cc
	@echo '  CXX  TEST  $@'
	@${HOST_CXX} ${HOST_CXXFLAGS} ${HOST_LDFLAGS} $< -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: ;