-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMakefile
262 lines (213 loc) · 6.64 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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
################################################################################
# EasyTest++ Makefile
#
# Copyright (c) 2015, Loïc Le Page
#
# This project is released under the MIT license.
# See: http://opensource.org/licenses/MIT
################################################################################
#Build configuration
#
#Available options are:
# BUILD=[debug] or release
# PLATFORM=[linux] or mingw
# ARCH=[64] or 32
#
# EXCEPTIONS=[true] or false
# THREADS=[true] or false
#
BUILD?=debug
PLATFORM?=linux
ARCH?=64
EXCEPTIONS?=true
THREADS?=true
ifneq ($(BUILD),debug)
ifneq ($(BUILD),release)
$(error Invalid BUILD type, please select "debug" or "release")
endif
endif
ifneq ($(PLATFORM),linux)
ifneq ($(PLATFORM),mingw)
$(error Invalid PLATFORM type, please select "linux" or "mingw")
endif
endif
ifneq ($(ARCH),64)
ifneq ($(ARCH),32)
$(error Invalid ARCH type, please select "64" or "32")
endif
endif
ifneq ($(EXCEPTIONS),true)
ifneq ($(EXCEPTIONS),false)
$(error Invalid EXCEPTIONS flag, please set to "true" or "false")
endif
endif
ifneq ($(THREADS),true)
ifneq ($(THREADS),false)
$(error Invalid THREADS flag, please set to "true" or "false")
endif
endif
#Project definitions
PROJECT_NAME=EasyTest++
SRC_DIR=src
INCLUDE_DIR=include
BUILD_DIR=build
BIN_DIR=bin
TEST_DIR=test
DOC_DIR=doc/generated
DOXYFILE=doc/Doxyfile
PACKAGE_EXTRA_FILES=LICENSE README.md doc/Build.md doc/Usage.md
#Build paths and tools
ifeq ($(PLATFORM),linux)
AR=ar
CXX=g++
else
ifeq ($(ARCH),64)
AR=x86_64-w64-mingw32-ar
CXX=x86_64-w64-mingw32-g++
else
AR=i686-w64-mingw32-ar
CXX=i686-w64-mingw32-g++
endif
endif
LD=$(CXX)
MKDIR=mkdir -p
RMDIR=rmdir --ignore-fail-on-non-empty
RM=rm -rf
SED=sed
DOCGEN=doxygen
TAR=tar
#Compiler/Linker default options
CXXFLAGS+=-Wall -Werror -std=c++11 -pedantic -fno-rtti
ARFLAGS=rcs
ifeq ($(EXCEPTIONS),false)
CXXFLAGS+=-fno-exceptions
endif
ifeq ($(ARCH),64)
CXXFLAGS+=-m64
LDFLAGS+=-m64
else
CXXFLAGS+=-m32
LDFLAGS+=-m32
endif
ifeq ($(THREADS),true)
ifeq ($(PLATFORM),linux)
CXXFLAGS+=-pthread
LDFLAGS+=-pthread
endif
else
CXXFLAGS:=-DEASYTEST_NO_THREADS $(CXXFLAGS)
endif
ifeq ($(BUILD),debug)
CXXFLAGS:=-g $(CXXFLAGS)
else
CXXFLAGS:=-O3 -s -DNDEBUG $(CXXFLAGS)
LDFLAGS:=-s $(LDFLAGS)
endif
ifeq ($(PLATFORM),mingw)
LDFLAGS+=-static
endif
#Build variables
STATIC_LIB_TARGET=$(BIN_DIR)/lib$(PROJECT_NAME)_$(PLATFORM)$(ARCH)_$(BUILD)
STATIC_OBJ_DIR=$(BUILD_DIR)/static_$(PLATFORM)$(ARCH)_$(BUILD)
TEST_TARGET=$(BIN_DIR)/Test_$(PROJECT_NAME)_$(PLATFORM)$(ARCH)_$(BUILD)
TEST_OBJ_DIR=$(BUILD_DIR)/test_$(PLATFORM)$(ARCH)_$(BUILD)
ifeq ($(EXCEPTIONS),false)
STATIC_LIB_TARGET:=$(STATIC_LIB_TARGET)_noexcept
STATIC_OBJ_DIR:=$(STATIC_OBJ_DIR)_noexcept
TEST_TARGET:=$(TEST_TARGET)_noexcept
TEST_OBJ_DIR:=$(TEST_OBJ_DIR)_noexcept
endif
ifeq ($(THREADS),false)
STATIC_LIB_TARGET:=$(STATIC_LIB_TARGET)_nothreads
STATIC_OBJ_DIR:=$(STATIC_OBJ_DIR)_nothreads
TEST_TARGET:=$(TEST_TARGET)_nothreads
TEST_OBJ_DIR:=$(TEST_OBJ_DIR)_nothreads
endif
STATIC_LIB_TARGET:=$(STATIC_LIB_TARGET).a
ifeq ($(PLATFORM),mingw)
TEST_TARGET:=$(TEST_TARGET).exe
endif
RECFIND_CPP=$(strip $(wildcard $1/*.cpp) $(foreach d, $(wildcard $1/*), $(call RECFIND_CPP, $d)))
SRC_FILES=$(call RECFIND_CPP, $(SRC_DIR))
TEST_FILES=$(call RECFIND_CPP, $(TEST_DIR)) $(SRC_FILES)
STATIC_OBJ_FILES=$(patsubst %.cpp, $(STATIC_OBJ_DIR)/%.o, $(SRC_FILES))
STATIC_DEP_FILES=$(patsubst %.cpp, $(STATIC_OBJ_DIR)/%.d, $(SRC_FILES))
TEST_OBJ_FILES=$(patsubst %.cpp, $(TEST_OBJ_DIR)/%.o, $(TEST_FILES))
TEST_DEP_FILES=$(patsubst %.cpp, $(TEST_OBJ_DIR)/%.d, $(TEST_FILES))
#Package variables
PACKAGE_NAME=$(BIN_DIR)/$(PROJECT_NAME)
PACKAGE_LIBS=$(BIN_DIR)/lib$(PROJECT_NAME)_???????_release
PACKAGE_MSC_LIBS=$(BIN_DIR)/$(PROJECT_NAME)_msc??_release
ifeq ($(EXCEPTIONS),false)
PACKAGE_NAME:=$(PACKAGE_NAME)_noexcept
PACKAGE_LIBS:=$(PACKAGE_LIBS)_noexcept
PACKAGE_MSC_LIBS:=$(PACKAGE_MSC_LIBS)_noexcept
endif
ifeq ($(THREADS),false)
PACKAGE_NAME:=$(PACKAGE_NAME)_nothreads
PACKAGE_LIBS:=$(PACKAGE_LIBS)_nothreads
PACKAGE_MSC_LIBS:=$(PACKAGE_MSC_LIBS)_nothreads
endif
PACKAGE_NAME:=$(PACKAGE_NAME).tgz
PACKAGE_LIBS:=$(PACKAGE_LIBS).a $(PACKAGE_MSC_LIBS).lib
#Build targets and rules
.PHONY: all clean test clean-test doc clean-doc package clean-package clean-all
all: $(STATIC_LIB_TARGET)
clean:
$(info Cleaning $(BUILD) build on $(PLATFORM) $(ARCH) bits...)
@$(RM) $(STATIC_OBJ_DIR) $(STATIC_LIB_TARGET)
@-$(RMDIR) $(BIN_DIR) $(BUILD_DIR) 2>/dev/null
test: $(TEST_TARGET)
clean-test:
$(info Cleaning $(BUILD) tests on $(PLATFORM) $(ARCH) bits...)
@$(RM) $(TEST_OBJ_DIR) $(TEST_TARGET)
@-$(RMDIR) $(BIN_DIR) $(BUILD_DIR) 2>/dev/null
doc:
$(info Building documentation...)
@$(DOCGEN) $(DOXYFILE)
clean-doc:
$(info Cleaning documentation...)
@$(RM) $(DOC_DIR)
package:
$(info Packaging $(PACKAGE_NAME)...)
@$(MAKE) --no-print-directory BUILD=release PLATFORM=linux ARCH=64
@$(MAKE) --no-print-directory BUILD=release PLATFORM=linux ARCH=32
@$(MAKE) --no-print-directory BUILD=release PLATFORM=mingw ARCH=64
@$(MAKE) --no-print-directory BUILD=release PLATFORM=mingw ARCH=32
@$(TAR) -czf $(PACKAGE_NAME) --no-acls --transform "s;^$(BIN_DIR)/;lib/;" --transform "s;_release;;" `ls -x $(PACKAGE_LIBS) 2>/dev/null` $(INCLUDE_DIR) $(PACKAGE_EXTRA_FILES)
clean-package:
$(info Cleaning package $(PACKAGE_NAME)...)
@$(RM) $(PACKAGE_NAME)
@-$(RMDIR) $(BIN_DIR) 2>/dev/null
clean-all:
$(info Cleaning all builds, tests, packages and documentation on all platforms...)
@$(RM) $(BIN_DIR) $(BUILD_DIR) $(DOC_DIR)
$(STATIC_LIB_TARGET): $(STATIC_OBJ_FILES)
$(info Creating $(BUILD) static library on $(PLATFORM) $(ARCH) bits...)
@$(MKDIR) $(@D)
$(AR) $(ARFLAGS) $@ $^
$(TEST_TARGET): $(TEST_OBJ_FILES)
$(info Linking $(BUILD) tests binary on $(PLATFORM) $(ARCH) bits...)
@$(MKDIR) $(@D)
$(LD) $(LDFLAGS) -o $@ $^ $(LDLIBS)
$(STATIC_OBJ_DIR)/%.d: %.cpp
@$(MKDIR) $(@D)
@$(CXX) -MM $(CXXFLAGS) $< 2>/dev/null | $(SED) "s#.*: *#$(STATIC_OBJ_DIR)/$*.o $@: #g" > $@
$(TEST_OBJ_DIR)/%.d: %.cpp
@$(MKDIR) $(@D)
@$(CXX) -MM $(CXXFLAGS) $< 2>/dev/null | $(SED) "s#.*: *#$(TEST_OBJ_DIR)/$*.o $@: #g" > $@
ifeq ($(MAKECMDGOALS),test)
-include $(TEST_DEP_FILES)
else ifneq ($(MAKECMDGOALS),doc)
ifneq ($(MAKECMDGOALS),package)
ifeq ($(findstring clean,$(MAKECMDGOALS)),)
-include $(STATIC_DEP_FILES)
endif
endif
endif
$(STATIC_OBJ_DIR)/%.o: %.cpp
@$(MKDIR) $(@D)
$(CXX) $(CXXFLAGS) -o $@ -c $<
$(TEST_OBJ_DIR)/%.o: %.cpp
@$(MKDIR) $(@D)
$(CXX) $(CXXFLAGS) -o $@ -c $<