Subversion Repositories freemyipod

Rev

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