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 geohash-int 19 20# Default settings 21STD=-std=c99 -pedantic -DREDIS_STATIC='' 22WARN=-Wall -W 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 49ifeq ($(USE_JEMALLOC),no) 50 MALLOC=libc 51endif 52 53# Override default settings if possible 54-include .make-settings 55 56FINAL_CFLAGS=$(STD) $(WARN) $(OPT) $(DEBUG) $(CFLAGS) $(REDIS_CFLAGS) -I../deps/geohash-int 57FINAL_LDFLAGS=$(LDFLAGS) $(REDIS_LDFLAGS) $(DEBUG) 58FINAL_LIBS=-lm 59DEBUG=-g -ggdb 60 61ifeq ($(uname_S),SunOS) 62 # SunOS 63 INSTALL=cp -pf 64 FINAL_CFLAGS+= -D__EXTENSIONS__ -D_XPG6 65 FINAL_LIBS+= -ldl -lnsl -lsocket -lresolv -lpthread -lrt 66else 67ifeq ($(uname_S),Darwin) 68 # Darwin (nothing to do) 69else 70ifeq ($(uname_S),AIX) 71 # AIX 72 FINAL_LDFLAGS+= -Wl,-bexpall 73 FINAL_LIBS+= -pthread -lcrypt -lbsd 74 75else 76 # All the other OSes (notably Linux) 77 FINAL_LDFLAGS+= -rdynamic 78 FINAL_LIBS+= -pthread 79endif 80endif 81endif 82# Include paths to dependencies 83FINAL_CFLAGS+= -I../deps/hiredis -I../deps/linenoise -I../deps/lua/src 84 85ifeq ($(MALLOC),tcmalloc) 86 FINAL_CFLAGS+= -DUSE_TCMALLOC 87 FINAL_LIBS+= -ltcmalloc 88endif 89 90ifeq ($(MALLOC),tcmalloc_minimal) 91 FINAL_CFLAGS+= -DUSE_TCMALLOC 92 FINAL_LIBS+= -ltcmalloc_minimal 93endif 94 95ifeq ($(MALLOC),jemalloc) 96 DEPENDENCY_TARGETS+= jemalloc 97 FINAL_CFLAGS+= -DUSE_JEMALLOC -I../deps/jemalloc/include 98 FINAL_LIBS+= ../deps/jemalloc/lib/libjemalloc.a -ldl 99endif 100 101REDIS_CC=$(QUIET_CC)$(CC) $(FINAL_CFLAGS) 102REDIS_LD=$(QUIET_LINK)$(CC) $(FINAL_LDFLAGS) 103REDIS_INSTALL=$(QUIET_INSTALL)$(INSTALL) 104 105CCCOLOR="\033[34m" 106LINKCOLOR="\033[34;1m" 107SRCCOLOR="\033[33m" 108BINCOLOR="\033[37;1m" 109MAKECOLOR="\033[32;1m" 110ENDCOLOR="\033[0m" 111 112ifndef V 113QUIET_CC = @printf ' %b %b\n' $(CCCOLOR)CC$(ENDCOLOR) $(SRCCOLOR)$@$(ENDCOLOR) 1>&2; 114QUIET_LINK = @printf ' %b %b\n' $(LINKCOLOR)LINK$(ENDCOLOR) $(BINCOLOR)$@$(ENDCOLOR) 1>&2; 115QUIET_INSTALL = @printf ' %b %b\n' $(LINKCOLOR)INSTALL$(ENDCOLOR) $(BINCOLOR)$@$(ENDCOLOR) 1>&2; 116endif 117 118REDIS_SERVER_NAME=redis-server 119REDIS_SENTINEL_NAME=redis-sentinel 120REDIS_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 crc64.o bitops.o sentinel.o notify.o setproctitle.o blocked.o hyperloglog.o latency.o sparkline.o redis-check-rdb.o geo.o 121REDIS_GEOHASH_OBJ=../deps/geohash-int/geohash.o ../deps/geohash-int/geohash_helper.o 122REDIS_CLI_NAME=redis-cli 123REDIS_CLI_OBJ=anet.o adlist.o redis-cli.o zmalloc.o release.o anet.o ae.o crc64.o 124REDIS_BENCHMARK_NAME=redis-benchmark 125REDIS_BENCHMARK_OBJ=ae.o anet.o redis-benchmark.o adlist.o zmalloc.o redis-benchmark.o 126REDIS_CHECK_RDB_NAME=redis-check-rdb 127REDIS_CHECK_AOF_NAME=redis-check-aof 128REDIS_CHECK_AOF_OBJ=redis-check-aof.o 129 130all: $(REDIS_SERVER_NAME) $(REDIS_SENTINEL_NAME) $(REDIS_CLI_NAME) $(REDIS_BENCHMARK_NAME) $(REDIS_CHECK_RDB_NAME) $(REDIS_CHECK_AOF_NAME) 131 @echo "" 132 @echo "Hint: It's a good idea to run 'make test' ;)" 133 @echo "" 134 135.PHONY: all 136 137# Deps (use make dep to generate this) 138include Makefile.dep 139 140dep: 141 $(REDIS_CC) -MM *.c > Makefile.dep 142 143.PHONY: dep 144 145persist-settings: distclean 146 echo STD=$(STD) >> .make-settings 147 echo WARN=$(WARN) >> .make-settings 148 echo OPT=$(OPT) >> .make-settings 149 echo MALLOC=$(MALLOC) >> .make-settings 150 echo CFLAGS=$(CFLAGS) >> .make-settings 151 echo LDFLAGS=$(LDFLAGS) >> .make-settings 152 echo REDIS_CFLAGS=$(REDIS_CFLAGS) >> .make-settings 153 echo REDIS_LDFLAGS=$(REDIS_LDFLAGS) >> .make-settings 154 echo PREV_FINAL_CFLAGS=$(FINAL_CFLAGS) >> .make-settings 155 echo PREV_FINAL_LDFLAGS=$(FINAL_LDFLAGS) >> .make-settings 156 -(cd ../deps && $(MAKE) $(DEPENDENCY_TARGETS)) 157 158.PHONY: persist-settings 159 160# Prerequisites target 161.make-prerequisites: 162 @touch $@ 163 164# Clean everything, persist settings and build dependencies if anything changed 165ifneq ($(strip $(PREV_FINAL_CFLAGS)), $(strip $(FINAL_CFLAGS))) 166.make-prerequisites: persist-settings 167endif 168 169ifneq ($(strip $(PREV_FINAL_LDFLAGS)), $(strip $(FINAL_LDFLAGS))) 170.make-prerequisites: persist-settings 171endif 172 173# redis-server 174$(REDIS_SERVER_NAME): $(REDIS_SERVER_OBJ) 175 $(REDIS_LD) -o $@ $^ ../deps/hiredis/libhiredis.a ../deps/lua/src/liblua.a $(REDIS_GEOHASH_OBJ) $(FINAL_LIBS) 176 177# redis-sentinel 178$(REDIS_SENTINEL_NAME): $(REDIS_SERVER_NAME) 179 $(REDIS_INSTALL) $(REDIS_SERVER_NAME) $(REDIS_SENTINEL_NAME) 180 181# redis-check-rdb 182$(REDIS_CHECK_RDB_NAME): $(REDIS_SERVER_NAME) 183 $(REDIS_INSTALL) $(REDIS_SERVER_NAME) $(REDIS_CHECK_RDB_NAME) 184 185# redis-cli 186$(REDIS_CLI_NAME): $(REDIS_CLI_OBJ) 187 $(REDIS_LD) -o $@ $^ ../deps/hiredis/libhiredis.a ../deps/linenoise/linenoise.o $(FINAL_LIBS) 188 189# redis-benchmark 190$(REDIS_BENCHMARK_NAME): $(REDIS_BENCHMARK_OBJ) 191 $(REDIS_LD) -o $@ $^ ../deps/hiredis/libhiredis.a $(FINAL_LIBS) 192 193# redis-check-aof 194$(REDIS_CHECK_AOF_NAME): $(REDIS_CHECK_AOF_OBJ) 195 $(REDIS_LD) -o $@ $^ $(FINAL_LIBS) 196 197# Because the jemalloc.h header is generated as a part of the jemalloc build, 198# building it should complete before building any other object. Instead of 199# depending on a single artifact, build all dependencies first. 200%.o: %.c .make-prerequisites 201 $(REDIS_CC) -c $< 202 203clean: 204 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 205 206.PHONY: clean 207 208distclean: clean 209 -(cd ../deps && $(MAKE) distclean) 210 -(rm -f .make-*) 211 212.PHONY: distclean 213 214test: $(REDIS_SERVER_NAME) $(REDIS_CHECK_AOF_NAME) 215 @(cd ..; ./runtest) 216 217test-sentinel: $(REDIS_SENTINEL_NAME) 218 @(cd ..; ./runtest-sentinel) 219 220check: test 221 222lcov: 223 $(MAKE) gcov 224 @(set -e; cd ..; ./runtest --clients 1) 225 @geninfo -o redis.info . 226 @genhtml --legend -o lcov-html redis.info 227 228test-sds: sds.c sds.h 229 $(REDIS_CC) sds.c zmalloc.c -DSDS_TEST_MAIN -o /tmp/sds_test 230 /tmp/sds_test 231 232.PHONY: lcov 233 234bench: $(REDIS_BENCHMARK_NAME) 235 ./$(REDIS_BENCHMARK_NAME) 236 23732bit: 238 @echo "" 239 @echo "WARNING: if it fails under Linux you probably need to install libc6-dev-i386" 240 @echo "" 241 $(MAKE) CFLAGS="-m32" LDFLAGS="-m32" 242 243gcov: 244 $(MAKE) REDIS_CFLAGS="-fprofile-arcs -ftest-coverage -DCOVERAGE_TEST" REDIS_LDFLAGS="-fprofile-arcs -ftest-coverage" 245 246noopt: 247 $(MAKE) OPTIMIZATION="-O0" 248 249valgrind: 250 $(MAKE) OPTIMIZATION="-O0" MALLOC="libc" 251 252src/help.h: 253 @../utils/generate-command-help.rb > help.h 254 255install: all 256 @mkdir -p $(INSTALL_BIN) 257 $(REDIS_INSTALL) $(REDIS_SERVER_NAME) $(INSTALL_BIN) 258 $(REDIS_INSTALL) $(REDIS_BENCHMARK_NAME) $(INSTALL_BIN) 259 $(REDIS_INSTALL) $(REDIS_CLI_NAME) $(INSTALL_BIN) 260 $(REDIS_INSTALL) $(REDIS_CHECK_RDB_NAME) $(INSTALL_BIN) 261 $(REDIS_INSTALL) $(REDIS_CHECK_AOF_NAME) $(INSTALL_BIN) 262 @ln -sf $(REDIS_SERVER_NAME) $(INSTALL_BIN)/$(REDIS_SENTINEL_NAME) 263