#*****************************************************************************
#
#    Copyright 2010 Marcus Jansson <mjansson256@yahoo.se>
#
#
#    This is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    This file is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this file.  If not, see <http://www.gnu.org/licenses/>.
#*****************************************************************************

##############################################################
# The name of the program, elf and binary
##############################################################
PROGRAM = separatecompile
ONELINECOMPILENAME = onelinecompile
ELF = $(PROGRAM).elf
BINARY = $(PROGRAM).bin

##############################################################
SOURCE = other.c main.c

##############################################################
#The kernel assembler sources of ROSA
##############################################################
ASMSOURCE= crt0.S asm.S asm1.S

##############################################################
#Header files are located in these files
##############################################################
INCDIRS = -I./

##############################################################
#The target board and MCU
##############################################################
BOARD = EVK1100
PART = uc3a0512

##############################################################
#Various build programs
##############################################################
CC = avr32-gcc
LD = avr32-ld
AS = avr32-as
AR = avr32-ar
OBJCOPY = avr32-objcopy
OBJDUMP = avr32-objdump
TEST = test
MKDIR = mkdir
LESS = less
SIZE = avr32-size

##############################################################
#JTAG tool which will be used
##############################################################
JTAGTOOL = avrdragon
PROGRAMMER = avr32program

##############################################################
#Various compile flags etc
##############################################################
LDSCRIPT = uc3a0512.lds
DEBUG = -ggdb
OPT = 3
AFLAGS = -x assembler-with-cpp -Wa,--linkrelax

#~ CFLAGS = $(DEBUG) -DO$(OPT) -Wall -Wa,-R -mrelax -c -muse-rodata-section -msoft-float -mpart=$(PART) -DBOARD=$(BOARD) -fdata-sections -ffunction-sections $(INCDIRS) -nostartfiles
#~ LDFLAGS = --gc-sections --relax --direct-data -nostartfiles -mpart=$(PART) -T$(LDSCRIPT)

#~ CFLAGS  = -c	//This is done separately in the %.o rules
CFLAGS = -mpart=$(PART) -DBOARD=$(BOARD)
CFLAGS += $(INCDIRS)
CFLAGS += $(DEBUG) -DO$(OPT)
CFLAGS += -Wall -Wa,-R
#~ CFLAGS += -msoft-float
CFLAGS += -nostartfiles
CFLAGS += -fomit-frame-pointer
CFLAGS += -mrelax
#~ CFLAGS += -fno-common
#~ CFLAGS += -fsection-anchors
#~ CFLAGS += -fdata-sections	//Documentation says this clobbers the linker optimisation
#~ CFLAGS += -ffunction-sections
#~ CFLAGS += -mno-use-rodata-section
CFLAGS += -Wa,--linkrelax

#~ LDFLAGS = --gc-sections --relax --direct-data -nostartfiles -T$(LDSCRIPT)
LDFLAGS = -nostartfiles --gc-sections --relax --direct-data -T$(LDSCRIPT)

OBJ = $(ASMSOURCE:%.S=%.o) $(SOURCE:%.c=%.o)

BUILDDIR = .

##############################################################
#Makefile rules
##############################################################
all: clean $(OBJ) elf $(BINARY) onelinecompile

#The four rules below is for compiling files separately with gcc -c flag. This does not produce rcall's correctly.
%.o: %.S
	$(CC) -c $(CFLAGS) $(AFLAGS) -O$(OPT) $< -o$@

%.o: %.c
	$(CC) -c $(CFLAGS) -O$(OPT) $< -o$@

$(BINARY):
	$(OBJCOPY) -O binary $(ELF) $(BINARY)

elf:
	$(CC) $(LDFLAGS) $(OBJ) -o $(ELF)

#The rule below is for compiling all files in one line. This produces rcall's correctly.
onelinecompile:
	@echo
	@echo --------------------------------------------
	@echo - Now compiling with all files on one line -
	@echo --------------------------------------------
	$(CC) -Wl,-T$(LDSCRIPT) $(CFLAGS) $(ASMSOURCE) $(SOURCE) -o $(ONELINECOMPILENAME)

dump:
	$(OBJDUMP) -S -x $(ELF)|$(LESS)

dumponelinecompile:
	$(OBJDUMP) -S -x $(ONELINECOMPILENAME)|$(LESS)

clean:
	rm -f $(OBJ) $(ELF) $(BINARY) onelinecompile


