xref: /redis-3.2.3/src/Makefile (revision ace86cfa)
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')
17OPTIMIZATION?=-O2
18DEPENDENCY_TARGETS=hiredis linenoise lua
19
20# Default settings
21STD=-std=c99 -pedantic
22WARN=-Wall
23OPT=$(OPTIMIZATION)
24
25PREFIX?=/usr/local
26INSTALL_BIN=$(PREFIX)/bin
27INSTALL=install
28
29# Default allocator
30ifeq ($(uname_S),Linux)
31	MALLOC=jemalloc
32else
33	MALLOC=libc
34endif
35
36# Backwards compatibility for selecting an allocator
37ifeq ($(USE_TCMALLOC),yes)
38	MALLOC=tcmalloc
39endif
40
41ifeq ($(USE_TCMALLOC_MINIMAL),yes)
42	MALLOC=tcmalloc_minimal
43endif
44
45ifeq ($(USE_JEMALLOC),yes)
46	MALLOC=jemalloc
47endif
48
49# Override default settings if possible
50-include .make-settings
51
52FINAL_CFLAGS=$(STD) $(WARN) $(OPT) $(DEBUG) $(CFLAGS) $(REDIS_CFLAGS)
53FINAL_LDFLAGS=$(LDFLAGS) $(REDIS_LDFLAGS) $(DEBUG)
54FINAL_LIBS=-lm
55DEBUG=-g -ggdb
56
57ifeq ($(uname_S),SunOS)
58	INSTALL=cp -pf
59	FINAL_CFLAGS+= -D__EXTENSIONS__ -D_XPG6
60	FINAL_LIBS+= -ldl -lnsl -lsocket -lpthread
61else ifeq ($(uname_S),Darwin)
62
63else
64	FINAL_LDFLAGS+= -rdynamic
65	FINAL_LIBS+= -pthread
66endif
67
68# Include paths to dependencies
69FINAL_CFLAGS+= -I../deps/hiredis -I../deps/linenoise -I../deps/lua/src
70
71ifeq ($(MALLOC),tcmalloc)
72	FINAL_CFLAGS+= -DUSE_TCMALLOC
73	FINAL_LIBS+= -ltcmalloc
74endif
75
76ifeq ($(MALLOC),tcmalloc_minimal)
77	FINAL_CFLAGS+= -DUSE_TCMALLOC
78	FINAL_LIBS+= -ltcmalloc_minimal
79endif
80
81ifeq ($(MALLOC),jemalloc)
82	DEPENDENCY_TARGETS+= jemalloc
83	FINAL_CFLAGS+= -DUSE_JEMALLOC -I../deps/jemalloc/include
84	FINAL_LIBS+= ../deps/jemalloc/lib/libjemalloc.a -ldl
85endif
86
87REDIS_CC=$(QUIET_CC)$(CC) $(FINAL_CFLAGS)
88REDIS_LD=$(QUIET_LINK)$(CC) $(FINAL_LDFLAGS)
89REDIS_INSTALL=$(QUIET_INSTALL)$(INSTALL)
90
91CCCOLOR="\033[34m"
92LINKCOLOR="\033[34;1m"
93SRCCOLOR="\033[33m"
94BINCOLOR="\033[37;1m"
95MAKECOLOR="\033[32;1m"
96ENDCOLOR="\033[0m"
97
98ifndef V
99QUIET_CC = @printf '    %b %b\n' $(CCCOLOR)CC$(ENDCOLOR) $(SRCCOLOR)$@$(ENDCOLOR) 1>&2;
100QUIET_LINK = @printf '    %b %b\n' $(LINKCOLOR)LINK$(ENDCOLOR) $(BINCOLOR)$@$(ENDCOLOR) 1>&2;
101QUIET_INSTALL = @printf '    %b %b\n' $(LINKCOLOR)INSTALL$(ENDCOLOR) $(BINCOLOR)$@$(ENDCOLOR) 1>&2;
102endif
103
104REDIS_SERVER_NAME=redis-server
105REDIS_SENTINEL_NAME=redis-sentinel
106REDIS_SERVER_OBJ=adlist.o ae.o anet.o dict.o redis.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
107REDIS_CLI_NAME=redis-cli
108REDIS_CLI_OBJ=anet.o sds.o adlist.o redis-cli.o zmalloc.o release.o anet.o ae.o crc64.o
109REDIS_BENCHMARK_NAME=redis-benchmark
110REDIS_BENCHMARK_OBJ=ae.o anet.o redis-benchmark.o sds.o adlist.o zmalloc.o redis-benchmark.o
111REDIS_CHECK_DUMP_NAME=redis-check-dump
112REDIS_CHECK_DUMP_OBJ=redis-check-dump.o lzf_c.o lzf_d.o crc64.o
113REDIS_CHECK_AOF_NAME=redis-check-aof
114REDIS_CHECK_AOF_OBJ=redis-check-aof.o
115
116all: $(REDIS_SERVER_NAME) $(REDIS_SENTINEL_NAME) $(REDIS_CLI_NAME) $(REDIS_BENCHMARK_NAME) $(REDIS_CHECK_DUMP_NAME) $(REDIS_CHECK_AOF_NAME)
117	@echo ""
118	@echo "Hint: To run 'make test' is a good idea ;)"
119	@echo ""
120
121.PHONY: all
122
123# Deps (use make dep to generate this)
124include Makefile.dep
125
126dep:
127	$(REDIS_CC) -MM *.c > Makefile.dep
128
129.PHONY: dep
130
131persist-settings: distclean
132	echo STD=$(STD) >> .make-settings
133	echo WARN=$(WARN) >> .make-settings
134	echo OPT=$(OPT) >> .make-settings
135	echo MALLOC=$(MALLOC) >> .make-settings
136	echo CFLAGS=$(CFLAGS) >> .make-settings
137	echo LDFLAGS=$(LDFLAGS) >> .make-settings
138	echo REDIS_CFLAGS=$(REDIS_CFLAGS) >> .make-settings
139	echo REDIS_LDFLAGS=$(REDIS_LDFLAGS) >> .make-settings
140	echo PREV_FINAL_CFLAGS=$(FINAL_CFLAGS) >> .make-settings
141	echo PREV_FINAL_LDFLAGS=$(FINAL_LDFLAGS) >> .make-settings
142	-(cd ../deps && $(MAKE) $(DEPENDENCY_TARGETS))
143
144.PHONY: persist-settings
145
146# Prerequisites target
147.make-prerequisites:
148	@touch $@
149
150# Clean everything, persist settings and build dependencies if anything changed
151ifneq ($(strip $(PREV_FINAL_CFLAGS)), $(strip $(FINAL_CFLAGS)))
152.make-prerequisites: persist-settings
153endif
154
155ifneq ($(strip $(PREV_FINAL_LDFLAGS)), $(strip $(FINAL_LDFLAGS)))
156.make-prerequisites: persist-settings
157endif
158
159# redis-server
160$(REDIS_SERVER_NAME): $(REDIS_SERVER_OBJ)
161	$(REDIS_LD) -o $@ $^ ../deps/hiredis/libhiredis.a ../deps/lua/src/liblua.a $(FINAL_LIBS)
162
163# redis-sentinel
164$(REDIS_SENTINEL_NAME): $(REDIS_SERVER_NAME)
165	$(REDIS_INSTALL) $(REDIS_SERVER_NAME) $(REDIS_SENTINEL_NAME)
166
167# redis-cli
168$(REDIS_CLI_NAME): $(REDIS_CLI_OBJ)
169	$(REDIS_LD) -o $@ $^ ../deps/hiredis/libhiredis.a ../deps/linenoise/linenoise.o $(FINAL_LIBS)
170
171# redis-benchmark
172$(REDIS_BENCHMARK_NAME): $(REDIS_BENCHMARK_OBJ)
173	$(REDIS_LD) -o $@ $^ ../deps/hiredis/libhiredis.a $(FINAL_LIBS)
174
175# redis-check-dump
176$(REDIS_CHECK_DUMP_NAME): $(REDIS_CHECK_DUMP_OBJ)
177	$(REDIS_LD) -o $@ $^ $(FINAL_LIBS)
178
179# redis-check-aof
180$(REDIS_CHECK_AOF_NAME): $(REDIS_CHECK_AOF_OBJ)
181	$(REDIS_LD) -o $@ $^ $(FINAL_LIBS)
182
183# Because the jemalloc.h header is generated as a part of the jemalloc build,
184# building it should complete before building any other object. Instead of
185# depending on a single artifact, build all dependencies first.
186%.o: %.c .make-prerequisites
187	$(REDIS_CC) -c $<
188
189clean:
190	rm -rf $(REDIS_SERVER_NAME) $(REDIS_SENTINEL_NAME) $(REDIS_CLI_NAME) $(REDIS_BENCHMARK_NAME) $(REDIS_CHECK_DUMP_NAME) $(REDIS_CHECK_AOF_NAME) *.o *.gcda *.gcno *.gcov redis.info lcov-html
191
192.PHONY: clean
193
194distclean: clean
195	-(cd ../deps && $(MAKE) distclean)
196	-(rm -f .make-*)
197
198.PHONY: distclean
199
200test: $(REDIS_SERVER_NAME) $(REDIS_CHECK_AOF_NAME)
201	@(cd ..; ./runtest)
202
203check: test
204
205lcov:
206	$(MAKE) gcov
207	@(set -e; cd ..; ./runtest --clients 1)
208	@geninfo -o redis.info .
209	@genhtml --legend -o lcov-html redis.info
210
211.PHONY: lcov
212
213bench: $(REDIS_BENCHMARK_NAME)
214	./$(REDIS_BENCHMARK_NAME)
215
21632bit:
217	@echo ""
218	@echo "WARNING: if it fails under Linux you probably need to install libc6-dev-i386"
219	@echo ""
220	$(MAKE) CFLAGS="-m32" LDFLAGS="-m32"
221
222gcov:
223	$(MAKE) REDIS_CFLAGS="-fprofile-arcs -ftest-coverage -DCOVERAGE_TEST" REDIS_LDFLAGS="-fprofile-arcs -ftest-coverage"
224
225noopt:
226	$(MAKE) OPTIMIZATION="-O0"
227
228valgrind:
229	$(MAKE) OPTIMIZATION="-O0" MALLOC="libc"
230
231src/help.h:
232	@../utils/generate-command-help.rb > help.h
233
234install: all
235	@mkdir -p $(INSTALL_BIN)
236	$(REDIS_INSTALL) $(REDIS_SERVER_NAME) $(INSTALL_BIN)
237	$(REDIS_INSTALL) $(REDIS_BENCHMARK_NAME) $(INSTALL_BIN)
238	$(REDIS_INSTALL) $(REDIS_CLI_NAME) $(INSTALL_BIN)
239	$(REDIS_INSTALL) $(REDIS_CHECK_DUMP_NAME) $(INSTALL_BIN)
240	$(REDIS_INSTALL) $(REDIS_CHECK_AOF_NAME) $(INSTALL_BIN)
241