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:=$(PREFIX)
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
79
80# Linux ARM needs -latomic at linking time
81ifneq (,$(filter aarch64 armv,$(uname_M)))
82        FINAL_LIBS+=-latomic
83else
84ifneq (,$(findstring armv,$(uname_M)))
85        FINAL_LIBS+=-latomic
86endif
87endif
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
105	OPENSSL_CFLAGS=-I/usr/local/opt/openssl/include
106	OPENSSL_LDFLAGS=-L/usr/local/opt/openssl/lib
107else
108ifeq ($(uname_S),FreeBSD)
109        # FreeBSD
110        FINAL_CFLAGS?= $(CFLAGS) $(REDIS_CFLAGS) -I${PREFIX}/include
111        FINAL_LDFLAGS= $(LDFLAGS) -pthread
112else
113ifeq ($(uname_S),AIX)
114        # AIX
115        FINAL_LDFLAGS+= -Wl,-bexpall
116        FINAL_LIBS+=-ldl -pthread -lcrypt -lbsd
117else
118ifeq ($(uname_S),OpenBSD)
119	# OpenBSD
120	FINAL_LIBS+= -lpthread
121	ifeq ($(USE_BACKTRACE),yes)
122	    FINAL_CFLAGS+= -DUSE_BACKTRACE -I/usr/local/include
123	    FINAL_LDFLAGS+= -L/usr/local/lib
124	    FINAL_LIBS+= -lexecinfo
125    	endif
126
127else
128ifeq ($(uname_S),NetBSD)
129	# NetBSD
130	FINAL_LIBS+= -lpthread
131	ifeq ($(USE_BACKTRACE),yes)
132	    FINAL_CFLAGS+= -DUSE_BACKTRACE -I/usr/pkg/include
133	    FINAL_LDFLAGS+= -L/usr/pkg/lib
134	    FINAL_LIBS+= -lexecinfo
135    	endif
136else
137ifeq ($(uname_S),FreeBSD)
138	# FreeBSD
139	FINAL_LIBS+= -lpthread -lexecinfo
140else
141ifeq ($(uname_S),DragonFly)
142	# DragonFly
143	FINAL_LIBS+= -lpthread -lexecinfo
144else
145ifeq ($(uname_S),OpenBSD)
146	# OpenBSD
147	FINAL_LIBS+= -lpthread -lexecinfo
148else
149ifeq ($(uname_S),NetBSD)
150	# NetBSD
151	FINAL_LIBS+= -lpthread -lexecinfo
152else
153ifeq ($(uname_S),Haiku)
154	# Haiku
155	FINAL_CFLAGS+= -DBSD_SOURCE
156	FINAL_LDFLAGS+= -lbsd -lnetwork
157	FINAL_LIBS+= -lpthread
158else
159	# All the other OSes (notably Linux)
160	FINAL_LDFLAGS+= -rdynamic
161	FINAL_LIBS+=-ldl -pthread -lrt
162endif
163endif
164endif
165endif
166endif
167endif
168endif
169endif
170endif
171endif
172endif
173# Include paths to dependencies
174FINAL_CFLAGS+= -I../deps/hiredis -I../deps/linenoise -I../deps/lua/src
175
176# Determine systemd support and/or build preference (defaulting to auto-detection)
177BUILD_WITH_SYSTEMD=no
178# If 'USE_SYSTEMD' in the environment is neither "no" nor "yes", try to
179# auto-detect libsystemd's presence and link accordingly.
180ifneq ($(USE_SYSTEMD),no)
181	LIBSYSTEMD_PKGCONFIG := $(shell $(PKG_CONFIG) --exists libsystemd && echo $$?)
182# If libsystemd cannot be detected, continue building without support for it
183# (unless a later check tells us otherwise)
184ifeq ($(LIBSYSTEMD_PKGCONFIG),0)
185	BUILD_WITH_SYSTEMD=yes
186endif
187endif
188ifeq ($(USE_SYSTEMD),yes)
189ifneq ($(LIBSYSTEMD_PKGCONFIG),0)
190$(error USE_SYSTEMD is set to "$(USE_SYSTEMD)", but $(PKG_CONFIG) cannot find libsystemd)
191endif
192# Force building with libsystemd
193	BUILD_WITH_SYSTEMD=yes
194endif
195ifeq ($(BUILD_WITH_SYSTEMD),yes)
196	FINAL_LIBS+=$(shell $(PKG_CONFIG) --libs libsystemd)
197	FINAL_CFLAGS+= -DHAVE_LIBSYSTEMD
198endif
199
200ifeq ($(MALLOC),tcmalloc)
201	FINAL_CFLAGS+= -DUSE_TCMALLOC
202	FINAL_LIBS+= -ltcmalloc
203endif
204
205ifeq ($(MALLOC),tcmalloc_minimal)
206	FINAL_CFLAGS+= -DUSE_TCMALLOC
207	FINAL_LIBS+= -ltcmalloc_minimal
208endif
209
210ifeq ($(MALLOC),jemalloc)
211	DEPENDENCY_TARGETS+= jemalloc
212	FINAL_CFLAGS+= -DUSE_JEMALLOC -I../deps/jemalloc/include
213	FINAL_LIBS := ../deps/jemalloc/lib/libjemalloc.a $(FINAL_LIBS)
214endif
215
216ifeq ($(BUILD_TLS),yes)
217	FINAL_CFLAGS+=-DUSE_OPENSSL $(OPENSSL_CFLAGS)
218	FINAL_LDFLAGS+=$(OPENSSL_LDFLAGS)
219	LIBSSL_PKGCONFIG := $(shell $(PKG_CONFIG) --exists libssl && echo $$?)
220ifeq ($(LIBSSL_PKGCONFIG),0)
221	LIBSSL_LIBS=$(shell $(PKG_CONFIG) --libs libssl)
222else
223	LIBSSL_LIBS=-lssl
224endif
225	LIBCRYPTO_PKGCONFIG := $(shell $(PKG_CONFIG) --exists libcrypto && echo $$?)
226ifeq ($(LIBCRYPTO_PKGCONFIG),0)
227	LIBCRYPTO_LIBS=$(shell $(PKG_CONFIG) --libs libcrypto)
228else
229	LIBCRYPTO_LIBS=-lcrypto
230endif
231	FINAL_LIBS += ../deps/hiredis/libhiredis_ssl.a $(LIBSSL_LIBS) $(LIBCRYPTO_LIBS)
232endif
233
234REDIS_CC=$(QUIET_CC)$(CC) $(FINAL_CFLAGS)
235REDIS_LD=$(QUIET_LINK)$(CC) $(FINAL_LDFLAGS)
236REDIS_INSTALL=$(QUIET_INSTALL)$(INSTALL)
237
238CCCOLOR="\033[34m"
239LINKCOLOR="\033[34;1m"
240SRCCOLOR="\033[33m"
241BINCOLOR="\033[37;1m"
242MAKECOLOR="\033[32;1m"
243ENDCOLOR="\033[0m"
244
245ifndef V
246QUIET_CC = @printf '    %b %b\n' $(CCCOLOR)CC$(ENDCOLOR) $(SRCCOLOR)$@$(ENDCOLOR) 1>&2;
247QUIET_LINK = @printf '    %b %b\n' $(LINKCOLOR)LINK$(ENDCOLOR) $(BINCOLOR)$@$(ENDCOLOR) 1>&2;
248QUIET_INSTALL = @printf '    %b %b\n' $(LINKCOLOR)INSTALL$(ENDCOLOR) $(BINCOLOR)$@$(ENDCOLOR) 1>&2;
249endif
250
251REDIS_SERVER_NAME=redis-server$(PROG_SUFFIX)
252REDIS_SENTINEL_NAME=redis-sentinel$(PROG_SUFFIX)
253REDIS_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 mt19937-64.o
254REDIS_CLI_NAME=redis-cli$(PROG_SUFFIX)
255REDIS_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 mt19937-64.o
256REDIS_BENCHMARK_NAME=redis-benchmark$(PROG_SUFFIX)
257REDIS_BENCHMARK_OBJ=ae.o anet.o redis-benchmark.o adlist.o dict.o zmalloc.o siphash.o mt19937-64.o
258REDIS_CHECK_RDB_NAME=redis-check-rdb$(PROG_SUFFIX)
259REDIS_CHECK_AOF_NAME=redis-check-aof$(PROG_SUFFIX)
260
261all: $(REDIS_SERVER_NAME) $(REDIS_SENTINEL_NAME) $(REDIS_CLI_NAME) $(REDIS_BENCHMARK_NAME) $(REDIS_CHECK_RDB_NAME) $(REDIS_CHECK_AOF_NAME)
262	@echo ""
263	@echo "Hint: It's a good idea to run 'make test' ;)"
264	@echo ""
265
266Makefile.dep:
267	-$(REDIS_CC) -MM *.c > Makefile.dep 2> /dev/null || true
268
269ifeq (0, $(words $(findstring $(MAKECMDGOALS), $(NODEPS))))
270-include Makefile.dep
271endif
272
273.PHONY: all
274
275persist-settings: distclean
276	echo STD=$(STD) >> .make-settings
277	echo WARN=$(WARN) >> .make-settings
278	echo OPT=$(OPT) >> .make-settings
279	echo MALLOC=$(MALLOC) >> .make-settings
280	echo BUILD_TLS=$(BUILD_TLS) >> .make-settings
281	echo USE_SYSTEMD=$(USE_SYSTEMD) >> .make-settings
282	echo CFLAGS=$(CFLAGS) >> .make-settings
283	echo LDFLAGS=$(LDFLAGS) >> .make-settings
284	echo REDIS_CFLAGS=$(REDIS_CFLAGS) >> .make-settings
285	echo REDIS_LDFLAGS=$(REDIS_LDFLAGS) >> .make-settings
286	echo PREV_FINAL_CFLAGS=$(FINAL_CFLAGS) >> .make-settings
287	echo PREV_FINAL_LDFLAGS=$(FINAL_LDFLAGS) >> .make-settings
288	-(cd ../deps && $(MAKE) $(DEPENDENCY_TARGETS))
289
290.PHONY: persist-settings
291
292# Prerequisites target
293.make-prerequisites:
294	@touch $@
295
296# Clean everything, persist settings and build dependencies if anything changed
297ifneq ($(strip $(PREV_FINAL_CFLAGS)), $(strip $(FINAL_CFLAGS)))
298.make-prerequisites: persist-settings
299endif
300
301ifneq ($(strip $(PREV_FINAL_LDFLAGS)), $(strip $(FINAL_LDFLAGS)))
302.make-prerequisites: persist-settings
303endif
304
305# redis-server
306$(REDIS_SERVER_NAME): $(REDIS_SERVER_OBJ)
307	$(REDIS_LD) -o $@ $^ ../deps/hiredis/libhiredis.a ../deps/lua/src/liblua.a $(FINAL_LIBS)
308
309# redis-sentinel
310$(REDIS_SENTINEL_NAME): $(REDIS_SERVER_NAME)
311	$(REDIS_INSTALL) $(REDIS_SERVER_NAME) $(REDIS_SENTINEL_NAME)
312
313# redis-check-rdb
314$(REDIS_CHECK_RDB_NAME): $(REDIS_SERVER_NAME)
315	$(REDIS_INSTALL) $(REDIS_SERVER_NAME) $(REDIS_CHECK_RDB_NAME)
316
317# redis-check-aof
318$(REDIS_CHECK_AOF_NAME): $(REDIS_SERVER_NAME)
319	$(REDIS_INSTALL) $(REDIS_SERVER_NAME) $(REDIS_CHECK_AOF_NAME)
320
321# redis-cli
322$(REDIS_CLI_NAME): $(REDIS_CLI_OBJ)
323	$(REDIS_LD) -o $@ $^ ../deps/hiredis/libhiredis.a ../deps/linenoise/linenoise.o $(FINAL_LIBS)
324
325# redis-benchmark
326$(REDIS_BENCHMARK_NAME): $(REDIS_BENCHMARK_OBJ)
327	$(REDIS_LD) -o $@ $^ ../deps/hiredis/libhiredis.a $(FINAL_LIBS)
328
329dict-benchmark: dict.c zmalloc.c sds.c siphash.c mt19937-64.c
330	$(REDIS_CC) $(FINAL_CFLAGS) $^ -D DICT_BENCHMARK_MAIN -o $@ $(FINAL_LIBS)
331
332DEP = $(REDIS_SERVER_OBJ:%.o=%.d) $(REDIS_CLI_OBJ:%.o=%.d) $(REDIS_BENCHMARK_OBJ:%.o=%.d)
333-include $(DEP)
334
335# Because the jemalloc.h header is generated as a part of the jemalloc build,
336# building it should complete before building any other object. Instead of
337# depending on a single artifact, build all dependencies first.
338%.o: %.c .make-prerequisites
339	$(REDIS_CC) -MMD -o $@ -c $<
340
341clean:
342	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
343	rm -f $(DEP)
344
345.PHONY: clean
346
347distclean: clean
348	-(cd ../deps && $(MAKE) distclean)
349	-(rm -f .make-*)
350
351.PHONY: distclean
352
353test: $(REDIS_SERVER_NAME) $(REDIS_CHECK_AOF_NAME)
354	@(cd ..; ./runtest)
355
356test-sentinel: $(REDIS_SENTINEL_NAME)
357	@(cd ..; ./runtest-sentinel)
358
359check: test
360
361lcov:
362	$(MAKE) gcov
363	@(set -e; cd ..; ./runtest --clients 1)
364	@geninfo -o redis.info .
365	@genhtml --legend -o lcov-html redis.info
366
367test-sds: sds.c sds.h
368	$(REDIS_CC) sds.c zmalloc.c -DSDS_TEST_MAIN $(FINAL_LIBS) -o /tmp/sds_test
369	/tmp/sds_test
370
371.PHONY: lcov
372
373bench: $(REDIS_BENCHMARK_NAME)
374	./$(REDIS_BENCHMARK_NAME)
375
37632bit:
377	@echo ""
378	@echo "WARNING: if it fails under Linux you probably need to install libc6-dev-i386"
379	@echo ""
380	$(MAKE) CFLAGS="-m32" LDFLAGS="-m32"
381
382gcov:
383	$(MAKE) REDIS_CFLAGS="-fprofile-arcs -ftest-coverage -DCOVERAGE_TEST" REDIS_LDFLAGS="-fprofile-arcs -ftest-coverage"
384
385noopt:
386	$(MAKE) OPTIMIZATION="-O0"
387
388valgrind:
389	$(MAKE) OPTIMIZATION="-O0" MALLOC="libc"
390
391helgrind:
392	$(MAKE) OPTIMIZATION="-O0" MALLOC="libc" CFLAGS="-D__ATOMIC_VAR_FORCE_SYNC_MACROS"
393
394src/help.h:
395	@../utils/generate-command-help.rb > help.h
396
397install: all
398	@mkdir -p $(INSTALL_BIN)
399	$(REDIS_INSTALL) $(REDIS_SERVER_NAME) $(INSTALL_BIN)
400	$(REDIS_INSTALL) $(REDIS_BENCHMARK_NAME) $(INSTALL_BIN)
401	$(REDIS_INSTALL) $(REDIS_CLI_NAME) $(INSTALL_BIN)
402	$(REDIS_INSTALL) $(REDIS_CHECK_RDB_NAME) $(INSTALL_BIN)
403	$(REDIS_INSTALL) $(REDIS_CHECK_AOF_NAME) $(INSTALL_BIN)
404	@ln -sf $(REDIS_SERVER_NAME) $(INSTALL_BIN)/$(REDIS_SENTINEL_NAME)
405
406uninstall:
407	rm -f $(INSTALL_BIN)/{$(REDIS_SERVER_NAME),$(REDIS_BENCHMARK_NAME),$(REDIS_CLI_NAME),$(REDIS_CHECK_RDB_NAME),$(REDIS_CHECK_AOF_NAME),$(REDIS_SENTINEL_NAME)}
408