xref: /f-stack/app/redis-5.0.5/src/Makefile (revision 8d76b62e)
1# Redis Makefile
2# Copyright (C) 2009 Salvatore Sanfilippo <antirez at gmail dot com>
3# This file is released under the BSD license, see the COPYING file
4#
5# The Makefile composes the final FINAL_CFLAGS and FINAL_LDFLAGS using
6# what is needed for Redis plus the standard CFLAGS and LDFLAGS passed.
7# However when building the dependencies (Jemalloc, Lua, Hiredis, ...)
8# CFLAGS and LDFLAGS are propagated to the dependencies, so to pass
9# flags only to be used when compiling / linking Redis itself REDIS_CFLAGS
10# and REDIS_LDFLAGS are used instead (this is the case of 'make gcov').
11#
12# Dependencies are stored in the Makefile.dep file. To rebuild this file
13# Just use 'make dep', but this is only needed by developers.
14
15ifeq ($(FF_PATH),)
16    FF_PATH=/usr/local
17    $(warning FF_PATH environment variable not defined, default FF_PATH=/usr/local)
18endif
19
20ifneq ($(shell pkg-config --exists libdpdk && echo 0),0)
21$(error "no installation of DPDK found, maybe you shuld export environment variable `PKG_CONFIG_PATH`")
22endif
23
24release_hdr := $(shell sh -c './mkreleasehdr.sh')
25uname_S := $(shell sh -c 'uname -s 2>/dev/null || echo not')
26uname_M := $(shell sh -c 'uname -m 2>/dev/null || echo not')
27OPTIMIZATION?=-O2
28DEPENDENCY_TARGETS=hiredis linenoise lua
29NODEPS:=clean distclean
30
31# Default settings
32STD=-std=c99 -pedantic -DREDIS_STATIC='' -D_POSIX_C_SOURCE=199506L
33ifneq (,$(findstring clang,$(CC)))
34ifneq (,$(findstring FreeBSD,$(uname_S)))
35  STD+=-Wno-c11-extensions
36endif
37endif
38WARN=-Wall -W -Wno-missing-field-initializers
39OPT=$(OPTIMIZATION)
40
41PREFIX?=/usr/local
42INSTALL_BIN=$(PREFIX)/bin
43INSTALL=install
44
45# Default allocator defaults to Jemalloc if it's not an ARM
46MALLOC=libc
47ifneq ($(uname_M),armv6l)
48ifneq ($(uname_M),armv7l)
49ifeq ($(uname_S),Linux)
50	MALLOC=jemalloc
51endif
52endif
53endif
54
55# To get ARM stack traces if Redis crashes we need a special C flag.
56ifneq (,$(filter aarch64 armv,$(uname_M)))
57        CFLAGS+=-funwind-tables
58else
59ifneq (,$(findstring armv,$(uname_M)))
60        CFLAGS+=-funwind-tables
61endif
62endif
63
64# Backwards compatibility for selecting an allocator
65ifeq ($(USE_TCMALLOC),yes)
66	MALLOC=tcmalloc
67endif
68
69ifeq ($(USE_TCMALLOC_MINIMAL),yes)
70	MALLOC=tcmalloc_minimal
71endif
72
73ifeq ($(USE_JEMALLOC),yes)
74	MALLOC=jemalloc
75endif
76
77ifeq ($(USE_JEMALLOC),no)
78	MALLOC=libc
79endif
80
81# Override default settings if possible
82-include .make-settings
83
84FINAL_CFLAGS=$(STD) $(WARN) $(OPT) $(DEBUG) $(CFLAGS) $(REDIS_CFLAGS)
85FINAL_LDFLAGS=$(LDFLAGS) $(REDIS_LDFLAGS) $(DEBUG)
86FINAL_LIBS=-lm
87DEBUG=-g -ggdb
88
89ifeq ($(uname_S),SunOS)
90	# SunOS
91        ifneq ($(@@),32bit)
92		CFLAGS+= -m64
93		LDFLAGS+= -m64
94	endif
95	DEBUG=-g
96	DEBUG_FLAGS=-g
97	export CFLAGS LDFLAGS DEBUG DEBUG_FLAGS
98	INSTALL=cp -pf
99	FINAL_CFLAGS+= -D__EXTENSIONS__ -D_XPG6
100	FINAL_LIBS+= -ldl -lnsl -lsocket -lresolv -lpthread -lrt
101else
102ifeq ($(uname_S),Darwin)
103	# Darwin
104	FINAL_LIBS+= -ldl
105else
106ifeq ($(uname_S),AIX)
107        # AIX
108        FINAL_LDFLAGS+= -Wl,-bexpall
109        FINAL_LIBS+=-ldl -pthread -lcrypt -lbsd
110else
111ifeq ($(uname_S),OpenBSD)
112	# OpenBSD
113	FINAL_LIBS+= -lpthread
114	ifeq ($(USE_BACKTRACE),yes)
115	    FINAL_CFLAGS+= -DUSE_BACKTRACE -I/usr/local/include
116	    FINAL_LDFLAGS+= -L/usr/local/lib
117	    FINAL_LIBS+= -lexecinfo
118    	endif
119
120else
121ifeq ($(uname_S),FreeBSD)
122	# FreeBSD
123	FINAL_LIBS+= -lpthread -lexecinfo
124else
125ifeq ($(uname_S),DragonFly)
126	# FreeBSD
127	FINAL_LIBS+= -lpthread -lexecinfo
128else
129	# All the other OSes (notably Linux)
130	FINAL_LDFLAGS+= -rdynamic
131	FINAL_LIBS+=-ldl -pthread -lrt
132endif
133endif
134endif
135endif
136endif
137endif
138# Include paths to dependencies
139FINAL_CFLAGS+= -I../deps/hiredis -I../deps/linenoise -I../deps/lua/src
140
141ifeq ($(MALLOC),tcmalloc)
142	FINAL_CFLAGS+= -DUSE_TCMALLOC
143	FINAL_LIBS+= -ltcmalloc
144endif
145
146ifeq ($(MALLOC),tcmalloc_minimal)
147	FINAL_CFLAGS+= -DUSE_TCMALLOC
148	FINAL_LIBS+= -ltcmalloc_minimal
149endif
150
151ifeq ($(MALLOC),jemalloc)
152	DEPENDENCY_TARGETS+= jemalloc
153	FINAL_CFLAGS+= -DUSE_JEMALLOC -I../deps/jemalloc/include
154	FINAL_LIBS := ../deps/jemalloc/lib/libjemalloc.a $(FINAL_LIBS)
155endif
156
157FINAL_CFLAGS+= -DHAVE_FF_KQUEUE
158FINAL_CFLAGS+= -I$(FF_PATH)/lib
159
160PKGCONF ?= pkg-config
161
162FINAL_LIBS+= $(shell $(PKGCONF) --static --libs libdpdk)
163FINAL_LIBS+= -L${FF_PATH}/lib -Wl,--whole-archive,-lfstack,--no-whole-archive
164FINAL_LIBS+= -Wl,--no-whole-archive -lrt -lm -ldl -lcrypto -lpthread -lnuma
165
166REDIS_CC=$(QUIET_CC)$(CC) $(FINAL_CFLAGS)
167REDIS_LD=$(QUIET_LINK)$(CC) $(FINAL_LDFLAGS)
168REDIS_INSTALL=$(QUIET_INSTALL)$(INSTALL)
169
170CCCOLOR="\033[34m"
171LINKCOLOR="\033[34;1m"
172SRCCOLOR="\033[33m"
173BINCOLOR="\033[37;1m"
174MAKECOLOR="\033[32;1m"
175ENDCOLOR="\033[0m"
176
177ifndef V
178QUIET_CC = @printf '    %b %b\n' $(CCCOLOR)CC$(ENDCOLOR) $(SRCCOLOR)$@$(ENDCOLOR) 1>&2;
179QUIET_LINK = @printf '    %b %b\n' $(LINKCOLOR)LINK$(ENDCOLOR) $(BINCOLOR)$@$(ENDCOLOR) 1>&2;
180QUIET_INSTALL = @printf '    %b %b\n' $(LINKCOLOR)INSTALL$(ENDCOLOR) $(BINCOLOR)$@$(ENDCOLOR) 1>&2;
181endif
182
183REDIS_SERVER_NAME=redis-server
184REDIS_SENTINEL_NAME=redis-sentinel
185REDIS_SERVER_OBJ=adlist.o quicklist.o ae.o anet.o anet_ff.o dict.o server.o sds.o zmalloc.o lzf_c.o lzf_d.o pqsort.o zipmap.o sha1.o ziplist.o release.o networking.o util.o object.o db.o replication.o rdb.o t_string.o t_list.o t_set.o t_zset.o t_hash.o config.o aof.o pubsub.o multi.o debug.o sort.o intset.o syncio.o cluster.o crc16.o endianconv.o slowlog.o scripting.o bio.o rio.o rand.o memtest.o crc64.o bitops.o sentinel.o notify.o setproctitle.o blocked.o hyperloglog.o latency.o sparkline.o redis-check-rdb.o redis-check-aof.o geo.o lazyfree.o module.o evict.o expire.o geohash.o geohash_helper.o childinfo.o defrag.o siphash.o rax.o t_stream.o listpack.o localtime.o lolwut.o lolwut5.o
186REDIS_CLI_NAME=redis-cli
187REDIS_CLI_OBJ=anet.o adlist.o dict.o redis-cli.o zmalloc.o release.o anet.o anet_ff.o ae.o crc64.o siphash.o crc16.o
188REDIS_BENCHMARK_NAME=redis-benchmark
189REDIS_BENCHMARK_OBJ=ae.o anet.o anet_ff.o redis-benchmark.o adlist.o zmalloc.o redis-benchmark.o
190REDIS_CHECK_RDB_NAME=redis-check-rdb
191REDIS_CHECK_AOF_NAME=redis-check-aof
192
193all: $(REDIS_SERVER_NAME) $(REDIS_SENTINEL_NAME) $(REDIS_CLI_NAME) $(REDIS_BENCHMARK_NAME) $(REDIS_CHECK_RDB_NAME) $(REDIS_CHECK_AOF_NAME)
194	@echo ""
195	@echo "Hint: It's a good idea to run 'make test' ;)"
196	@echo ""
197
198Makefile.dep:
199	-$(REDIS_CC) -MM *.c > Makefile.dep 2> /dev/null || true
200
201ifeq (0, $(words $(findstring $(MAKECMDGOALS), $(NODEPS))))
202-include Makefile.dep
203endif
204
205.PHONY: all
206
207persist-settings: distclean
208	echo STD=$(STD) >> .make-settings
209	echo WARN=$(WARN) >> .make-settings
210	echo OPT=$(OPT) >> .make-settings
211	echo MALLOC=$(MALLOC) >> .make-settings
212	echo CFLAGS=$(CFLAGS) >> .make-settings
213	echo LDFLAGS=$(LDFLAGS) >> .make-settings
214	echo REDIS_CFLAGS=$(REDIS_CFLAGS) >> .make-settings
215	echo REDIS_LDFLAGS=$(REDIS_LDFLAGS) >> .make-settings
216	echo PREV_FINAL_CFLAGS=$(FINAL_CFLAGS) >> .make-settings
217	echo PREV_FINAL_LDFLAGS=$(FINAL_LDFLAGS) >> .make-settings
218	-(cd ../deps && $(MAKE) $(DEPENDENCY_TARGETS))
219
220.PHONY: persist-settings
221
222# Prerequisites target
223.make-prerequisites:
224	@touch $@
225
226# Clean everything, persist settings and build dependencies if anything changed
227ifneq ($(strip $(PREV_FINAL_CFLAGS)), $(strip $(FINAL_CFLAGS)))
228.make-prerequisites: persist-settings
229endif
230
231ifneq ($(strip $(PREV_FINAL_LDFLAGS)), $(strip $(FINAL_LDFLAGS)))
232.make-prerequisites: persist-settings
233endif
234
235# redis-server
236$(REDIS_SERVER_NAME): $(REDIS_SERVER_OBJ)
237	$(REDIS_LD) -o $@ $^ ../deps/hiredis/libhiredis.a ../deps/lua/src/liblua.a $(FINAL_LIBS)
238
239# redis-sentinel
240$(REDIS_SENTINEL_NAME): $(REDIS_SERVER_NAME)
241	$(REDIS_INSTALL) $(REDIS_SERVER_NAME) $(REDIS_SENTINEL_NAME)
242
243# redis-check-rdb
244$(REDIS_CHECK_RDB_NAME): $(REDIS_SERVER_NAME)
245	$(REDIS_INSTALL) $(REDIS_SERVER_NAME) $(REDIS_CHECK_RDB_NAME)
246
247# redis-check-aof
248$(REDIS_CHECK_AOF_NAME): $(REDIS_SERVER_NAME)
249	$(REDIS_INSTALL) $(REDIS_SERVER_NAME) $(REDIS_CHECK_AOF_NAME)
250
251# redis-cli
252$(REDIS_CLI_NAME): $(REDIS_CLI_OBJ)
253	$(REDIS_LD) -o $@ $^ ../deps/hiredis/libhiredis.a ../deps/linenoise/linenoise.o $(FINAL_LIBS)
254
255# redis-benchmark
256$(REDIS_BENCHMARK_NAME): $(REDIS_BENCHMARK_OBJ)
257	$(REDIS_LD) -o $@ $^ ../deps/hiredis/libhiredis.a $(FINAL_LIBS)
258
259dict-benchmark: dict.c zmalloc.c sds.c siphash.c
260	$(REDIS_CC) $(FINAL_CFLAGS) $^ -D DICT_BENCHMARK_MAIN -o $@ $(FINAL_LIBS)
261
262# Because the jemalloc.h header is generated as a part of the jemalloc build,
263# building it should complete before building any other object. Instead of
264# depending on a single artifact, build all dependencies first.
265%.o: %.c .make-prerequisites
266	$(REDIS_CC) -c $<
267
268clean:
269	rm -rf $(REDIS_SERVER_NAME) $(REDIS_SENTINEL_NAME) $(REDIS_CLI_NAME) $(REDIS_BENCHMARK_NAME) $(REDIS_CHECK_RDB_NAME) $(REDIS_CHECK_AOF_NAME) *.o *.gcda *.gcno *.gcov redis.info lcov-html Makefile.dep dict-benchmark
270
271.PHONY: clean
272
273distclean: clean
274	-(cd ../deps && $(MAKE) distclean)
275	-(rm -f .make-*)
276
277.PHONY: distclean
278
279test: $(REDIS_SERVER_NAME) $(REDIS_CHECK_AOF_NAME)
280	@(cd ..; ./runtest)
281
282test-sentinel: $(REDIS_SENTINEL_NAME)
283	@(cd ..; ./runtest-sentinel)
284
285check: test
286
287lcov:
288	$(MAKE) gcov
289	@(set -e; cd ..; ./runtest --clients 1)
290	@geninfo -o redis.info .
291	@genhtml --legend -o lcov-html redis.info
292
293test-sds: sds.c sds.h
294	$(REDIS_CC) sds.c zmalloc.c -DSDS_TEST_MAIN $(FINAL_LIBS) -o /tmp/sds_test
295	/tmp/sds_test
296
297.PHONY: lcov
298
299bench: $(REDIS_BENCHMARK_NAME)
300	./$(REDIS_BENCHMARK_NAME)
301
30232bit:
303	@echo ""
304	@echo "WARNING: if it fails under Linux you probably need to install libc6-dev-i386"
305	@echo ""
306	$(MAKE) CFLAGS="-m32" LDFLAGS="-m32"
307
308gcov:
309	$(MAKE) REDIS_CFLAGS="-fprofile-arcs -ftest-coverage -DCOVERAGE_TEST" REDIS_LDFLAGS="-fprofile-arcs -ftest-coverage"
310
311noopt:
312	$(MAKE) OPTIMIZATION="-O0"
313
314valgrind:
315	$(MAKE) OPTIMIZATION="-O0" MALLOC="libc"
316
317helgrind:
318	$(MAKE) OPTIMIZATION="-O0" MALLOC="libc" CFLAGS="-D__ATOMIC_VAR_FORCE_SYNC_MACROS"
319
320src/help.h:
321	@../utils/generate-command-help.rb > help.h
322
323install: all
324	@mkdir -p $(INSTALL_BIN)
325	$(REDIS_INSTALL) $(REDIS_SERVER_NAME) $(INSTALL_BIN)
326	$(REDIS_INSTALL) $(REDIS_BENCHMARK_NAME) $(INSTALL_BIN)
327	$(REDIS_INSTALL) $(REDIS_CLI_NAME) $(INSTALL_BIN)
328	$(REDIS_INSTALL) $(REDIS_CHECK_RDB_NAME) $(INSTALL_BIN)
329	$(REDIS_INSTALL) $(REDIS_CHECK_AOF_NAME) $(INSTALL_BIN)
330	@ln -sf $(REDIS_SERVER_NAME) $(INSTALL_BIN)/$(REDIS_SENTINEL_NAME)
331
332uninstall:
333	rm -f $(INSTALL_BIN)/{$(REDIS_SERVER_NAME),$(REDIS_BENCHMARK_NAME),$(REDIS_CLI_NAME),$(REDIS_CHECK_RDB_NAME),$(REDIS_CHECK_AOF_NAME),$(REDIS_SENTINEL_NAME)}
334