# Makefile
# ex.  > make PLATFORM_BITS=64 LIB_PREFIX=/usr
#

ifeq ($(OS),Windows_NT)
    UNAME := Windows
else
    UNAME := $(shell uname -s)
endif

ifeq ($(UNAME),Darwin)
	CCOPT = -I.  -I/opt/local/include  -fno-strict-aliasing -Wno-deprecated-declarations 
	SOEXT := dylib
	OPT_SOLINK := -dynamiclib -Wl,-undefined -Wl,dynamic_lookup
else
	CCOPT = -I.  
	SOEXT := so
	OPT_SOLINK := -shared -dynamiclib
endif

NAME = socket
DATE_TAG:=`date +%Y%m%d`

LIB_PREFIX = /usr/local

LDOPT =  -L$(LIB_PREFIX)/lib

CFLAGS = -Wall -O2 $(CCOPT)

AZ_CCOPT = -Wall -O2 $(CCOPT)
AZPC_LIBS = socket
AZ_LIB_EXT = $(LIB_PREFIX)/lib/azprolog/ext
AZ_LIB_OBJ = $(LIB_PREFIX)/lib/azprolog/obj

LDLIBS= -lssl -lcrypto

EXEC_TARGETS = socket.$(SOEXT) lib_socket.a socket_ssl.$(SOEXT) lib_socket_ssl.a socket 

default: socket.$(SOEXT) lib_socket.a socket_ssl.$(SOEXT) lib_socket_ssl.a

socket.$(SOEXT): socket.c
	$(CC) $(CFLAGS) -fPIC -o socket.o -c socket.c
	$(CC) $(OPT_SOLINK) -o $@ socket.o -lc
	rm -f socket.o
	chmod 644 socket.$(SOEXT)

socket_ssl.$(SOEXT): socket_ssl.c
	$(CC) $(CFLAGS) -fPIC -o socket_ssl.o -c socket_ssl.c
	$(CC) $(OPT_SOLINK) -o $@ socket_ssl.o -lc $(LDLIBS)
	rm -f socket_ssl.o
	chmod 644 socket_ssl.$(SOEXT)

lib_socket.a: socket.c
	$(CC) $(CFLAGS) -o socket.o -c socket.c
	ar rv $@ socket.o
	ranlib $@
	chmod 644 lib_socket.a

lib_socket_ssl.a: socket_ssl.c
	$(CC) $(CFLAGS) -o socket_ssl.o -c socket_ssl.c
	ar rv $@ socket_ssl.o
	ranlib $@
	chmod 644 lib_socket_ssl.a

socket.o: socket.c
socket_ssl.o: socket_ssl.c

install:
	cp -p socket.$(SOEXT) lib_socket.a $(AZ_LIB_EXT)
	cp -p socket_ssl.$(SOEXT) lib_socket_ssl.a $(AZ_LIB_EXT)

clean:
	rm -f *~ .*~ *.o $(EXEC_TARGETS)
