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