-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMakefile
200 lines (147 loc) · 4.82 KB
/
Makefile
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
include ./config.mk
srctree := .
objtree := .
real_srctree := $(srctree)
real_objtree := $(objtree)
# This is our default target - The default is the first target in the file so
# we need to define this fairly high-up.
all: real-all
PHONY += all install clean dist real-all configure
# Predefine this variable. It contains a list of extra files to clean. Ex.
CLEAN_LIST :=
# List of files that dependency files should be generated for
DEPS :=
# Current project being compiled - Blank for core
PROJ :=
EXES :=
SRC_OBJS :=
# Set configuration options
Q := @
quiet := quiet
ifeq ($(V),y)
quiet :=
endif
ifdef silent
quiet := silent
endif
_echo_cmd = echo $(2)
quiet_echo_cmd = echo $(1)
slient_echo_cmd = true
mecho = $(call $(quiet)_echo_cmd,$(1),$(2))
# This includes everything in the 'include' folder of the $(objtree)
# This is so that the code can reference generated include files
CPPFLAGS += -I'$(objtree)/include/'
define create_link_rule
$(1): $(2)
@$$(call mecho," LD $$@","$$(LD) -r $(2) -o $$@")
$$(Q)$$(LD) -r $(2) -o $$@
endef
define create_cc_rule
ifneq ($$(wildcard $(2)),)
$(1): $(2)
@$$(call mecho," CC $$@","$$(CC) $$(CFLAGS) $$(CPPFLAGS) $(3) -c $$< -o $$@")
$$(Q)$$(CC) $$(CFLAGS) $$(CPPFLAGS) $(3) -c $$< -o $$@
$(dir $(1)).$(basename $(notdir $(1))).d: $(2) $$(real_objtree)/include/gen_config.h
@$$(call mecho," CCDEP $$@","$$(CC) -MM -MP -MF $$@ $$(CPPFLAGS) $$(CFLAGS) $(3) $$< -MT $$(objtree)/$$*.o -MT $$@")
$$(Q)$$(CC) -MM -MP -MF $$@ $$(CPPFLAGS) $$(CFLAGS) $(3) $$< -MT $(1) -MT $$@
endif
endef
# Traverse into tree
define subdir_inc
objtree := $$(objtree)/$(1)
srctree := $$(srctree)/$(1)
cflags-sav := $$(cflags-y)
subdir-y :=
objs-y :=
clean-list-y :=
_tmp := $$(shell mkdir -p $$(objtree))
include $$(srctree)/Makefile
CLEAN_LIST += $$(patsubst %,$$(objtree)/%,$$(objs-y)) $$(patsubst %,$$(objtree)/%,$$(clean-list-y)) $$(objtree).o
DEPS += $$(patsubst %,$$(objtree)/%,$$(objs-y))
objs := $$(patsubst %,$$(objtree)/%,$$(objs-y)) $$(patsubst %,$$(objtree)/%.o,$$(subdir-y))
$$(foreach obj,$$(patsubst %,$$(objtree)/%,$$(objs-y)),$$(eval $$(call create_cc_rule,$$(obj),$$(obj:.o=.c),$$($$(PROJ)_CFLAGS) $$(cflags-y))))
$$(eval $$(call create_link_rule,$$(objtree).o,$$(objs)))
$$(foreach subdir,$$(subdir-y),$$(eval $$(call subdir_inc,$$(subdir))))
cflags-y := $$(cflags-sav)
srctree := $$(patsubst %/$(1),%,$$(srctree))
objtree := $$(patsubst %/$(1),%,$$(objtree))
endef
define proj_ccld_rule
$(1): $(2) | $$(objtree)/bin
@$$(call mecho," CCLD $$@","$$(CC) $(3) $(2) -o $$@ $(4)")
$$(Q)$$(CC) $$(LDFLAGS) $(3) $(2) -o $$@ $(4)
endef
define proj_inc
include $(1)/config.mk
PROG := $$(objtree)/bin/$$(EXE)
PROJ := $$(EXEC)
objs := $$(sort $$($$(EXEC)_OBJS) $$(SRC_OBJS))
$$(eval $$(call proj_ccld_rule,$$(PROG),$$(objs),$$($$(EXEC)_CFLAGS),$$($$(EXEC)_LIBFLAGS)))
$$(eval $$(call subdir_inc,$$(EXE)))
CLEAN_LIST += $$(PROG)
endef
$(eval $(call proj_inc,config))
-include $(objtree)/gen_config.mk
ifeq ($(CONFIG_DEBUG),y)
CPPFLAGS += -DNCLYR_DEBUG
CFLAGS += -g
ASFLAGS += -g
LDFLAGS += -g
endif
ifeq ($(CONFIG_PROF),y)
CFLAGS += -pg
LDFLAGS += -pg
endif
$(eval $(call proj_inc,nclyr))
CLEAN_LIST += $(objtree)/bin
EXES := $(objtree)/bin/nclyr
# Include tests
ifneq (,$(filter $(MAKECMDGOALS),check clean))
include ./test/Makefile
endif
# Actual entry
real-all: configure $(EXES)
install:
$(Q)mkdir -p $(BINDIR)
@echo " INSTALL nclyr"
$(Q)install -m 775 ./bin/nclyr $(BINDIR)
@echo " nclyr Installation done"
dist: clean
$(Q)mkdir -p $(EXE)-$(VERSION_N)
$(Q)cp -R Makefile README.md config.mk LICENSE ./doc ./include ./src ./nclyr $(EXE)-$(VERSION_N)
$(Q)tar -cf $(EXE)-$(VERSION_N).tar $(EXE)-$(VERSION_N)
$(Q)gzip $(EXE)-$(VERSION_N).tar
$(Q)rm -fr $(EXE)-$(VERSION_N)
@$(call mecho," Created $(EXE)-$(VERSION_N).tar.gz","gzip $(EXE)-$(VERSION_N).tar")
clean:
$(Q)for file in $(CLEAN_LIST); do \
if [ -e $$file ]; then \
$(call mecho," RM $$file";,"rm -fr $$file";) \
rm -rf $$file; \
fi \
done
configure: $(objtree)/gen_config.mk $(objtree)/include/gen_config.h
$(objtree)/bin:
@$(call mecho," MKDIR $@","$(MKDIR) $@")
$(Q)$(MKDIR) $@
$(objtree)/%.o: $(srctree)/%.c
@$(call mecho," CC $@","$(CC) $(CPPFLAGS) $(CFLAGS) -c $< -o $@")
$(Q)$(CC) $(CPPFLAGS) $(CFLAGS) -c $< -o $@
$(objtree)/.%.d: $(srctree)/%.c $(objtree)/include/gen_config.h
@$(call mecho," CCDEP $@","$(CC) -MM -MP -MF $@ $(CPPFLAGS) $(CFLAGS) $< -MT $(objtree)/$*.o -MT $@")
$(Q)$(CC) -MM -MP -MF $@ $(CPPFLAGS) $(CFLAGS) $< -MT $(objtree)/$*.o -MT $@
PHONY+=cscope
cscope:
@echo " Generating cscope for nclyr"
$(Q)find ./ \
-name "*.[ch]" -print \
> ./cscope.files
$(Q)cscope -b -q
DEP_LIST := $(foreach dep,$(DEPS),$(dir $(dep)).$(notdir $(dep)))
DEP_LIST := $(DEP_LIST:.o=.d)
real-all: $(DEP_LIST)
ifneq ($(MAKECMDGOALS),clean)
-include $(DEP_LIST)
endif
CLEAN_LIST += $(DEP_LIST)
.PHONY: $(PHONY)