-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
gcc.mk
143 lines (123 loc) · 3.53 KB
/
gcc.mk
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
RELEASE ?= 0 # default debug
UNICODE ?= 0 # default ansi
VERSION ?= 0
FILENAME ?= 1
ifdef PLATFORM
CROSS:=$(PLATFORM)-
else
CROSS:=
PLATFORM:=linux
endif
ifeq ($(RELEASE),1)
BUILD:=release
else
BUILD:=debug
endif
KERNEL := $(shell uname -s)
ifeq ($(KERNEL),Linux)
DEFINES += OS_LINUX
endif
ifeq ($(KERNEL),Darwin)
DEFINES += OS_MAC
endif
#--------------------------------Compile-----------------------------
#
#--------------------------------------------------------------------
AR := $(CROSS)ar
CC := $(CROSS)gcc
CXX := $(CROSS)g++
CFLAGS += -Wall -fPIC
CXXFLAGS += -Wall
ifeq ($(RELEASE),1)
CFLAGS += -Wall -O2
CXXFLAGS += $(CFLAGS)
DEFINES += NDEBUG
else
CFLAGS += -g -Wall
# CFLAGS += -fsanitize=address
CXXFLAGS += $(CFLAGS)
DEFINES += DEBUG _DEBUG
endif
ifeq ($(FILENAME),1)
CFLAGS += -Wno-builtin-macro-redefined -D'__FILE_NAME__="$(notdir $<)"'
endif
ifeq ($(VERSION),1)
VERSIONFILE = version.o
endif
# default don't export anything
CFLAGS += -fvisibility=hidden
COMPILE.CC = $(CC) $(addprefix -I,$(INCLUDES)) $(addprefix -D,$(DEFINES)) $(CFLAGS)
COMPILE.CXX = $(CXX) $(addprefix -I,$(INCLUDES)) $(addprefix -D,$(DEFINES)) $(CXXFLAGS)
#-------------------------Link---------------------------
#
#--------------------------------------------------------------------
ifeq ($(STATIC_LINK),1)
LDFLAGS += -static
endif
#-------------------------Compile Output---------------------------
#
#--------------------------------------------------------------------
ifeq ($(UNICODE),1)
OUTPATH += unicode.$(BUILD).$(PLATFORM)
else
OUTPATH += $(BUILD).$(PLATFORM)
endif
OBJSPATH = $(OUTPATH)/objs
OBJECT_FILES := $(patsubst %.c,%.o,$(patsubst %.cpp,%.o,$(SOURCE_FILES)))
OBJECT_FILES := $(addprefix $(OBJSPATH)/,$(OBJECT_FILES))
DEPENDENCE_FILES := $(OBJECT_FILES:%.o=%.d)
MKDIR = @mkdir -p $(dir $@)
#--------------------------Makefile Rules----------------------------
#
#--------------------------------------------------------------------
$(OUTPATH)/$(OUTFILE): $(OBJECT_FILES) $(STATIC_LIBS) $(VERSIONFILE)
ifeq ($(OUTTYPE),0)
$(CXX) -o $@ -Wl,-rpath . $(LDFLAGS) $^ $(addprefix -L,$(LIBPATHS)) $(addprefix -l,$(LIBS))
else
ifeq ($(OUTTYPE),1)
$(CXX) -o $@ -shared -fPIC -rdynamic -Wl,-rpath . $(LDFLAGS) $^ $(addprefix -L,$(LIBPATHS)) $(addprefix -l,$(LIBS))
else
@echo -e "\033[35m AR $(notdir $@)\033[0m"
@$(AR) -rc $@ $^
endif
endif
@echo make ok, output: $(OUTPATH)/$(OUTFILE)
$(OBJSPATH)/%.o : %.c
$(MKDIR)
@$(COMPILE.CC) -c -o $@ $<
@echo -e "\033[35m CC $(notdir $@)\033[0m"
$(OBJSPATH)/%.o : %.cpp
$(MKDIR)
@$(COMPILE.CXX) -c -o $@ $<
@echo -e "\033[35m CXX $(notdir $@)\033[0m"
$(OBJSPATH)/%.d: %.c
$(MKDIR)
@echo -e "\033[32m CREATE $(notdir $@)\033[0m"
@rm -f $@; \
$(COMPILE.CC) -MM $(CFLAGS) $< > $@.$$$$; \
sed 's,\($(notdir $*)\)\.o[ :]*,$*\.o $@ : ,g' < $@.$$$$ > $@; \
rm -f $@.$$$$
$(OBJSPATH)/%.d: %.cpp
$(MKDIR)
@echo -e "\033[32m CREATE $(notdir $@)\033[0m"
@rm -f $@; \
$(COMPILE.CXX) -MM $(CXXFLAGS) $< > $@.$$$$; \
sed 's,\($(notdir $*)\)\.o[ :]*,$*\.o $@ : ,g' < $@.$$$$ > $@; \
rm -f $@.$$$$
ifeq ($(MAKECMDGOALS), clean)
else ifeq ($(MAKECMDGOALS), debug)
else
sinclude $(DEPENDENCE_FILES)
endif
version.o: version.ver $(OBJECT_FILES) $(STATIC_LIBS)
$(ROOT)/gitver.sh version.ver version.c
@$(COMPILE.CXX) -c -o $@ version.c
.PHONY: clean
clean:
@echo -e "\033[35m rm -rf *.o *.d version.h $(OUTPATH)/$(OUTFILE) \033[0m"
@rm -f $(OBJECT_FILES) $(OUTPATH)/$(OUTFILE) $(DEPENDENCE_FILES)
@rm -f version.c version.o
debug:
echo $(OUTPATH)/$(OUTFILE)
echo $(OBJECT_FILES)
echo $(DEPENDENCE_FILES)