################################################################################
# Copyright (C) 2006-2007 Andes Technology, Inc.
#
# Abstract:
#
#   Makefile of fLib for NDS32/GNU solution.
#
################################################################################

PREFIX = $(shell which nds32le-elf-gcc | sed s/\\/bin\\/nds32le-elf-gcc//g | sed s/" "/"\\\ "/g)
FLIB = $(shell pwd)/../../fLib1

#==============================================================================
#
# cross
#
#==============================================================================

CROSS=$(PREFIX)/bin/nds32le-elf-

CC = $(CROSS)gcc
AS = $(CROSS)as
AR = $(CROSS)ar
LD = $(CROSS)ld
NM = $(CROSS)nm
STRIP = $(CROSS)strip
OBJCOPY = $(CROSS)objcopy
OBJDUMP = $(CROSS)objdump
READELF = $(CROSS)readelf

#==============================================================================
#
# path
#
#==============================================================================

INCLUDE_PATH = -Iinclude 

#==============================================================================
#
# flags
#
# FPGA_20070127 means FPGA netlist version after 20070127
#==============================================================================

CFLAGS = -g -O0 -EL -c -mlib=newlib \
			$(INCLUDE_PATH) -w #-mno-16bit

ASFLAGS = -g -EL $(INCLUDE_PATH) #-mno-16bit

LDFLAGS = -static -mlib=newlib -nostartfiles -EL -Wl,-Tmyapp.ld \
			-Wl,-Map -Wl,hello.map -Wl,-lgcc -Wl,-lc -Wl,-lgcc

ARFLAGS = -c -r

#==============================================================================
#
# rules
#
#==============================================================================

.PHONY: all clean help

UNUSED = keypad_autoscan.o \

OBJS = hello.o ssp.o cmdui.o interrupt.o irq.o fiq.o boot.o

TBD = $(UNUSED) $(FUSB100)

#
# Note: I never think about to check in following stub and dhrystone.
#       because, for me, I designed these for tricks only,
#       not diagnostic. Harry@Feb.07.2007
#
TEST = dhry_1.o dhry_2.o

help:
	@echo  'Simple helpers of the typical targets used:'
	@echo  ''
	@echo  '' make all              - make image
	@echo  '' make clean            - clean all object files
	@echo  '' make rebuild          - equal to make clean plus make all
	@echo  ''
	@echo  'For further info please refer to the ./README.txt file'

all: hello.bin
	@if [ -f hello.bin ]; then \
	echo Build completed; \
	else \
	echo Build failed; \
	fi

rebuild: clean all

clean:
	rm -f $(OBJS) hello.map hello.bin hello.dbg hello.obj hello.txt

hello.bin: $(OBJS) 
	$(CC) -mlib=newlib -Wl,--whole-archive -Wl,--start-group $(OBJS) -Wl,--end-group -Wl,--no-whole-archive $(LDFLAGS) -o hello.dbg
	$(STRIP) -s hello.dbg -o hello.obj
	$(OBJCOPY) -S -O binary hello.obj hello.bin
	ls -lh hello.*

hello.o: hello.c
	$(CC) -c $(CFLAGS) hello.c -o hello.o
	
ssp.o: ssp.c
	$(CC) -c $(CFLAGS) ssp.c -o ssp.o	

cmdui.o: .c
	$(CC) -c $(CFLAGS) cmdui.c -o cmdui.o

interrupt.o: .c
	$(CC) -c $(CFLAGS) interrupt.c -o interrupt.o

irq.o: .c
	$(CC) -c $(CFLAGS) irq.c -o irq.o
	
fiq.o: .c
	$(CC) -c $(CFLAGS) fiq.c -o fiq.o
	
boot.o: boot.S start.S
	$(AS) $(ASFLAGS) boot.S -o boot.o

