-
Notifications
You must be signed in to change notification settings - Fork 2
/
Makefile
252 lines (200 loc) · 6.84 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
# Copyright 2016 Ben Trask
# MIT licensed (see LICENSE for details)
USE_MDB ?= 1
USE_LEVELDB ?= 1
USE_ROCKSDB ?= 0
USE_HYPER ?= 0
USE_DEBUG ?= 1
USE_DISTRIBUTED ?= 0
USE_DUMMY ?= 1
USE_RAFT ?= 0
DESTDIR ?=
PREFIX ?= /usr/local
.SUFFIXES:
.SECONDARY:
ROOT_DIR := .
BUILD_DIR := $(ROOT_DIR)/build
SRC_DIR := $(ROOT_DIR)/src
DEPS_DIR := $(ROOT_DIR)/deps
INCLUDE_DIR := $(BUILD_DIR)/include
CFLAGS += -std=c99 -D_POSIX_C_SOURCE=200809L -D_XOPEN_SOURCE=500
CFLAGS += -g -fno-omit-frame-pointer
CFLAGS += -fstack-protector
CFLAGS += -fPIC
CFLAGS += -I$(DEPS_DIR)
CFLAGS += -I$(INCLUDE_DIR)
CFLAGS += -DKVS_DYNAMIC
WARNINGS := -Werror -Wall -Wextra -Wunused -Wuninitialized -Wvla
# TODO: Unsupported under Clang.
#WARNINGS += -Wlogical-op
# Disabled because it causes a lot of problems on Raspbian (GCC 4.6.3)
# without much perceivable benefit.
#WARNINGS += -Wshadow
# TODO: Useful with GCC but Clang doesn't like it.
#WARNINGS += -Wmaybe-uninitialized
# Causes all string literals to be marked const.
# This would be way too annoying if we don't use const everywhere already.
# The only problem is uv_buf_t, which is const sometimes and not others.
WARNINGS += -Wwrite-strings
# A function's interface is an abstraction and shouldn't strictly reflect
# its implementation. I don't believe in cluttering the code with UNUSED(X).
WARNINGS += -Wno-unused-parameter
# Seems too noisy for static functions in headers.
WARNINGS += -Wno-unused-function
# For OS X.
WARNINGS += -Wno-deprecated
# Checking that an unsigned variable is less than a constant which happens
# to be zero should be okay.
WARNINGS += -Wno-type-limits
# Usually happens for a ssize_t after already being checked for non-negative,
# or a constant that I don't want to stick a "u" on.
WARNINGS += -Wno-sign-compare
# Checks that format strings are literals amongst other things.
WARNINGS += -Wformat=2
# Used for log functions.
WARNINGS += -Wno-format-nonliteral
SHARED_LIBS :=
STATIC_LIBS :=
LIBS := -lpthread
OBJECTS := \
$(BUILD_DIR)/src/kvs_base_dynamic.o \
$(BUILD_DIR)/src/kvs_helper.o \
$(BUILD_DIR)/src/kvs_wrbuf.o \
$(BUILD_DIR)/src/kvs_base_prefix.o \
$(BUILD_DIR)/src/kvs_schema.o
SHARED_LIBS += $(DEPS_DIR)/liblmdb/liblmdb.so
STATIC_LIBS += $(DEPS_DIR)/liblmdb/liblmdb.a
ifeq ($(DB),rocksdb)
CFLAGS += -DKVS_BASE_DEFAULT=kvs_base_leveldb
else ifeq ($(DB),hyper)
CFLAGS += -DKVS_BASE_DEFAULT=kvs_base_leveldb
else ifeq ($(DB),leveldb)
CFLAGS += -DKVS_BASE_DEFAULT=kvs_base_leveldb
else ifeq ($(DB),debug)
CFLAGS += -DKVS_BASE_DEFAULT=kvs_base_debug
else ifeq ($(DB),distributed)
CFLAGS += -DKVS_BASE_DEFAULT=kvs_base_distributed
else ifeq ($(DB),mdb)
CFLAGS += -DKVS_BASE_DEFAULT=kvs_base_mdb
else ifndef DB
CFLAGS += -DKVS_BASE_DEFAULT=kvs_base_mdb
else
$(error Unknown back-end $(DB))
endif
ifeq ($(USE_MDB),1)
CFLAGS += -DKVS_BASE_MDB
OBJECTS += $(BUILD_DIR)/src/kvs_base_mdb.o
endif
ifeq ($(USE_LEVELDB),1)
CFLAGS += -DKVS_BASE_LEVELDB
CFLAGS += -I$(DEPS_DIR)/leveldb/include -I$(DEPS_DIR)/snappy/include
SHARED_LIBS += $(DEPS_DIR)/leveldb/out-shared/libleveldb.so $(DEPS_DIR)/snappy/.libs/libsnappy.so
STATIC_LIBS += $(DEPS_DIR)/leveldb/out-static/libleveldb.a $(DEPS_DIR)/snappy/.libs/libsnappy.a
LIBS += -lstdc++
OBJECTS += $(BUILD_DIR)/src/kvs_base_leveldb.o
endif
ifeq ($(USE_ROCKSDB),1)
CFLAGS += -DKVS_BASE_ROCKSDB
SHARED_LIBS += $(DEPS_DIR)/snappy/.libs/libsnappy.so
STATIC_LIBS += $(DEPS_DIR)/snappy/.libs/libsnappy.a
LIBS += -lrocksdb
LIBS += -lz
LIBS += -lstdc++
OBJECTS += $(BUILD_DIR)/src/kvs_base_leveldb.o
endif
ifeq ($(USE_HYPER),1)
CFLAGS += -DKVS_BASE_HYPER
SHARED_LIBS += $(DEPS_DIR)/snappy/.libs/libsnappy.so
STATIC_LIBS += $(DEPS_DIR)/snappy/.libs/libsnappy.a
LIBS += -lhyperleveldb
LIBS += -lstdc++
OBJECTS += $(BUILD_DIR)/src/kvs_base_leveldb.o
endif
ifeq ($(USE_DEBUG),1)
CFLAGS += -DKVS_BASE_DEBUG
OBJECTS += $(BUILD_DIR)/src/kvs_base_debug.o
endif
ifeq ($(USE_DISTRIBUTED),1)
CFLAGS += -DKVS_BASE_DISTRIBUTED
OBJECTS += $(BUILD_DIR)/src/kvs_base_distributed.o
endif
ifeq ($(USE_DUMMY),1)
CFLAGS += -DKVS_BASE_DUMMY
OBJECTS += $(BUILD_DIR)/src/kvs_base_dummy.o
endif
ifeq ($(USE_RAFT),1)
CFLAGS += -DKVS_BASE_RAFT
LIBS += -lcraft -Wl,-rpath -Wl,/usr/local/lib
OBJECTS += $(BUILD_DIR)/src/kvs_base_raft.o
endif
HEADERS := \
$(INCLUDE_DIR)/kvstore/kvs_base.h \
$(INCLUDE_DIR)/kvstore/kvs_base_custom.h \
$(INCLUDE_DIR)/kvstore/kvs_helper.h \
$(INCLUDE_DIR)/kvstore/kvs_schema.h
.PHONY: all
all: $(BUILD_DIR)/libkvstore.so $(BUILD_DIR)/libkvstore.a $(HEADERS)
$(BUILD_DIR)/libkvstore.so: $(OBJECTS) $(STATIC_LIBS)
@- mkdir -p $(dir $@)
$(CC) $(CFLAGS) -shared $^ -o $@
$(BUILD_DIR)/libkvstore.a: $(OBJECTS) $(STATIC_LIBS)
@- mkdir -p $(dir $@)
$(AR) rs $@ $(OBJECTS)
$(BUILD_DIR)/%.o: $(ROOT_DIR)/%.c | $(HEADERS)
@- mkdir -p $(dir $@)
@- mkdir -p $(dir $(BUILD_DIR)/h/$*.d)
$(CC) -c $(CFLAGS) $(WARNINGS) -MMD -MP -MF $(BUILD_DIR)/h/$*.d -o $@ $<
# TODO: Find files in subdirectories without using shell?
-include $(shell find $(BUILD_DIR)/h -name "*.d")
$(INCLUDE_DIR)/kvstore/%.h: $(SRC_DIR)/%.h
@- mkdir -p $(dir $@)
cp $^ $@
.PHONY: clean
clean:
rm -rf $(BUILD_DIR)
rm -rf ./testdb ./testdb-lock ./testdb-log
.PHONY: distclean
distclean: clean
- $(MAKE) clean -C $(DEPS_DIR)/leveldb
- $(MAKE) clean -C $(DEPS_DIR)/liblmdb
- $(MAKE) distclean -C $(DEPS_DIR)/snappy
.PHONY: test
test: mtest.run general.run
.PHONY: %.run
%.run: $(BUILD_DIR)/test/%
rm -rf ./testdb ./testdb-lock
$^ mdb ./testdb > $^.mdb.log
rm -rf ./testdb ./testdb-lock
$^ debug ./testdb > $^.debug.log 2>&1
rm -rf ./testdb ./testdb-lock
$^ leveldb ./testdb > $^.leveldb.log
$(BUILD_DIR)/test/%: $(BUILD_DIR)/test/%.o $(BUILD_DIR)/libkvstore.a $(STATIC_LIBS)
@- mkdir -p $(dir $@)
$(CC) $(CFLAGS) $^ $(LIBS) -o $@
$(DESTDIR)$(PREFIX)/include/%: $(INCLUDE_DIR)/%
@- mkdir -p $(dir $@)
cp $^ $@
$(DESTDIR)$(PREFIX)/lib/%: $(BUILD_DIR)/%
@- mkdir -p $(dir $@)
cp $^ $@
.PHONY: install
install: $(DESTDIR)$(PREFIX)/include/kvstore/kvs_base.h $(DESTDIR)$(PREFIX)/include/kvstore/kvs_base_custom.h $(DESTDIR)$(PREFIX)/include/kvstore/kvs_schema.h $(DESTDIR)$(PREFIX)/lib/libkvstore.so $(DESTDIR)$(PREFIX)/lib/libkvstore.a
.PHONY: uninstall
uninstall:
rm -rf $(DESTDIR)$(PREFIX)/include/kvstore
rm $(DESTDIR)$(PREFIX)/lib/libkvstore.so
rm $(DESTDIR)$(PREFIX)/lib/libkvstore.a
$(DEPS_DIR)/liblmdb/liblmdb.a: | mdb
.PHONY: mdb
mdb:
XCFLAGS="-fPIC" $(MAKE) -C $(DEPS_DIR)/liblmdb --no-print-directory
$(DEPS_DIR)/leveldb/out-shared/libleveldb.so: | leveldb
$(DEPS_DIR)/leveldb/out-static/libleveldb.a: | leveldb
.PHONY: leveldb
leveldb: snappy
CFLAGS="-fPIC -DSNAPPY -I../snappy/" CXXFLAGS="-fPIC -DSNAPPY -I../snappy/" LIBS="-L../snappy/.libs/ -lsnappy" $(MAKE) -C $(DEPS_DIR)/leveldb --no-print-directory
$(DEPS_DIR)/snappy/.libs/libsnappy.so: | snappy
$(DEPS_DIR)/snappy/.libs/libsnappy.a: | snappy
.PHONY: snappy
snappy:
$(MAKE) -C $(DEPS_DIR)/snappy --no-print-directory