-
Notifications
You must be signed in to change notification settings - Fork 0
/
Makefile
203 lines (153 loc) · 4.21 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
200
201
202
203
#
# 'make' build executable file 'main'
# 'make clean' removes all .o and executable files
#
.DEFAULT_GOAL := all
# CROSS_COMPILE ?= arm-linux-gnueabihf-
CROSS_COMPILE ?=
TARGET ?= main
# define the C compiler to use
CC := $(CROSS_COMPILE)gcc
LD := $(CROSS_COMPILE)ld
OBJCOPY := $(CROSS_COMPILE)objcopy
OBJDUMP := $(CROSS_COMPILE)objdump
SIZE := $(CROSS_COMPILE)size
# define any compile-time flags
CFLAGS ?=
CFLAGS += -O0
CFLAGS += -g
# warning param setting
CFLAGS += -Wall
# think use -Os.
CFLAGS += -Wno-unused-function
CFLAGS += -Wno-unused-variable
CFLAGS += -Wstrict-prototypes
CFLAGS += -Wshadow
# CFLAGS += -Werror
# spec c version
CFLAGS += -std=c99
# CFLAGS += -Wno-format
# for makefile depend tree create
CFLAGS += -MMD -MP
# for weak.
CFLAGS += -fno-common
## MAKEFILE COMPILE MESSAGE CONTROL ##
ifeq ($(V),1)
Q=
else
Q=@
endif
# Put functions and data in their own binary sections so that ld can
# garbage collect them
ifeq ($(NOGC),1)
GC_CFLAGS =
GC_LDFLAGS =
else
GC_CFLAGS = -ffunction-sections -fdata-sections
GC_LDFLAGS = -Wl,--gc-sections -Wl,--check-sections
endif
CFLAGS += $(GC_CFLAGS)
# define ld params
LDFLAGS :=
LDFLAGS += $(GC_LDFLAGS)
# define library paths in addition to /usr/lib
# if I wanted to include libraries not in /usr/lib I'd specify
# their path using -Lpath, something like:
LFLAGS :=
# define output directory
OUTPUT_PATH := output
# define source directory
SRC :=
# define include directory
INCLUDE :=
# define lib directory
LIB :=
OUTPUT_TARGET := $(OUTPUT_PATH)/$(TARGET)
OBJDIR = $(OUTPUT_PATH)/obj
# include user build.mk
include build.mk
ifeq ($(OS),Windows_NT)
ifdef ComSpec
WINCMD:=$(ComSpec)
endif
ifdef COMSPEC
WINCMD:=$(COMSPEC)
endif
SHELL:=$(WINCMD)
MAIN := $(TARGET).exe
ECHO=echo
SOURCEDIRS := $(SRC)
INCLUDEDIRS := $(INCLUDE)
LIBDIRS := $(LIB)
FIXPATH = $(subst /,\,$1)
RM := del /q /s
MD := mkdir
MD_CHECK := $(Q)if not exist "$@"
else
MAIN := $(TARGET)
ECHO=echo
SOURCEDIRS := $(shell find $(SRC) -type d)
INCLUDEDIRS := $(shell find $(INCLUDE) -type d)
LIBDIRS := $(shell find $(LIB) -type d)
FIXPATH = $1
RM = rm -rf
MD := mkdir -p
MD_CHECK :=
endif
# define any directories containing header files other than /usr/include
INCLUDES := $(patsubst %,-I%, $(INCLUDEDIRS:%/=%))
@echo INCLUDES: $(INCLUDES)
# define the C libs
LIBS := $(patsubst %,-L%, $(LIBDIRS:%/=%))
# define the C source files
SOURCES := $(wildcard $(patsubst %,%/*.c, $(SOURCEDIRS)))
# define the C object files
OBJECTS := $(patsubst %, $(OBJDIR)/%, $(SOURCES:.c=.o))
OBJ_MD := $(addprefix $(OBJDIR)/, $(SOURCEDIRS))
ALL_DEPS := $(OBJECTS:.o=.d)
# include dependency files of application
ifneq ($(MAKECMDGOALS),clean)
-include $(ALL_DEPS)
endif
#
# The following part of the makefile is generic; it can be used to
# build any executable just by changing the definitions above and by
# deleting dependencies appended to the file from 'make depend'
#
OUTPUT_MAIN := $(OUTPUT_PATH)/$(MAIN)
# Fix path error.
#OUTPUT_MAIN := $(call FIXPATH,$(OUTPUT_MAIN))
.PHONY: all clean
all: main
@$(ECHO) Start Build Image.
$(OBJCOPY) -v -O binary $(OUTPUT_MAIN) $(OUTPUT_TARGET).bin
$(OBJDUMP) --source --all-headers --demangle --line-numbers --wide $(OUTPUT_MAIN) > $(OUTPUT_TARGET).lst
@$(ECHO) Print Size
$(Q)@$(SIZE) --format=berkeley $(OUTPUT_MAIN)
# @$(ECHO) Executing 'all' complete!
# mk path for object.
$(OBJ_MD):
$(MD_CHECK) $(Q)$(MD) $(call FIXPATH, $@)
# mk output path.
$(OUTPUT_PATH):
$(MD_CHECK) $(Q)$(MD) $(call FIXPATH, $@)
$(OBJDIR):
$(MD_CHECK) $(Q)$(MD) $(call FIXPATH, $@)
$(OUTPUT_MAIN): $(OBJECTS)
@$(ECHO) Linking : "$@"
$(Q)$(CC) $(CFLAGS) $(LDFLAGS) $(INCLUDES) -Wl,-Map,$(OUTPUT_TARGET).map -o $(OUTPUT_MAIN) $(OBJECTS) $(LFLAGS) $(LIBS) $(OUTPUT_BT_LIB)
main: | $(OUTPUT_PATH) $(OBJDIR) $(OBJ_MD) $(OUTPUT_MAIN)
@$(ECHO) Building : "$(OUTPUT_MAIN)"
# Static Pattern Rules [targets ...: target-pattern: prereq-patterns ...]
$(OBJECTS): $(OBJDIR)/%.o : %.c
@$(ECHO) Compiling : "$<"
$(Q)$(CC) $(CFLAGS) $(INCLUDES) -c $< -o $@
clean:
# $(RM) $(OUTPUT_MAIN)
# $(RM) $(OBJECTS)
# $(RM) $(OBJDIR)
$(Q)$(RM) $(call FIXPATH, $(OUTPUT_PATH))
@$(ECHO) Cleanup complete!
run: all
./$(OUTPUT_MAIN)
@$(ECHO) Executing 'run: all' complete!