概述
Redis Makefile注解
Redis的makefile是阅读源码的第一步,总共有292行,读起来也是头大,记录之。
个人博客原文排版体验更佳。
4.02版本源码为:
# Redis Makefile
# Copyright (C) 2009 Salvatore Sanfilippo <antirez at gmail dot com>
# This file is released under the BSD license, see the COPYING file
#
# The Makefile composes the final FINAL_CFLAGS and FINAL_LDFLAGS using
# what is needed for Redis plus the standard CFLAGS and LDFLAGS passed.
# However when building the dependencies (Jemalloc, Lua, Hiredis, ...)
# CFLAGS and LDFLAGS are propagated to the dependencies, so to pass
# flags only to be used when compiling / linking Redis itself REDIS_CFLAGS
# and REDIS_LDFLAGS are used instead (this is the case of 'make gcov').
#
# Dependencies are stored in the Makefile.dep file. To rebuild this file
# Just use 'make dep', but this is only needed by developers.
release_hdr := $(shell sh -c './mkreleasehdr.sh')
# uname -s 获取操作系统的类型 Linux
uname_S := $(shell sh -c 'uname -s 2>/dev/null || echo not')
#uname -m 获取机子的架构 x86_64
uname_M := $(shell sh -c 'uname -m 2>/dev/null || echo not')
# 优化选项
OPTIMIZATION?=-O2
# 依赖目标
DEPENDENCY_TARGETS=hiredis linenoise lua
NODEPS:=clean distclean
# Default settings
# 使用c99标准编译,-pedantic 保证代码规范满足ISO C和ISO C++标准
STD=-std=c99 -pedantic -DREDIS_STATIC=''
# 输出所有编译警告信息 ,Wno-missing-field-initializers 不输出missing-的警告信息
WARN=-Wall -W -Wno-missing-field-initializers
OPT=$(OPTIMIZATION)
#默认目录
PREFIX?=/usr/local
#安装的默认目录
INSTALL_BIN=$(PREFIX)/bin
INSTALL=install
# Default allocator defaults to Jemalloc if it's not an ARM
#内存分配器的指定 默认libc,linux系统而且架构不是armv61和71的时候则是jemalloc,
MALLOC=libc
ifneq ($(uname_M),armv6l)
ifneq ($(uname_M),armv7l)
ifeq ($(uname_S),Linux)
MALLOC=jemalloc
endif
endif
endif
# To get ARM stack traces if Redis crashes we need a special C flag.
ifneq (,$(findstring armv,$(uname_M)))
CFLAGS+=-funwind-tables
endif
# Backwards compatibility for selecting an allocator
#编译的时候指定内存分配器
ifeq ($(USE_TCMALLOC),yes)
MALLOC=tcmalloc
endif
ifeq ($(USE_TCMALLOC_MINIMAL),yes)
MALLOC=tcmalloc_minimal
endif
ifeq ($(USE_JEMALLOC),yes)
MALLOC=jemalloc
endif
ifeq ($(USE_JEMALLOC),no)
MALLOC=libc
endif
# Override default settings if possible
-include .make-settings
# 最终的编译选项CFLAGS是-c的选项,LDFLAGS是链接的选项
FINAL_CFLAGS=$(STD) $(WARN) $(OPT) $(DEBUG) $(CFLAGS) $(REDIS_CFLAGS)
FINAL_LDFLAGS=$(LDFLAGS) $(REDIS_LDFLAGS) $(DEBUG)
# m这个lib是libmath 也就是math的链接
FINAL_LIBS=-lm
# 调试信息
DEBUG=-g -ggdb
#根据操作系统继续指定编译选项
ifeq ($(uname_S),SunOS)
# SunOS
ifneq ($(@@),32bit)
CFLAGS+= -m64
LDFLAGS+= -m64
endif
DEBUG=-g
DEBUG_FLAGS=-g
export CFLAGS LDFLAGS DEBUG DEBUG_FLAGS
INSTALL=cp -pf
FINAL_CFLAGS+= -D__EXTENSIONS__ -D_XPG6
FINAL_LIBS+= -ldl -lnsl -lsocket -lresolv -lpthread -lrt
else
ifeq ($(uname_S),Darwin)
# Darwin
FINAL_LIBS+= -ldl
else
ifeq ($(uname_S),AIX)
# AIX
FINAL_LDFLAGS+= -Wl,-bexpall
FINAL_LIBS+=-ldl -pthread -lcrypt -lbsd
else
ifeq ($(uname_S),OpenBSD)
# OpenBSD
FINAL_LIBS+= -lpthread
else
ifeq ($(uname_S),FreeBSD)
# FreeBSD
FINAL_LIBS+= -lpthread
else
# 特别是对Linux的指定
# All the other OSes (notably Linux)
# -rdynamic将链接器将所有符号添加到动态符号表
FINAL_LDFLAGS+= -rdynamic
#pthread库 用于多线程, dl是libdl 动态链接库
FINAL_LIBS+=-ldl -pthread
endif
endif
endif
endif
endif
# Include paths to dependencies
# -I 指定头文件的目录
FINAL_CFLAGS+= -I../deps/hiredis -I../deps/linenoise -I../deps/lua/src
ifeq ($(MALLOC),tcmalloc)
FINAL_CFLAGS+= -DUSE_TCMALLOC
FINAL_LIBS+= -ltcmalloc
endif
ifeq ($(MALLOC),tcmalloc_minimal)
FINAL_CFLAGS+= -DUSE_TCMALLOC
FINAL_LIBS+= -ltcmalloc_minimal
endif
#使用jemalloc的话 链接 libjemalloc.a -I指定jemalloc的头文件目录
ifeq ($(MALLOC),jemalloc)
DEPENDENCY_TARGETS+= jemalloc
FINAL_CFLAGS+= -DUSE_JEMALLOC -I../deps/jemalloc/include
FINAL_LIBS+= ../deps/jemalloc/lib/libjemalloc.a
endif
#redis 的gcc -c 选项
REDIS_CC=$(QUIET_CC)$(CC) $(FINAL_CFLAGS)
#redis的gcc 链接选项
REDIS_LD=$(QUIET_LINK)$(CC) $(FINAL_LDFLAGS)
#redis的安装选项
REDIS_INSTALL=$(QUIET_INSTALL)$(INSTALL)
CCCOLOR="