-
Notifications
You must be signed in to change notification settings - Fork 1
/
Makefile
283 lines (235 loc) · 9.56 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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
#
# Use "make help" to find out about configuration options.
#
EXE_NAME ?= $(shell cat build_helpers/findexename)
EXE_VER_MAJOR ?= 1
EXE_VER_MINOR ?= 0
EXE_VER_PATCHLEVEL ?= 3
EXE_VERSION ?= $(EXE_VER_MAJOR).$(EXE_VER_MINOR)-$(EXE_VER_PATCHLEVEL)
EXE ?= $(BIN_PATH)/$(EXE_NAME)
EXE_UNSTRIPPED ?= $(EXE)-unstripped
PACKAGE_NAME ?= $(shell cat build_helpers/packagename)
SOURCE_PATH ?= ./source
BIN_PATH ?= ./bin
EXTERNAL_PATH ?= ./external
PACKAGING_PATH ?= ./packaging
BUILD_HELPERS_PATH ?= ./build_helpers
INST_PATH ?= /usr/local/bin
PKG_INST_PATH ?= /usr/bin
CXX ?= g++
STRIP ?= strip
CXX_FLAVOR ?= c++17
CXXFLAGS_COMMON = -D_LARGEFILE64_SOURCE -D_FILE_OFFSET_BITS=64 $(CXXFLAGS_BOOST) \
-DNCURSES_NOMACROS -DEXE_NAME=\"$(EXE_NAME)\" -DEXE_VERSION=\"$(EXE_VERSION)\" \
-I $(SOURCE_PATH) \
-Wall -Wunused-variable -Woverloaded-virtual -Wextra -Wno-unused-parameter -fmessage-length=0 \
-fno-strict-aliasing -pthread -ggdb -std=$(CXX_FLAVOR)
CXXFLAGS_RELEASE = -O3 -Wuninitialized
CXXFLAGS_DEBUG = -O0 -D_FORTIFY_SOURCE=2 -DBUILD_DEBUG
LDFLAGS_COMMON = -rdynamic -pthread -lrt -lstdc++fs
LDFLAGS_RELASE = -O3
LDFLAGS_DEBUG = -O0
SOURCES := $(shell find $(SOURCE_PATH) -name '*.cpp')
OBJECTS := $(SOURCES:.cpp=.o)
OBJECTS_CLEANUP := $(shell find $(SOURCE_PATH) -name '*.o') # separate to clean after C file rename
DEPENDENCY_FILES := $(shell find $(SOURCE_PATH) -name '*.d')
# Release & debug flags for compiler and linker
ifeq ($(BUILD_DEBUG), 1)
CXXFLAGS = $(CXXFLAGS_COMMON) $(CXXFLAGS_DEBUG) $(CXXFLAGS_EXTRA)
LDFLAGS = $(LDFLAGS_COMMON) $(LDFLAGS_DEBUG) $(LDFLAGS_EXTRA)
else
CXXFLAGS = $(CXXFLAGS_COMMON) $(CXXFLAGS_RELEASE) $(CXXFLAGS_EXTRA)
LDFLAGS = $(LDFLAGS_COMMON) $(LDFLAGS_RELASE) $(LDFLAGS_EXTRA)
endif
# Dynamic or static linking
ifeq ($(BUILD_STATIC), 1)
LDFLAGS += -static
else # dynamic linking
endif
# Use Microsoft mimalloc for memory allocations.
# Note: This needs to come as very last in link order, thus we have a separate variable to ensure
# it's the trailing arg for the linker. (Can be confirmed e.g. via MIMALLOC_SHOW_STATS=1)
ifeq ($(USE_MIMALLOC), 1)
CXXFLAGS += -DUSE_MIMALLOC
LDFLAGS_MIMALLOC_TAIL := -L external/mimalloc/build -l:libmimalloc.a
endif
# Support build in Cygwin environment
ifeq ($(CYGWIN_SUPPORT), 1)
# EXE_UNSTRIPPED includes EXE in definition, so must be updated first
EXE_UNSTRIPPED := $(EXE_UNSTRIPPED).exe
EXE := $(EXE).exe
CXX_FLAVOR := gnu++17
CXXFLAGS += -DCYGWIN_SUPPORT
endif
all: $(SOURCES) $(EXE)
$(EXE): $(EXE_UNSTRIPPED)
ifdef BUILD_VERBOSE
$(STRIP) --strip-debug $(EXE_UNSTRIPPED) -o $(EXE)
else
@echo [STRIP] $@
@$(STRIP) --strip-debug $(EXE_UNSTRIPPED) -o $(EXE)
endif
$(EXE_UNSTRIPPED): $(OBJECTS)
ifdef BUILD_VERBOSE
$(CXX) -o $(EXE_UNSTRIPPED) $(OBJECTS) $(LDFLAGS) $(LDFLAGS_MIMALLOC_TAIL)
else
@echo [LINK] $@
@$(CXX) -o $(EXE_UNSTRIPPED) $(OBJECTS) $(LDFLAGS) $(LDFLAGS_MIMALLOC_TAIL)
endif
.c.o:
ifdef BUILD_VERBOSE
$(CXX) $(CXXFLAGS) -c $(@:.o=.c) -E -MMD -MF $(@:.o=.d) -MT $(@) -o /dev/null
$(CXX) $(CXXFLAGS) -c $(@:.o=.c) -o $(@)
else
@echo [DEP] $(@:.o=.d)
@$(CXX) $(CXXFLAGS) -c $(@:.o=.c) -E -MMD -MF $(@:.o=.d) -MT $(@) -o /dev/null
@echo [CXX] $@
@$(CXX) $(CXXFLAGS) -c $(@:.o=.c) -o $(@)
endif
.cpp.o:
ifdef BUILD_VERBOSE
$(CXX) $(CXXFLAGS) -c $(@:.o=.cpp) -E -MMD -MF $(@:.o=.d) -MT $(@) -o /dev/null
$(CXX) $(CXXFLAGS) -c $(@:.o=.cpp) -o $(@)
else
@echo [DEP] $(@:.o=.d)
@$(CXX) $(CXXFLAGS) -c $(@:.o=.cpp) -E -MMD -MF $(@:.o=.d) -MT $(@) -o /dev/null
@echo [CXX] $@
@$(CXX) $(CXXFLAGS) -c $(@:.o=.cpp) -o $(@)
endif
$(OBJECTS): Makefile | externals features-info # Makefile dep to rebuild all on Makefile change
externals:
ifdef BUILD_VERBOSE
PREP_MIMALLOC=$(USE_MIMALLOC) $(EXTERNAL_PATH)/prepare-external.sh
else
@PREP_MIMALLOC=$(USE_MIMALLOC) $(EXTERNAL_PATH)/prepare-external.sh
endif
features-info:
ifeq ($(USE_MIMALLOC),1)
$(info [OPT] mimalloc enabled)
else
$(info [OPT] mimalloc disabled)
endif
clean: clean-packaging clean-buildhelpers
ifdef BUILD_VERBOSE
rm -rf $(OBJECTS_CLEANUP) $(DEPENDENCY_FILES) $(EXE) $(EXE).exe $(EXE_UNSTRIPPED) \
$(EXE_UNSTRIPPED).exe
else
@echo "[DELETE] OBJECTS, DEPENDENCY_FILES, EXECUTABLES"
@rm -rf $(OBJECTS_CLEANUP) $(DEPENDENCY_FILES) $(EXE) $(EXE).exe $(EXE_UNSTRIPPED) \
$(EXE_UNSTRIPPED).exe
endif
clean-externals:
ifdef BUILD_VERBOSE
rm -rf $(EXTERNAL_PATH)/mimalloc
else
@echo "[DELETE] EXTERNALS"
@rm -rf $(EXTERNAL_PATH)/mimalloc
endif
clean-packaging:
ifdef BUILD_VERBOSE
rm -rf \
$(EXE)-*-static-* \
$(PACKAGING_PATH)/$(PACKAGE_NAME)-*-static-*.tar.* \
rm -rf \
$(PACKAGING_PATH)/BUILDROOT \
$(PACKAGING_PATH)/RPMS/* $(PACKAGING_PATH)/SPECS/rpm.spec
bash -c "rm -rf $(PACKAGING_PATH)/$(PACKAGE_NAME)*.{deb,ddeb,build,buildinfo,changes}"
else
@echo "[DELETE] PACKAGING_FILES"
@rm -rf \
$(EXE)-*-static-* \
$(PACKAGING_PATH)/$(PACKAGE_NAME)-*-static-*.tar.* \
@rm -rf \
$(PACKAGING_PATH)/BUILDROOT \
$(PACKAGING_PATH)/RPMS/* $(PACKAGING_PATH)/SPECS/rpm.spec
@bash -c "rm -rf $(PACKAGING_PATH)/$(PACKAGE_NAME)*.{deb,ddeb,build,buildinfo,changes}"
endif
clean-all: clean clean-externals clean-packaging clean-buildhelpers
install: all
@echo "Installing..."
install -p -m u=rwx,g=rx,o=rx $(EXE) $(INST_PATH)/
uninstall:
rm -f $(INST_PATH)/$(EXE_NAME)
# prepare generic part of build-root (not the .rpm or .deb specific part)
prepare-buildroot: | all clean-packaging
@echo "[PACKAGING] PREPARE BUILDROOT"
mkdir -p $(PACKAGING_PATH)/BUILDROOT/$(PKG_INST_PATH)
# copy main executable
cp --preserve $(EXE) $(PACKAGING_PATH)/BUILDROOT/$(PKG_INST_PATH)
# copy contents of dist subdir
for dir in $(shell find dist/ -mindepth 1 -type d -printf "%P\n"); do \
mkdir -p $(PACKAGING_PATH)/BUILDROOT/$$dir; \
done
for file in $(shell find dist/ -mindepth 1 -type f -printf "%P\n"); do \
cp --preserve dist/$$file $(PACKAGING_PATH)/BUILDROOT/$$file; \
done
rpm: | prepare-buildroot
@echo "[PACKAGING] PREPARE RPM PACKAGE"
cp $(PACKAGING_PATH)/SPECS/rpm.spec.template $(PACKAGING_PATH)/SPECS/rpm.spec
sed -i "s/__PACKAGE_NAME__/$(PACKAGE_NAME)/g" $(PACKAGING_PATH)/SPECS/rpm.spec
sed -i "s/__FIND_EXE_NAME__/$(EXE_NAME)/g" $(PACKAGING_PATH)/SPECS/rpm.spec
sed -i "s/__VERSION__/$(EXE_VER_MAJOR).$(EXE_VER_MINOR).$(EXE_VER_PATCHLEVEL)/g" \
$(PACKAGING_PATH)/SPECS/rpm.spec
rpmbuild $(PACKAGING_PATH)/SPECS/rpm.spec --bb --define "_topdir $(PWD)/$(PACKAGING_PATH)" \
--define "__spec_install_pre /bin/true" --buildroot=$(PWD)/$(PACKAGING_PATH)/BUILDROOT
@echo
@echo "All done. Your package is here:"
@find $(PACKAGING_PATH)/RPMS -name $(PACKAGE_NAME)*.rpm
deb: | prepare-buildroot
@echo "[PACKAGING] PREPARE DEB PACKAGE"
cp -r $(PACKAGING_PATH)/debian $(PACKAGING_PATH)/BUILDROOT
cp $(PACKAGING_PATH)/BUILDROOT/debian/control.template \
$(PACKAGING_PATH)/BUILDROOT/debian/control
cp $(PACKAGING_PATH)/BUILDROOT/debian/rules.template \
$(PACKAGING_PATH)/BUILDROOT/debian/rules
sed -i "s/__PACKAGE_NAME__/$(PACKAGE_NAME)/g" $(PACKAGING_PATH)/BUILDROOT/debian/control
sed -i "s/__PACKAGE_NAME__/$(PACKAGE_NAME)/g" $(PACKAGING_PATH)/BUILDROOT/debian/rules
sed -i "s/__FIND_EXE_NAME__/$(EXE_NAME)/g" $(PACKAGING_PATH)/BUILDROOT/debian/rules
cd $(PACKAGING_PATH)/BUILDROOT && \
EDITOR=/bin/true VISUAL=/bin/true debchange --create --package $(PACKAGE_NAME) \
--urgency low --noquery -M \
--newversion "$(EXE_VER_MAJOR).$(EXE_VER_MINOR).$(EXE_VER_PATCHLEVEL)" \
"Custom package build."
cd $(PACKAGING_PATH)/BUILDROOT && \
debuild -b -us -uc
@echo
@echo "All done. Your package is here:"
@find $(PACKAGING_PATH) -name $(PACKAGE_NAME)*.deb
version:
@echo $(EXE_VERSION)
help:
@echo 'Optional Build Features:'
@echo ' CYGWIN_SUPPORT=0|1 - Adapt build features to enable build in Cygwin'
@echo ' environment. (Default: 0)'
@echo ' USE_MIMALLOC=0|1 - Use Microsoft mimalloc library for memory'
@echo ' allocation management. Recommended when using'
@echo ' musl-libc. (Default: 0)'
@echo
@echo 'Optional Compile/Link Arguments:'
@echo ' CXX=<string> - Path to alternative C++ compiler. (Default: g++)'
@echo ' CXX_FLAVOR=<string> - C++ standard compiler flag. (Default: c++17)'
@echo ' CXXFLAGS_EXTRA=<string> - Additional C++ compiler flags.'
@echo ' LDFLAGS_EXTRA=<string> - Additional linker flags.'
@echo ' BUILD_VERBOSE=1 - Enable verbose build output.'
@echo ' BUILD_STATIC=1 - Generate a static binary without dependencies.'
@echo ' (Tested only on Alpine Linux.)'
@echo ' BUILD_DEBUG=1 - Include debug info in executable.'
@echo
@echo 'Targets:'
@echo ' all (default) - Build executable'
@echo ' clean - Remove build artifacts'
@echo ' clean-all - Remove build artifacts and external sources'
@echo ' install - Install executable to /usr/local/bin'
@echo ' uninstall - Uninstall executable from /usr/local/bin'
@echo ' rpm - Create RPM package file'
@echo ' deb - Create Debian package file'
@echo ' help - Print this help message'
@echo
@echo 'Note: Use "make clean-all" when changing any optional build features.'
.PHONY: clean clean-all clean-externals clean-packaging clean-buildhelpers deb externals \
features-info help prepare-buildroot rpm version
.DEFAULT_GOAL := all
# Include dependency files
ifneq ($(DEPENDENCY_FILES),)
include $(DEPENDENCY_FILES)
endif