
#########
#only linux/MacOSX x86/x64 is supported by this makefile

TARGET_OS = $(shell uname)
#TARGET_OS = Linux
#TARGET_OS = Darwin
TARGET_ARCH = $(shell uname -m)
#TARGET_ARCH = x86_64

#########
#setup

CC = gcc
CPP = g++
COMPILERFLAGS = -Wall -g

LDFLAGS_PLAIN = -lpthread
EXE_PATH = ./

ifeq ($(TARGET_OS),Darwin)
CFLAGS = $(COMPILERFLAGS) -I./
LDFLAGS_D_OUT = -dynamiclib -o libFZ_API.dylib
else

ifeq ($(TARGET_ARCH),x86_64)
CFLAGS = -m64 -fPIC $(COMPILERFLAGS) -I./
LDFLAGS_D_OUT = -m64 -shared -Wl,-soname,libFZ_API.so.1 -o libFZ_API.so.1.0
else
CFLAGS = $(COMPILERFLAGS) -I./
LDFLAGS_D_OUT = -shared -Wl,-soname,libFZ_API.so.1 -o libFZ_API.so.1.0
endif

endif

#########
#objects

API_OBJECTS  = ./timer.o ./socket_2.o ./logger.o ./common.o \
	./fzapi_channel.o ./fzapi.o ./fz_ll.o ./stream.o

#########
#this will build a .cpp or .c into .o
# make a .d dependency file
# fix so that a new or renamed .h file will cause a rebuild
# and finally fix paths in the .d file
%.o: %.cpp
	$(CPP) $(CFLAGS) -c $*.cpp -o $*.o
	$(CPP) -MM $(CFLAGS) $*.cpp > $*.d
	@mv -f $*.d $*.d.tmp
	@sed -e 's|.*:|$*.o:|' < $*.d.tmp > $*.d
	@sed -e 's/.*://' -e 's/\\$$//' < $*.d.tmp | fmt -1 | sed -e 's/^ *//' -e 's/$$/:/' >> $*.d
	@rm -f $*.d.tmp

%.o: %.c
	$(CC) $(CFLAGS) -c $*.c -o $*.o
	$(CC) -MM $(CFLAGS) $*.c > $*.d
	@mv -f $*.d $*.d.tmp
	@sed -e 's|.*:|$*.o:|' < $*.d.tmp > $*.d
	@sed -e 's/.*://' -e 's/\\$$//' < $*.d.tmp | fmt -1 | sed -e 's/^ *//' -e 's/$$/:/' >> $*.d
	@rm -f $*.d.tmp

#########
#targets
all: FZ_API_so FZ_API

clean:
	rm -f ./*.o ./*.d
	rm -f ./*.a
	rm -f ./*.so.*

#########
#private

FZ_API: $(API_OBJECTS)
	ar rcs ./libFZ_API.a $(API_OBJECTS)
-include $(API_OBJECTS:.o=.d)

FZ_API_so: $(API_OBJECTS)
	$(CPP) -fPIC $(LDFLAGS_D_OUT) $(API_OBJECTS) $(LDFLAGS_PLAIN)
-include $(API_OBJECTS:.o=.d)

