include ../Makefile.spec

ifndef CSTUB_PATH


#
### FLAGS for the $(CC)
OPT = -O3
BUILD_MODE = RELEASE
COMMON_DEFINE_FLAGS =    
LIB_DEFAULT_FLAGS      = -g $(OPT) -MD -Wall -Werror -save-temps -mlongcalls  
LIB_DEFAULT_FLAGS += $(COMMON_DEFINE_FLAGS)

LIB_COMPILE_FLAGS = $(LIB_DEFAULT_FLAGS)

# Commands
LIB_CC        =  xt-ar
CC            = xt-clang
ISS = xt-run
INCLDIRS   = -I. -I./inc

TILE_MANAGER_SRCS = $(wildcard src/*.c)

TILE_MANAGER_OBJS = $(TILE_MANAGER_SRCS:.c=.o)

ifneq ($(findstring xt-xcc, $(CC)),)
IPA = $(findstring -IPA, $(LIB_COMPILE_FLAGS))
ifeq ($(IPA), )
IPA = $(findstring -ipa, $(LIB_COMPILE_FLAGS))
endif
endif
#
CUR_DIR_DMA := ${CURDIR}
LIB_NAME := ${notdir $(CUR_DIR_DMA)}

ifeq ($(OS), Windows_NT)
RMRF = del /s /q
else
RMRF = rm -rf
endif

TILE_MANAGER_LIB = $(LIB_NAME).a
OBJS = $(TILE_MANAGER_OBJS)

%.o:%.c
	$(CC) $(LIB_COMPILE_FLAGS) $(INCLDIRS) -c -o $@ $<

ifeq ($(IPA), )
$(TILE_MANAGER_LIB): $(OBJS)
	$(LIB_CC) r $@ $(OBJS)
	$(LIB_CC) -s $@
else
$(TILE_MANAGER_LIB): $(OBJS)
	$(LIB_CC) r -keep $@ $(OBJS)
	$(LIB_CC) -s $@
endif

all: $(TILE_MANAGER_LIB)
	@echo ".: Done Library DMA make all"

ifeq ($(OS), Windows_NT)
clean:
	del /s /q *.gcno *.gcda *.d *.o *.s *.gcov *.a *.ipakeep *.B *.i* *.M *.spin *.bc 	
else
clean:
	$(RMRF) $(TILE_MANAGER_LIB) $(OBJS)  *.ipakeep *.s *.B *.i* *.M *.d *.a *.spin *.bc *.gcov    
	find . -type f -name '*.gcno' -delete
	find . -type f -name '*.gcda' -delete
	find . -type f -name '*.d' -delete	
endif


else
###################CSTUB BUILD #################################
#
LIB_CC        =   g++
CC            =   g++
INCLUDES =  -I ./inc   -I $(CSTUB_PATH)
LIB_DEFINES = -m32

CUR_DIR_DMA := $(shell pwd)
LIB_NAME = $(shell basename $(CUR_DIR_DMA))

TILE_MANAGER_LIB = $(LIB_NAME).so

TILE_MANAGER_SRCS = $(wildcard src/*.c)

TILE_MANAGER_OBJS = $(TILE_MANAGER_SRCS:.c=.o)
OBJS = $(TILE_MANAGER_OBJS)


%.o:%.c
	$(CC) $(INCLUDES)  $(LIB_DEFINES) -c $< -o $@

$(TILE_MANAGER_LIB): $(OBJS)
	$(LIB_CC) -m32 -shared -o $@ $^


all: $(TILE_MANAGER_LIB)
	@echo ".: Done Library DMA make all"

clean:
	-$(RMRF) $(TILE_MANAGER_LIB) $(OBJS)  *.ipakeep *.s *.B *.i* *.M *.a *.spin src/*.d *.d src/*.o ../idma/idma.o

endif


.PHONY: all clean
