From 857c9212d31f4487b570c8adef15ca37a755a1b5 Mon Sep 17 00:00:00 2001 From: Mina Naguib Date: Thu, 3 Jun 2010 11:33:54 -0400 Subject: [PATCH] Build a shared library --- .gitignore | 6 +++++- Makefile | 41 +++++++++++++++++++++++++---------------- 2 files changed, 30 insertions(+), 17 deletions(-) diff --git a/.gitignore b/.gitignore index 5761abc..ef62ee1 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,5 @@ -*.o +/hiredis-test +/hiredis-example +/*.o +/*.so +/*.dylib diff --git a/Makefile b/Makefile index e852c03..c639c8f 100644 --- a/Makefile +++ b/Makefile @@ -2,25 +2,31 @@ # Copyright (C) 2010 Salvatore Sanfilippo # This file is released under the BSD license, see the COPYING file +OBJ = anet.o hiredis.o sds.o +BINS = hiredis-example hiredis-test + uname_S := $(shell sh -c 'uname -s 2>/dev/null || echo not') OPTIMIZATION?=-O2 ifeq ($(uname_S),SunOS) - CFLAGS?= -std=c99 -pedantic $(OPTIMIZATION) -Wall -W -D__EXTENSIONS__ -D_XPG6 + CFLAGS?= -std=c99 -pedantic $(OPTIMIZATION) -fPIC -Wall -W -D__EXTENSIONS__ -D_XPG6 CCLINK?= -ldl -lnsl -lsocket -lm -lpthread -else - CFLAGS?= -std=c99 -pedantic $(OPTIMIZATION) -Wall -W -Wwrite-strings $(ARCH) $(PROF) + DYLIBNAME?=libhiredis.so + DYLIB_MAKE_CMD?=gcc -shared -Wl,-soname,${DYLIBNAME} -o ${DYLIBNAME} ${OBJ} +else ifeq ($(uname_S),Darwin) + CFLAGS?= -std=c99 -pedantic $(OPTIMIZATION) -fPIC -Wall -W -Wwrite-strings $(ARCH) $(PROF) CCLINK?= -lm -pthread + DYLIBNAME?=libhiredis.dylib + DYLIB_MAKE_CMD?=libtool -dynamic -o ${DYLIBNAME} -lm ${DEBUG} - ${OBJ} +else + CFLAGS?= -std=c99 -pedantic $(OPTIMIZATION) -fPIC -Wall -W -Wwrite-strings $(ARCH) $(PROF) + CCLINK?= -lm -pthread + DYLIBNAME?=libhiredis.so + DYLIB_MAKE_CMD?=gcc -shared -Wl,-soname,${DYLIBNAME} -o ${DYLIBNAME} ${OBJ} endif CCOPT= $(CFLAGS) $(CCLINK) $(ARCH) $(PROF) -DEBUG?= -g -rdynamic -ggdb +DEBUG?= -g -ggdb -OBJ = anet.o hiredis.o sds.o example.o -TESTOBJ = anet.o hiredis.o sds.o test.o - -PRGNAME = hiredis-example -TESTNAME = hiredis-test - -all: hiredis-example hiredis-test +all: ${DYLIBNAME} ${BINS} # Deps (use make dep to generate this) anet.o: anet.c fmacros.h anet.h @@ -30,18 +36,21 @@ hiredis.o: hiredis.c hiredis.h sds.h anet.h sds.o: sds.c sds.h hiredis.o: hiredis.c hiredis.h sds.h anet.h -hiredis-example: $(OBJ) - $(CC) -o $(PRGNAME) $(CCOPT) $(DEBUG) $(OBJ) +${DYLIBNAME}: ${OBJ} + ${DYLIB_MAKE_CMD} -hiredis-test: $(TESTOBJ) - $(CC) -o $(TESTNAME) $(CCOPT) $(DEBUG) $(TESTOBJ) +# Binaries: +hiredis-%: %.o ${DYLIBNAME} + $(CC) -o $@ $(CCOPT) $(DEBUG) -L. -l hiredis -Wl,-rpath,. $< + +test: hiredis-test ./hiredis-test .c.o: $(CC) -c $(CFLAGS) $(DEBUG) $(COMPILE_TIME) $< clean: - rm -rf $(PRGNAME) $(TESTNAME) *.o *.gcda *.gcno *.gcov + rm -rf ${DYLIBNAME} $(BINS) *.o *.gcda *.gcno *.gcov dep: $(CC) -MM *.c