-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathmake.rules
More file actions
executable file
·187 lines (139 loc) · 4.22 KB
/
Copy pathmake.rules
File metadata and controls
executable file
·187 lines (139 loc) · 4.22 KB
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
#-*- mode: makefile;-*-
### make.rules file for c/c++ projects
### Usage: set cpp_sources to the c/c++ sourece files
### and then set out_executable = a.out if target is an execute file
### or set out_library = libfoo.so or out_library=libfoo.a if target is a out_library
### For example:
### -------------- makefile ---------------------
### cpp_sources = a.c b.cpp
### out_executable = foo.bin
### include make.rules
### -----------------------------------------------
###
### include_dirs : set head files include path ( -I )
### Example: include_dirs = /home/myinclude /home/foo
### link_dirs : set link path ( -L )
### Example: link_dirs = /home/usr/libs /home/devel/libs
### link_libs : set link out_library ( -l )
### Example: link_libs = m pthread
### debug : set debug flags ( -g or -O2 )
### quiet : 0 or 1 display detail compiling info
.PHONY : build clean strip
CROSS_PREFIX=
# set the default build directory
BUILD_DIR ?= objects
MKDIR ?= mkdir -p
RMDIR ?= rmdir -p
AR = ar rcs
CPPFLAGS += $(include_dirs:%=-I%)
debug ?= 0
quiet ?= 0
CXX = $(CROSS_PREFIX)g++
CC = $(CROSS_PREFIX)gcc
STRIP = $(CROSS_PREFIX)strip
CXXFLAGS += -Wall -pipe
CFLAGS += -Wall -pipe
# build shared lib
ifneq ($(out_library),)
CXXFLAGS += -fPIC
CFLAGS += -fPIC
endif
LDFLAGS += -rdynamic
LDFLAGS += $(link_dirs:%=-L%)
LDFLAGS += $(link_libs:%=-l%)
ifneq ($(out_library),)
LDFLAGS += -shared
endif
ifeq ($(debug),1)
CXXFLAGS += -g
CFLAGS += -g
CPPFLAGS += -D_DEBUG
else
CXXFLAGS += -g
CFLAGS += -g
CFLAGS += -O2
CXXFLAGS += -O2
CPPFLAGS += -DNDEBUG
endif
CPPFLAGS += -D_REENTRANT=1
ifeq ($(quiet),1)
SILENT = @
else
SILENT =
endif
SRCS_NOEXT := $(addprefix $(BUILD_DIR)/, $(notdir $(basename $(cpp_sources))))
OBJS := $(addsuffix .o, $(SRCS_NOEXT))
vpath %.cpp $(dir $(cpp_sources))
vpath %.c $(dir $(cpp_sources))
ifneq (,$(findstring .cpp, $(cpp_sources)))
LINKER = $(CXX)
else
LINKER = $(CC)
endif
CXX_DEPEND = $(CXX) -MM $(CPPFLAGS) $(include_dirs:%=-I%)
CC_DEPEND = $(CC) -MM $(CPPFLAGS) $(include_dirs:%=-I%)
DEPS := $(OBJS:.o=.d)
# default target
build: $(BUILD_DIR)/folder.exist $(OBJS) $(out_executable) $(out_library)
@echo Build $(out_executable) $(out_library) Finished.
$(BUILD_DIR)/folder.exist :
@echo Create $(BUILD_DIR) folder ...
@$(MKDIR) $(BUILD_DIR) > /dev/null 2>&1 || :
@touch $@
# Delete the default suffixes
.SUFFIXES:
$(BUILD_DIR)/%.o: %.cpp
@echo Compile $<
$(SILENT) $(CXX) -c $(CXXFLAGS) $(CPPFLAGS) $< -o $@
$(BUILD_DIR)/%.o: %.c
@echo Compile $<
$(SILENT) $(CC) -c $(CFLAGS) $(CPPFLAGS) $< -o $@
$(BUILD_DIR)/%.d : %.cpp
# @echo Generating $< dependence ...
@$(CXX_DEPEND) $< | sed '1s,^,$@ $(BUILD_DIR)/,' > $@
$(BUILD_DIR)/%.d : %.c
# @echo Generating $< dependence ...
@$(CC_DEPEND) $< | sed '1s,^,$@ $(BUILD_DIR)/,' > $@
ifneq ($(out_executable),)
$(out_executable): $(PREREQ) $(OBJS)
$(PRE_LINK_COMMAND)
@echo Linking program $(out_executable)...
@$(MKDIR) $(dir $@) > /dev/null 2>&1 || :
$(SILENT) $(LINKER) -o $@ $(OBJS) $(LDFLAGS) $(STATIC_LIBS)
$(POST_BUILD_COMMAND)
endif
ifneq ($(out_library),)
$(out_library): $(PREREQ) $(OBJS)
$(PRE_LINK_COMMAND)
@echo Linking out_library $(out_library)...
@$(MKDIR) $(dir $@) > /dev/null 2>&1 || :
ifeq ($(suffix $(out_library)), .a)
@echo $(AR) $@ $(OBJS) $(STATIC_LIBS)
$(SILENT) $(AR) $@ $(OBJS) $(STATIC_LIBS)
else
@echo $(LINKER) -o $@ $(OBJS) $(LDFLAGS) $(STATIC_LIBS)
$(SILENT) $(LINKER) -o $@ $(OBJS) $(LDFLAGS) $(STATIC_LIBS)
endif
$(POST_BUILD_COMMAND)
endif
clean::
@echo clean $(out_executable) $(out_library)...
$(SILENT)-$(RM) $(OBJS)
$(SILENT)-$(RM) $(DEPS)
ifneq ($(out_executable),)
$(SILENT)-$(RM) $(out_executable)
endif
ifneq ($(out_library),)
$(SILENT)-$(RM) $(out_library)
endif
$(SILENT)-$(RM) $(BUILD_DIR)/folder.exist
$(SILENT)-$(RMDIR) $(BUILD_DIR) 2>/dev/null || :
strip:: build
$(STRIP) $(out_executable) $(out_library)
ifneq ($(MAKECMDGOALS),clean)
ifneq ($(DEPS),)
-include $(BUILD_DIR)/folder.exist
-include $(DEPS)
-include $(BUILD_DIR)/folder.exist
endif
endif