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