2 #### TOOLCHAIN CONFIGURATION ####
4 CROSS_COMPILE?=arm-none-eabi-
14 CFLAGS=-mcpu=arm7tdmi $(INCLUDES)
15 CFLAGS += -Wall -Wextra -Wcast-align -Wimplicit -Wunused
16 CFLAGS += -Wswitch -Wredundant-decls -Wreturn-type -Wshadow -Wnested-externs
17 CFLAGS += -Wbad-function-cast -Wsign-compare -Waggregate-return
18 CFLAGS += -Os -ffunction-sections
21 # some older toolchains don't support this, ignore it for now
22 #ASFLAGS=--g$(DEBUGF) $(INCLUDES) -D__ASSEMBLY__
23 ASFLAGS=$(INCLUDES) -D__ASSEMBLY__
25 LDFLAGS = -nostartfiles -nostdlib -nodefaultlibs --gc-sections --cref
27 #### QUIET OUTPUT ####
33 Q_CC = $(if $(V:1=),@echo " CC $@";)
34 Q_LD = $(if $(V:1=),@echo " LD $@";)
35 Q_AR = $(if $(V:1=),@echo " AR $@";)
36 Q_OBJ = $(if $(V:1=),@echo " OBJ $@";)
37 Q_SIZE = $(if $(V:1=),@echo " SIZE $@";)
41 GIT_COMMIT:=$(shell git describe --always)
42 GIT_SHORTHASH:=$(shell git rev-parse --short HEAD)
43 GIT_MODIFIED:=$(shell (git status | grep "modified:\|added:\|deleted:" -q) && echo "-modified")
45 GIT_REVISION:=$(GIT_COMMIT)$(GIT_MODIFIED)
47 ASFLAGS += -DGIT_REVISION=\"$(GIT_REVISION)\"
48 CFLAGS += -DGIT_REVISION=\"$(GIT_REVISION)\"
50 ASFLAGS += -DGIT_SHORTHASH=\"$(GIT_SHORTHASH)\"
51 CFLAGS += -DGIT_SHORTHASH=\"$(GIT_SHORTHASH)\"
57 ALL_LSTS=$(ALL_OBJS:.o=.lst)
58 ALL_DEPS=$(ALL_OBJS:.o=.p)
60 #### APPLICATION DATA ####
64 ALL_APP_TARGETS=$(ALL_APPS:.elf=.bin) $(ALL_APPS:.elf=.size) $(ALL_APPS) $(ALL_APPS:.elf=.map)
66 #### LIBRARY DATA ####
70 ALL_LIB_TARGETS=$(ALL_LIBS)
73 #### DEFAULT RULE ####
79 #### APPLICATION RULES ####
81 # template for application rules
82 define APPLICATION_BOARD_ENVIRONMENT_template
84 # define set of objects for this binary
85 $(1)_$(2)_$(3)_OBJS := apps/$(1)/main.o $(ANY_APP_OBJS) $$(APP_$(1)_OBJS) $$(BOARD_$(2)_OBJS) $$(ENV_$(3)_OBJS)
86 $(1)_$(2)_$(3)_LIBS := $(ANY_APP_LIBS)
88 # define manifest compilation
89 board/$(2)/$(1).$(3).manifest.o: board/manifest.c
90 $$(Q_CC)$(CROSS_COMPILE)$(CC) $(CFLAGS) -DAPPLICATION=\"$(1)\" -DBOARD=\"$(2)\" -DENVIRONMENT=\"$(3)\" -c -o $$@ $$<
92 # generate dummy dependencies for manifest
93 board/$(2)/$(1).$(3).manifest.p: board/manifest.c
94 @touch board/$(2)/$(1).$(3).manifest.p
96 # add manifest object to object list
97 $(1)_$(2)_$(3)_OBJS+=board/$(2)/$(1).$(3).manifest.o
99 # define compilation rule, also generates map file
100 board/$(2)/$(1).$(3).elf board/$(2)/$(1).$(3).map: $$($(1)_$(2)_$(3)_OBJS) $$($(1)_$(2)_$(3)_LIBS) $$(ENV_$(3)_LDS)
101 $$(Q_LD)$(CROSS_COMPILE)$(LD) $(LDFLAGS) -T $$(ENV_$(3)_LDS) -Bstatic \
102 -Map board/$(2)/$(1).$(3).map -o board/$(2)/$(1).$(3).elf \
103 --start-group $$($(1)_$(2)_$(3)_OBJS) $$($(1)_$(2)_$(3)_LIBS) --end-group
106 board/$(2)/$(1).$(3).size: board/$(2)/$(1).$(3).elf
107 $$(Q_SIZE)$(CROSS_COMPILE)$(SIZE) board/$(2)/$(1).$(3).elf | tee board/$(2)/$(1).$(3).size
109 ALL_APPS+=board/$(2)/$(1).$(3).elf
110 ALL_OBJS+=board/$(2)/$(1).$(3).manifest.o
114 define BOARD_template
115 ALL_OBJS+=$$(BOARD_$(1)_OBJS)
118 define BOARD_ENVIRONMENT_template
119 ALL_OBJS+=$$(ENV_$(1)_OBJS)
122 define APPLICATION_template
123 APP_$(1)_SRCS_REL=$$(patsubst %,$$(APP_$(1)_DIR)/%,$$(APP_$(1)_SRCS))
124 APP_$(1)_OBJS:=$$(APP_$(1)_SRCS_REL:.c=.o)
125 APP_$(1)_OBJS:=$$(APP_$(1)_OBJS:.S=.o)
127 ALL_OBJS+=$$(APP_$(1)_OBJS) apps/$(1)/main.o
130 # define rules for all defined applications
131 $(foreach brd,$(BOARDS), \
132 $(eval $(call BOARD_template,$(brd)) \
133 $(foreach env,$(BOARD_$(brd)_ENVIRONMENTS), \
134 $(eval $(call BOARD_ENVIRONMENT_template,$(env))))))
136 $(foreach app,$(APPLICATIONS), \
137 $(eval $(call APPLICATION_template,$(app))))
139 $(foreach app,$(APPLICATIONS), \
140 $(foreach brd,$(BOARDS), \
141 $(foreach env,$(shell ./solve_envs.py "$(BOARD_$(brd)_ENVIRONMENTS)" "$(APP_$(app)_ENVIRONMENTS)"), \
142 $(eval $(call APPLICATION_BOARD_ENVIRONMENT_template,$(app),$(brd),$(env))))))
145 # add common things to global lists
146 ALL_OBJS+=$(ANY_APP_OBJS)
148 #### LIBRARY RULES ####
150 # template for library rules
151 define LIBRARY_template
153 LIB_$(1)_SRCS_REL=$$(patsubst %,$$(LIB_$(1)_DIR)/%,$$(LIB_$(1)_SRCS))
154 LIB_$(1)_OBJS:=$$(LIB_$(1)_SRCS_REL:.c=.o)
155 LIB_$(1)_OBJS:=$$(LIB_$(1)_OBJS:.S=.o)
157 $$(LIB_$(1)_DIR)/lib$(1).a: $$(LIB_$(1)_OBJS)
158 $$(Q_AR)$(CROSS_COMPILE)$(AR) cru $$(LIB_$(1)_DIR)/lib$(1).a $$(LIB_$(1)_OBJS)
160 ALL_LIBS+=$$(LIB_$(1)_DIR)/lib$(1).a
162 ALL_OBJS+=$$(LIB_$(1)_OBJS)
166 # define rules for all defined libraries
167 $(foreach lbr,$(LIBRARIES),$(eval $(call LIBRARY_template,$(lbr))))
170 #### TOPLEVEL RULES ####
173 all: $(ALL_DEPS) $(ALL_APPS:.elf=.bin) $(ALL_APPS:.elf=.size)
179 #### COMPILATION RULES ####
182 @$(CROSS_COMPILE)$(CC) $(CFLAGS) -M -o $(*).d $(<)
183 @sed 's|.*\.o:|$(@:.p=.o): |g' < $*.d > $@; rm -f $*.d; [ -s $@ ] || rm -f $@
186 @$(CROSS_COMPILE)$(CC) $(ASFLAGS) -M -o $(*).d $(<)
187 @sed 's|.*\.o:|$(@:.p=.o): |g' < $*.d > $@; rm -f $*.d; [ -s $@ ] || rm -f $@
190 $(Q_CC)$(CROSS_COMPILE)$(CC) $(CFLAGS) -Wa,-adhlns=$(@:.o=.lst) -c -o $@ $<
193 $(Q_CC)$(CROSS_COMPILE)$(CC) $(ASFLAGS) -Wa,-adhlns=$(@:.o=.lst) -c -o $@ $<
197 $(Q_OBJ)$(CROSS_COMPILE)objcopy --gap-fill=0xff -O binary $^ $@
200 #### CLEANUP RULES ####
204 rm -f $(ALL_APP_TARGETS) $(ALL_LIB_TARGETS) $(ALL_OBJS) $(ALL_DEPS) $(ALL_LSTS)
208 find . -name '*.o' -or -name '*.bin' -or -name '*.map' -or -name '*.lst' -or -name '*.p' -exec rm '{}' ';'
211 #### DEPENDENCY LOAD ####