一、redis源码编译
redis可以直接在官网下载(本文使用版本 3.0.7):https://redis.io/download
安装:
$ tar xzf redis-3.0.7.tar.gz $ cd redis-3.0.7 $ make
make执行以后主要编译产物在src/redis-server src/redis-cli
如果想把redis-server直接install到可执行目录/usr/local/bin,还需要执行:
$ make install
Run Redis with:
$ src/redis-server
You can interact with Redis using the built-in client:
$ src/redis-cli
redis> set foo bar
OK
redis> get foo
"bar"
二、redis Makefile
redis根目录下的Makefile内容:
1 # Top level makefile, the real shit is at src/Makefile 2 3 default: all 4 5 .DEFAULT: 6 cd src && $(MAKE) $@ 7 8 install: 9 cd src && $(MAKE) $@ 10 11 .PHONY: install
主要调用src/Makefile, 内容较长,分析有用信息:
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 15 release_hdr := $(shell sh -c './mkreleasehdr.sh') # 这个脚本生成系统版本信息 16 uname_S := $(shell sh -c 'uname -s 2>/dev/null || echo not') # uname -r输出操作系统类型,Linux 17 OPTIMIZATION?=-O2 # 优化级别 ?=表示没有定义则赋值 18 DEPENDENCY_TARGETS=hiredis linenoise lua 19 20 # Default settings 21 STD=-std=c99 -pedantic 22 WARN=-Wall -W # -Wall表示输出所有warning信息 23 OPT=$(OPTIMIZATION) 24 25 PREFIX?=/usr/local # 这几行表示make install路径 26 INSTALL_BIN=$(PREFIX)/bin 27 INSTALL=install 28 29 # Default allocator # 下面根据操作系统类型定义内存MALLOC分配器 30 ifeq ($(uname_S),Linux) 31 MALLOC=jemalloc 32 else 33 MALLOC=libc 34 endif 35 36 # Backwards compatibility for selecting an allocator 37 ifeq ($(USE_TCMALLOC),yes) 38 MALLOC=tcmalloc 39 endif 40 41 ifeq ($(USE_TCMALLOC_MINIMAL),yes) 42 MALLOC=tcmalloc_minimal 43 endif 44 45 ifeq ($(USE_JEMALLOC),yes) 46 MALLOC=jemalloc 47 endif 48 49 # Override default settings if possible #默认make设置, 50 -include .make-settings 51 52 FINAL_CFLAGS=$(STD) $(WARN) $(OPT) $(DEBUG) $(CFLAGS) $(REDIS_CFLAGS) # 最终CCFLAGS 53 FINAL_LDFLAGS=$(LDFLAGS) $(REDIS_LDFLAGS) $(DEBUG) # 最终LDFLAGS 54 FINAL_LIBS=-lm 55 DEBUG=-g -ggdb 56 57 ifeq ($(uname_S),SunOS) # 非Linux操作系统处理 58 # SunOS 59 INSTALL=cp -pf 60 FINAL_CFLAGS+= -D__EXTENSIONS__ -D_XPG6 61 FINAL_LIBS+= -ldl -lnsl -lsocket -lresolv -lpthread -lrt 62 else 63 ifeq ($(uname_S),Darwin) 64 # Darwin (nothing to do) 65 else 66 ifeq ($(uname_S),AIX) 67 # AIX 68 FINAL_LDFLAGS+= -Wl,-bexpall 69 FINAL_LIBS+= -pthread -lcrypt -lbsd 70 71 else 72 # All the other OSes (notably Linux) 73 FINAL_LDFLAGS+= -rdynamic 74 FINAL_LIBS+= -pthread 75 endif 76 endif 77 endif 78 # Include paths to dependencies 79 FINAL_CFLAGS+= -I../deps/hiredis -I../deps/linenoise -I../deps/lua/src 80 81 ifeq ($(MALLOC),tcmalloc) 82 FINAL_CFLAGS+= -DUSE_TCMALLOC 83 FINAL_LIBS+= -ltcmalloc 84 endif 85 86 ifeq ($(MALLOC),tcmalloc_minimal) 87 FINAL_CFLAGS+= -DUSE_TCMALLOC 88 FINAL_LIBS+= -ltcmalloc_minimal 89 endif 90 91 ifeq ($(MALLOC),jemalloc) 92 DEPENDENCY_TARGETS+= jemalloc 93 FINAL_CFLAGS+= -DUSE_JEMALLOC -I../deps/jemalloc/include 94 FINAL_LIBS+= ../deps/jemalloc/lib/libjemalloc.a -ldl 95 endif 96 # 下面QUIET_XXX表示编译的时候,如果定义V=1,即使用make V=1, 输出详细编译信息,否则输出简单编译信息 97 REDIS_CC=$(QUIET_CC)$(CC) $(FINAL_CFLAGS) 98 REDIS_LD=$(QUIET_LINK)$(CC) $(FINAL_LDFLAGS) 99 REDIS_INSTALL=$(QUIET_INSTALL)$(INSTALL) 100 101 CCCOLOR="