From: Chris Lamb <lamby@debian.org>
Date: Wed, 3 Oct 2018 19:24:16 +0100
Subject: Add support for a USE_SYSTEM_HIREDIS flag.

---
 deps/Makefile |  2 ++
 src/Makefile  | 19 ++++++++++++++-----
 2 files changed, 16 insertions(+), 5 deletions(-)

diff --git a/deps/Makefile b/deps/Makefile
index 2ed7736..d6b8e3e 100644
--- a/deps/Makefile
+++ b/deps/Makefile
@@ -33,7 +33,9 @@ ifneq ($(shell sh -c '[ -f .make-ldflags ] && cat .make-ldflags || echo none'),
 endif
 
 distclean:
+ifneq ($(USE_SYSTEM_HIREDIS),yes)
 	-(cd hiredis && $(MAKE) clean) > /dev/null || true
+endif
 	-(cd linenoise && $(MAKE) clean) > /dev/null || true
 ifneq ($(USE_SYSTEM_LUA),yes)
 	-(cd lua && $(MAKE) clean) > /dev/null || true
diff --git a/src/Makefile b/src/Makefile
index 49085f2..9e7e6f1 100644
--- a/src/Makefile
+++ b/src/Makefile
@@ -16,7 +16,7 @@ release_hdr := $(shell sh -c './mkreleasehdr.sh')
 uname_S := $(shell sh -c 'uname -s 2>/dev/null || echo not')
 uname_M := $(shell sh -c 'uname -m 2>/dev/null || echo not')
 OPTIMIZATION?=-O2
-DEPENDENCY_TARGETS=hiredis linenoise
+DEPENDENCY_TARGETS=linenoise
 NODEPS:=clean distclean
 
 # Default settings
@@ -107,7 +107,7 @@ endif
 endif
 endif
 # Include paths to dependencies
-FINAL_CFLAGS+= -I../deps/hiredis -I../deps/linenoise
+FINAL_CFLAGS+= -I../deps/linenoise
 
 ifeq ($(MALLOC),tcmalloc)
 	FINAL_CFLAGS+= -DUSE_TCMALLOC
@@ -139,6 +139,15 @@ else
 	FINAL_LIBS := ../deps/lua/src/liblua.a $(FINAL_LIBS)
 endif
 
+ifeq ($(USE_SYSTEM_HIREDIS),yes)
+	FINAL_LIBS+= -lhiredis
+	FINAL_CFLAGS+= -I/usr/include/hiredis
+else
+	DEPENDENCY_TARGETS += hiredis
+	FINAL_LIBS+= ../deps/hiredis/libhiredis.a
+	FINAL_CFLAGS+= -I../deps/hiredis
+endif
+
 REDIS_CC=$(QUIET_CC)$(CC) $(FINAL_CFLAGS) $(CPPFLAGS)
 REDIS_LD=$(QUIET_LINK)$(CC) $(FINAL_LDFLAGS)
 REDIS_INSTALL=$(QUIET_INSTALL)$(INSTALL)
@@ -210,7 +219,7 @@ endif
 
 # redis-server
 $(REDIS_SERVER_NAME): $(REDIS_SERVER_OBJ)
-	$(REDIS_LD) -o $@ $^ ../deps/hiredis/libhiredis.a $(FINAL_LIBS)
+	$(REDIS_LD) -o $@ $^ $(FINAL_LIBS)
 
 # redis-sentinel
 $(REDIS_SENTINEL_NAME): $(REDIS_SERVER_NAME)
@@ -226,11 +235,11 @@ $(REDIS_CHECK_AOF_NAME): $(REDIS_SERVER_NAME)
 
 # redis-cli
 $(REDIS_CLI_NAME): $(REDIS_CLI_OBJ)
-	$(REDIS_LD) -o $@ $^ ../deps/hiredis/libhiredis.a ../deps/linenoise/linenoise.o $(FINAL_LIBS)
+	$(REDIS_LD) -o $@ $^ ../deps/linenoise/linenoise.o $(FINAL_LIBS)
 
 # redis-benchmark
 $(REDIS_BENCHMARK_NAME): $(REDIS_BENCHMARK_OBJ)
-	$(REDIS_LD) -o $@ $^ ../deps/hiredis/libhiredis.a $(FINAL_LIBS)
+	$(REDIS_LD) -o $@ $^ $(FINAL_LIBS)
 
 dict-benchmark: dict.c zmalloc.c sds.c siphash.c
 	$(REDIS_CC) $(FINAL_CFLAGS) $^ -D DICT_BENCHMARK_MAIN -o $@ $(FINAL_LIBS)
