AR=@AR@
ARFLAGS=crs
RM=rm -rf
SHELL=@SHELL@
ARCH=@ARCH@
abs_top_srcdir=@abs_top_srcdir@
rel_top_srcdir=$(subst $(abspath $(abs_top_srcdir))/,,$(abspath $(CURDIR))/)
prefix=@prefix@
exec_prefix=@exec_prefix@
bindir=@bindir@
sbindir=@sbindir@
libexecdir=@libexecdir@
sysconfdir=@sysconfdir@
sharedstatedir=@sharedstatedir@
localstatedir=@localstatedir@
libdir=@libdir@
includedir=@includedir@
oldincludedir=@oldincludedir@
datarootdir=@datarootdir@
datadir=@datadir@
infodir=@infodir@
localedir=@localedir@
mandir=@mandir@
PACKAGE_TARNAME=@PACKAGE_TARNAME@
docdir=@docdir@
htmldir=@htmldir@
dvidir=@dvidir@
pdfdir=@pdfdir@
psdir=@psdir@
CXX=@CXX@
CXX_FOR_BUILD=@CXX_FOR_BUILD@
CC=@CC@
CC_FOR_BUILD=@CC_FOR_BUILD@
CXXFLAGS+=@CXXFLAGS@
CXXFLAGS_FOR_BUILD+=@CXXFLAGS_FOR_BUILD@
CFLAGS+=@CFLAGS@
CFLAGS_FOR_BUILD+=@CFLAGS_FOR_BUILD@
LDFLAGS+=@LDFLAGS@
LDFLAGS_FOR_BUILD+=@LDFLAGS_FOR_BUILD@
INCLUDES+=-I@abs_top_srcdir@
INCLUDES+=-I@abs_top_srcdir@/lib
INCLUDES+=-I@abs_top_srcdir@/xbmc
INCLUDES+=-I@abs_top_srcdir@/xbmc/addons/include
INCLUDES+=-I@abs_top_srcdir@/addons/library.kodi.guilib
INCLUDES+=-I@abs_top_srcdir@/addons/library.xbmc.addon
INCLUDES+=-I@abs_top_srcdir@/addons/library.kodi.adsp
INCLUDES+=-I@abs_top_srcdir@/addons/library.kodi.audioengine
INCLUDES+=-I@abs_top_srcdir@/addons/library.xbmc.pvr
INCLUDES+=-I@abs_top_srcdir@/addons/library.xbmc.codec
INCLUDES+=$(sort @INCLUDES@)
INCLUDES+=-I@abs_top_srcdir@/xbmc/linux
INCLUDES+=-I@abs_top_srcdir@/xbmc/cores/dvdplayer
DEFINES+= \
	@ARCH_DEFINES@ \
	-D_FILE_DEFINED \
	-D__STDC_CONSTANT_MACROS \
	-DBIN_INSTALL_PATH="\"$(libdir)/@APP_NAME_LC@\"" \
	-DINSTALL_PATH="\"$(datarootdir)/@APP_NAME_LC@\"" \
	@SDL_DEFINES@ \
	@UPNP_DEFINES@ \
	@DEFS@ \

ifeq ($(findstring osx,$(ARCH)), osx)
	INCLUDES+=-I@abs_top_srcdir@/xbmc/osx
endif

ifeq ($(ARCH), powerpc-osx)
	BUNDLE1_O=-lbundle1.o
endif

ifneq ($(V), 1)
	SILENT_CPP=@echo "CPP     $(rel_top_srcdir)$@";
	SILENT_CC =@echo "CC      $(rel_top_srcdir)$@";
	SILENT_S  =@echo "S       $(rel_top_srcdir)$@";
	SILENT_GCH=@echo "GCH     $(rel_top_srcdir)$@";
	SILENT_MM =@echo "MM      $(rel_top_srcdir)$@";
	SILENT_AR =@echo "AR      $(rel_top_srcdir)$@";
	SILENT_LD =@echo "LD      $(rel_top_srcdir)$@";
endif

OBJS+=$(filter %.o,$(SRCS:.cpp=.o))
OBJS+=$(filter %.o,$(SRCS:.c=.o))
OBJS+=$(filter %.o,$(SRCS:.S=.o))
OBJS+=$(filter %.o,$(SRCS:.mm=.o))
OBJS+=$(filter %.o,$(SRCS:.m=.o))

DEPS+=$(filter %.P,$(OBJS:.o=.P))

GEN_DEPS=\
  cp $*.d $*.P \
  && sed -e 's/\#.*//' -e 's/^[^:]*: *//' -e 's/ *\\$$//' \
         -e '/^$$/ d' -e 's/$$/ :/' < $*.d >> $*.P \
  && rm -f $*.d \
  || ( rm -f $*.P $@ && exit 1 )

%.o: %.cpp
	@rm -f $@
	$(SILENT_CPP) $(CXX) -MF $*.d -MD -c $(CXXFLAGS) $(DEFINES) $(INCLUDES) $(CPPFLAGS) $< -o $@ \
	&& $(GEN_DEPS)

%.o: %.cc
	@rm -f $@
	$(SILENT_CPP) $(CXX) -MF $*.d -MD -c $(CXXFLAGS) $(DEFINES) $(INCLUDES) $(CPPFLAGS) $< -o $@ \
	&& $(GEN_DEPS)

%.o: %.c
	@rm -f $@
	$(SILENT_CC) $(CC) -MF $*.d -MD -c $(CFLAGS) $(DEFINES) $(INCLUDES) $(CPPFLAGS) $< -o $@ \
	&& $(GEN_DEPS)

%.o: %.C
	@rm -f $@
	$(SILENT_CPP) $(CXX) -MF $*.d -MD -c $(CFLAGS) $(DEFINES) $(INCLUDES) $(CPPFLAGS) $< -o $@ \
	&& $(GEN_DEPS)

%.o: %.S
	@rm -f $@
	$(SILENT_CC) $(CC) -c $(CFLAGS) $(DEFINES) $(INCLUDES) $< -o $@


%.h.gch : %.h
	$(SILENT_GCH) $(CXX) -c $(CFLAGS) $(DEFINES) $(INCLUDES) $< -o $@

%.o: %.m
	@rm -f $@
	$(SILENT_MM) $(CC) -MF $*.d -MD -c $(CFLAGS) $(DEFINES) $(INCLUDES) $< -o $@ \
	&& $(GEN_DEPS)

%.o: %.mm
	@rm -f $@
	$(SILENT_MM) $(CXX) -MF $*.d -MD -c $(CXXFLAGS) $(DEFINES) $(INCLUDES) $< -o $@ \
	&& $(GEN_DEPS)


.PHONY : lib pch clean distclean distclean_evil check testsuite

$(LIB): $(OBJS)
	$(SILENT_AR) $(AR) $(ARFLAGS) $(LIB) $(OBJS)

pch: $(PCH:.h=.h.gch)
	for d in $(DIRS); do (cd "$$d"; $(MAKE) pch ); done

clean: 
	$(RM) $(OBJS) *.o $(LIB) $(SLIB) $(CLEAN_FILES) $(PCH:.h=.h.gch) $(DEPS)
	for d in $(DIRS); do (if test -f "$$d/Makefile"; then ($(MAKE) -C "$$d" clean); fi ); done
	for d in $(CHECK_DIRS) $(GTEST_DIR); do if test -f $$d/Makefile; then $(MAKE) -C $$d $@; fi; done

distclean:
	$(RM) $(OBJS) *.o $(LIB) $(SLIB) $(STATICLIB) $(CLEAN_FILES) $(DISTCLEAN_FILES) $(PCH:.h=.h.gch) $(DEPS)
	for d in $(DIRS); do (if test -f "$$d/Makefile"; then ($(MAKE) -C "$$d" distclean || $(MAKE) -C "$$d" clean); fi ); done
	for d in $(CHECK_DIRS) $(GTEST_DIR); do (if test -f "$$d/Makefile"; then ($(MAKE) -C "$$d" $@ || $(MAKE) -C "$$d" clean); fi ); done
	if [ "$(shell pwd -P)" = "$(abs_top_srcdir)" ] || \
	   [ "$(shell pwd)" = "$(abs_top_srcdir)" ]; then rm -f $(AUTOGENERATED_MAKEFILES) Makefile.include Makefile; fi