summaryrefslogtreecommitdiff
path: root/digital/avr/make/Makefile.gen
blob: 2c1b34bb871b451a17210baea1abfe9c34b1ccd3 (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
# Makefile.gen - General Makefile.

# Flags

INCLUDES := -I$(BASE) -I$(BASE)/common
CFLAGS := -g -Wall -W -Wundef -Wno-unused-parameter -Wno-pointer-sign \
	  $(OPTIMIZE)
ASFLAGS := -Wa,--gstabs
CPPFLAGS := $(DEFS) $(INCLUDES) -MMD \
	    $(if $(CONFIGFILE),$(CONFIGFILE:%=-include %))
LDFLAGS :=
LDLIBS := $(LIBS)

# Usefull macros.
## Return $(A) only if defined, else return B.
## $(call defval A,B)
defval = $(if $(filter undefined,$(origin $(1))),$(2),$($(1)))

# Main rules.

all: avr simu host

.PHONY: all clean doc

# General rules.

ALL_PROGS := $(PROGS) $(AVR_PROGS) $(HOST_PROGS) $(SIMU_PROGS)
ALL_SOURCES = $(foreach prog,$(ALL_PROGS),$($(prog)_SOURCES))

# Modules.

MODULES += host

ALL_MODULES := $(sort $(MODULES) \
		 $(foreach prog,$(ALL_PROGS),$($(prog)_MODULES)))

include $(ALL_MODULES:%=$(BASE)/modules/%/Makefile.module)

define MODULES_template
$(1)_SOURCES += $(foreach module,\
	$(call defval,$(1)_MODULES,$(MODULES)),\
	$($(subst /,_,$(module))_SOURCES)\
)
endef

$(foreach prog,$(ALL_PROGS),$(eval $(call MODULES_template,$(prog))))

vpath %.c $(ALL_MODULES:%=$(BASE)/modules/%)
vpath %.S $(ALL_MODULES:%=$(BASE)/modules/%)

# Objects directory.

OBJDIR := obj

$(OBJDIR):
	mkdir -p $(OBJDIR)

# Compilation test rules.

define TEST_MCU_template
test.sub::
	$$(MAKE) CONFIGFILE=$(1) AVR_MCU=$(2) clean elf
endef

define TEST_template
$$(foreach mcu,\
	$$(if $$($(1:%.h=%)_TEST_MCU),$$($(1:%.h=%)_TEST_MCU),$$(TEST_MCU)),\
	$$(eval $$(call TEST_MCU_template,$(1),$$(mcu))))
test.sub::
	$$(MAKE) CONFIGFILE=$(1) clean host
endef

$(foreach config,$(TEST_CONFIGFILES),$(eval $(call TEST_template,$(config))))

test: test.sub clean

# Include other Makefiles.

ifneq (,$(filter-out %.c %.avr.S,$(ALL_SOURCES)))
$(error Sources should be .c or .avr.S files)
endif

ifneq (,$(strip $(ALL_PROGS)))
include $(BASE)/make/Makefile.avr
include $(BASE)/make/Makefile.host
else
avr:
simu:
host:
clean.avr:
clean.simu:
clean.host:
endif

# Rules for building the doc.

doc: $(DOC)

%.html: %.txt %.exd
	rst2html $< $@

EXTRACTDOC_BIN = $(BASE)/../../tools/extractdoc/extractdoc

%.exd: $(EXTRACTDOC:+%=%)
	test -n "$^" && $(EXTRACTDOC_BIN) $(EXTRACTDOC) > $@ || true

# Cleaning.

clean: clean.avr clean.simu clean.host
	rm -f *.bak *~ $(DOC) *.exd $(EXTRA_CLEAN_FILES)
	rmdir $(OBJDIR) || true