CC			= arm-none-eabi-gcc
LD			= arm-none-eabi-gcc
OBJCOPY		= arm-none-eabi-objcopy
LKR			= arm_lib.lkr
LTO			= -flto
CCFLAGS		= $(LTO) -Os -g -ggdb3 -I. -ffunction-sections -fdata-sections -ffixed-r9 -DUSE_TRAPS=0 -fpic -Wno-multichar -Wall
LDFLAGS		= $(LTO) -Os -g -ggdb3 -Wl,--gc-sections -Wl,-T $(LKR) -fpic -ffixed-r9

RSRC		= amdc0000
PRC			= dma_driver.prc
SRCS		+= crt.S libpalmos.c librepalm.c
TYPE		?= stm32



ifeq ($(TYPE),stm32)
	CCFLAGS		+= -mthumb -march=armv7e-m -mcpu=cortex-m4
	LDFLAGS		+= -mthumb -march=armv7e-m -mcpu=cortex-m4
	SRCS		+= main_stm32.c
	CRTGENARCH	= v7m
else ifeq ($(TYPE),pxaG1)
	CCFLAGS		+= -marm -march=armv5te -mtune=xscale
	LDFLAGS		+= -marm -march=armv5te -mtune=xscale
	SRCS		+= main_pxaG1.c
	CRTGENARCH	= v4
else
	ERR			+= $(error build TYPE not valid)
endif


#add SDK
CCFLAGS		+=  -I../../SDK
CCFLAGS		+=  -I../../SDK/Core
CCFLAGS		+=  -I../../SDK/Core/System
CCFLAGS		+=  -I../../SDK/Core/Hardware
CCFLAGS		+=  -I../../SDK/Core/UI
CCFLAGS		+=  -I../../SDK/Dynamic
CCFLAGS		+=  -I../../SDK/libraries

#add repalm
CCFLAGS		+=  -isystem../dal
EXTRA_RSRCS	+= amdi0000.bin



#do not touch below
OBJS		= $(patsubst %.S,%.o,$(patsubst %.c,%.o,$(SRCS)))
INCS		= $(wildcard *.h)

$(PRC): $(RSRC).bin $(EXTRA_RSRCS) $(ERR)
	../../tools/mkrom/mkprc DmaDriver libr stDM $^ > $@

$(RSRC).bin: $(RSRC).elf  Makefile
	$(OBJCOPY) -I elf32-littlearm -O binary $(RSRC).elf $(RSRC).bin -j.vec -j.text -j.rodata -j.data

$(RSRC).elf: $(OBJS) $(LKR) Makefile $(SLIBS)
	$(LD) -o $(RSRC).elf $(LDFLAGS) $(OBJS) $(SLIBS)

libpalmos.c: ../dal/libpalmos.c
	cp $< $@

librepalm.c: ../dal/librepalm.c
	cp $< $@

%.o : %.c $(INCS) Makefile
	$(CC) $(CCFLAGS) -c $< -o $@
	
%.o : %.S Makefile
	$(CC) $(CCFLAGS) -c $< -o $@

crt.S: Makefile DmaDriver.libdescr
	../../tools/libtools/crtgen.sh $(CRTGENARCH) DmaDriver.libdescr >crt.S

clean:
	rm -f $(OBJS) $(RSRC).bin $(RSRC).elf $(PRC) crt.S libpalmos.c librepalm.c


