Subversion Repositories freemyipod

Rev

Rev 870 | Rev 883 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
881 theseven 1
CCACHE  ?= $(shell which ccache)
870 theseven 2
CROSS   ?= arm-elf-eabi-
3
CC      := $(CCACHE) $(CROSS)gcc
4
LD      := $(CROSS)ld
5
OBJCOPY := $(CROSS)objcopy
881 theseven 6
OBJDUMP := $(CROSS)objdump
870 theseven 7
 
881 theseven 8
CFLAGS_GENERAL  := -c -ffunction-sections -fdata-sections -fmessage-length=0 -Wall $(CFLAGS_GENERAL)
9
CFLAGS_ASM      := -x assembler-with-cpp $(CFLAGS_ASM)
10
CFLAGS_debug    := -O0 -g3 -gdwarf-2 $(CFLAGS_DEBUG)
11
CFLAGS_release  := -flto -flto-partition=none -Os -fno-pie -fno-stack-protector -fomit-frame-pointer $(CFLAGS_RELEASE)
12
LDFLAGS_GENERAL := -nostdlib --gc-sections $(LDFLAGS_GENERAL)
13
LDFLAGS_debug   := -O0 $(LDFLAGS_DEBUG)
14
LDFLAGS_release := -flto -flto-partition=none -Os $(LDFLAGS_RELEASE)
15
PPFLAGS_GENERAL := -DTARGET_$(subst /,_,$(TARGET)) -DTARGET=$(TARGET) -DCONFIG_H=target/$(TARGET)/config.h -DTARGET_H=target/$(TARGET)/target.h -Isrc
16
PPFLAGS_debug   := -DDEBUG
17
PPFLAGS_release := -DRELEASE
18
PPONLY_FLAGS    := -E -P -x c -DASM_FILE
19
SPEC_FLAGS      := -E -P -v -dD
870 theseven 20
 
881 theseven 21
ifeq ($(VERY_VERBOSE),)
22
VQ := @
23
else
24
VERBOSE := 1
25
endif
870 theseven 26
 
881 theseven 27
ifeq ($(VERBOSE),)
28
Q := @
29
endif
870 theseven 30
 
881 theseven 31
CWD := $(shell pwd)
32
relpath = $(patsubst $(CWD)/%,%,$(realpath $(1)))
33
preprocess = $(shell $(CC) $(2) $(PPONLY_FLAGS) $(1) | grep -v "^\#")
34
preprocesspaths = $(shell $(CC) $(2) $(PPONLY_FLAGS) $(1) | grep -v "^\#" | sed -e "s:^..*:$(dir $(1))&:" | sed -e "s:^\\./::")
35
 
870 theseven 36
TARGETS := $(call preprocess,TARGETS,-I.)
37
 
881 theseven 38
ifeq ($(TARGET),)
39
all: $(TARGETS)
870 theseven 40
define TARGET_template
881 theseven 41
$(1):
42
	$(Q)+$(MAKE) TARGET=$(1)
43
endef
44
$(foreach target,$(TARGETS),$(eval $(call TARGET_template,$(target))))
45
else
870 theseven 46
 
881 theseven 47
ifneq ($(TYPE),debug)
48
ifneq ($(TYPE),release)
49
$(error Please define either TYPE=debug or TYPE=release)
50
endif
51
endif
870 theseven 52
 
881 theseven 53
ifeq ($(LISTING),)
54
all: $(TARGET)
55
else
56
all: $(TARGET) LISTINGS
57
endif
870 theseven 58
 
881 theseven 59
_PPFLAGS := $(PPFLAGS_GENERAL) $(PPFLAGS_$(TYPE)) $(PPFLAGS)
870 theseven 60
 
881 theseven 61
define MODULE_template
62
ifeq ($(1),)
63
$$(shell echo Cannot resolve module $(2) 1>&2)
870 theseven 64
else
881 theseven 65
ifeq ($$(filter $(1),$$(INCLUDED_MODULES)),)
66
INCLUDED_MODULES += $(1)
67
ifneq ($(VERBOSE),)
68
$$(shell echo Including module $(1) 1>&2)
870 theseven 69
endif
881 theseven 70
-include $(1)/target.mk
71
ifneq ($(wildcard $(1)/SOURCES),)
72
SOURCES += $(1)/SOURCES
73
SRC += $$(call preprocesspaths,$(1)/SOURCES,$(_PPFLAGS))
870 theseven 74
endif
881 theseven 75
ifneq ($(wildcard $(1)/DEPS),)
76
DEPS += $(1)/DEPS
77
$$(foreach module,$$(strip $$(call preprocesspaths,$(1)/DEPS,$(_PPFLAGS))),$$(eval $$(call MODULE_template,$$(call relpath,$$(module)),$$(module))))
870 theseven 78
endif
79
else
881 theseven 80
ifneq ($(VERY_VERBOSE),)
81
$$(shell echo Module $(1) is already included 1>&2)
870 theseven 82
endif
881 theseven 83
endif
84
endif
870 theseven 85
endef
86
 
881 theseven 87
$(eval $(call MODULE_template,src,src))
870 theseven 88
 
881 theseven 89
SRC := $(foreach file,$(SRC),$(call relpath,$(file)))
90
OBJ := $(SRC:src/%.c=build/$(TARGET)/$(TYPE)/%.o)
91
OBJ := $(OBJ:src/%.S=build/$(TARGET)/$(TYPE)/%.o)
92
_LDSCRIPT = $(LDSCRIPT:src/%.lds=build/$(TARGET)/$(TYPE)/%.lds)
870 theseven 93
 
881 theseven 94
_ASMFLAGS := $(CFLAGS_GENERAL) $(CFLAGS_ASM) $(CFLAGS_$(TYPE)) $(_CFLAGS) $(_PPFLAGS) $(CFLAGS) $(ASMFLAGS)
95
_CFLAGS := $(CFLAGS_GENERAL) $(CFLAGS_$(TYPE)) $(_CFLAGS) $(_PPFLAGS) $(CFLAGS)
96
_LDFLAGS := $(LDFLAGS_GENERAL) $(LDFLAGS_$(TYPE)) $(_LDFLAGS) $(LDFLAGS)
97
 
98
define CCRULE_template
99
build/$(TARGET)/$(TYPE)/%.$(1): src/%.$(2)
100
	$(VQ)echo $(3) $$<
101
	$(VQ)-mkdir -p $$(dir $$@)
102
	$(Q)$(CC) -Wa,-adhlns="build/$(TARGET)/$(TYPE)/$$*.lst" $(4) -o $$@ $$<
103
	$(VQ)$(CC) -MM $(4) $$< > $$@.dep.tmp
104
	$(VQ)sed -e "s|.*:|$$@:|" < $$@.dep.tmp > $$@.dep
105
	$(VQ)sed -e 's/.*://' -e 's/\\$$$$//' < $$@.dep.tmp | fmt -1 | sed -e 's/^ *//' -e 's/$$$$/:/' >> $$@.dep
106
	$(VQ)rm -f $$@.dep.tmp
107
endef
108
 
109
-include $(OBJ:%=%.dep)
110
 
111
build/$(TARGET)/$(TYPE)/%.bin: build/$(TARGET)/$(TYPE)/%.elf
112
	$(VQ)echo "[OC]    " $<
113
	$(Q)$(OBJCOPY) -O binary $^ $@
114
 
115
build/$(TARGET)/$(TYPE)/$(NAME).elf: $(_LDSCRIPT) $(OBJ) $(SOURCES) $(DEPS)
116
	$(VQ)echo "[LD]    " $@
117
	$(Q)$(LD) -Map "$@.map" $(_LDFLAGS) -o $@ -T $(_LDSCRIPT) $(OBJ)
118
 
119
%.lst: %
120
	$(VQ)echo "[OD]    " $<
121
	$(Q)$(OBJDUMP) -S $< > $@
122
 
123
LISTINGS: $(OBJ:%=%.lst)
124
 
125
$(eval $(call CCRULE_template,lds,lds,"[PP]    ",$(_PPFLAGS) $(PPONLY_FLAGS)))
126
 
127
$(eval $(call CCRULE_template,o,c,"[CC]    ",$(_CFLAGS)))
128
 
129
$(eval $(call CCRULE_template,o,S,"[CC]    ",$(_CFLAGS)))
130
 
870 theseven 131
endif
132
 
881 theseven 133
spec:
134
	$(Q)$(CC) $(_CFLAGS) $(SPEC_FLAGS) src/spec.c
135
 
870 theseven 136
clean:
881 theseven 137
	$(Q)rm -rf build
870 theseven 138
 
881 theseven 139
.PHONY: all spec clean $(TARGETS) SOURCES DEPS LISTINGS