forked from injinj/rdbparser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGNUmakefile
320 lines (277 loc) · 10.5 KB
/
GNUmakefile
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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
# rdbparser makefile
lsb_dist := $(shell if [ -f /etc/os-release ] ; then \
grep '^NAME=' /etc/os-release | sed 's/.*=[\"]*//' | sed 's/[ \"].*//' ; \
elif [ -x /usr/bin/lsb_release ] ; then \
lsb_release -is ; else echo Linux ; fi)
lsb_dist_ver := $(shell if [ -f /etc/os-release ] ; then \
grep '^VERSION=' /etc/os-release | sed 's/.*=[\"]*//' | sed 's/[ \"].*//' ; \
elif [ -x /usr/bin/lsb_release ] ; then \
lsb_release -rs | sed 's/[.].*//' ; else uname -r | sed 's/[-].*//' ; fi)
#lsb_dist := $(shell if [ -x /usr/bin/lsb_release ] ; then lsb_release -is ; else uname -s ; fi)
#lsb_dist_ver := $(shell if [ -x /usr/bin/lsb_release ] ; then lsb_release -rs | sed 's/[.].*//' ; else uname -r | sed 's/[-].*//' ; fi)
uname_m := $(shell uname -m)
short_dist_lc := $(patsubst CentOS,rh,$(patsubst RedHatEnterprise,rh,\
$(patsubst RedHat,rh,\
$(patsubst Fedora,fc,$(patsubst Ubuntu,ub,\
$(patsubst Debian,deb,$(patsubst SUSE,ss,$(lsb_dist))))))))
short_dist := $(shell echo $(short_dist_lc) | tr a-z A-Z)
pwd := $(shell pwd)
rpm_os := $(short_dist_lc)$(lsb_dist_ver).$(uname_m)
# this is where the targets are compiled
build_dir ?= $(short_dist)$(lsb_dist_ver)_$(uname_m)$(port_extra)
bind := $(build_dir)/bin
libd := $(build_dir)/lib64
objd := $(build_dir)/obj
dependd := $(build_dir)/dep
default_cflags := -ggdb -O3
# use 'make port_extra=-g' for debug build
ifeq (-g,$(findstring -g,$(port_extra)))
default_cflags := -ggdb
endif
ifeq (-a,$(findstring -a,$(port_extra)))
default_cflags := -fsanitize=address -ggdb -O3
endif
ifeq (-mingw,$(findstring -mingw,$(port_extra)))
CC := /usr/bin/x86_64-w64-mingw32-gcc
CXX := /usr/bin/x86_64-w64-mingw32-g++
mingw := true
endif
ifeq (,$(port_extra))
build_cflags := $(shell if [ -x /bin/rpm ]; then /bin/rpm --eval '%{optflags}' ; \
elif [ -x /bin/dpkg-buildflags ] ; then /bin/dpkg-buildflags --get CFLAGS ; fi)
endif
# msys2 using ucrt64
ifeq (MSYS2,$(lsb_dist))
mingw := true
endif
CC ?= gcc
CXX ?= g++
cc := $(CC) -std=c11
cpp := $(CXX)
arch_cflags := -fno-omit-frame-pointer
gcc_wflags := -Wall -Wextra -Werror
# if windows cross compile
ifeq (true,$(mingw))
dll := dll
exe := .exe
soflag := -shared -Wl,--subsystem,windows
fpicflags := -fPIC -DRDB_SHARED
dynlink_lib := -lpcre2-8
NO_STL := 1
else
dll := so
exe :=
soflag := -shared
fpicflags := -fPIC
dynlink_lib := -lpcre2-8
endif
# make apple shared lib
ifeq (Darwin,$(lsb_dist))
dll := dylib
endif
# rpmbuild uses RPM_OPT_FLAGS
#ifeq ($(RPM_OPT_FLAGS),)
CFLAGS ?= $(build_cflags) $(default_cflags)
#else
#CFLAGS ?= $(RPM_OPT_FLAGS)
#endif
cflags := $(gcc_wflags) $(CFLAGS) $(arch_cflags)
INCLUDES ?= -Iinclude
DEFINES ?=
includes := $(INCLUDES)
defines := $(DEFINES)
# if not linking libstdc++
ifdef NO_STL
cppflags := -std=c++11 -fno-rtti -fno-exceptions
cpplink := $(CC)
else
cppflags := -std=c++11
cpplink := $(CXX)
endif
# test submodules exist (they don't exist for dist_rpm, dist_dpkg targets)
test_makefile = $(shell if [ -f ./$(1)/GNUmakefile ] ; then echo ./$(1) ; \
elif [ -f ../$(1)/GNUmakefile ] ; then echo ../$(1) ; fi)
lzf_home := $(call test_makefile,lzf)
ifneq (,$(lzf_home))
lzf_lib := $(lzf_home)/$(libd)/liblzf.a
lzf_dll := $(lzf_home)/$(libd)/liblzf.$(dll)
lnk_lib += $(lzf_lib)
lnk_dep += $(lzf_lib)
dlnk_lib += -L$(lzf_home)/$(libd) -llzf
dlnk_dep += $(lzf_dll)
rpath1 = ,-rpath,$(pwd)/$(lzf_home)/$(libd)
includes += -I$(lzf_home)/include
else
lnk_lib += -llzf
dlnk_lib += -llzf
includes += -Iliblzf
endif
rpath := -Wl,-rpath,$(pwd)/$(libd)$(rpath1)
math_lib := -lm
.PHONY: everything
everything: all
# copr/fedora build (with version env vars)
# copr uses this to generate a source rpm with the srpm target
-include .copr/Makefile
# debian build (debuild)
# target for building installable deb: dist_dpkg
-include deb/Makefile
all_exes :=
all_libs :=
all_dlls :=
all_depends :=
librdbparser_files := rdb_decode rdb_json rdb_restore rdb_pcre
librdbparser_cfile := $(addprefix src/, $(addsuffix .cpp, $(librdbparser_files)))
librdbparser_objs := $(addprefix $(objd)/, $(addsuffix .o, $(librdbparser_files)))
librdbparser_dbjs := $(addprefix $(objd)/, $(addsuffix .fpic.o, $(librdbparser_files)))
librdbparser_deps := $(addprefix $(dependd)/, $(addsuffix .d, $(librdbparser_files))) \
$(addprefix $(dependd)/, $(addsuffix .fpic.d, $(librdbparser_files)))
librdbparser_dlnk := $(dlnk_lib)
librdbparser_spec := $(version)-$(build_num)_$(git_hash)
librdbparser_ver := $(major_num).$(minor_num)
$(libd)/librdbparser.a: $(librdbparser_objs)
$(libd)/librdbparser.$(dll): $(librdbparser_dbjs)
all_libs += $(libd)/librdbparser.a
all_dlls += $(libd)/librdbparser.$(dll)
all_depends += $(librdbparser_deps)
rdbp_files := rdb_main
rdbp_cfile := src/rdb_main.cpp
rdbp_objs := $(addprefix $(objd)/, $(addsuffix .o, $(rdbp_files)))
rdbp_deps := $(addprefix $(dependd)/, $(addsuffix .d, $(rdbp_files)))
rdbp_libs := $(libd)/librdbparser.a
# statically link it
rdbp_lnk := $(rdbp_libs) $(lnk_lib)
$(bind)/rdbp$(exe): $(rdbp_objs) $(rdbp_libs)
all_exes += $(bind)/rdbp$(exe)
all_depends += $(rdbp_deps)
all_dirs := $(bind) $(libd) $(objd) $(dependd)
all: $(all_libs) $(all_dlls) $(all_exes) cmake
.PHONY: cmake
cmake: CMakeLists.txt
.ONESHELL: CMakeLists.txt
CMakeLists.txt: .copr/Makefile
@cat <<'EOF' > $@
cmake_minimum_required (VERSION 3.9.0)
if (POLICY CMP0111)
cmake_policy(SET CMP0111 OLD)
endif ()
project (rdbparser)
include_directories (
include
$${CMAKE_SOURCE_DIR}/lzf/include
)
if (CMAKE_SYSTEM_NAME STREQUAL "Windows")
add_definitions(/DPCRE2_STATIC)
if ($$<CONFIG:Release>)
add_compile_options (/arch:AVX2 /GL /std:c11)
else ()
add_compile_options (/arch:AVX2 /std:c11)
endif ()
if (NOT TARGET pcre2-8-static)
add_library (pcre2-8-static STATIC IMPORTED)
set_property (TARGET pcre2-8-static PROPERTY IMPORTED_LOCATION_DEBUG ../pcre2/build/Debug/pcre2-8-staticd.lib)
set_property (TARGET pcre2-8-static PROPERTY IMPORTED_LOCATION_RELEASE ../pcre2/build/Release/pcre2-8-static.lib)
include_directories (../pcre2/build)
else ()
include_directories ($${CMAKE_BINARY_DIR}/pcre2)
endif ()
if (NOT TARGET lzf)
add_library (lzf STATIC IMPORTED)
set_property (TARGET lzf PROPERTY IMPORTED_LOCATION_DEBUG ../lzf/build/Debug/lzf.lib)
set_property (TARGET lzf PROPERTY IMPORTED_LOCATION_RELEASE ../lzf/build/Release/lzf.lib)
endif ()
else ()
add_compile_options ($(cflags))
if (TARGET pcre2-8-static)
include_directories ($${CMAKE_BINARY_DIR}/pcre2)
endif ()
if (NOT TARGET lzf)
add_library (lzf STATIC IMPORTED)
set_property (TARGET lzf PROPERTY IMPORTED_LOCATION ../lzf/build/liblzf.a)
endif ()
endif ()
add_library (rdbparser STATIC $(librdbparser_cfile))
if (TARGET pcre2-8-static)
link_libraries (rdbparser lzf pcre2-8-static)
else ()
link_libraries (rdbparser lzf -lpcre2-8)
endif ()
add_executable (rdbp $(rdbp_cfile))
EOF
# create directories
$(dependd):
@mkdir -p $(all_dirs)
# remove target bins, objs, depends
.PHONY: clean
clean:
rm -r -f $(bind) $(libd) $(objd) $(dependd)
if [ "$(build_dir)" != "." ] ; then rmdir $(build_dir) ; fi
.PHONY: clean_dist
clean_dist:
rm -rf dpkgbuild rpmbuild
.PHONY: clean_all
clean_all: clean clean_dist
$(dependd)/depend.make: $(dependd) $(all_depends)
@echo "# depend file" > $(dependd)/depend.make
@cat $(all_depends) >> $(dependd)/depend.make
ifeq (SunOS,$(lsb_dist))
remove_rpath = rpath -r
else
remove_rpath = chrpath -d
endif
# target used by rpmbuild, dpkgbuild
.PHONY: dist_bins
dist_bins: $(all_libs) $(all_dlls) $(bind)/rdbp$(exe)
$(remove_rpath) $(libd)/librdbparser.$(dll)
$(remove_rpath) $(bind)/rdbp$(exe)
# target for building installable rpm
.PHONY: dist_rpm
dist_rpm: srpm
( cd rpmbuild && rpmbuild --define "-topdir `pwd`" -ba SPECS/rdbparser.spec )
# force a remake of depend using 'make -B depend'
.PHONY: depend
depend: $(dependd)/depend.make
# dependencies made by 'make depend'
-include $(dependd)/depend.make
ifeq ($(DESTDIR),)
# 'sudo make install' puts things in /usr/local/lib, /usr/local/include
install_prefix ?= /usr/local
else
# debuild uses DESTDIR to put things into debian/libdecnumber/usr
install_prefix = $(DESTDIR)/usr
endif
# this should be 64 for rpm based, /64 for SunOS
install_lib_suffix ?=
install: dist_bins
install -d $(install_prefix)/lib$(install_lib_suffix)
install -d $(install_prefix)/bin $(install_prefix)/include/rdbparser
for f in $(libd)/librdbparser.* ; do \
if [ -h $$f ] ; then \
cp -a $$f $(install_prefix)/lib$(install_lib_suffix) ; \
else \
install $$f $(install_prefix)/lib$(install_lib_suffix) ; \
fi ; \
done
install $(bind)/rdbp $(install_prefix)/bin
install -m 644 include/rdbparser/*.h $(install_prefix)/include/rdbparser
$(objd)/%.o: src/%.cpp
$(cpp) $(cflags) $(cppflags) $(includes) $(defines) $($(notdir $*)_includes) $($(notdir $*)_defines) -c $< -o $@
$(objd)/%.fpic.o: src/%.cpp
$(cpp) $(cflags) $(fpicflags) $(cppflags) $(includes) $(defines) $($(notdir $*)_includes) $($(notdir $*)_defines) -c $< -o $@
$(libd)/%.a:
ar rc $@ $($(*)_objs)
ifeq (Darwin,$(lsb_dist))
$(libd)/%.dylib:
$(cpplink) -dynamiclib $(cflags) -o $@.$($(*)_dylib).dylib -current_version $($(*)_dylib) -compatibility_version $($(*)_ver) $($(*)_dbjs) $($(*)_dlnk) $(sock_lib) $(math_lib) $(thread_lib) $(malloc_lib) $(dynlink_lib) && \
cd $(libd) && ln -f -s $(@F).$($(*)_dylib).dylib $(@F).$($(*)_ver).dylib && ln -f -s $(@F).$($(*)_ver).dylib $(@F)
else
$(libd)/%.$(dll):
$(cpplink) $(soflag) $(rpath) $(cflags) -o $@.$($(*)_spec) -Wl,-soname=$(@F).$($(*)_ver) $($(*)_dbjs) $($(*)_dlnk) $(sock_lib) $(math_lib) $(thread_lib) $(malloc_lib) $(dynlink_lib) && \
cd $(libd) && ln -f -s $(@F).$($(*)_spec) $(@F).$($(*)_ver) && ln -f -s $(@F).$($(*)_ver) $(@F)
endif
$(bind)/%$(exe):
$(cpplink) $(cflags) $(rpath) -o $@ $($(*)_objs) -L$(libd) $($(*)_lnk) $(cpp_lnk) $(sock_lib) $(math_lib) $(thread_lib) $(malloc_lib) $(dynlink_lib)
$(dependd)/%.d: src/%.cpp
$(cpp) $(arch_cflags) $(defines) $(includes) $($(notdir $*)_includes) $($(notdir $*)_defines) -MM $< -MT $(objd)/$(*).o -MF $@
$(dependd)/%.fpic.d: src/%.cpp
$(cpp) $(arch_cflags) $(defines) $(includes) $($(notdir $*)_includes) $($(notdir $*)_defines) -MM $< -MT $(objd)/$(*).fpic.o -MF $@