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
15release_hdr := $(shell sh -c './mkreleasehdr.sh')
16uname_S := $(shell sh -c 'uname -s 2>/dev/null || echo not')
17uname_M := $(shell sh -c 'uname -m 2>/dev/null || echo not')
18OPTIMIZATION?=-O2
19DEPENDENCY_TARGETS=hiredis linenoise lua
20NODEPS:=clean distclean
21
22# Default settings
23STD=-std=c11 -pedantic -DREDIS_STATIC=''
24ifneq (,$(findstring clang,$(CC)))
25ifneq (,$(findstring FreeBSD,$(uname_S)))
26  STD+=-Wno-c11-extensions
27endif
28endif
29WARN=-Wall -W -Wno-missing-field-initializers
30OPT=$(OPTIMIZATION)
31
32PREFIX?=/usr/local
33INSTALL_BIN=$(PREFIX)/bin
34INSTALL=install
35PKG_CONFIG?=pkg-config
36
37# Default allocator defaults to Jemalloc if it's not an ARM
38MALLOC=libc
39ifneq ($(uname_M),armv6l)
40ifneq ($(uname_M),armv7l)
41ifeq ($(uname_S),Linux)
42	MALLOC=jemalloc
43endif
44endif
45endif
46
47# To get ARM stack traces if Redis crashes we need a special C flag.
48ifneq (,$(filter aarch64 armv,$(uname_M)))
49        CFLAGS+=-funwind-tables
50else
51ifneq (,$(findstring armv,$(uname_M)))
52        CFLAGS+=-funwind-tables
53endif
54endif
55
56# Backwards compatibility for selecting an allocator
57ifeq ($(USE_TCMALLOC),yes)
58	MALLOC=tcmalloc
59endif
60
61ifeq ($(USE_TCMALLOC_MINIMAL),yes)
62	MALLOC=tcmalloc_minimal
63endif
64
65ifeq ($(USE_JEMALLOC),yes)
66	MALLOC=jemalloc
67endif
68
69ifeq ($(USE_JEMALLOC),no)
70	MALLOC=libc
71endif
72
73# Override default settings if possible
74-include .make-settings
75
76FINAL_CFLAGS=$(STD) $(WARN) $(OPT) $(DEBUG) $(CFLAGS) $(REDIS_CFLAGS)
77FINAL_LDFLAGS=$(LDFLAGS) $(REDIS_LDFLAGS) $(DEBUG)
78FINAL_LIBS=-lm
79DEBUG=-g -ggdb
80
81# Linux ARM needs -latomic at linking time
82ifneq (,$(filter aarch64 armv,$(uname_M)))
83        FINAL_LIBS+=-latomic
84else
85ifneq (,$(findstring armv,$(uname_M)))
86        FINAL_LIBS+=-latomic
87endif
88endif
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
106	OPENSSL_CFLAGS=-I/usr/local/opt/openssl/include
107	OPENSSL_LDFLAGS=-L/usr/local/opt/openssl/lib
108else
109ifeq ($(uname_S),AIX)
110        # AIX
111        FINAL_LDFLAGS+= -Wl,-bexpall
112        FINAL_LIBS+=-ldl -pthread -lcrypt -lbsd
113else
114ifeq ($(uname_S),OpenBSD)
115	# OpenBSD
116	FINAL_LIBS+= -lpthread
117	ifeq ($(USE_BACKTRACE),yes)
118	    FINAL_CFLAGS+= -DUSE_BACKTRACE -I/usr/local/include
119	    FINAL_LDFLAGS+= -L/usr/local/lib
120	    FINAL_LIBS+= -lexecinfo
121    	endif
122
123else
124ifeq ($(uname_S),FreeBSD)
125	# FreeBSD
126	FINAL_LIBS+= -lpthread -lexecinfo
127else
128ifeq ($(uname_S),DragonFly)
129	# FreeBSD
130	FINAL_LIBS+= -lpthread -lexecinfo
131else
132ifeq ($(uname_S),OpenBSD)
133	# OpenBSD
134	FINAL_LIBS+= -lpthread -lexecinfo
135else
136ifeq ($(uname_S),NetBSD)
137	# NetBSD
138	FINAL_LIBS+= -lpthread -lexecinfo
139else
140	# All the other OSes (notably Linux)
141	FINAL_LDFLAGS+= -rdynamic
142	FINAL_LIBS+=-ldl -pthread -lrt
143endif
144endif
145endif
146endif
147endif
148endif
149endif
150endif
151# Include paths to dependencies
152FINAL_CFLAGS+= -I../deps/hiredis -I../deps/linenoise -I../deps/lua/src
153
154# Determine systemd support and/or build preference (defaulting to auto-detection)
155BUILD_WITH_SYSTEMD=no
156# If 'USE_SYSTEMD' in the environment is neither "no" nor "yes", try to
157# auto-detect libsystemd's presence and link accordingly.
158ifneq ($(USE_SYSTEMD),no)
159	LIBSYSTEMD_PKGCONFIG := $(shell $(PKG_CONFIG) --exists libsystemd && echo $$?)
160# If libsystemd cannot be detected, continue building without support for it
161# (unless a later check tells us otherwise)
162ifeq ($(LIBSYSTEMD_PKGCONFIG),0)
163	BUILD_WITH_SYSTEMD=yes
164endif
165endif
166ifeq ($(USE_SYSTEMD),yes)
167ifneq ($(LIBSYSTEMD_PKGCONFIG),0)
168$(error USE_SYSTEMD is set to "$(USE_SYSTEMD)", but $(PKG_CONFIG) cannot find libsystemd)
169endif
170# Force building with libsystemd
171	BUILD_WITH_SYSTEMD=yes
172endif
173ifeq ($(BUILD_WITH_SYSTEMD),yes)
174	FINAL_LIBS+=$(shell $(PKG_CONFIG) --libs libsystemd)
175	FINAL_CFLAGS+= -DHAVE_LIBSYSTEMD
176endif
177
178ifeq ($(MALLOC),tcmalloc)
179	FINAL_CFLAGS+= -DUSE_TCMALLOC
180	FINAL_LIBS+= -ltcmalloc
181endif
182
183ifeq ($(MALLOC),tcmalloc_minimal)
184	FINAL_CFLAGS+= -DUSE_TCMALLOC
185	FINAL_LIBS+= -ltcmalloc_minimal
186endif
187
188ifeq ($(MALLOC),jemalloc)
189	DEPENDENCY_TARGETS+= jemalloc
190	FINAL_CFLAGS+= -DUSE_JEMALLOC -I../deps/jemalloc/include
191	FINAL_LIBS := ../deps/jemalloc/lib/libjemalloc.a $(FINAL_LIBS)
192endif
193
194ifeq ($(BUILD_TLS),yes)
195	FINAL_CFLAGS+=-DUSE_OPENSSL $(OPENSSL_CFLAGS)
196	FINAL_LDFLAGS+=$(OPENSSL_LDFLAGS)
197	LIBSSL_PKGCONFIG := $(shell $(PKG_CONFIG) --exists libssl && echo $$?)
198ifeq ($(LIBSSL_PKGCONFIG),0)
199	LIBSSL_LIBS=$(shell $(PKG_CONFIG) --libs libssl)
200else
201	LIBSSL_LIBS=-lssl
202endif
203	LIBCRYPTO_PKGCONFIG := $(shell $(PKG_CONFIG) --exists libcrypto && echo $$?)
204ifeq ($(LIBCRYPTO_PKGCONFIG),0)
205	LIBCRYPTO_LIBS=$(shell $(PKG_CONFIG) --libs libcrypto)
206else
207	LIBCRYPTO_LIBS=-lcrypto
208endif
209	FINAL_LIBS += ../deps/hiredis/libhiredis_ssl.a $(LIBSSL_LIBS) $(LIBCRYPTO_LIBS)
210endif
211
212REDIS_CC=$(QUIET_CC)$(CC) $(FINAL_CFLAGS)
213REDIS_LD=$(QUIET_LINK)$(CC) $(FINAL_LDFLAGS)
214REDIS_INSTALL=$(QUIET_INSTALL)$(INSTALL)
215
216CCCOLOR="\033[34m"
217LINKCOLOR="\033[34;1m"
218SRCCOLOR="\033[33m"
219BINCOLOR="\033[37;1m"
220MAKECOLOR="\033[32;1m"
221ENDCOLOR="\033[0m"
222
223ifndef V
224QUIET_CC = @printf '    %b %b\n' $(CCCOLOR)CC$(ENDCOLOR) $(SRCCOLOR)$@$(ENDCOLOR) 1>&2;
225QUIET_LINK = @printf '    %b %b\n' $(LINKCOLOR)LINK$(ENDCOLOR) $(BINCOLOR)$@$(ENDCOLOR) 1>&2;
226QUIET_INSTALL = @printf '    %b %b\n' $(LINKCOLOR)INSTALL$(ENDCOLOR) $(BINCOLOR)$@$(ENDCOLOR) 1>&2;
227endif
228
229REDIS_SERVER_NAME=redis-server
230REDIS_SENTINEL_NAME=redis-sentinel
231REDIS_SERVER_OBJ=adlist.o quicklist.o ae.o anet.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 crcspeed.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 lolwut6.o acl.o gopher.o tracking.o connection.o tls.o sha256.o timeout.o setcpuaffinity.o
232REDIS_CLI_NAME=redis-cli
233REDIS_CLI_OBJ=anet.o adlist.o dict.o redis-cli.o zmalloc.o release.o ae.o crcspeed.o crc64.o siphash.o crc16.o
234REDIS_BENCHMARK_NAME=redis-benchmark
235REDIS_BENCHMARK_OBJ=ae.o anet.o redis-benchmark.o adlist.o dict.o zmalloc.o siphash.o
236REDIS_CHECK_RDB_NAME=redis-check-rdb
237REDIS_CHECK_AOF_NAME=redis-check-aof
238
239all: $(REDIS_SERVER_NAME) $(REDIS_SENTINEL_NAME) $(REDIS_CLI_NAME) $(REDIS_BENCHMARK_NAME) $(REDIS_CHECK_RDB_NAME) $(REDIS_CHECK_AOF_NAME)
240	@echo ""
241	@echo "Hint: It's a good idea to run 'make test' ;)"
242	@echo ""
243
244Makefile.dep:
245	-$(REDIS_CC) -MM *.c > Makefile.dep 2> /dev/null || true
246
247ifeq (0, $(words $(findstring $(MAKECMDGOALS), $(NODEPS))))
248-include Makefile.dep
249endif
250
251.PHONY: all
252
253persist-settings: distclean
254	echo STD=$(STD) >> .make-settings
255	echo WARN=$(WARN) >> .make-settings
256	echo OPT=$(OPT) >> .make-settings
257	echo MALLOC=$(MALLOC) >> .make-settings
258	echo BUILD_TLS=$(BUILD_TLS) >> .make-settings
259	echo USE_SYSTEMD=$(USE_SYSTEMD) >> .make-settings
260	echo CFLAGS=$(CFLAGS) >> .make-settings
261	echo LDFLAGS=$(LDFLAGS) >> .make-settings
262	echo REDIS_CFLAGS=$(REDIS_CFLAGS) >> .make-settings
263	echo REDIS_LDFLAGS=$(REDIS_LDFLAGS) >> .make-settings
264	echo PREV_FINAL_CFLAGS=$(FINAL_CFLAGS) >> .make-settings
265	echo PREV_FINAL_LDFLAGS=$(FINAL_LDFLAGS) >> .make-settings
266	-(cd ../deps && $(MAKE) $(DEPENDENCY_TARGETS))
267
268.PHONY: persist-settings
269
270# Prerequisites target
271.make-prerequisites:
272	@touch $@
273
274# Clean everything, persist settings and build dependencies if anything changed
275ifneq ($(strip $(PREV_FINAL_CFLAGS)), $(strip $(FINAL_CFLAGS)))
276.make-prerequisites: persist-settings
277endif
278
279ifneq ($(strip $(PREV_FINAL_LDFLAGS)), $(strip $(FINAL_LDFLAGS)))
280.make-prerequisites: persist-settings
281endif
282
283# redis-server
284$(REDIS_SERVER_NAME): $(REDIS_SERVER_OBJ)
285	$(REDIS_LD) -o $@ $^ ../deps/hiredis/libhiredis.a ../deps/lua/src/liblua.a $(FINAL_LIBS)
286
287# redis-sentinel
288$(REDIS_SENTINEL_NAME): $(REDIS_SERVER_NAME)
289	$(REDIS_INSTALL) $(REDIS_SERVER_NAME) $(REDIS_SENTINEL_NAME)
290
291# redis-check-rdb
292$(REDIS_CHECK_RDB_NAME): $(REDIS_SERVER_NAME)
293	$(REDIS_INSTALL) $(REDIS_SERVER_NAME) $(REDIS_CHECK_RDB_NAME)
294
295# redis-check-aof
296$(REDIS_CHECK_AOF_NAME): $(REDIS_SERVER_NAME)
297	$(REDIS_INSTALL) $(REDIS_SERVER_NAME) $(REDIS_CHECK_AOF_NAME)
298
299# redis-cli
300$(REDIS_CLI_NAME): $(REDIS_CLI_OBJ)
301	$(REDIS_LD) -o $@ $^ ../deps/hiredis/libhiredis.a ../deps/linenoise/linenoise.o $(FINAL_LIBS)
302
303# redis-benchmark
304$(REDIS_BENCHMARK_NAME): $(REDIS_BENCHMARK_OBJ)
305	$(REDIS_LD) -o $@ $^ ../deps/hiredis/libhiredis.a $(FINAL_LIBS)
306
307dict-benchmark: dict.c zmalloc.c sds.c siphash.c
308	$(REDIS_CC) $(FINAL_CFLAGS) $^ -D DICT_BENCHMARK_MAIN -o $@ $(FINAL_LIBS)
309
310DEP = $(REDIS_SERVER_OBJ:%.o=%.d) $(REDIS_CLI_OBJ:%.o=%.d) $(REDIS_BENCHMARK_OBJ:%.o=%.d)
311-include $(DEP)
312
313# Because the jemalloc.h header is generated as a part of the jemalloc build,
314# building it should complete before building any other object. Instead of
315# depending on a single artifact, build all dependencies first.
316%.o: %.c .make-prerequisites
317	$(REDIS_CC) -MMD -o $@ -c $<
318
319clean:
320	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
321	rm -f $(DEP)
322
323.PHONY: clean
324
325distclean: clean
326	-(cd ../deps && $(MAKE) distclean)
327	-(rm -f .make-*)
328
329.PHONY: distclean
330
331test: $(REDIS_SERVER_NAME) $(REDIS_CHECK_AOF_NAME)
332	@(cd ..; ./runtest)
333
334test-sentinel: $(REDIS_SENTINEL_NAME)
335	@(cd ..; ./runtest-sentinel)
336
337check: test
338
339lcov:
340	$(MAKE) gcov
341	@(set -e; cd ..; ./runtest --clients 1)
342	@geninfo -o redis.info .
343	@genhtml --legend -o lcov-html redis.info
344
345test-sds: sds.c sds.h
346	$(REDIS_CC) sds.c zmalloc.c -DSDS_TEST_MAIN $(FINAL_LIBS) -o /tmp/sds_test
347	/tmp/sds_test
348
349.PHONY: lcov
350
351bench: $(REDIS_BENCHMARK_NAME)
352	./$(REDIS_BENCHMARK_NAME)
353
35432bit:
355	@echo ""
356	@echo "WARNING: if it fails under Linux you probably need to install libc6-dev-i386"
357	@echo ""
358	$(MAKE) CFLAGS="-m32" LDFLAGS="-m32"
359
360gcov:
361	$(MAKE) REDIS_CFLAGS="-fprofile-arcs -ftest-coverage -DCOVERAGE_TEST" REDIS_LDFLAGS="-fprofile-arcs -ftest-coverage"
362
363noopt:
364	$(MAKE) OPTIMIZATION="-O0"
365
366valgrind:
367	$(MAKE) OPTIMIZATION="-O0" MALLOC="libc"
368
369helgrind:
370	$(MAKE) OPTIMIZATION="-O0" MALLOC="libc" CFLAGS="-D__ATOMIC_VAR_FORCE_SYNC_MACROS"
371
372src/help.h:
373	@../utils/generate-command-help.rb > help.h
374
375install: all
376	@mkdir -p $(INSTALL_BIN)
377	$(REDIS_INSTALL) $(REDIS_SERVER_NAME) $(INSTALL_BIN)
378	$(REDIS_INSTALL) $(REDIS_BENCHMARK_NAME) $(INSTALL_BIN)
379	$(REDIS_INSTALL) $(REDIS_CLI_NAME) $(INSTALL_BIN)
380	$(REDIS_INSTALL) $(REDIS_CHECK_RDB_NAME) $(INSTALL_BIN)
381	$(REDIS_INSTALL) $(REDIS_CHECK_AOF_NAME) $(INSTALL_BIN)
382	@ln -sf $(REDIS_SERVER_NAME) $(INSTALL_BIN)/$(REDIS_SENTINEL_NAME)
383
384uninstall:
385	rm -f $(INSTALL_BIN)/{$(REDIS_SERVER_NAME),$(REDIS_BENCHMARK_NAME),$(REDIS_CLI_NAME),$(REDIS_CHECK_RDB_NAME),$(REDIS_CHECK_AOF_NAME),$(REDIS_SENTINEL_NAME)}
386