
#########
#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 ./libyuv/cpu_id.o \
	./libyuv/compare.o ./libyuv/compare_common.o ./libyuv/compare_neon.o \
	./libyuv/compare_posix.o ./libyuv/compare_win.o ./libyuv/convert.o \
	./libyuv/convert_argb.o ./libyuv/convert_from.o ./libyuv/convert_from_argb.o \
	./libyuv/format_conversion.o ./libyuv/mjpeg_decoder.o ./libyuv/planar_functions.o \
	./libyuv/rotate.o ./libyuv/rotate_argb.o ./libyuv/rotate_mips.o \
	./libyuv/rotate_neon.o ./libyuv/row_posix.o ./libyuv/row_win.o \
	./libyuv/scale.o ./libyuv/scale_argb.o \
	./libyuv/scale_argb_neon.o ./libyuv/scale_mips.o ./libyuv/scale_neon.o \
	./libyuv/video_common.o ./libyuv/row_any.o ./libyuv/row_common.o \
	./libyuv/row_mips.o ./libyuv/row_neon.o 


#########
#this will build a .cpp , .cc 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: %.cc
	g++ $(CFLAGS) -c $*.cc -o $*.o

%.o: %.c
	$(CC) $(CFLAGS) -c $*.c -o $*.o
	$(CC) -MM $(CFLAGS) $*.c > $*a.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.*
	rm -f ./libyuv/*.o ./libyuv/*.d
	rm -f ./libyuv/*.a
	rm -f ./libyuv/*.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)

