PWDDIR  = $(shell pwd)
OBJDIR  = $(PWDDIR)/obj
BINDIR	= $(PWDDIR)/bin
LIBDIR  = $(PWDDIR)/lib

TARGET = $(shell basename $(PWDDIR))

AS      = as
CC      = gcc
CPP     = g++
AR      = ar
LD      = ld

OBJCOPY = objcopy
OBJDUMP = objdump
MAKE    = make

ASFLAGS  = -D -g -I$(PWDDIR)
CCFLAGS  = -O3 -c -ffunction-sections -fdata-sections -I$(PWDDIR)
CPPFLAGS = -O3 -c -ffunction-sections -fdata-sections -I$(PWDDIR)
ARFLAGS  = rcsv
LDFLAGS  = -L$(LIBDIR) -lm -lc

EXCNAME = $(TARGET)
LIBNAME = lib$(TARGET)
DLLNAME = dll$(TARGET)

ASFILES  = $(wildcard *.S)
CCFILES  = $(wildcard *.c)
CPPFILES = $(wildcard *.cpp)

ASOBJS = $(patsubst %.c, $(OBJDIR)/%.o, $(ASFILES))
CCOBJS = $(patsubst %.c, $(OBJDIR)/%.o, $(CCFILES))
CPPOBJS = $(patsubst %.cpp, $(OBJDIR)/%.o, $(CPPFILES))

ALLOBJS = $(ASOBJS)  $(CCOBJS)  $(CPPOBJS)


all:$(ALLOBJS)
	# $(MAKE) -C ccvui
	# $(MAKE) -C hardware
	$(LD) $(LDFLAGS) -T default-t.lds -Map=$(BINDIR)/$(EXCNAME).map -o $(BINDIR)/$(EXCNAME).elf $(ALLOBJS)

.PHONY:$(LIBNAME)
$(LIBNAME):$(ALLOBJS)
	$(AR) $(ARFLAGS) $(LIBDIR)/$(LIBNAME).a $(ALLOBJS)

.PHONY:$(DLLNAME)
$(DLLNAME):$(ALLOBJS)
	$(CC) $(ALLOBJS) $(LDLAGS) -o $(BINDIR)/$(DLLNAME).so
	
$(OBJDIR)/%.o : %.S
	$(AS) $(ASFLAGS) $< -o $@
	
$(OBJDIR)/%.o : %.c
	$(CC) $(CCFLAGS) $< -o $@
	
$(OBJDIR)/%.o : %.cpp
	$(CPP) $(CPPFLAGS) $< -o $@

.PHONY : clean
clean:
	rm -f $(BINDIR)/*app_*.bin  $(BINDIR)/*.elf $(BINDIR)/*.dis $(BINDIR)/*.map  $(BINDIR)/*.c $(OBJDIR)/*.o
